Skip to content

fix(Dialog): a dialog that unmounts while open returns focus to its opener - #9627

Merged
cixzhang merged 6 commits into
mainfrom
fix/dialog-unmount-returns-focus
Nov 3, 2026
Merged

cixzhang merged 6 commits into
mainfrom
fix/dialog-unmount-returns-focus

Conversation

@cixzhang

@cixzhang cixzhang commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Part of #8963 (Dialog; Lightbox, BottomSheet and Drawer follow in their own PRs).

User impact

A Dialog that 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:

  • a dialog rendered only while open, {isEditing && <Dialog isOpen …>}, where an action inside it sets the state to false;
  • a dialog removed with the rest of a view (a route change, a list row that goes away after a save).

The user loses their place, and the next Tab starts from the top of the page. AlertDialog and CommandPalette render Dialog and had the same problem. After this change, focus goes back to the opener, as on a normal close.

Authority

component:Dialog AR2: "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 run close(), 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.
    • Once the commit has finished, if the dialog element really left the document, it returns focus to the recorded opener with the same focusForLastInput a close uses.
    • It acts only when focus is lost, through the shared isFocusLost(dialog) from utils/focusReturn (the check useFocusTrap uses): 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 shared isFocusLost(host). Focus the user already moved elsewhere is left alone.
    • StrictMode's simulated unmount keeps the dialog in the document, so it changes nothing.
  • Dialog.spec.md: one AR2 verification row for the unmount-while-open cases. AR2's own text is unchanged.
  • Changeset: Core patch, [fix].

Verification

  • The case table. One table-driven test in Dialog.test.tsx covers 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.

    Dialog in Opener in Removed Focus before Focus after
    page page dialog inside opener
    page page dialog nowhere opener
    page page dialog page control page control
    frame page dialog inside opener
    frame page dialog nowhere opener
    frame page dialog page control page control
    frame page whole frame inside opener
    frame page whole frame page control page control
    frame frame dialog inside opener
    frame frame dialog page control page control
    frame frame whole frame inside nothing (the opener is gone)
    frame frame whole frame page control page control
    • The harness models two browser behaviours that jsdom lacks. Focus in a frame is also the frame element's focus in the page. A removed frame's document has no window.
    • On 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 main and pass here. Dialog, AlertDialog and CommandPalette suites pass (326 tests). Lint and type check are clean, and check-knowledge is 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.

    Step main This PR
    Dialog opens "Edit profile" title "Edit profile" title
    "Save" unmounts the dialog page body "Edit" (the opener)

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

@vercel

vercel Bot commented Nov 2, 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 3, 2026 1:29am UTC

Request Review

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

astracat-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

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

@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 2, 2026
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

Current run: The current CI run concluded skipped. Current analysis is unavailable.


Generated by PR Enrichment workflow | View current CI run

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

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) from utils/focusReturn reads the dialog's own document, fixes this case, and is the shared check useFocusTrap already uses.
  • The description has no ## Writing rubric results section, which spec approval reads against.

[Automated review]

Comment thread packages/core/src/Dialog/Dialog.tsx Outdated
Comment on lines +658 to +662
const doc = trigger.ownerDocument;
const active = doc.activeElement;
if (active == null || active === doc.body || dialog.contains(active)) {
focusForLastInput(trigger);
}

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

Comment on lines +637 to +640
// 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.

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.

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.

@cixzhang
cixzhang marked this pull request as ready for review November 2, 2026 21:27
@cixzhang
cixzhang marked this pull request as draft November 2, 2026 21:58

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

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:657 moves 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]

Comment thread packages/core/src/Dialog/Dialog.tsx Outdated
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)) {

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.

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);

Comment thread packages/core/src/Dialog/Dialog.tsx Outdated
Comment on lines +655 to +656
// isFocusLost reads the dialog's own document, so a dialog in an
// iframe opened from the page is covered too.

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.

Non-blocking: this restates isFocusLost's own JSDoc ("Reads surface's own document"). Delete it, or keep only the constraint the iframe fix adds.

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

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-671 skips 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]

Comment thread packages/core/src/Dialog/Dialog.tsx Outdated
Comment on lines +668 to +671
const isPageFocusLost =
dialog.ownerDocument === triggerDocument ||
isFocusDetached(triggerDocument) ||
triggerDocument.activeElement === host;

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.

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.

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

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:666 finds 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-670 re-implements the shared isFocusLost for the frame element, so please call isFocusLost(host) instead.

[Automated review]

Comment thread packages/core/src/Dialog/Dialog.tsx Outdated
Comment on lines +666 to +671
const host = dialog.ownerDocument.defaultView?.frameElement ?? null;
const isHostFocusLost =
host == null ||
isFocusDetached(host.ownerDocument) ||
host.ownerDocument.activeElement === host;
if (isFocusLost(dialog) && isHostFocusLost) {

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.

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.

Comment thread packages/core/src/Dialog/Dialog.tsx Outdated
Comment on lines +662 to +665
// 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.

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.

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.

@cixzhang

cixzhang commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec b9c1872

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

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]

Comment on lines +661 to +665
// 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.

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.

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.

Comment on lines +654 to +656
// Checked once the commit has finished: StrictMode's simulated unmount
// leaves the dialog in the document, and only a real removal returns
// focus.

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.

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.

@cixzhang
cixzhang marked this pull request as ready for review November 3, 2026 03:11
@cixzhang
cixzhang merged commit c0929c8 into main Nov 3, 2026
107 checks passed
@github-actions
github-actions Bot deleted the fix/dialog-unmount-returns-focus branch November 3, 2026 06:05

This branch was successfully deployed

1 active deployment
Preview — b9c1872d Deployed Nov 3, 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