Skip to content

fix(Comments): after a focused Mark as resolved or Reopen, or one picked from the more menu, focus stays in the comment's header in a narrow comment - #14251

Draft
cixzhang wants to merge 8 commits into
mainfrom
lane2/comments-more-menu-focus

Conversation

@cixzhang

@cixzhang cixzhang commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

User impact

In a narrow comment, such as the Margin Comment story, a keyboard user who presses Mark as resolved loses focus to the page. The comment header has no room for Reopen, so OverflowList puts it in the more menu, and the control that replaced the pressed one is never on screen. The user must tab from the top of the page back to the thread. Reopen from a narrow resolved comment does the same. And when Reopen or Mark as resolved is picked from the more menu, the header button that replaces it appears while the now-empty menu goes, so focus falls to the page there too.

Problem and solution fit

  1. Why does focus drop? fix(Comments): Mark as resolved and Reopen hand focus to the replacing control in the comment header #14155 hands focus to the control that replaces Mark as resolved while it shows in the header. When the header collapses that control into the more menu, nothing on screen replaces the pressed button, so focus falls to the body.
  2. Why does it matter? The Comments docs promise "Focus never drops to the page", and spec:AST-085 FR4 requires the component to name where focus goes when it removes a focused control.

When the focused header button is replaced by an action the header has no room for, Comments moves focus to the comment's more menu button, the visible control that now holds it. The menu stays closed. When the action was picked from the more menu and a header button replaces it, that button takes focus, as pickReaction already does for Add reaction. If the menu still holds other actions, it stays, and focus returns to the more menu button. A thread whose resolve control did not have focus moves no focus, and a resolve whose replacement shows in the header still hands focus to it, as #14155 does.

Expected behavior and authority

  • spec:AST-085 FR4 (when a component removes a focused control, the component names where focus goes). Specs ruled this case records-decided: the destination is the control that replaces the pressed one, or the visible control that holds it, which here is the more menu button.
  • The Comments docs (Accessibility, "Focus after changes"): "Focus never drops to the page." This pull request adds the case to that row: "Mark as resolved and Reopen hand focus to each other; when the header has no room for the one that replaces it, focus goes to the more menu button that holds it."
  • WCAG 2.4.3 Focus Order.

Smallest restoration

  • CommentActions.tsx and CommentRow.tsx: the resolve control's ref records, when it detaches, whether it had focus, until the end of that commit's work. It reads focus from the control's own root (getRootNode().activeElement), so a Comments panel in a shadow root works too.
  • OverflowList's onOverflowChange report, the callback that says which actions collapsed, marks the hand-off pending when the replacing action is among them.
  • The header cluster's ref, which attaches after the list's layout, then focuses the more menu button if focus is on the page. The more menu's own ref does the same when the menu mounts with the collapsed action.
  • The more menu joins the resolve control's useFocusAcrossSwap ref, with a getFocusTarget that resolves it to its trigger. So when an action picked from the menu is replaced by a header button and the emptied menu goes in the same commit, Core's hook hands focus to that button, and a first collapse that mounts the menu hands focus to its trigger. The real OverflowList can show the replacing button one commit before the emptied menu goes, so nothing attaches after the menu: then the menu's own ref, as it leaves holding focus, focuses the visible replacing control.
  • No Effect, no new prop, no Core change. The one case the hook cannot serve as it stands is a replacement that renders into a more menu already in the page: the hook moves focus only to a node that attaches, and nothing attaches. useFocusAcrossSwap has no fallback-target option for that, so the small local hand-off (the detach mark and the overflow report) covers it until the hook gains that option, which is tracked to land, with this code removed, before Comments moves to Core. Comments.doc.mjs names the case in "Focus after changes".

Evidence

  • The OverflowList stand-in in Comments.test.tsx can report a commit after it renders (reportsLate), as the real list measures first; "hands focus to Mark as resolved after a menu pick when the list reports a commit later" uses it.
  • Regression test in Comments.test.tsx: "hands focus to the more menu trigger when Reopen collapses into it". It fails with main's CommentActions.tsx (focus on <body>) and passes here.
  • "hands focus to Mark as resolved after Reopen is picked from the more menu" and "hands focus to the more menu trigger inside a shadow root" cover the menu path and a shadow root. "returns focus to the more menu trigger when the menu stays after a pick" covers a menu that still holds other actions: it stays, and the menu returns focus to its trigger.
  • Unchanged path: "moves no focus when the host resolves a thread whose control did not have focus" passes, and so does fix(Comments): Mark as resolved and Reopen hand focus to the replacing control in the comment header #14155's "hands focus to Reopen after a keyboard resolve, and back after Reopen".

Branded Chrome stills

Google Chrome on macOS through the repository's stills workflow, at 1b05dee2 (this head adds only a test wait). Each cell drives the Margin Comment story with the keyboard and checks the focused element in real Chrome before the still: a cell fails if focus is anywhere else.

