Skip to content

fix(core): one focus return for Popover and DropdownMenu (popover presentation) and Dialog that never strands focus (AST-116 FR17–FR21, AR1) - #11121

Merged
nynexman4464 merged 7 commits into
mainfrom
nynexman4464/feat/popover-return-focus
Nov 20, 2026
Merged

nynexman4464 merged 7 commits into
mainfrom
nynexman4464/feat/popover-return-focus

Conversation

@nynexman4464

@nynexman4464 nynexman4464 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

#11117 (anchorRef) has merged, so this pull request is based on main. The sizes and the file list below are against #11117's code. Implements spec:AST-116 FR17–FR21 and AR1, which is on main.

User need

Losing focus strands keyboard and screen reader users. Today each layer returns focus its own way, and each one falls to the page body in a common case:

  • a Popover whose opener remounted or was removed;
  • a tour step or coachmark opened by code.

The browser's own restore does not cover these. In Chromium 149, Firefox 151 and WebKit 26.5:

  • it works only for an auto popover or a <dialog> whose opener still exists;
  • everything else ends on the body;
  • it paints a focus ring after mouse input in Chromium and Firefox, against the shipped modality rule;
  • it behaves differently in WebKit after a click inside.

Problem and solution fit

One shared return in utils/focusReturn.ts (trackLayerFocus, isLayerFocusStranded, restoreLayerFocus), used by Popover (through the focus trap), Dialog and DropdownMenu (AST-116 FR17–FR21, AR1, DEC-6).

  • When it acts. Only when focus entered the layer and is now inside it, inside another closing layer, or on nothing. Focus already outside stays put, including focus a caller moved in onHide or onOpenChange.

  • Handoff. The runtime records which layer opened which. A closing layer hands its return to a layer it opened only when that layer holds focus, or is opening and still to take it; the child then inherits the chain, so menu → dialog → the menu trigger works. A child that never took focus (a typeahead's result list), or that focus has left, does not take the return: the closing layer returns focus itself (FR19).

  • Chain. Focus goes to the first candidate that is connected, rendered, not inert (including under an open modal dialog), not inside a closed dialog, and not in a closing layer. An opener, an ancestor or main that cannot take focus gets a temporary tab stop. A trigger or an anchor that cannot take focus gets none: its first control stands in for it, and it is skipped when it has no control (FR20 gap:):

    1. the opener;
    2. the trigger now, or its first control;
    3. the element whose aria-controls names the layer;
    4. the component's own fallback;
    5. the anchor, or its first control;
    6. the opener's former place among its container's tab stops: the stop now at that index, then the one before, the one after, then the first;
    7. the nearest surviving ancestor;
    8. the parent layer;
    9. main.

    It never lands on the body; a development warning fires if nothing qualifies.

  • Removal. A layer removed while open (with its owner, or its whole view) returns focus to its opener when the opener is still on the page: at once for the focus trap, a microtask later for Dialog. Otherwise it waits until the removal is done, so it never lands on an element the same removal is taking away, and it does not warn if nothing survives.

  • Timing. A popover returns focus as it starts to close, while it still holds focus: in the hook's own hide(), and in beforetoggle for a close the browser makes. The browser's restore then has nothing to do, and the ring follows the last input. A modal <dialog> keeps the page inert until close(), so Dialog reads whether focus is stranded first, closes, then returns focus.

  • Move. Popover moves focus to a control with a plain focus, as released (it scrolls the control into view). A container that only stands in (an ancestor, main) gets a temporary tab stop and takes focus without a scroll, so the page does not jump to its top. Dialog and DropdownMenu keep their ring-follows-last-input moves.

  • A surface that loses its anchor (AR1). When a usePopover surface hides because its anchorRef element left the document (FR6) while focus is inside it, focus moves to the trigger when it takes focus, else along the chain.

  • No off switch. There is no option, and the types accept no false.

Current specification

spec:AST-116 FR17–FR21, AR1 and DEC-6 (current, on main). It supersedes the earlier returnFocus option this PR carried.

Record changes here, all in spec:AST-116 unless named:

  • Gap lines this PR meets are removed: FR17 gap:, the earlier FR19 gap: (a Dialog opened from a menu row), the earlier FR20 gap: (a removed opener left focus on the body), the earlier FR21 gap: (no warning named the layer) and AR1 gap:.
  • Gap lines for what is still missing:
    • FR16 gap: gains the sheet's return (a sheet keeps BottomSheet's own return);
    • FR18 gap: (new): a Dialog's close notification runs while the page is inert, so a focus move there does nothing;
    • FR19 gap: (new): a popover that a control opens through show() in the handler or commit that closes that control's own layer does not reach that layer's candidates;
    • FR20 gap: (new text): the chain also tries a component's own stand-in for its opener, and a trigger or an anchor that cannot take focus gets no temporary tab stop;
    • FR21 gap: (new text): a layer removed with its page or view gives no warning; the warning names a Popover by its generated id and a Dialog with no id only as "a layer".
  • Four verification rows name the test files that exist (FR17/FR18, FR19, FR20/FR21, AR1), and two Current-state impact bullets follow.
  • family:overlay-dismissal gains FR9 gap:: a Popover's focus return to a control is a plain focus, which can scroll the control into view, where FR9 says the return does not scroll. That is the released behavior on main; this PR keeps it for a control, because spec:AST-116 FR22 names no change to it, and records the gap.
  • component:Dialog, component:DropdownMenu and component:Popover drop the matching gap lines.

