Skip to content

fix(Drawer): a non-modal drawer that unmounts while open returns focus to its opener - #9672

Merged
cixzhang merged 7 commits into
mainfrom
lane4/drawer-unmount-focus
Nov 9, 2026
Merged

cixzhang merged 7 commits into
mainfrom
lane4/drawer-unmount-focus

Conversation

@cixzhang

@cixzhang cixzhang commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Part of #8963 (Drawer; Dialog is #9627 and Lightbox is #9629).

User impact

A non-modal Drawer (hasScrim={false}) that unmounts while it is still open drops focus to the page body instead of returning it to the control that opened it. Keyboard and screen-reader users meet this in two common React patterns:

  • A drawer rendered only while open ({isEditing && <Drawer isOpen …>}), whose own Save button removes it.
  • A drawer removed with the rest of its view.

They then have to find their place again from the top of the page. A close through onOpenChange already returns focus, and so does a modal drawer's focus trap on unmount, so only the non-modal unmount drops it.

Problem and solution fit

  • Why does it fail? An unmount never runs the close path that restores focus. Removing a <dialog> doesn't run the browser's own focus return, so focus inside the drawer falls to the body.
  • Why wasn't it already guarded? The unmount cleanup in useDrawerDialogPresence left the native host and fired the owed close event, but it said "no focus restore on this path". A modal drawer is covered by useFocusTrap's restore, but a non-modal drawer has no trap.
  • Why does it matter? Lost focus on an ordinary flow, which WCAG 2.4.3 asks to keep in a meaningful order.

Expected behavior and authority

Smallest restoration

  • One shared restore: returnFocusIfLost(surface, target) in utils/focusReturn.ts is the rule useFocusTrap already ran on deactivation. It focuses the target only when focus would be lost (isFocusLost) and the target is still in the document. useFocusTrap now calls it, so its behavior is unchanged.
  • Non-modal unmount: the unmount cleanup calls it for the trigger captured at open, once the commit has finished (a microtask), and only when the drawer has left the document.
    • It runs outside the "host still open" check, because removal already hid a popover host.
    • StrictMode's simulated unmount and a hidden <Activity> keep the drawer in the document, so they leave focus alone.
  • Unchanged: focus the user already moved elsewhere stays where it is. The close path, the synthetic close ordering, and the modal host don't change.

Evidence

  • Reproduction before the change: the new non-modal Drawer.test.tsx cases ("Drawer unmounting while open") fail on the base. Their popover stand-ins model the browser: a removed popover host is no longer :popover-open, and a non-modal drawer sets no open attribute. With the call inside the old "host still open" check, they fail too.
  • Result after the change: focus returns to the opener for a non-modal drawer, whether its own control removes it or its view goes. Modal drawers keep returning it through the trap.
  • Representative unchanged path:
    • Focus moved outside first stays there.
    • A StrictMode remount keeps focus inside the open drawer.
    • The Drawer, Dialog, Popover and focus-trap suites pass (346 tests).
  • Regression test or other mutation-sensitive proof: DrawerUnmountFocus.a11y.chromium.spec.ts runs on the new UnmountWhileOpen story in Chromium. Open "Edit row", then click Save, or press Enter on it. The drawer is gone and "Edit row" has focus.

Scope

  • 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

  • pnpm vitest run packages/core/src/Drawer: 91 passed.
  • pnpm check:knowledge: aligned.
  • The Chromium spec runs in CI's scoped a11y leg.

Writing rubric results

Run over the new Drawer FR8 sentence and its verification row:

  • Present tense, one rule per sentence, and the MUST names the observable outcome: focus returns to the captured element.
  • It states the exception ("a development-only simulated unmount is not a removal") so StrictMode behavior is decided by the record.
  • No proposal, history, or pull-request reference is in the record.

…pener

