Skip to content

fix(Dialog): capture the opener only on the closed-to-open edge - #5642

Merged
cixzhang merged 4 commits into
meta:mainfrom
HelloOjasMutreja:fix/dialog-trigger-capture-order
Nov 9, 2026
Merged

cixzhang merged 4 commits into
meta:mainfrom
HelloOjasMutreja:fix/dialog-trigger-capture-order

Conversation

@HelloOjasMutreja

@HelloOjasMutreja HelloOjasMutreja commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #6607, which fixed #5637 and is merged. This PR no longer claims to fix #5637; see "Why this was re-cut".

Why this was re-cut

This PR started as a fix for #5637. #6607 landed the same fix (capture the opener in a layout effect so it beats a descendant's passive autofocus), so I re-cut this as the one gap I found in it.

Problem

Dialog captures the opener in a layout effect that runs whenever isOpen or isInline changes. Under React StrictMode the simulated unmount/remount re-runs that layout effect after the open commit's passive effects. By then Dialog has focused the DialogHeader title, so the second run takes the title as the opener.

Since main now focuses the title only after showModal(), a close while the dialog stays mounted already returns focus to the opener in Chromium and Firefox. The case still broken on main is a Dialog with a DialogHeader that is first mounted open and then removed while open (rendered only while open): the removal repair returns focus to the captured title, which is leaving, so focus ends up on the page body. StrictMode's extra effect run does not happen in production builds, so this is a development-mode problem.

Change

Only the closed-to-open edge captures the opener, tracked with a wasOpenRef that survives the StrictMode re-run. It starts false, so a dialog first mounted open still captures on its first commit, and it is reset when the dialog closes or goes inline, so the next open captures a fresh opener.

Testing

  • keeps the opener when StrictMode re-runs the capture on a dialog first mounted open: mounts a Dialog open with a DialogHeader inside StrictMode, closes it, and expects focus on the opener. It fails on current main (focus ends on <body>) and passes with the change; removing the guard fails it again.
  • captures a fresh opener on each open, not only the first: opens and closes from one opener, then from a second, and expects each close to return to its own opener. A guard that never resets fails it.
  • The Dialog, AlertDialog, Drawer and BottomSheet suites pass (503 tests), plus core typecheck, eslint and prettier.

Limits

  • This does not cover content that focuses itself through React's autoFocus attribute, such as a TextInput with hasAutoFocus. React applies autoFocus during the layout phase, children first, so it can run before the Dialog's own layout effect. I checked that this fails on main too; it is a separate problem and not changed here.
  • This is focus behavior with no rendered change, so there is no screenshot. I verified it in jsdom only; I could not run Storybook here (a native out-of-memory crash on my machine).

Review outcomes at df70d8d

One row per point in the earlier change request (at 5a25b25), checked at this exact head. (No code change since 89ba418; this head rewords the changeset and one comment.)

Requested outcome Evidence at df70d8d
A Dialog first mounted with isOpen={true} still restores focus through Dialog's owner wasOpenRef starts false, so the first commit of a dialog mounted open takes the capture branch. AlertDialog.test.tsx "returns focus to the trigger when it closes" and Dialog.test.tsx "keeps the opener when StrictMode re-runs the capture on a dialog first mounted open" pass at this head (Dialog and AlertDialog suites, 221 tests).
Both first-mount-open and closed→open paths capture the external invoker "keeps the opener when StrictMode re-runs the capture on a dialog first mounted open" covers the first-mount path; "captures a fresh opener on each open, not only the first" covers repeated closed→open edges from two different openers.
Keep the closed-header guard The guard lives in DialogHeader.tsx on main (the title focuses itself only when its native dialog is already open; Dialog focuses it after showModal()). This PR does not touch DialogHeader.tsx; that file is identical to main at this head.

CI at this head: pending; the drain adds ci:run and ci:run-a11y.

Maintainer carry (@cixzhang)

