Repository navigation
fix(Dialog): a dialog that unmounts while open returns focus to its opener - #9627
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report
Generated by PR Enrichment workflow | View current CI run |
There was a problem hiding this comment.
The fix works for a Dialog rendered in the page: focus now returns to the opener when the open dialog is removed, and the new tests fail before the change and pass after it. Approval waits on an engineering-owner decision about the new AR2 verification row in Dialog.spec.md, because AR2 speaks of closing a Dialog and no current record yet says that removing an open Dialog counts as closing; an engineering owner records that decision by commenting /approve-spec <full head SHA> on this pull request, and the author may do so if they are an engineering owner. Whatever that decision, please change the focus-loss check in the new Effect:
- A Dialog in a same-origin iframe opened from the page still leaves focus on the frame's body when it is removed while open;
isFocusLost(dialog)fromutils/focusReturnreads the dialog's own document, fixes this case, and is the shared checkuseFocusTrapalready uses. - The description has no
## Writing rubric resultssection, which spec approval reads against.
[Automated review]
| const doc = trigger.ownerDocument; | ||
| const active = doc.activeElement; | ||
| if (active == null || active === doc.body || dialog.contains(active)) { | ||
| focusForLastInput(trigger); | ||
| } |
There was a problem hiding this comment.
This decides focus loss from the opener's document. For a Dialog portalled into a same-origin iframe and opened by a control in the page, the page's active element after removal is the <iframe>, so focus stays on the frame's body. isFocusLost in ../utils/focusReturn reads the dialog's own document, and useFocusTrap already uses it for the same restore:
if (isFocusLost(dialog)) {
focusForLastInput(trigger);
}Import it beside focusForLastInput, and add the iframe case to the "unmounting while open" tests.
| // A dialog that unmounts while open (rendered only while open, or removed | ||
| // with the rest of a view) never runs the close branch above, and removing a | ||
| // <dialog> does not run the browser's own focus return. Focus left inside | ||
| // it, or dropped to the body by the removal, goes back to the opener. |
There was a problem hiding this comment.
Non-blocking: the first sentence has 36 words and two topics. A shorter version that keeps the reason:
// An open dialog that unmounts skips the close branch above. Removing a
// <dialog> does not run the browser's focus return. If focus is lost, return
// it to the opener.There was a problem hiding this comment.
Focus now returns to the opener when an open Dialog (and AlertDialog or CommandPalette) is removed from the page, and the new tests fail before the change and pass after it. Approval waits on an engineering-owner decision about the new AR2 row in Dialog.spec.md: AR2 speaks of closing a Dialog, and no current record yet says that removing an open Dialog counts as closing. An engineering owner records that decision by commenting /approve-spec <full head SHA> on this pull request, and the author may do so as an engineering owner. Whatever that decision, one change is needed:
- When a Dialog lives in a same-origin iframe and the person clicks a page control that removes it,
Dialog.tsx:657moves focus from that control back to the opener because it checks focus loss only in the iframe's document, so please also require that the page's focus is lost or rests on the frame that hosts the dialog, and add that page-control case to the iframe test.
[Automated review]
| queueMicrotask(() => { | ||
| // isFocusLost reads the dialog's own document, so a dialog in an | ||
| // iframe opened from the page is covered too. | ||
| if (!dialog.isConnected && trigger.isConnected && isFocusLost(dialog)) { |
There was a problem hiding this comment.
When the dialog lives in a same-origin iframe, this reads focus loss only in the iframe's document. If the person clicks a page control that removes the dialog, the frame's active element is its body, so focus jumps from that control back to the opener; the base leaves it on the control. Please also require focus to be lost in the opener's document, or to rest on the frame that hosts the dialog, and add the page-control case to the iframe test:
const dialogDocument = dialog.ownerDocument;
const triggerDocument = trigger.ownerDocument;
const lostInTriggerDocument =
dialogDocument === triggerDocument ||
isFocusLost(dialogDocument.defaultView?.frameElement ?? null, triggerDocument);| // isFocusLost reads the dialog's own document, so a dialog in an | ||
| // iframe opened from the page is covered too. |
There was a problem hiding this comment.
Non-blocking: this restates isFocusLost's own JSDoc ("Reads surface's own document"). Delete it, or keep only the constraint the iframe fix adds.
There was a problem hiding this comment.
Focus now returns to the opener when an open Dialog (and AlertDialog or CommandPalette) is removed from the page, and a page control that removes a Dialog in an iframe opened from the page now keeps its focus. Approval waits on an engineering-owner decision about the new AR2 row in Dialog.spec.md: AR2 speaks of closing a Dialog, and no current record yet says that removing an open Dialog counts as closing. An engineering owner records that decision by commenting /approve-spec <full head SHA> on this pull request, and the author may do so as an engineering owner. Whatever that decision, one change is needed:
- When a Dialog and its opener are both inside a same-origin iframe and the person clicks a page control that removes the dialog,
Dialog.tsx:668-671skips the page check because the opener shares the dialog's document and pulls focus from that control into the iframe, so please check the frame that hosts the dialog in its own document whatever document the opener is in, and add that case to the iframe test.
[Automated review]
| const isPageFocusLost = | ||
| dialog.ownerDocument === triggerDocument || | ||
| isFocusDetached(triggerDocument) || | ||
| triggerDocument.activeElement === host; |
There was a problem hiding this comment.
When the opener is also inside the iframe, dialog.ownerDocument === triggerDocument is true, so the page check is skipped. A page control that removes the dialog then loses its focus: the frame's active element falls to its body, isFocusLost(dialog) is true, and focus moves into the iframe, onto the opener. In Chromium the base keeps focus on the page control. Checking the hosting frame in its own document, whatever document the opener is in, keeps focus there in both iframe cases, and all 76 Dialog tests still pass:
// In a frame, return focus only if the page's focus is also lost or rests on
// that frame. A page control that removed the dialog keeps its focus.
const host = dialog.ownerDocument.defaultView?.frameElement ?? null;
const isPageFocusLost =
(host == null || isFocusLost(host, host.ownerDocument)) &&
(dialog.ownerDocument === triggerDocument ||
isFocusLost(host, triggerDocument));Please add the opener-inside-the-frame case to the iframe test too, asserting that the page control keeps focus. In jsdom that test fails on this head and passes on the base and with the condition above.
There was a problem hiding this comment.
Focus now returns to the opener when an open Dialog (and AlertDialog or CommandPalette) is removed from the page, and a page control that removes a Dialog whose opener is inside an iframe now keeps its focus. Approval waits on an engineering-owner decision about the new AR2 row in Dialog.spec.md: AR2 speaks of closing a Dialog, and no current record yet says that removing an open Dialog counts as closing. An engineering owner records that decision by commenting /approve-spec <full head SHA> on this pull request, and the author may do so as an engineering owner. Whatever that decision, two changes are needed:
- When a page control removes the whole iframe that holds an open Dialog opened from the page,
Dialog.tsx:666finds no host frame, because a removed frame's document has no window, so it skips the page check and moves focus from that control to the opener; please read the host frame when the Effect sets up and cover this case in a browser test. Dialog.tsx:669-670re-implements the sharedisFocusLostfor the frame element, so please callisFocusLost(host)instead.
[Automated review]
| const host = dialog.ownerDocument.defaultView?.frameElement ?? null; | ||
| const isHostFocusLost = | ||
| host == null || | ||
| isFocusDetached(host.ownerDocument) || | ||
| host.ownerDocument.activeElement === host; | ||
| if (isFocusLost(dialog) && isHostFocusLost) { |
There was a problem hiding this comment.
When a page control removes the whole iframe that holds this Dialog (a preview pane closed while its dialog is open), the frame's document has no window by the time this microtask runs, so host is null and the page check is skipped. In Chromium, focus then moves from the control the person clicked to the opener; the base and the previous head keep it on the control.
Reading the host when the Effect sets up keeps it, and calling the shared isFocusLost replaces the copy on lines 669-670. With this, all 317 Dialog, AlertDialog and CommandPalette tests pass, and Chromium keeps focus on the page control:
const dialog = dialogRef.current;
const host = dialog?.ownerDocument.defaultView?.frameElement ?? null;
// …in the microtask:
if (isFocusLost(dialog) && (host == null || isFocusLost(host))) {
focusForLastInput(trigger);
}jsdom keeps a window on a removed frame's document, so this case needs a browser test.
| // isFocusLost reads the dialog's own document. For a dialog in an | ||
| // iframe, the document that hosts the frame must also have lost focus | ||
| // or keep it on that frame, wherever the opener is: a control the | ||
| // user pressed outside the frame to remove the dialog keeps its focus. |
There was a problem hiding this comment.
Non-blocking: the first sentence restates isFocusLost's JSDoc, and the second has 42 words. With the host read at setup, a shorter version that keeps the reasons:
// In a frame, return focus only if the hosting document also lost focus.
// A control outside the frame that removed the dialog keeps its focus.and beside the setup line: // Read the host frame now. A removed frame's document has no window.
… shared isFocusLost for it
|
/approve-spec b9c1872 |
There was a problem hiding this comment.
Approval recommended. Focus now returns to the opener when an open Dialog (and AlertDialog or CommandPalette) is removed from the page, and a control outside an iframe that removes the dialog, or the whole iframe, keeps its focus.
The new AR2 verification row is approved for this head, both changes asked for last time are in (the host frame is read when the Effect sets up, and the shared isFocusLost is used for it), and the new case-table tests fail before the change and pass after it.
Optional: three comments in the new Effect could be shorter, one topic per sentence; the inline suggestions show rewrites.
[Automated review]
| // isFocusLost reads the dialog's own document. For a dialog in an | ||
| // iframe, the document that hosts the frame must also have lost focus | ||
| // or keep it on that frame, wherever the opener is: a control the | ||
| // user pressed outside the frame to remove the dialog, or the frame | ||
| // itself, keeps its focus. |
There was a problem hiding this comment.
Non-blocking: the first sentence restates isFocusLost's JSDoc, and the second has 46 words and two topics. A shorter version that keeps the reason:
// In a frame, return focus only if the hosting document also lost focus.
// A control outside the frame that removed the dialog, or the frame itself,
// keeps its focus.| // Checked once the commit has finished: StrictMode's simulated unmount | ||
| // leaves the dialog in the document, and only a real removal returns | ||
| // focus. |
There was a problem hiding this comment.
Non-blocking: a passive opening and two topics in one sentence. Suggest:
// Wait until the commit finishes. A StrictMode replay keeps the dialog in
// the document, so only a real removal returns focus.
Part of #8963 (Dialog; Lightbox, BottomSheet and Drawer follow in their own PRs).
User impact
A
Dialogthat unmounts while it is still open drops focus to the page body instead of returning it to the control that opened it. This affects keyboard and screen-reader users of two common React patterns:{isEditing && <Dialog isOpen …>}, where an action inside it sets the state to false;The user loses their place, and the next Tab starts from the top of the page.
AlertDialogandCommandPaletterenderDialogand had the same problem. After this change, focus goes back to the opener, as on a normal close.Authority
component:DialogAR2: "Focus returns to context. Closing a native Dialog MUST restore focus to its still-connected external invoking context". APG modal dialog: when a dialog closes, focus returns to the element that invoked it. Removing a<dialog>from the document doesn't runclose(), so the browser's own focus return doesn't apply.Change
Dialog.tsx: a new effect's cleanup handles the unmount-while-open path, which the open/close effect's close branch never reaches.focusForLastInputa close uses.isFocusLost(dialog)fromutils/focusReturn(the checkuseFocusTrapuses): nothing focused, or focus inside the removed dialog. It reads the dialog's own document, so a dialog in an iframe opened from the page is covered. For a dialog in an iframe, the document hosting the frame must also have lost focus, or keep it on that frame, wherever the opener is. So a control the user pressed outside the frame to remove the dialog, or the whole frame, keeps its focus. The host frame is read when the effect sets up, because a removed frame's document has no window, and the check uses the sharedisFocusLost(host). Focus the user already moved elsewhere is left alone.Dialog.spec.md: one AR2 verification row for the unmount-while-open cases. AR2's own text is unchanged.[fix].Verification
The case table. One table-driven test in
Dialog.test.tsxcovers every way a dialog can disappear while open. The rule is that focus goes back to the opener only when the removal lost it. A control the user pressed outside the dialog, or outside the frame that holds it, keeps its focus. An opener that left with a removed frame gets nothing.main, every "opener" row fails. Each row a review round added fails on the head before it.Other tests: "returns focus to the opener when the dialog unmounts" (by the dialog's own action, and with its view) and the StrictMode case also fail on
mainand pass here. Dialog, AlertDialog and CommandPalette suites pass (326 tests). Lint and type check are clean, andcheck-knowledgeis aligned.Real Chromium 154: the [Bug] Dialog, Lightbox, BottomSheet, Drawer: unmounting while open drops focus to the body #8963 repro. Focus "Edit", Enter, then Enter on "Save", which unmounts the dialog.
mainLane 4's #9613 also edits
Dialog.tsx, in the backdrop handler only. The hunks don't overlap.Writing rubric results
R1 narration: none. The new AR2 verification row states what is checked, in the present tense.
R2 research: none in the record.
R3 evidence: the verification cell names tests, not pull requests or commits.
R4 rejected alternatives: none added.
R5 change references: none. No
#-number,/pull/or/issues/text in the record changes.R6 open questions: none touched.