Light Dark
Tab to Mark as resolved, Enter: focus on the more menu button
Then Enter on the more menu, Enter on Reopen: focus on Mark as resolved

Scope

Components: Comments.

Files: packages/lab/src/Comments/CommentActions.tsx, CommentRow.tsx, Comments.test.tsx, Comments.doc.mjs, and the changeset .changeset/comments-more-menu-resolve-focus.md.

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

  • Lab Comments: 4 files, 107/107, twice; the 9 focused tests pass; tsc (lab) clean; eslint on CommentActions.tsx clean (Linux).
  • The new test fails with main's CommentActions.tsx and passes with this branch's.

Overlap checked: #14155 (merged; this fills its known gap, the More-menu case), #13740 (ernestt's Comments API pass; it also edits CommentActions.tsx, so whichever lands second merges main), #13741 (stacked on #13740; no overlap with these lines)

@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Dec 3, 2026 4:11pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Dec 3, 2026
@astracat-bot

astracat-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🔄 In progress (for maintainers only) Dec 3, 2026, 4:11 PM UTC

@cixzhang cixzhang added the ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) label Dec 3, 2026
@cixzhang

cixzhang commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 65931a4

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

Current run: The current CI run concluded skipped. Current analysis is unavailable.


Generated by PR Enrichment workflow | View current CI run

@astracat-bot astracat-bot Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: the hand-off to the more menu button works for a keyboard Mark as resolved in a narrow header (reproduced locally in Chromium on the Margin Comment story, and the new test fails without the fix), but three points need fixing first.

  • Reopen picked from the more menu still drops focus to the page: in a narrow resolved comment Reopen lives in the more menu, and picking it brings Mark as resolved back to the header and unmounts the now-empty menu with focus on <body> (reproduced locally in Chromium on this branch), so either focus the replacing header control from that menu item's handler, as pickReaction does, or narrow the new docs sentence and the description to the header-button case.
  • A second focus tracker beside useFocusAcrossSwap misses focus inside a shadow root: trackResolveControl copies the shared hook's detach-time focus capture but reads node.ownerDocument.activeElement instead of the hook's shadow-aware ownsFocus, so in a shadow root the hand-off never fires (reproduced locally in jsdom) and the title, changeset and docs sentence are untrue there, so please extend useFocusAcrossSwap with a fallback target for a replacement that collapses and drop the component-level tracker.
  • The title claims the hand-off for presses that leave the control unfocused: a Safari pointer press never focuses the button and a host may resolve after the viewer moved focus away, and focus rightly stays put in both cases, so retitle to something like "when a focused Mark as resolved or Reopen is replaced and the header has no room for the replacement, focus goes to the more menu button".
  • The file header and two new comments need a touch-up: the @input header omits the new isFocusLost import, and "The menu stays closed." and "Moves focus to the more menu's trigger once it is in the document." restate the code (non-blocking).
Claims and files checked

Review mode: full; reason: no-earlier-review

  • Claim (title): after Mark as resolved or Reopen, focus goes to the more menu button when the header has no room: false for presses that never focus the control, and inside a shadow root.
  • Claim (changeset): in a narrow comment focus moves to the more menu button instead of the page, menu closed: verified for keyboard and Chromium mouse (local Chromium capture; Comments.test.tsx:1269); false inside a shadow root (local jsdom probe).
  • Claim: on main a keyboard Mark as resolved in a narrow comment loses focus: verified (local Chromium capture on main: <body>).
  • Claim: the header has no room for Reopen, so it collapses into the more menu: verified (capture: no header actions after resolving).
  • Claim: Reopen from a narrow resolved comment loses focus the same way: verified on main, and still true on this branch, so false as a fixed case.
  • Claim: the earlier hand-off still focuses a replacement that shows in the header: verified (existing test passes locally).
  • Claim: the docs promise focus never drops to the page: verified (Comments.doc.mjs:156), still false for Reopen from the menu.
  • Claim: the focus spec asks a component to name the destination: verified on main; Lab is exempt.
  • Claim: the menu stays closed: verified (aria-expanded="false" in the test and the capture).
  • Claim: no focus moves when the control did not have focus: verified (Comments.test.tsx:1307; CommentActions.tsx:299).
  • Claim: specs ruled the case decided: reported; no record linked, and Lab is exempt.
  • Claim (new docs sentence): the two actions hand focus to each other, else to the more menu button: false (Reopen from the menu; shadow root).
  • Claim: the resolve control's ref records focus at detach until the commit's work ends: verified (CommentActions.tsx:292-309).
  • Claim: the overflow report marks the hand-off pending: verified (CommentActions.tsx:359-368).
  • Claim: the cluster's ref, or the more menu's ref when the menu mounts, focuses the button when focus is on the page: verified (CommentActions.tsx:371-383, :451-455, :548-551; capture where the menu mounts).
  • Claim: no Effect, no new prop, no Core change: verified (diff).
  • Claim: the new test fails with main's component and passes here: verified (run locally; main's file leaves focus on <body>).
  • Claim: the other two named tests pass: verified (run locally).
  • Claim: Chrome stills table: reported, not present in the description.
  • Claim (scope): Comments only, four files: verified (changed-file list).
  • Claim (checklist): one defect restored: false in part (Reopen from the menu); no new public API: verified; no internal context in public text: verified.
  • Claim (testing): 4 files, 103/103: verified (run locally); lab tsc clean: verified (run locally after building Core); eslint on CommentActions.tsx clean: verified (run locally).
  • Claim (overlap): #14155 merged and #13740 open and editing the same file: verified (GitHub); #13741 is stacked on #13740 and leaves CommentActions.tsx alone: verified (GitHub), its line overlap in the docs and tests unchecked.
  • File .changeset/comments-more-menu-resolve-focus.md: untrue inside a shadow root (second finding).
  • File packages/lab/src/Comments/CommentActions.tsx: first and second findings; header and comment touch-up.
  • File packages/lab/src/Comments/Comments.doc.mjs: first and second findings.
  • File packages/lab/src/Comments/Comments.test.tsx: clear.