Public delta and caller burden

  • No new public API.
  • Behavior changes where focus used to fall to the body, where a ring showed after mouse input, and where a DropdownMenu focused its trigger though focus never entered the menu.
  • Owner question. The public useFocusTrap hook is the trap that carries Popover's part, so its direct callers get the same return. spec:AST-116 names Popover, Dialog and DropdownMenu only. The release note names useFocusTrap; whether it stays in, and the spec's scope line with it, is the owner's call. A Drawer keeps its own return at close: its trap stands down while the <dialog> is still modal, and the shared return reaches a Drawer only when it is removed while open.
  • DropdownMenu keeps its own pointer-ring handling through the package-internal restoreFocusTo.
  • Internal:
    • useFocusTrapInternal gains getTrigger, getAnchor and layerId, and returns returnFocus and moveFocusOut;
    • useLayer's close-start hook drives it for Popover.

Design and alternatives

  • Rejected: "defer to native, fill gaps". Native covers one of the six cases measured, paints the wrong ring there, and differs by engine. The probe and its results are in the evidence bundle.
  • Rejected: a boolean or value that skips the return (DEC-6).

Evidence

  • New layerFocusReturn.test.tsx:
    • a tour step opened by code → its anchor's control;
    • a trigger remounted while open → the new trigger;
    • an opener removed → the control that took its place;
    • focus already outside → no move;
    • menu row → Dialog → the menu trigger;
    • nested popovers, one layer at a time;
    • every stand-in gone → main, never the body;
    • a layer that closes over an open popup that never took focus returns focus itself;
    • an outer popover closed from a control inside it, with an inner popover still open that focus has left, returns focus to the outer trigger (fails with the earlier condition);
    • a parent that closes while its child is opening and still to take focus moves no focus, and the child's close returns to the parent's opener (fails without the opening flag);
    • a controlled menu whose row opens a Dialog in the same commit → the menu trigger;
    • focus that the hide notification moved stays where it is (FR18, Popover);
    • the default move is a plain focus with no preventScroll for a control (fails with it), and with preventScroll for main;
    • an anchor that takes focus only from script (tabindex="-1") takes it, not the first control inside it;
    • no candidate left → a development warning that names the layer by the id its record carries (fails without it);
    • a candidate that an open modal dialog makes inert is skipped (the browser sets no inert attribute for it);
    • a candidate inside a closed nested dialog is skipped, even when a display utility keeps it laid out.
  • PopoverAnchor.test.tsx (AR1): with focus in a popover whose anchor leaves the document, focus moves to the trigger; with no trigger, to the element focused when it opened. Both fail without the move. When the anchor returns, the surface shows and focus is not moved back.
  • Real browser at this head, Chromium and WebKit:
    • new LayerFocusReturn.interaction.browser.spec.ts, with its fixture stories:
      • a Dialog opened by keyboard from a row of a controlled menu returns focus to the menu trigger when it closes, after the menu and its row are gone (FR19). This proves the outcome. It also passes with the handoff switched off, because the menu then returns focus to its trigger itself before the Dialog takes focus; the handoff rests on the unit test above;
      • a Popover whose trigger was replaced while it was open returns focus to the new trigger (FR20);
    • new test in PopoverAnchor.interaction.browser.spec.ts: a control inside a popover with no trigger removes the popover's anchor; the surface hides and focus goes to the element that held focus when the popover opened; when the anchor is back and the caller shows again, focus is not moved back (AR1). The story gains that control, and its outside controls move clear of the open popover;
    • PopoverFocusReturn.interaction.browser.spec.ts, already on main, still passes: a controlled popover opened from the keyboard returns focus to its trigger on Escape and after a pick.
  • Real browser on an earlier head of this PR, not run again at this head, Chromium 149 and Firefox 151 (app, drive script and probe): all 7 checks passed, including the menu → dialog handoff by keyboard (ring) and by mouse (no ring), and a mouse pick returning to the trigger with no ring. WebKit has not run that script; the bundle includes focus-drive.mjs for a macOS run.
  • Changed expectations in existing tests, all driven by the new timing:
    • DropdownMenu's "ring the browser already drew" test: the menu now returns focus before the browser can draw one, so there is no blur.
    • Dialog's second-document ring test now focuses inside the dialog before Escape, as showModal() does in a browser.
    • DropdownMenu's mock restores focus only while focus is inside the closing layer, as browsers do.
    • Dialog's own pointer-ring tests are unchanged.
  • Unchanged and passing: the Dialog unmount-while-open case table, the iframe and Picture-in-Picture document cases, Typeahead/PowerSearch's focus-stays-on-input cases, and useFocusTrap.test.tsx.

