Repository navigation
fix(Popover): usePopover keeps its focus-return target when show() reruns - #11682
Conversation
A controlled popover with auto focus, opened from the keyboard, loses its trigger as the focus-return target: initial focus moves inside as the layer shows, and the controlled sync calls show() again before the open state turns on. That second call records no opener, so a close by Escape or by a pick leaves focus inside or on the page. A unit case drives that order through usePopover, and a browser case opens the Confirmation story from the keyboard and closes it both ways.
Since initial focus moves inside as the layer shows, a controlled popover's sync can call show() again with focus already inside. That call read the focused control as the opener, saw it inside the content and recorded none, so the focus trap fell back to the control and a close left focus inside or on the page. A show() that finds focus inside now keeps the opener it has. Only show() in usePopover changes.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
|
/approve-spec 724790f |
PR Analysis Report🔎 Preview ·
|
| Component | Size | LoC | Visual · A11y · RTL |
|---|---|---|---|
| lab/Comments | 367.6 kB (+39 B) · 2.5 kB CSS (0) | — | — · — · — |
| ChatComposer | 84.4 kB (+30 B) · 12.9 kB CSS (0) | — | — · — · — |
| DateRangeInput | 99.1 kB (+26 B) · 13.3 kB CSS (0) | — | — · — · — |
28 more components
| Component | Size | LoC | Visual · A11y · RTL |
|---|---|---|---|
| lab/TransferListSelector | 120.6 kB (+26 B) · 1.4 kB CSS (0) | — | — · — · — |
| MultiSelector | 101.3 kB (+25 B) · 17.7 kB CSS (0) | — | — · — · — |
| TopNavMenu | 81.4 kB (+24 B) · 12.0 kB CSS (0) | — | — · — · — |
| Typeahead | 87.5 kB (+22 B) · 13.3 kB CSS (0) | — | — · — · — |
| lab/ChatReactionBar | 113.9 kB (+21 B) · 1.1 kB CSS (0) | — | — · — · — |
| Tokenizer | 103.5 kB (+20 B) · 20.0 kB CSS (0) | — | — · — · — |
| ChatComposerInput | 82.0 kB (+19 B) · 12.1 kB CSS (0) | — | — · — · — |
| ChatComposerTokenElement | 82.0 kB (+19 B) · 12.1 kB CSS (0) | — | — · — · — |
| SideNavItem | 76.8 kB (+19 B) · 11.9 kB CSS (0) | — | — · — · — |
| TopNavHeading | 82.0 kB (+19 B) · 12.0 kB CSS (0) | — | — · — · — |
| lab/CommentThread | 360.3 kB (+19 B) · 2.4 kB CSS (0) | — | — · — · — |
| ComplexSelector | 77.4 kB (+18 B) · 12.6 kB CSS (0) | — | — · — · — |
| lab/CommentComposer | 161.8 kB (+18 B) · 1.9 kB CSS (0) | — | — · — · — |
| TopNavMegaMenu | 75.9 kB (+17 B) · 12.6 kB CSS (0) | — | — · — · — |
| PowerSearch | 305.8 kB (+16 B) · 25.4 kB CSS (0) | — | — · — · — |
| MoreMenu | 106.4 kB (+15 B) · 18.6 kB CSS (0) | — | — · — · — |
| Selector | 111.9 kB (+14 B) · 18.5 kB CSS (0) | — | — · — · — |
| BaseTypeahead | 78.2 kB (+13 B) · 11.7 kB CSS (0) | — | — · — · — |
| Pagination | 122.7 kB (+13 B) · 18.9 kB CSS (0) | — | — · — · — |
| TabMenu | 78.7 kB (+13 B) · 11.9 kB CSS (0) | — | — · — · — |
| DateTimeInput | 223.1 kB (+12 B) · 19.7 kB CSS (0) | — | — · — · — |
| DropdownMenu | 106.1 kB (+11 B) · 18.6 kB CSS (0) | — | — · — · — |
| DateInput | 199.6 kB (+10 B) · 17.9 kB CSS (0) | — | — · — · — |
| SideNavHeading | 82.3 kB (+9 B) · 12.1 kB CSS (0) | — | — · — · — |
| lab/ChatEmojiPicker | 112.7 kB (+8 B) · 773 B CSS (0) | — | — · — · — |
| richtext/RichTextEditorToolbar | 298.5 kB (+7 B) · 454 B CSS (0) | — | — · — · — |
| BreadcrumbItem | 96.1 kB (+6 B) · 13.1 kB CSS (0) | — | — · — · — |
| Popover · Storybook | 97.8 kB (+1 B) · 15.1 kB CSS (0) | 1,556 (+4) | ✓ · ✓ · ✓ |
Package size: @astryxdesign/core 7.56 MB packed (+154 B); no new dependencies.
Touched components checked; add ci:run-visual or ci:run-rtl to run the full suites.
Generated by PR Enrichment workflow | View full report
There was a problem hiding this comment.
Requesting changes: the fix in show() looks right, but the changeset describes a failure that main does not have.
- The changeset says a controlled Popover opened from the keyboard and closed by Escape or by a choice inside it "left focus on the page", but on
mainthat path already returns focus to the trigger (the Confirmation story in Chromium, and this pull request's own browser spec passes onmain), so please reword the changeset and the title to the case the new unit test proves:show()running again after initial focus has moved inside.
The guard itself is sound: the new unit case fails on main and passes here, and no public API changes. A wording that matches the evidence: "usePopover keeps its focus-return target when show() runs again after initial focus has moved inside the popover, so a close returns focus to the element that opened it instead of losing it."
Claims and files checked
- Claims:
- Changeset: a keyboard-opened controlled Popover closed by Escape or by a choice inside it left focus on the page. False: on
mainfocus returns to the trigger (Confirmation story in Chromium; the new browser spec passes onmain). - Changeset: it now returns focus to its trigger. Verified for a rerun of
show()(usePopover.tsx:614-620and the new unit case). - Description:
show()records the focused element, and a rerun with focus inside records none, so the focus trap falls back to the control inside. Verified (usePopover.tsx:621-622,hooks/useFocusTrap.ts:679-681). - Description: one guard in
show(); the opener assignment, Layer, the focus trap and the controlled sync are unchanged; no API change. Verified (the diff; the API surface comparison has no rows). - Description: the unit case fails before the fix and passes after it, three runs each. Verified.
- Description: the browser case passes on
maintoo and guards against a regression. Verified in Chromium, where the spec passes onmainand here; WebKit not run. - Description: an uncontrolled popover still returns focus to its trigger on Escape. Verified (the existing unit case passes).
- Description: the quoted Popover focus-return clause and the overlay family's focus-return requirement say what is quoted. Verified.
- Description: other open work changes different lines of
show(). Verified. - Description: formatting, strict lint, the core typecheck and the changeset check pass. Verified (CI's typecheck and lint pass at this head); the earlier-head interaction, accessibility and wide unit runs were not repeated, and 2,407 related unit tests pass at this head.
- Changeset: a keyboard-opened controlled Popover closed by Escape or by a choice inside it left focus on the page. False: on
- Files:
.changeset/popover-controlled-focus-return.md: the finding above.packages/core/src/Popover/usePopover.tsx: clear.packages/core/src/Popover/Popover.test.tsx: clear; the new case fails onmainand passes here.packages/core/src/Popover/__tests__/PopoverFocusReturn.interaction.browser.spec.ts: clear; a regression guard that also passes onmain.
The changeset described a keyboard close that left focus on the page, which the browser case does not show on main. It now describes the case the unit case proves: show() running again after initial focus has moved inside the popover. No code or test changes.
|
/approve-spec e3457e8 |
There was a problem hiding this comment.
Requesting changes for one sentence in the description; the code and tests need no change.
- Miss: the description's "Smallest restoration" section says #11121 "changes other lines of
show()", but #11121 changes no line ofshow()(it rewritesuseFocusTrap's focus restore and the trap wiring inusePopover, which read the opener this guard keeps), so please reword it, for example: "#11121, open work on one focus return for Popover, Dialog and DropdownMenu, rewritesuseFocusTrap's restore and the trap wiring inusePopover, which read the opener this guard keeps; it changes no line ofshow()."
The changeset now names exactly the case the new unit test proves, which settles the earlier finding. The new unit case fails on main and passes here, and no public API changes. The CI run on this head did not run its test jobs because CI was full, so it needs a re-run.
Claims and files checked
- Claims:
- Changeset and title:
usePopoverkeeps its focus-return target whenshow()runs again after focus moved inside, so a close returns focus to the opener. Verified (usePopover.tsx:614-622; the new unit case fails onmainand passes here). show()records the focused element as the opener, a second call with focus inside records none, and the focus trap then falls back to the control inside. Verified (usePopover.tsx:621-622,hooks/useFocusTrap.ts:679-681; onmainthe unit case leaves focus on the control inside).- A controlled popover's sync can make that second call before the open state turns on. Verified in jsdom (a controlled Popover mounted open from another button keeps focus inside on
mainand returns it to that button here); not reproduced in Chromium. - The fix keeps the opener when
show()runs again with focus inside. Verified (usePopover.tsx:617-620). - The quoted Popover focus-return clause, its states row and the overlay family's focus-return requirement say what is quoted. Verified.
- One guard; the opener assignment, Layer, the focus trap and the controlled sync are unchanged; no API change. Verified (the diff; the API surface comparison has no rows).
- The related open focus-return work changes other lines of
show(). False: it changes no line ofshow(). - The unit case fails before the fix and passes after it, three runs each. Verified.
- The browser case passes on
maintoo and guards against a regression. Verified in Chromium (same end state onmainand here); WebKit not run. - An uncontrolled popover still returns focus to its trigger on Escape. Verified (the existing cases pass in 2,614 Popover, focus-trap and menu unit tests).
- Both tests were added before the fix. Verified (commit order).
- This round changes only the changeset. Verified.
- Scope: one defect, no tagalongs, no hidden API, default or interaction change, no internal context. Verified.
- Prettier, strict lint, the core typecheck and the changeset check pass. Prettier, lint on the changed files and the changeset check verified here; the core typecheck passes here too (source and its three test shards, plus the docs project).
- Runs at earlier commits outside this pull request (interaction, accessibility and wide unit suites). Not reproducible here; consistent with this head's results.
- The hotfix release-path note. A process statement, not checkable against the code.
- Changeset and title:
- Files:
.changeset/popover-controlled-focus-return.md: clear; it now matches the unit case.packages/core/src/Popover/usePopover.tsx: clear.packages/core/src/Popover/Popover.test.tsx: clear; the new case fails onmainand passes here.packages/core/src/Popover/__tests__/PopoverFocusReturn.interaction.browser.spec.ts: clear; a regression guard that also passes onmain.
There was a problem hiding this comment.
Approved pending CI: CI has not run its tests on this head yet, because the last run was refused while CI was full, so re-run all jobs before merging.
The guard in usePopover's show() keeps the focus-return target when show() runs again after focus has moved inside, which restores the Popover focus-return requirement; the new unit case fails on main and passes here, and no public API changes. The description's sentence about #11121 and the changeset now match the code, which settles the earlier findings.
Claims and files checked
- Claims:
- Changeset and title: the hook keeps its focus-return target when
show()runs again after focus moved inside, so a close returns focus to the opener. Verified (usePopover.tsx:614-622; the new unit case fails onmainand passes here). - A second
show()with focus already inside records no opener, and the focus trap then falls back to the control inside. Verified (usePopover.tsx:621-622,hooks/useFocusTrap.ts:679-681; onmainthe unit case leaves focus on the control inside). - A controlled popover's sync can make that second call. Verified in jsdom (a controlled Popover mounted open by a press on another button keeps focus inside on
mainand returns it to that button here); not reproduced in Chromium. - Lost focus leaves the next key press on the page. Verified in Chromium for a direct hook caller whose press runs
show()twice with light dismiss off. - The fix keeps the opener when
show()runs again with focus inside. Verified (usePopover.tsx:617-620). - The quoted Popover focus-return clause, its states row and the overlay family's focus-return requirement say what is quoted. Verified.
- One guard; the opener assignment, Layer, the focus trap and the controlled sync are unchanged; no API change. Verified (the diff; the API surface comparison has no rows).
- The related open focus-return work rewrites the focus trap's restore and the trap wiring in
usePopover, which read the opener this guard keeps. Verified against that pull request's own patch. - The unit case fails before the fix and passes after it, three runs each. Verified.
- The browser case passes on
maintoo and guards against a regression. Verified in Chromium; WebKit not run. - The browser case passes after the change. Verified in Chromium (6 of 6 runs); WebKit not run.
- An uncontrolled popover still returns focus to its trigger on Escape. Verified (the existing case passes among 1,609 unit tests).
- Both tests were added before the fix. Verified (commit order).
- This round changes only the changeset. Verified.
- Scope: one defect, no tagalongs, no hidden API, default or interaction change, no internal context. Verified.
- Prettier, strict lint, the core typecheck, the changeset check and changeset coverage pass. Verified (the first four here; changeset coverage in this head's Lint run).
- Runs at earlier commits outside this pull request, and Popover's keyboard, touch and accessibility specs. Not run here; consistent with this head's results.
- The release-path note. A process statement, not checkable against the code.
- Changeset and title: the hook keeps its focus-return target when
- Files:
.changeset/popover-controlled-focus-return.md: clear.packages/core/src/Popover/usePopover.tsx: clear.packages/core/src/Popover/Popover.test.tsx: clear.packages/core/src/Popover/__tests__/PopoverFocusReturn.interaction.browser.spec.ts: clear.
User impact
usePopoverloses its focus-return target whenshow()runs again after initial focus has moved inside the popover. A controlled popover's sync can make that second call before the open state turns on. A close then does not return focus to the element that opened the popover.Problem and solution fit
show()records the focused element as the opener, where focus returns on close. A second call with focus already inside records none. The focus trap then falls back to the control inside, which the close removes.The fix keeps the opener when
show()runs again with focus already inside.Expected behavior and authority
component:PopoverAR4: "Dismissal returns focus to the trigger when focus had entered the closing Popover and would otherwise be lost." Its states table says the same for "Close after focus entered the popup".family:overlay-dismissalFR9 returns focus to the member's return target when it would otherwise be lost.Smallest restoration
One guard in
usePopover'sshow(): when the focused element is inside the content, the opener stays as the first call recorded it. The assignment that records the opener, Layer, the focus trap and the controlled sync are unchanged. No API change. #11121 is open work on one focus return for Popover, Dialog and DropdownMenu. It rewritesuseFocusTrap's restore and the trap wiring inusePopover, which read the opener this guard keeps.Evidence
PopoverFocusReturn.interaction.browser.spec.ts, both added in f5b61f7 before the fix.Scope
Components: Popover
Testing
This round, at e3457e8: the changeset names the case the unit case proves,
show()running again after initial focus has moved inside. No code or test changes. Local checks: prettier on the changeset, check:changesets and changeset coverage.Local checks at 724790f, the parent of this head, on main 1addd72, one worker:
At ceaec78, an earlier version of this head that is not in this pull request, which differs only in two comments:
pnpm review:pre(0 findings; merges clean).At 378f6ad, an earlier version that is not in this pull request, with the same package code (it adds one story):
test:ui --changed): 6,528 passed;pnpm check:repo.Public and internal API delta
None. No export, prop, type or default changes.
Release-path exception: flipped before CodeBunny's review for a planned v0.7.17 hotfix. That release was canceled, so this merges on normal gates for the next scheduled release.