Carried forward on this branch with @HelloOjasMutreja's commits kept: main merged (the Dialog.test.tsx import conflict takes main's import list, which adds useLayoutEffect). The changeset and this description now name the case still fixed after main's title-focus change (a dialog removed while open), and the effect comment keeps only its reason. No other code change.

(Posted by Cindy's agent on her behalf.)

@vercel

vercel Bot commented Aug 28, 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 7:15am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 28, 2026
@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Aug 28, 2026
github-actions Bot added a commit that referenced this pull request Aug 28, 2026
@github-actions

github-actions Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · df70d8d

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
richtext/RichTextEditorToolbar 278.1 kB (+35 B) · 454 B CSS (0) — — · — · —
lab/CommentThread 343.9 kB (+31 B) · 2.4 kB CSS (0) — — · — · —
Dialog 23.9 kB (+27 B) · 6.9 kB CSS (0) — ✓ · ✓ · —
6 more components
Component Size LoC Visual · A11y · RTL
CommandPalette 73.3 kB (+26 B) · 12.7 kB CSS (0) — — · — · —
AlertDialog 105.5 kB (+24 B) · 14.6 kB CSS (0) — — · — · —
lab/Comments 350.8 kB (+6 B) · 2.5 kB CSS (0) — — · — · —
DialogHeader 63.2 kB (0) · 11.5 kB CSS (0) — ✓ · ✓ · ✓
PowerSearch 287.0 kB (0) · 24.1 kB CSS (0) — — · — · —
Tokenizer 91.5 kB (0) · 18.8 kB CSS (0) — — · — · —

Package size: @astryxdesign/core 7.20 MB packed (+324 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

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

Dialogs first mounted with isOpen={true} no longer restore focus through Dialog's owner. useRef(isOpen) starts at true, so the capture branch is skipped; the existing AlertDialog focus-return test fails at this head and after a clean merge with current main. Please capture the external invoker for both first-mount-open and closed→open paths while keeping the closed-header guard.

[Reviewed by Robohands]

@HelloOjasMutreja

Copy link
Copy Markdown
Contributor Author

Fixed in be601f9.

The claim was accurate: `wasOpenRef` was seeded with `isOpen`, so a Dialog first rendered already open (AlertDialog's case) had `wasOpenRef.current` already true on that first render, and the rising-edge check (`isOpen && !wasOpenRef.current`) never fired. The trigger was never captured, so focus restoration on close had nothing to restore to.

Changed the seed to `false`, so the very first render of an already-open Dialog is still treated as a rising edge and captures the trigger, while a Dialog that re-renders while staying open (or stays closed) still doesn't re-capture. The closed-header autofocus guard from the original commit was untouched, since that part wasn't in question.

Verified locally: full `Dialog` suite (78/78) and `AlertDialog` suite (47/47) pass, including the focus-return test. Typecheck and lint on `Dialog.tsx` are both clean.

astracat-bot[bot]
astracat-bot Bot previously requested changes Sep 15, 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.

Thanks—the first-mount-open path Cindy called out in the last review now restores focus. The new capture point runs during render, though, so server-rendering any initially open Dialog or AlertDialog throws document is not defined; the exact-head sandbox build fails while prerendering AlertDialogAsyncAction. Please preserve both focus-return paths without reading browser state during server render, and remove the stale focus-restoration known failure so the browser contract reflects the fix.

If you'd rather talk it through with someone, we're in Discord.

Comment thread packages/core/src/Dialog/Dialog.tsx Resolved
@HelloOjasMutreja
HelloOjasMutreja force-pushed the fix/dialog-trigger-capture-order branch from be601f9 to 57e979f Compare September 16, 2026 05:58
@HelloOjasMutreja

Copy link
Copy Markdown
Contributor Author

Fixed both.

The SSR crash: the render-time capture reads document.activeElement, which does not exist on the server. Guarded it with typeof document !== 'undefined'; the client's first render (hydration) still captures the trigger correctly. Added a regression test that stubs document away and asserts renderToString on an initially open Dialog does not throw.

Removed the stale modal-dialog.focus.restored known failure for the conditional-content-focus-restoration state, since this head fixes it.

While verifying against the full Chromium a11y contract, I also found the DialogHeader open-gate change from the prior round (shouldAutoFocus = ... && dialogContext?.isOpen !== false) broke two other states: labelled-described-default-title and explicit-descendant-focus started failing FR1 (focus entering the dialog before it reaches the browser's native modal state), because gating on isOpen shifted when the title's mount-focus effect fires relative to the dialog becoming :modal. The render-time trigger capture already prevents the header's own mount focus from being mistaken for the external trigger, so the gate was redundant for #5637 and actively harmful elsewhere. Reverted it; all five Dialog Chromium a11y states pass at this head, plus the existing 127 jsdom unit tests for Dialog/AlertDialog.

astracat-bot[bot]
astracat-bot Bot previously requested changes Sep 16, 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.

Thanks—the SSR crash and both focus-return paths are fixed. One scope issue remains: this still says Fixes #5637, but the closed-dialog header focus theft from that report still reproduces and remains a known failure. Please keep #5637 open and remove the stale claim that this PR ships the closed-state guard, so users’ remaining focus bug doesn’t disappear from tracking.

@HelloOjasMutreja

Copy link
Copy Markdown
Contributor Author

Updated the PR body: removed the Fixes #5637 claim and its stale description of the DialogHeader gate, and added a section stating plainly that this PR only fixes the wrong-trigger-capture half of #5637 (plus the SSR crash it introduced along the way). The closed-dialog header focus-theft half stays unfixed here and #5637 should stay open for it.

The opener is captured in a layout effect so it lands before a descendant's passive autofocus. StrictMode re-runs that effect after the commit's passive effects, by which time a DialogHeader has already focused its title, so the second run took the title as the opener and closing left focus on the page body.

Only the closed-to-open edge captures now, tracked by a ref that survives the re-run and starts false so a dialog first mounted open still captures on its first commit. Adds a StrictMode test for that case, and a test that a second open from a different opener still returns to that opener.
@HelloOjasMutreja

Copy link
Copy Markdown
Contributor Author

Rebased onto main: the closed-to-open capture now sits alongside the new check for which document holds focus, and the Dialog and AlertDialog suites pass (172 tests). CI here stops at the ci:run gate, so lint, test, build and docsite-test have not run on this head. Could a maintainer add the ci:run label?

astracat-bot[bot]
astracat-bot Bot previously requested changes Nov 1, 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.

Thanks — this fixes the case it targets: under React StrictMode, a Dialog first mounted open with a DialogHeader now returns focus to its opener on close (checked in Chromium), and the new StrictMode test fails without the guard. One small fix is needed in the description: its Problem section gives an AlertDialog as an example, but an AlertDialog first mounted open already returned focus to its opener under StrictMode before this change, so please name a Dialog with a DialogHeader there, as the changeset already does. Approval also waits on the Dialog owner re-reviewing or dismissing the change request they left before this PR was re-cut, and on a maintainer adding ci:run so CI runs on this revision.

[Automated review]

@HelloOjasMutreja

Copy link
Copy Markdown
Contributor Author

The description's Problem section names a Dialog with a DialogHeader first mounted open, as the changeset does; the AlertDialog example is gone. Nothing in the code changed. The change request from the Dialog owner and the ci:run label are still what the approval waits on.

# Conflicts:
#	packages/core/src/Dialog/Dialog.test.tsx
@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 9, 2026
@cixzhang cixzhang closed this Nov 9, 2026
@cixzhang cixzhang reopened this Nov 9, 2026
@cixzhang
cixzhang dismissed stale reviews from astracat-bot[bot], astracat-bot[bot], astracat-bot[bot], astracat-bot[bot], and astracat-bot[bot] November 9, 2026 06:13

(Posted by Cindy's agent.) Dismissing as addressed at head 89ba418. The opener is captured in a layout effect, on the closed-to-open edge only (wasOpenRef starts false), so nothing reads browser state or writes refs during render, and a dialog first mounted open still captures. The new StrictMode test fails without the guard. The description and changeset now name a Dialog with a DialogHeader rather than an AlertDialog, and are scoped to that case (content that focuses itself through autoFocus is stated as out of scope). The PR no longer claims to fix #5637, which #6607 fixed. CI is green at this head. The owner's separate review is handled through an owner approval.

astracat-bot[bot]
astracat-bot Bot previously requested changes Nov 9, 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.

Thanks — the edge guard fixes a real StrictMode problem: a Dialog with a DialogHeader that is first mounted open and then removed while open now returns focus to its opener instead of the page body (checked in Chromium and Firefox), the new tests protect the guard, and CI passes on this revision. One change is needed before approval:

  • Regression: since #10408 landed on main (now merged into this branch), closing such a dialog while it stays mounted already returns focus to its opener in Chromium and Firefox, so the changeset (line 5) and the description's Problem section describe a close that no longer loses focus; please name the case this fixes instead, for example “under React StrictMode, a Dialog with a DialogHeader that is first mounted open and then removed while open (rendered only while open) now returns focus to the element that opened it, instead of leaving focus on the page body.”

A non-blocking suggestion for the new effect comment is inline. Approval also waits on the Dialog owner's own change request from an earlier revision being resolved.

[Automated review]

Comment thread packages/core/src/Dialog/Dialog.tsx Resolved
…ile open

Since main focuses the title only after showModal(), a close while
mounted already returns focus to the opener. The changeset names the
case still fixed here: under StrictMode, a Dialog with a DialogHeader
first mounted open and then removed while open. The effect comment
keeps only its reason.

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

Thanks — this revision fixes the release-note wording: the changeset and the description now name the case the edge guard fixes, a Dialog with a DialogHeader that is first mounted open and then removed while open under React StrictMode, which now returns focus to its opener instead of the page body (checked in Chromium and Firefox). No code or evidence issue remains, the new tests protect the guard, CI passes on this revision, and nothing more is needed from you.

Approval now waits only on the Dialog owner's own change request from an earlier revision being re-reviewed or dismissed.

[Automated review]

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

(Posted by Cindy's agent on her behalf.) Engineering-owner approval at this exact head; it supersedes my earlier change request. The "Review outcomes at df70d8d" table in the PR body maps each of its points to this head: under React StrictMode, a Dialog with a DialogHeader that is first mounted open and then removed while open now returns focus to its opener, with the StrictMode test failing on main and passing here. Thanks for the contribution!

@cixzhang
cixzhang dismissed astracat-bot[bot]’s stale review November 9, 2026 07:32

(Posted by Cindy's agent.) Dismissing as addressed at head df70d8d: the changeset and the description's Problem section now name the case this fixes (under React StrictMode, a Dialog with a DialogHeader first mounted open and then removed while open returns focus to its opener), as requested. The latest automated review at this head confirms no code or evidence issue remains, and CI is green.

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Nov 9, 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. Under React StrictMode, a Dialog with a DialogHeader that is first mounted open and then removed while open now returns focus to its opener instead of leaving it on the page body; I checked this in Chromium, and the new StrictMode test fails without the guard. The changeset describes that case accurately, CI passes on this revision, and the Dialog owner's approval replaces her earlier change request. Nothing more is needed from you. Thanks for carrying this through.

[Automated review]

@cixzhang
cixzhang merged commit 7fefc8a into meta:main Nov 9, 2026
87 checks passed
@HelloOjasMutreja
HelloOjasMutreja deleted the fix/dialog-trigger-capture-order branch November 9, 2026 08:25

This branch was successfully deployed

1 active deployment
Preview — df70d8d8 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 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. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dialog captures the wrong trigger when focusable content mounts on open

2 participants