Drawer FR8 (Specs ruling for #8963, same as Dialog): when an open Drawer
unmounts, it returns focus to the captured element if focus would otherwise
be lost. The check runs after the commit, so StrictMode's simulated unmount
and a hidden Activity, which keep the drawer in the document, leave focus
alone. Tests cover modal and non-modal, removal by its own control and with
its view, focus moved outside first, and StrictMode; a Chromium spec covers
click and Enter on the UnmountWhileOpen story.
@vercel

vercel Bot commented Nov 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 Nov 9, 2026 12:51am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 3, 2026
@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 5c32b9c

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
Drawer 50.7 kB (+41 B) · 8.3 kB CSS (0) — ✓ · ✓ · ✓
DrawerHeader 63.4 kB (0) · 12.9 kB CSS (0) — ✓ · ✓ · ✓

Package size: @astryxdesign/core 7.18 MB packed (+248 B); no new dependencies.

API changes: none in this pull request

Committed API report behind this build: cli 29 rows, core 34 rows, theme-butter 1 row, theme-y2k 1 row; run pnpm api:report.

Touched components checked; add ci:run-visual or ci:run-rtl to run the full suites.


Generated by PR Enrichment workflow | View full report

@astracat-bot

astracat-bot Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 9, 2026, 1:07 AM UTC

@cixzhang

cixzhang commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec ba74f3e

@github-actions github-actions Bot removed the needs:spec-owner-review Current knowledge records await owner approval label Nov 3, 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: the unmount focus return does not run in a real browser for the drawers it targets.

  • The new returnFocusAfterRemoval call sits inside the isDrawerHostOpen check (useDrawerDialogPresence.ts:182-188), which is already false once a non-modal (popover) drawer has been removed, so in Chromium the UnmountWhileOpen story still leaves focus on the page body after Save (click or Enter), and moving the call below the check fixes it.
  • The non-modal jsdom cases pass only because the test's showPopover mock sets open, which jsdom never clears on removal, so they need a mock that models removal, and the Chromium spec (skipped on this draft) needs to run green.
  • returnFocusAfterRemoval repeats the restore useFocusTrap already runs for modal drawers (useFocusTrap.ts:297-341), so please extend that shared restore so both presentations make one call, and drop the local copy.
  • The changeset and title say any drawer is fixed, but a modal drawer already returned focus on main (as #8963 notes), so the wording should name non-modal drawers (hasScrim={false}).

The FR8 sentence itself is approved for this head. A non-blocking suggestion on the new doc comment is inline.

[Automated review]

…res the focus trap's rule

CodeBunny on #9672: removal already hid a popover host, so the call inside
the host-open check never ran for a non-modal drawer in a browser. It now
runs outside it. returnFocusIfLost in utils/focusReturn is the restore
useFocusTrap ran on deactivation; the trap and the drawer's unmount both
call it. The unmount tests model popover removal, so they fail with the old
placement. The changeset names non-modal drawers; a modal drawer already
returned focus through its trap.
@cixzhang cixzhang changed the title fix(Drawer): a drawer that unmounts while open returns focus to its opener fix(Drawer): a non-modal drawer that unmounts while open returns focus to its opener Nov 3, 2026
@cixzhang

cixzhang commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 6edf3ac

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

Approve: a non-modal Drawer (hasScrim={false}) removed while open now returns focus to the control that opened it, and the points from the previous review are addressed.

  • The removal check now runs for popover drawers, and the new DrawerUnmountFocus.a11y.chromium.spec.ts covers click and Enter on the UnmountWhileOpen story.
  • Non-blocking: main has since added component:Drawer FR19 and a new import in useDrawerDialogPresence.ts, which conflict with this branch, so keep both when you rebase.

[Automated review]

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

# Conflicts:
#	docs/specs/components/Drawer/Drawer.spec.md
#	packages/core/src/Drawer/Drawer.test.tsx
#	packages/core/src/Drawer/useDrawerDialogPresence.ts
#	packages/core/src/hooks/useFocusTrap.ts
@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 8, 2026
…11y-spec wrapper

Every Playwright browser context stays off remote hosts through the
wrapper, which the lint rule now requires.

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

Approval waits on an engineering owner's decision on the new Drawer FR8 sentence, and two fixes are needed.

  • FR8 now says "When an open Drawer unmounts, it MUST return focus to the captured element if focus would otherwise be lost…". This is new spec text, and the earlier /approve-spec comments name older commits. An engineering owner records the decision by commenting /approve-spec 5f20da09f12fe8ea2b8c22d14aff78a01aa6a0cc; the author may do so as an engineering owner.

Blocking:

  • Regression: when Drawer.test.tsx runs as a whole file, "returns focus to the opener (non-modal, save)" fails with RangeError: Maximum call stack size exceeded, because the keyboard-open test merged from main leaves its Element.prototype.matches spy in place; adding vi.restoreAllMocks() to the file-level afterEach fixes it.
  • Regression: returnFocusAfterRemoval in useDrawerDialogPresence.ts copies the removal focus repair that prepareFocusReturnOnRemoval in utils/focusReturn.ts now provides for Dialog and Lightbox; call that helper from the unmount cleanup and delete the copy.

The fix itself works: in Chromium, focus now returns to the opener after Save, by click or Enter.

[Automated review]

Main's keyboard-open test also replaces matches, so reading it in
beforeEach could capture this suite's own stand-in, which then called
itself. The suite now reads the native method at collection.
@cixzhang

cixzhang commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec c6e3050

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

Changes requested: the non-modal unmount focus return works, and the new FR8 sentence is approved for this head, but the removal repair should reuse the shared helper.

  • Regression: returnFocusAfterRemoval in packages/core/src/Drawer/useDrawerDialogPresence.ts (lines 221-233) re-implements the removal focus repair that prepareFocusReturnOnRemoval in packages/core/src/utils/focusReturn.ts already provides for Dialog and Lightbox, so create that repair in the unmount effect, call it from the cleanup with the captured trigger, and delete the copy.

In Chromium, focus now lands on "Edit row" after Save by click or Enter, and the new non-modal tests fail without the fix and pass with it. With the shared helper in its place, the whole Drawer suite still passes.

[Automated review]

@cixzhang
cixzhang marked this pull request as ready for review November 9, 2026 00:19
@cixzhang
cixzhang marked this pull request as draft November 9, 2026 00:42
prepareFocusReturnOnRemoval already returns focus to an opener when
Dialog or Lightbox leaves the document. The drawer now calls it, and
its own copy and the focus-trap refactor it needed are gone.
@cixzhang

cixzhang commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 5c32b9c

@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; it runs once the pull request is marked ready for review.

A non-modal Drawer removed while it is open now returns focus to its opener through the shared removal repair, and the new tests fail without the fix and pass with it. The earlier request to reuse that shared repair is addressed.

  • Non-blocking: the description's "Smallest restoration" and "Evidence" sections still mention a returnFocusIfLost helper and a useFocusTrap change that this head no longer contains; please update them.

[Automated review]

@cixzhang
cixzhang marked this pull request as ready for review November 9, 2026 01:12
@cixzhang
cixzhang merged commit b1a6f16 into main Nov 9, 2026
130 checks passed
@github-actions
github-actions Bot deleted the lane4/drawer-unmount-focus branch November 9, 2026 06:03
cixzhang added a commit that referenced this pull request Nov 12, 2026
failingSpecs no longer waits after its fourth and last try, so a log that keeps
answering 5xx gives up after 30 s of waits, and a test pins four server errors
in a row. This also corrects the earlier commit's wording: the three pull
requests that now replay a narrow selection (#9672, #10271, #10944) are ones
whose story was their only full-suite trigger, not ones that change only
stories. Their stories are Drawer, TableSelection and Avatar; Button's specs run
because those stories import Button.
cixzhang added a commit that referenced this pull request Nov 12, 2026
… retries a server error (#11317)

* ci: the selection replay record covers the story rule; a log download retries a server error

The record is regenerated with the Chromium lane's story rule (#11250). Every
verdict matches the earlier record: 144 no failure, 4 caught, 4 reviewed, 1 out
of scope. Three pull requests that change only stories now replay a narrow
selection with no failure (#9672, #10271, #10944). GitHub's job-log endpoint
answers 502 now and then, so failingSpecs tries a server error again, up to
four times, before it calls a log unreadable.

* Merge remote-tracking branch 'origin/main' into ci/replay-story-rule

* ci: a log download waits only between tries, not after the last

failingSpecs no longer waits after its fourth and last try, so a log that keeps
answering 5xx gives up after 30 s of waits, and a test pins four server errors
in a row. This also corrects the earlier commit's wording: the three pull
requests that now replay a narrow selection (#9672, #10271, #10944) are ones
whose story was their only full-suite trigger, not ones that change only
stories. Their stories are Drawer, TableSelection and Avatar; Button's specs run
because those stories import Button.

This branch was successfully deployed

1 active deployment
Preview — 5c32b9cc Deployed Nov 9, 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