Scope

Components: Popover, usePopover, Dialog, DropdownMenu, useFocusTrap, Drawer (only when removed while open), utils/focusReturn (internal)

  • One capability is added.
  • Unrelated fixes, cleanup, and policy changes were removed or split.
  • Consumer docs, focused tests, and migration evidence ship with the feature.
  • Public text and artifacts contain no internal Meta context.

Testing

  • pnpm exec vitest run --project ui packages/core/src/utils packages/core/src/Popover packages/core/src/Dialog packages/core/src/DropdownMenu packages/core/src/Typeahead packages/core/src/Layer packages/core/src/hooks packages/core/src/PowerSearch packages/core/src/Drawer packages/core/src/BottomSheet: 5,416 passed
  • tsc --noEmit (core), eslint: clean
  • pnpm exec vitest run --project node packages/cli/test/experimental-isolation.test.mjs packages/cli/test/golden-reads.test.mjs, the CLI golden suites: 68 passed
  • pnpm test:interaction for the three specs above: Chromium 7 passed; WebKit 7 passed (Playwright's container image)
  • Not covered: Firefox for the in-repo specs; in a real browser at this head, a removed opener's other stand-ins (FR20), the AR1 case with a trigger, and the focus ring after a mouse close (the drive script above covered the ring on an earlier head); none of the new browser tests was run against the base.

Changes since the last review

  • The CLI search golden for close counts one more match, 52 where it was 51: DropdownMenu's doc now says where focus goes on close. CI's test-node caught it; the golden's two files change by that number only.
  • Restacked onto the anchorRef head of feat(usePopover): anchorRef names an anchor apart from the trigger (AST-116 FR1–FR3, FR5, FR6) #11117. The code that moved focus out of a surface hidden by anchorOutOfViewBehavior is gone with that option.
  • A closing layer hands its return only to a child that holds focus now, or that is opening and still to take focus. Before, a child that focus had entered and left also took it, and focus fell to the page body when the parent closed.
  • AR1: focus leaves a surface that hides because its anchor left the document.
  • The default move no longer passes preventScroll for a control, so a Popover's return scrolls its trigger into view, as released; a stand-in container still takes focus without a scroll.
  • The development warning names the layer by its record's id when the layer's element has none (a Popover's generated id).
  • takesFocusAsIs, the chain's test for an element that takes focus with no temporary tab stop, now lives in this PR (utils/focusReturn.ts), with its unit tests.
  • Records: four gap lines added or extended (FR16, FR18, FR19, FR20), and the verification rows name what exists.
  • isModalDialog in utils/nodeType.ts is now the one test for a modal dialog: isOpenModal in hooks/useAnnounce.ts and BottomSheet's stack call it. BottomSheet keeps its aria-modal fallback, which jsdom needs.
  • The opener's former places are looked for in its nearest ancestors only, so an opener that is not a tab stop no longer makes every ancestor up to the body be queried on each open.
  • Two comments in Dialog that restated names are gone.

Writing rubric results

The record changes are gap lines (removed where this PR meets the requirement, added or reworded where part is still missing), verification rows and two Current-state impact bullets.

  • R1 narration: none in the changed lines.
  • R2 research in the record: none.
  • R3 evidence naming a change: none.
  • R4 rejected alternatives: none added.
  • R5 pull request or issue references: none.
  • R6 ruled open questions still listing options: none.
  • R7 project status: only gap lines, each stating an observable mismatch.

First-load size (INV10)

pnpm size:pages --page popover=Popover,Button,Text, this PR's change in gzip bytes against #11117:

docs form menu table popover
+1 +15 +2,048 +1,598 +2,039

Focus return is not optional (it always runs), so INV10's 0.3 KB allowance does not apply; this is the shared-module growth INV10 asks to report.

@vercel

vercel Bot commented Nov 10, 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 20, 2026 12:40am UTC

Request Review

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

astracat-bot Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 20, 2026, 12:53 AM UTC

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

Needs an owner decision on the spec, plus three fixes.

returnFocus adds a caller-chosen focus target that no current record covers yet: the Popover contract returns focus to the trigger, and spec:AST-113 FR17 is still draft with the input name open. An engineering owner needs to approve that direction, by promoting FR17 or by commenting /approve-spec <full head SHA> here (the author can do this as an engineering owner).

Blocking either way:

  • In Chromium, Escape and the fallback close button still put focus on the opener with returnFocus="anchor" because hidePopover() restores focus before the focus trap reads the target, which the jsdom tests cannot show, so please make the decision in the hide path and add a browser interaction test for Escape, the close button and an outside press.
  • With presentation="bottom-sheet" (or "adaptive" on a phone), returnFocus never reaches the sheet and focus goes back to the trigger, so please pass the target to the sheet (for example through BottomSheet's finalFocusRef) or document the exception in the JSDoc, the docs and the changeset.
  • focusableWithin re-implements the focus trap's getFocusableElements without its visibility filter, so when the anchor's first control is hidden, focus ends on the page body; please reuse the shared helper and remove the copy.

[Automated review]

Comment thread packages/core/src/hooks/useFocusTrap.ts Resolved
Comment thread packages/core/src/Popover/Popover.tsx Resolved
@nynexman4464
nynexman4464 force-pushed the nynexman4464/feat/popover-anchor branch from 88e86f0 to b905360 Compare November 11, 2026 00:18
@nynexman4464
nynexman4464 force-pushed the nynexman4464/feat/popover-return-focus branch 3 times, most recently from 45a09cf to ad85352 Compare November 11, 2026 01:14
@nynexman4464 nynexman4464 changed the title feat(Popover): returnFocus chooses where focus goes on close (AST-113 FR17) fix(core): one focus return for Popover, Dialog and DropdownMenu that never strands focus (AST-113 FR17–FR21) Nov 11, 2026
@nynexman4464
nynexman4464 changed the base branch from nynexman4464/feat/popover-anchor to nynexman4464/feat/popover-rect-anchor November 11, 2026 01:14

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

Needs an engineering owner's decision on the spec, plus three fixes.

The shared focus return (the stand-in chain after the opener, the parent-to-child handoff and the rule that nothing turns it off) is defined only in draft spec:AST-113 FR17–FR21 and DEC-6, while the current Popover, Dialog and DropdownMenu contracts return focus only to the trigger or invoker. An engineering owner records the decision by approving and promoting AST-113 or by commenting /approve-spec <full head SHA> on this pull request (the author can do this as an engineering owner), and should also say whether the public useFocusTrap hook and Drawer, which now use the same chain, are in scope.

Blocking either way:

  • The Dialog docs and the changeset tell callers to focus their target in onOpenChange, but Dialog calls it while the modal still makes the page inert, so the call does nothing and focus goes to the opener; please document a route that works after close(), or say there is none yet.
  • The description and changeset present the menu-row Dialog and the manual (hasLightDismiss={false}) popover as falling to the page body, yet both already return focus to their trigger on the base branch in Chromium and WebKit (the new menu-row test also passes there), and the "one rule" does not reach the opt-in bottom-sheet presentations, so please narrow the changeset to the popover-presentation cases this change fixes.
  • utils/focusReturn.ts adds its own reachability test (tabStopsIn, isRendered, canTakeReturnedFocus) beside the focus trap's getFocusableElements, without its aria-hidden rule, so returned focus can land on a control screen readers skip; please share one helper and remove the copy.

Automated review (maintainer link)

Comment thread packages/core/src/Dialog/Dialog.doc.mjs Resolved
Comment thread .changeset/layer-focus-return.md Resolved
Comment thread packages/core/src/utils/focusReturn.ts Resolved

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

Needs an engineering owner's decision on the spec, plus four fixes.

The shared focus return's stand-in chain after the opener, the parent-to-child handoff and the rule that nothing turns it off are defined only in draft spec:AST-113 FR17–FR21 and DEC-6, while the current Popover, Dialog and DropdownMenu contracts return focus only to the trigger or invoker. An engineering owner records the decision by approving and promoting AST-113 or by commenting /approve-spec <full head SHA> on this pull request (the author may do so as an engineering owner), and should also say whether the public useFocusTrap hook and Drawer, which now use the same chain, are in scope.

Blocking either way:

  • The Dialog docs and the changeset tell callers to focus their target in onOpenChange, but Dialog calls it while the modal still makes the page inert, so the call does nothing and focus goes to the opener; please document a route that works after close(), or say there is none yet.
  • The description and changeset present the menu-row Dialog and the manual (hasLightDismiss={false}) popover as falling to the page body, yet both already return focus to their trigger on the base branch (the new menu-row test passes there), and the "one rule" does not reach the bottom-sheet presentations, so please narrow the changeset to the cases this change fixes.
  • utils/focusReturn.ts adds its own reachability test (tabStopsIn, isRendered, canTakeReturnedFocus) beside the focus trap's getFocusableElements, without its aria-hidden rule, so returned focus can land on a control screen readers skip; please share one helper and remove the copy.
  • Miss: DropdownMenu's restoreFocusTo moves focus without the temporary tab stop the default move adds, so the ancestor, parent-layer and main stand-ins cannot take focus and a menu removed while open still leaves focus on the page body in Chromium and WebKit; please add the tab stop for caller-supplied moves and cover DropdownMenu in the main test.

Automated review (maintainer link)

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

Needs an engineering owner's decision on the spec, plus five fixes.

The shared focus return's stand-in chain after the opener, the parent-to-child handoff, the development warning and the rule that nothing turns the return off are defined only in draft spec:AST-113 FR17–FR21 and DEC-6, while the current Popover, Dialog and DropdownMenu contracts return focus only to the trigger or invoker. An engineering owner records the decision by approving and promoting AST-113 or by commenting /approve-spec <full head SHA> on this pull request (the author may do so as an engineering owner), and should also say whether the public useFocusTrap hook and Drawer, which now use the same chain, are in scope.

Blocking either way:

  • Miss: DropdownMenu's restoreFocusTo moves focus without the temporary tab stop the default move adds, so the ancestor, parent-layer and main stand-ins cannot take focus and a menu removed while open still leaves focus on the page body; please add the tab stop for caller-supplied moves and cover DropdownMenu in the main test.
  • The Dialog docs and the changeset tell callers to focus their target in onOpenChange, but Dialog calls it while the modal still makes the page inert, so the call does nothing and focus goes to the opener; please document a route that works after close(), or say there is none yet.
  • Miss: a scrim Drawer's trap deactivates while its dialog is still modal for the slide-out, so every stand-in is inert and each close logs "no element could take focus … focus is left on the page body" although Drawer then returns focus to its opener; please skip the trap's return while its container is an open modal dialog, or keep Drawer off the chain until it is in scope.
  • utils/focusReturn.ts adds its own reachability test (tabStopsIn, isRendered, canTakeReturnedFocus) beside the focus trap's getFocusableElements, without its aria-hidden rule, so returned focus can land on a control screen readers skip; please share one helper and remove the copy.
  • The description and changeset present the menu-row Dialog and the manual (hasLightDismiss={false}) popover as falling to the page body, yet both already return focus to their trigger on the base branch (the new menu-row and nested manual-popover tests pass there), and the "one rule" does not reach the bottom-sheet presentations, so please narrow the changeset to the cases this change fixes.

Automated review (maintainer link)

Comment thread packages/core/src/hooks/useFocusTrap.ts Resolved

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

Needs an engineering owner's decision on the spec, plus six fixes.

The shared focus return's stand-in chain after the opener, the parent-to-child handoff, the development warning and the rule that nothing turns the return off are defined only in draft spec:AST-113 FR17–FR21 and DEC-6, while the current Popover, Dialog and DropdownMenu contracts return focus only to the trigger or invoker. An engineering owner records the decision by approving and promoting AST-113 or by commenting /approve-spec <full head SHA> on this pull request (the author may do so as an engineering owner), and should also say whether the public useFocusTrap hook and Drawer, which now use the same chain, are in scope.

Blocking either way:

  • The new closed-dialog check in canTakeReturnedFocus (utils/focusReturn.ts) skips everything inside a non-modal Drawer or BottomSheet, which is a <dialog> shown with showPopover() that never has open, so closing a Popover opened from one sends focus to main instead of its trigger; please keep the focus trap's popover-open exception and add a test for it.
  • DropdownMenu's restoreFocusTo moves focus without the temporary tab stop the default move adds, so the ancestor, parent-layer and main stand-ins cannot take focus and a menu removed while open still leaves focus on the page body; please add the tab stop for caller-supplied moves and cover DropdownMenu in the main test.
  • The Dialog docs and the changeset tell callers to focus their target in onOpenChange, but Dialog calls it while the modal still makes the page inert, so the call does nothing and focus goes to the opener; please document a route that works after close(), or say there is none yet.
  • A scrim Drawer's trap deactivates while its dialog is still modal for the slide-out, so every stand-in is inert and each close logs "no element could take focus … focus is left on the page body" although Drawer then returns focus to its opener; please skip the trap's return while its container is an open modal dialog, or keep Drawer off the chain until it is in scope.
  • utils/focusReturn.ts adds its own reachability test (tabStopsIn, isRendered, canTakeReturnedFocus) beside the focus trap's getFocusableElements, and it has already drifted (no aria-hidden rule, no popover-open exception), so please share one helper and remove the copy.
  • The description and changeset present the menu-row Dialog and the manual (hasLightDismiss={false}) popover as falling to the page body, yet both already return focus to their trigger on the base branch (the new menu-row and nested manual-popover tests pass there), and the "one rule" does not reach the bottom-sheet presentations, so please narrow the changeset to the cases this change fixes.

Automated review (maintainer link)

Comment thread packages/core/src/utils/focusReturn.ts Resolved
@nynexman4464
nynexman4464 force-pushed the nynexman4464/feat/popover-rect-anchor branch from 92976e6 to f2bbdae Compare November 11, 2026 15:43
@nynexman4464
nynexman4464 force-pushed the nynexman4464/feat/popover-return-focus branch from 923cd1e to 2ffbdf5 Compare November 11, 2026 15:43
@nynexman4464 nynexman4464 changed the title fix(core): one focus return for Popover, Dialog and DropdownMenu that never strands focus (AST-113 FR17–FR21) fix(core): one focus return for Popover, Dialog and DropdownMenu that never strands focus (AST-116 FR17–FR21) Nov 11, 2026
@nynexman4464

Copy link
Copy Markdown
Contributor Author

/approve-spec 74e7d5c

@github-actions github-actions Bot removed the needs:spec-owner-review Current knowledge records await owner approval label Nov 19, 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.

Approved pending CI: CI has not run on this head yet, because the pull request is a draft.

The engineering owner's /approve-spec at this head settles the two readings the AST-116 record left open: the public useFocusTrap hook and a scrim Drawer removed while open use the shared chain, and FR20 gains the component's own stand-in step and the first-control reading for a trigger or anchor. No blocking defect remains, and the merge of the main branch changes nothing this pull request does. In the stack, this pull request lands only when #11117 below it is approved too.

  • Two non-blocking notes from the last review still apply: a misplaced comment in usePopover.tsx, and isInsideOrLost repeating isFocusLost in the focus-return utility (see the earlier inline threads).
Claims and files checked

Review mode: full; reason: spec-record

Claims:

  • Popover and DropdownMenu in their popover presentation, and Dialog, move focus at close only when the close would strand it: verified (the stranded check in the focus-return utility; jsdom tests).
  • Focus that used to fall to the page body goes, in the listed order, to the opener, the trigger, the aria-controls owner, the component's own stand-in, the anchor, the former place, an ancestor, the parent layer, then main: verified (the candidate chain; in Chromium a remounted trigger takes focus where the main branch leaves it on the body).
  • A DropdownMenu no longer focuses its trigger when focus never entered the menu: verified (the menu's own refocus is removed and the shared return needs focus to have entered).
  • useFocusTrap returns focus the same way, and a Drawer gets the shared return only when it is removed while open: verified (the public hook is the internal trap, which stands down on a modal dialog).
  • There is no option to turn focus return off: verified.
  • Redirect focus in onOpenChange or in usePopover's onHide, and for a Dialog after it closes: verified (hide-notification test; Dialog docs).
  • A Popover or DropdownMenu returns focus before the browser's own restore, so its trigger rings only after keyboard input: verified (close-start return; the Chromium keyboard path rings; the pointer-pick test shows no ring).
  • The browser's own restore leaves focus on the body when the opener is gone: verified in Chromium (main branch, remounted trigger); the Firefox and WebKit figures describe the author's own earlier measurement.
  • A closing layer hands its return only to a child that holds focus or is still opening: verified (code; tests).
  • An opener, ancestor or main gets a temporary tab stop, while a trigger or anchor that cannot take focus hands it to its first control: verified (code; tests).
  • Focus never lands on the body, and a development warning names the layer when nothing qualifies: verified (code; test).
  • A layer removed while open returns focus to a surviving opener at once, else after the removal: verified (code; the Dialog unmount cases pass).
  • Dialog reads whether focus is stranded before it closes, then returns it: verified (code).
  • A control takes a plain focus, and a stand-in container takes one without scrolling: verified (tests).
  • Focus leaves a surface hidden because its anchor left the document: verified (jsdom; Chromium).
  • No new public API: verified (generated API comparison; its one row is the anchor option from the pull request below).
  • The record edits are the listed gap lines, verification rows and impact bullets: verified (diff).
  • One modal-dialog helper now serves the focus return, the modal announcer and the sheet stack: verified.
  • The open-time scan looks only at the opener's nearest ancestors: verified (four ancestors).
  • The listed suites pass: verified (233 files, 5,426 tests at this head; the description's 5,416 predates the merge of the main branch).
  • Typecheck and lint are clean: verified (core build with tsc; eslint on the changed files).
  • The new browser specs pass in Chromium and WebKit: the Chromium outcomes are verified by this review's runs of the specs' steps; WebKit could not run here.
  • First-load size growth on the reference pages: verified (docs +1 B, form +15 B, menu +2,049 B, table +1,598 B, popover +2,039 B gzip against the pull request below).

Files:

  • .changeset/layer-focus-return.md: clear
  • apps/storybook/stories/LayerFocusReturn.stories.tsx, apps/storybook/stories/PopoverAnchorRef.stories.tsx: clear
  • docs/families/overlay-dismissal.md: clear (gap line; the merge keeps both sides)
  • docs/specs/AST-116-popover-anchoring/spec.md: clear (owner-approved at this head)
  • docs/specs/components/Dialog/Dialog.spec.md, DropdownMenu/DropdownMenu.spec.md, Popover/Popover.spec.md: clear
  • internal/eslint-plugin-astryx/no-comment-provenance.baseline.json: clear (generated)
  • packages/core/src/BottomSheet/bottomSheetStack.ts, hooks/useAnnounce.ts, utils/nodeType.ts: clear
  • packages/core/src/Dialog/Dialog.doc.mjs, Dialog.test.tsx, Dialog.tsx: clear
  • packages/core/src/DropdownMenu/DropdownMenu.doc.mjs, DropdownMenu.test.tsx, DropdownMenu.tsx: clear
  • packages/core/src/Layer/__tests__/LayerFocusReturn.interaction.browser.spec.ts: clear
  • packages/core/src/Popover/Popover.doc.mjs, usePopover.doc.mjs, PopoverAnchor.test.tsx, __tests__/PopoverAnchor.interaction.browser.spec.ts: clear
  • packages/core/src/Popover/usePopover.tsx: clear (one non-blocking comment note)
  • packages/core/src/hooks/focusableSelector.ts, hooks/useFocusTrap.ts: clear
  • packages/core/src/utils/focusReturn.ts: clear (one non-blocking duplicate-helper note)
  • packages/core/src/utils/focusReturn.test.ts, utils/layerFocusReturn.test.tsx: clear

Automated review (maintainer link)

@nynexman4464

Copy link
Copy Markdown
Contributor Author

/approve-spec fb5e34e

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

This pull request is reviewed as part of its stack; see the review on #12891.

Automated review (maintainer link)

@nynexman4464

Copy link
Copy Markdown
Contributor Author

/approve-spec 6663322

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

This pull request is reviewed as part of its stack; see the review on #13015.

Automated review (maintainer link)

@nynexman4464
nynexman4464 marked this pull request as draft November 20, 2026 00:30
@nynexman4464

Copy link
Copy Markdown
Contributor Author

/approve-spec ccdeb5c

@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 on this head yet because the pull request is a draft, and marking it ready for review lets the required checks run.

Popover, usePopover, DropdownMenu (popover presentation) and Dialog now return stranded focus through one chain that ends at main, and the engineering owner's spec approval at this head covers the readings the spec text left open (the public useFocusTrap hook and a scrim Drawer removed while open, and the recorded fallback deviations). In Chromium and Firefox, a remounted trigger and an anchor removed with focus inside now land on the new trigger and the opener, where the base leaves focus on the page body.

Two non-blocking points remain from earlier comments:

  • usePopover.tsx:561: // Core layer for popover positioning now sits above anchorMissingRef, and the comment at 565–569 opens with a 38-word sentence; move the first and split the second.
  • focusReturn.ts:478: isInsideOrLost repeats isFocusLost in the same module; one definition is enough.
Claims and files checked

Review mode: full; reason: other:previous review was a stack pointer

Claims:

  • Popover and DropdownMenu (popover presentation) and Dialog move focus only when the close would strand it: verified (isLayerFocusStranded, utils/focusReturn.ts:684; Dialog reads it before close(), Dialog.tsx:785).
  • The fallback order (opener, trigger, aria-controls owner, component stand-in, anchor, former place, ancestor, parent layer, main): verified (focusReturn.ts:615-664).
  • A DropdownMenu no longer focuses its trigger when focus never entered the menu: verified (its own refocus in the hide handler is removed; the trap's record returns only after focus entered).
  • The useFocusTrap hook returns focus the same way: verified (useFocusTrap.ts:503-507).
  • A Drawer keeps its own return at close and gets the shared one only when removed while open: verified (useFocusTrap.ts:655 stands down while the dialog is still modal).
  • No option turns focus return off: verified (no new prop or option; the API report lists no change).
  • Focus moved in onOpenChange or onHide stays; a Dialog needs a move after it closes: verified (unit test for the hide notification; Dialog's inert-page gap is recorded).
  • A Popover or DropdownMenu returns focus before the browser's own restore: verified (useLayer's close-start hook runs in hide() and in beforetoggle); the no-ring-after-mouse result was not re-run at this head.
  • Handoff only to a child that holds focus or is still opening: verified (focusReturn.ts handoff loop; jsdom tests).
  • Removed while open: the trap returns to a present opener at once, Dialog a microtask later, with no warning when nothing survives: verified.
  • Plain focus for a control, no-scroll focus with a temporary tab stop for a container: verified (focusReturnTarget, focusCandidate).
  • Focus leaves a surface whose anchor left the document: verified (the two new tests fail on the base and pass on the head; Chromium and Firefox checks).
  • The warning names the layer by its record's id: verified (nameOf).
  • One modal-dialog test in utils/nodeType.ts, used by the announcer and the sheet stack: verified.
  • No new public API: verified (API report: 0 rows for Core and the seven themes).
  • The CLI search golden for "close" counts 52: verified (golden suite passes).
  • Record changes (gap lines removed and added, verification rows, impact bullets): verified against the diff.
  • First-load size deltas: reported, not re-measured.
  • Full-suite and WebKit counts: not re-run; the touched suites pass here (653 tests).

Files (31): every file visited; all clear except the two non-blocking notes above (packages/core/src/Popover/usePopover.tsx, packages/core/src/utils/focusReturn.ts).

Automated review (maintainer link)

@nynexman4464
nynexman4464 marked this pull request as ready for review November 20, 2026 01:02
@nynexman4464
nynexman4464 merged commit 416b167 into main Nov 20, 2026
159 checks passed
@github-actions
github-actions Bot deleted the nynexman4464/feat/popover-return-focus branch November 20, 2026 06:06

This branch was successfully deployed

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

Labels

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