Automated review (maintainer link)

… header button that replaces it; the focus check reads the control's own root
@cixzhang cixzhang changed the title fix(Comments): after Mark as resolved or Reopen, focus goes to the more menu button when the header has no room for the replacing action fix(Comments): after a focused Mark as resolved or Reopen, or one picked from the more menu, focus stays in the comment's header in a narrow comment Dec 3, 2026
@cixzhang

cixzhang commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 4af7de6

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested: the hand-off to the more menu button now works for a focused Mark as resolved in a narrow header, including when the menu already exists and inside a shadow root (reproduced locally in Chromium, and the new tests fail on main), but two points need fixing first.

  • A Reopen picked from the more menu still drops focus to the page: in the Margin Comment story (reproduced locally in Chromium, keyboard and mouse) the real OverflowList attaches Mark as resolved one commit before the emptied menu unmounts, so useFocusAcrossSwap has no attaching node to hand focus to and it lands on <body>, while the new test passes only because its OverflowList stand-in renders and reports in one commit; please move focus to the visible replacing control when the more menu leaves holding focus (or give the hook the fallback target the description defers), make the stand-in report a commit after it renders, and reword the title, docs row, changeset and description, which also misdescribe a menu that keeps other items, where focus rightly returns to the more menu button.
  • The Testing line reports the earlier head's test count: the four Comments test files hold 106 tests at this head (all pass, run locally), not 103.
  • resolveFocusTarget took over ThreadSurface's JSDoc: the new helper sits between that comment block and ThreadSurface in CommentRow.tsx, so move it above the block (non-blocking).
  • The file header and two comments still need a touch-up: the @input header omits isFocusLost, and "The menu stays closed." and "Moves focus to the more menu's trigger once it is in the document." restate the code (non-blocking).
Claims and files checked

