Repository navigation
fix(Dialog): capture the opener only on the closed-to-open edge - #5642
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report🔎 Preview ·
|
| 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
left a comment
There was a problem hiding this comment.
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]
5a25b25 to
be601f9
Compare
|
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. |
There was a problem hiding this comment.
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.
be601f9 to
57e979f
Compare
|
Fixed both. The SSR crash: the render-time capture reads Removed the stale While verifying against the full Chromium a11y contract, I also found the |
There was a problem hiding this comment.
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.
|
Updated the PR body: removed the |
57e979f to
08b4a56
Compare
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.
3ad67bc to
9530575
Compare
|
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 |
There was a problem hiding this comment.
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]
|
The description's Problem section names a |
# Conflicts: # packages/core/src/Dialog/Dialog.test.tsx
(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.
There was a problem hiding this comment.
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
Dialogwith aDialogHeaderthat 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]
…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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
(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!
(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.
There was a problem hiding this comment.
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]
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
Dialogcaptures the opener in a layout effect that runs wheneverisOpenorisInlinechanges. Under React StrictMode the simulated unmount/remount re-runs that layout effect after the open commit's passive effects. By then Dialog has focused theDialogHeadertitle, 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 aDialogwith aDialogHeaderthat 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
wasOpenRefthat survives the StrictMode re-run. It startsfalse, 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 aDialogHeaderinsideStrictMode, closes it, and expects focus on the opener. It fails on currentmain(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.Limits
autoFocusattribute, such as aTextInputwithhasAutoFocus. React appliesautoFocusduring the layout phase, children first, so it can run before the Dialog's own layout effect. I checked that this fails onmaintoo; it is a separate problem and not changed here.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.)
isOpen={true}still restores focus through Dialog's ownerwasOpenRefstartsfalse, so the first commit of a dialog mounted open takes the capture branch.AlertDialog.test.tsx"returns focus to the trigger when it closes" andDialog.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).DialogHeader.tsxonmain(the title focuses itself only when its native dialog is already open; Dialog focuses it aftershowModal()). This PR does not touchDialogHeader.tsx; that file is identical tomainat this head.CI at this head: pending; the drain adds
ci:runandci:run-a11y.Maintainer carry (@cixzhang)
Carried forward on this branch with @HelloOjasMutreja's commits kept:
mainmerged (theDialog.test.tsximport conflict takesmain's import list, which addsuseLayoutEffect). 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.)