Skip to content

fix(Popover): usePopover keeps its focus-return target when show() reruns - #11682

Merged
cixzhang merged 3 commits into
meta:mainfrom
vjeux:fix/popover-focus-return
Nov 14, 2026
Merged

cixzhang merged 3 commits into
meta:mainfrom
vjeux:fix/popover-focus-return

Conversation

@vjeux

@vjeux vjeux commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

User impact

usePopover loses its focus-return target when show() 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

  • Why focus is lost: 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.
  • Why it harms the task: after a choice, the next key press acts on the page, not near the control the person used.
  • Why that matters: focus return keeps keyboard and screen reader users oriented.

The fix keeps the opener when show() runs again with focus already inside.

Expected behavior and authority

component:Popover AR4: "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-dismissal FR9 returns focus to the member's return target when it would otherwise be lost.

Smallest restoration

One guard in usePopover's show(): 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 rewrites useFocusTrap's restore and the trap wiring in usePopover, which read the opener this guard keeps.

Evidence

  • Reproduction before the change: at f5b61f7 (tests only), the unit case fails in 3 of 3 runs, with focus left on the control inside. The browser case passes on main too, as every controlled shape tried in Chromium and WebKit does, so it guards against a regression.
  • Result after the change: at 724790f, the unit case passes in 3 of 3 runs. The browser case passes in both engines (Testing).
  • Representative unchanged path: an uncontrolled popover still returns focus to its trigger on Escape (existing unit case). Popover's keyboard and touch interaction specs pass in both engines.
  • Regression test or other mutation-sensitive proof: a unit case and PopoverFocusReturn.interaction.browser.spec.ts, both added in f5b61f7 before the fix.

Scope

Components: Popover

  • 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

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:

  • the unit case, three runs: 3 passed (at f5b61f7, before the fix: 3 failed, at the trigger-focus check);
  • strict eslint and prettier on the changed files, and the core typecheck.

At ceaec78, an earlier version of this head that is not in this pull request, which differs only in two comments:

  • the browser case, every case three times: Chromium 6 passed; WebKit under the CI profile 6 passed. Before the fix, at e0c5f5f (also not in this pull request), the same;
  • the Popover units: 149 passed;
  • check:changesets, changeset coverage, and 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):

  • Popover's keyboard and touch interaction specs: Chromium 29 passed; WebKit 20 passed, 9 skipped;
  • Popover's a11y specs in Chromium: 36 passed;
  • the Popover, Layer and focus-trap units: 883 passed; the tests the change reaches (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.

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.
@vercel

vercel Bot commented Nov 14, 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 Nov 14, 2026 5:44am UTC

Request Review

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

astracat-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 14, 2026, 6:35 AM UTC

@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec 724790f

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · e3457e8

Storybook · Sandbox · built from this PR's head

Modified components

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

@cixzhang cixzhang added ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) ci:run-a11y Run the accessibility suite on this PR; write access, next push (AST-030 FR12) labels Nov 14, 2026
@vjeux
vjeux marked this pull request as ready for review November 14, 2026 04:55

@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.

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 main that path already returns focus to the trigger (the Confirmation story in Chromium, and this pull request's own browser spec passes on main), 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 main focus returns to the trigger (Confirmation story in Chromium; the new browser spec passes on main).
    • Changeset: it now returns focus to its trigger. Verified for a rerun of show() (usePopover.tsx:614-620 and 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 main too and guards against a regression. Verified in Chromium, where the spec passes on main and 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.
  • 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 on main and passes here.
    • packages/core/src/Popover/__tests__/PopoverFocusReturn.interaction.browser.spec.ts: clear; a regression guard that also passes on main.

Automated review (maintainer link)

@vjeux
vjeux marked this pull request as draft November 14, 2026 05:31
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.
@vjeux vjeux changed the title fix(Popover): a controlled popover returns focus to its trigger on close fix(Popover): usePopover keeps its focus-return target when show() reruns Nov 14, 2026
@cixzhang

Copy link
Copy Markdown
Contributor

/approve-spec e3457e8

@vjeux
vjeux marked this pull request as ready for review November 14, 2026 05:42
@cixzhang cixzhang added ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) and removed ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) labels Nov 14, 2026

@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.

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 of show() (it rewrites useFocusTrap's focus restore and the trap wiring in usePopover, 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, rewrites useFocusTrap's restore and the trap wiring in usePopover, which read the opener this guard keeps; it changes no line of show()."

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: usePopover 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 on main and 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; on main the 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 main and 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 of show().
    • The unit case fails before the fix and passes after it, three runs each. Verified.
    • The browser case passes on main too and guards against a regression. Verified in Chromium (same end state on main and 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.
  • 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 on main and passes here.
    • packages/core/src/Popover/__tests__/PopoverFocusReturn.interaction.browser.spec.ts: clear; a regression guard that also passes on main.

Automated review (maintainer link)

@vjeux
vjeux marked this pull request as draft November 14, 2026 06:14
@vjeux

vjeux commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Reworded in the description: #11121 rewrites useFocusTrap's restore and the trap wiring in usePopover, which read the opener this guard keeps. The description no longer makes a claim about show(), since #11121 does add one call there (bindAnchor(true) after the opener assignment).

@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.

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 on main and 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; on main the 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 main and 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 main too 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.
  • 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.

Automated review (maintainer link)

@vjeux
vjeux marked this pull request as ready for review November 14, 2026 18:59
@cixzhang
cixzhang merged commit 67379ec into meta:main Nov 14, 2026
207 of 213 checks passed
@vjeux
vjeux deleted the fix/popover-focus-return branch November 14, 2026 19:27

This branch was successfully deployed

1 active deployment
Preview — e3457e8f Deployed Nov 14, 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 A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) 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.

2 participants