Review mode: incremental; reason: none

  • changed rules (0): none
  • Claim (title): a focused Mark as resolved or Reopen, or one picked from the more menu, keeps focus in the header of a narrow comment: false for a menu pick in the Margin Comment story (Chromium: <body>); true for the focused header button.
  • Claim: on main, a keyboard Mark as resolved in a narrow comment loses focus: verified (Chromium on main: <body>).
  • Claim: on main, a Reopen picked from the more menu of a narrow resolved comment loses focus the same way: verified (Chromium on main: <body>).
  • Claim: a focused header button replaced by an action with no room hands focus to the more menu button, menu closed: verified (new test; Chromium in the Margin Comment story and in a viewer-owned thread whose menu already exists).
  • Claim: a menu pick replaced by a header button hands focus to that button: false (Margin Comment: <body>; a menu that keeps other items keeps focus on its own button).
  • Claim: a control without focus moves no focus: verified (test passes).
  • Claim: a replacement shown in the header still takes focus, as before: verified (existing test passes).
  • Claim: the docs promise focus never drops to the page: verified that the row says so; still false for a menu pick.
  • Claim: the focus case was ruled decided: reported; no ruling linked, and this Lab-only change needs none.
  • Claim: the detach mark reads the control's own root, so a shadow root works: verified (shadow-root test passes here and fails on main).
  • Claim: the overflow report marks the hand-off pending, and the cluster's or menu's ref then focuses the button: verified (CommentActions.tsx:366-390, :394-403, :471-475).
  • Claim: the more menu in the shared swap hands a menu pick's focus to the header button: false in Chromium (first finding); a first collapse that mounts the menu hands focus to its button: verified.
  • Claim: the shared hook cannot serve a replacement rendered into a menu already present: verified (it focuses only a node that attaches); the later hook change is reported.
  • Claim: no Effect, no new prop, no Core change: verified (diff).
  • Claim: the collapse regression test fails with main's component and passes here: verified (run locally).
  • Claim: the menu-pick and shadow-root tests cover those paths: shadow root verified; the menu-pick test passes only against the stand-in (first finding).
  • Claim: the menu-stays test covers a menu that keeps other items: verified (passes here and on main).
  • Claim: Chrome stills table: reported, not present in the description.
  • Claim (scope): Comments only, five files: verified (changed-file list).
  • Claim (checklist): one defect restored: false in part (first finding); no tagalongs, no new public API, no internal context in public text: verified.
  • Claim (testing): 4 files, 103/103: false count (106 at this head); lab tsc clean and eslint clean: verified (run locally: lab tsc exits 0 after building Core; eslint reports no errors on the three changed source files)
  • Claim: the new test fails with main's component file and passes here: verified (run locally).
  • Claim (overlap): the earlier focus fix merged; the API pass also edits CommentActions.tsx; the stacked follow-up touches none of these lines: verified (GitHub).
  • Claim (changeset): both actions keep focus off the page in a narrow comment: false for a menu pick (first finding); the no-room hand-off: verified; the menu-pick hand-off: false.
  • File .changeset/comments-more-menu-resolve-focus.md: first finding.
  • File packages/lab/src/Comments/CommentActions.tsx: first finding; header and comment touch-up.
  • File packages/lab/src/Comments/CommentRow.tsx: first finding; JSDoc placement.
  • File packages/lab/src/Comments/Comments.doc.mjs: first finding.
  • File packages/lab/src/Comments/Comments.test.tsx: first finding (stand-in timing); second finding (count).

Automated review (maintainer link)

{additionalActions}
{items.length > 0 && (
<HStack data-comment-more-menu="true">
<HStack ref={attachMoreMenu} data-comment-more-menu="true">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Margin Comment story this hand-off never fires (reproduced locally in Chromium, keyboard and mouse: focus ends on <body> after Reopen is picked). The real OverflowList attaches Mark as resolved in its measurement commit, and only the report commit that follows empties items and unmounts this wrapper, so when it leaves holding focus no node attaches through the shared ref. One fix: in this ref's cleanup, when the trigger holds focus and visibleCommentAction(clusterRef.current, resolveAction.kind) exists, focus that control.

it('hands focus to Mark as resolved after Reopen is picked from the more menu', async () => {
// Only Reopen collapses, so the more menu empties and unmounts once Mark
// as resolved replaces it in the header.
overflowControl.collapsed = new Set(['reopen']);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This passes only because the OverflowList stand-in renders the new header button and drops the menu in one commit: it never calls onOverflowChange when nothing collapses (line 78), so collapsed stays {'reopen'} and the menu empties at once. The real OverflowList reports a commit later. A stand-in that reports in a later commit than it renders (or a browser test of the Margin Comment story) would catch the regression.

criterion: '2.4.3 Focus Order',
description:
'Focus never drops to the page. After a delete it moves to the next thread, or to the thread when a reply was deleted. Load earlier comments keeps focus on itself, or on the Retry that replaces it when loading fails. A thread selected when the panel mounts opens without taking focus.',
'Focus never drops to the page. Mark as resolved and Reopen hand focus to each other. When the header has no room for the one that replaces it, focus goes to the more menu button that holds it; when one picked from the more menu is replaced by a header button, that button takes focus. After a delete it moves to the next thread, or to the thread when a reply was deleted. Load earlier comments keeps focus on itself, or on the Retry that replaces it when loading fails. A thread selected when the panel mounts opens without taking focus.',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"when one picked from the more menu is replaced by a header button, that button takes focus" is false twice at this head: in the Margin Comment story focus lands on <body> (reproduced locally in Chromium), and when the menu keeps other items focus returns to the more menu button, as the new test at Comments.test.tsx:1350 asserts. The changeset says the same.


// The resolve control's focus swap includes the comment's more menu, whose
// focus target is its trigger button.
function resolveFocusTarget(node: HTMLElement): HTMLElement | null {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this helper now sits between ThreadSurface's JSDoc (lines 183-189) and ThreadSurface, so that description attaches to resolveFocusTarget. Moving the helper above the JSDoc block keeps both documented.

github-actions Bot added a commit that referenced this pull request Dec 3, 2026
…visible replacing control; the stand-in can report a commit later
github-actions Bot added a commit that referenced this pull request Dec 3, 2026
@cixzhang

cixzhang commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 91a636f

This branch was successfully deployed

1 active deployment
Preview — 91a636f0 Deployed Dec 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant