Skip to content

fix(Lightbox): a viewer that unmounts while open returns focus to its opener - #9629

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

cixzhang merged 2 commits into
mainfrom
fix/lightbox-unmount-returns-focus

Conversation

@cixzhang

@cixzhang cixzhang commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Part of #8963 (Lightbox; Dialog merged as #9627, BottomSheet and Drawer follow from their owners).

User impact

A Lightbox that unmounts while it's open drops focus to the page body instead of returning it to the element that opened it. This affects keyboard and screen-reader users when the viewer is rendered only while open and the page removes it (a route change, a gallery that refreshes). The next Tab then starts from the top of the page. After this change, focus goes back to the opener, as on a normal close, when the opener is still on the page. A removal that also takes the opener, such as a route change, has nowhere to return focus to, as AR3 allows.

Authority

component:Lightbox AR3: focus "on close returns to the element that opened the viewer when that element is still connected and can receive focus" (family:overlay-dismissal AV1). Removing a <dialog> from the document doesn't run close(), so the browser's own focus return doesn't apply.

Change

  • utils/focusReturn.ts: new internal prepareFocusReturnOnRemoval(surface), the one rule for an open surface removed while open. It is called while the surface is mounted and reads the host frame then, because a removed frame's document has no window. It returns the function the effect cleanup calls with the opener.
    • Once the commit has finished, focus moves to the opener only when the surface really left the document (StrictMode's simulated unmount keeps it) and the opener is still on the page.
    • It also needs the removal to have lost focus, through the shared isFocusLost, in the surface's document and in the document that hosts its frame. A control the user pressed outside the surface keeps its focus.
  • Dialog.tsx: the unmount-while-open effect from fix(Dialog): a dialog that unmounts while open returns focus to its opener #9627 now calls the helper. There is no behaviour change, and its 12-case table still passes.
  • Lightbox.tsx: the same helper handles a viewer removed while open.
  • Lightbox.spec.md: one AR3 verification row.
  • Changeset: Core patch, [fix].

Verification

  • Unit, red first: two new tests fail on main and pass here: the unmount itself, and StrictMode (focus stays inside while open, then returns on unmount). A third pins that a page control which removes the viewer keeps its focus. Lightbox, Dialog, AlertDialog, CommandPalette and utils suites pass (1162 tests). Lint and type check are clean, and check-knowledge is aligned.

  • Real Chromium 154: focus "View photos", Enter, and the page removes the viewer 1.5 s later.

    Step main This PR
    Viewer open Close Close
    Viewer removed by the page page body "View photos" (the opener)

Writing rubric results

R1 narration: none. The new AR3 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.
R4 rejected alternatives: none added.
R5 change references: none in the record change.
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 3:46am 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
@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval 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, 3:52 AM UTC

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

Requesting changes: the unmount focus return restores Lightbox's documented focus behaviour and the new tests prove it, but two things need fixing first.

  • Move the after-removal return into utils/focusReturn.ts, using its isFocusLost instead of the inline check at Lightbox.tsx:465-467, and call that one helper from here and from the Dialog change (#9627), because the two copies already test lost focus and move focus differently.
  • Add "when the opener is still on the page" to the changeset, the user-impact text and the new AR3 verification row, because a removal that also takes the opener (such as a route change) still leaves focus on the page, as AR3 allows.

The two comment rewrites suggested inline are optional.

[Automated review]

Comment thread packages/core/src/Lightbox/Lightbox.tsx Outdated
Comment on lines +465 to +468
const doc = trigger.ownerDocument;
const active = doc.activeElement;
if (active == null || active === doc.body || dialog.contains(active)) {
trigger.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.

This is isFocusLost(dialog, trigger.ownerDocument) from utils/focusReturn.ts without its documentElement case. Please move the whole after-removal return (post-commit check, isFocusLost, shared focus move) into one helper in utils/focusReturn.ts, and call it once from this cleanup and from the Dialog change. Today the two copies already differ: Dialog uses focusForLastInput, this one uses focus(). As a minimum, if (isFocusLost(dialog, trigger.ownerDocument)) trigger.focus(); keeps the Lightbox tests green.

'@astryxdesign/core': patch
---

[fix] `Lightbox`: a viewer that unmounts while it is open (rendered only while open, or removed with its view) now returns focus to the element that opened it instead of dropping it to the page.

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 view that is removed also holds the opener, the code leaves focus on the page (the !trigger.isConnected return), so "removed with its view" overstates the fix. Suggested: "[fix] Lightbox: a viewer that unmounts while it is open, for example one rendered only while open, now returns focus to the element that opened it when that element is still on the page, instead of dropping it to the page."

| FR2 | Source inspection and `themingTargets.test.ts` | Viewer overlay, Media, Caption, and Counter | Removing the root target or documenting an unshipped child target fails evidence or inventory. | `audit:Lightbox/theming` |
| FR3 | Source inspection plus Button and IconButton public composition | Close and gallery navigation controls | Re-owning a control surface requires the delegated owner and this map to change. | `audit:Lightbox/theming` |
| FR4, AR3 | Browser keyboard test: Tab and Shift+Tab at both ends, Escape, arrows in LTR and RTL, `+`, `-`, `0`, close focus | Single image, gallery, zoomed image, removed opener | Focus leaving the viewer, a key without its action, or focus not returning to a connected opener fails. | `audit:Lightbox/accessibility` |
| AR3 | `Lightbox.test.tsx` "Lightbox unmounting while open" suite | Viewer rendered only while open and removed by the page; StrictMode | Focus left on the page body after an open viewer unmounts fails. | `audit:Lightbox/accessibility` |

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.

AR3 returns focus only to an opener that is still connected, so this failure condition needs the same qualifier: "Focus left on the page body after an open viewer unmounts while its opener is still connected fails."

Comment thread packages/core/src/Lightbox/Lightbox.tsx Outdated
Comment on lines +447 to +450
// A viewer 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 several topics. Suggested: "Removing an open dialog skips the close branch above and the browser's focus return. If that loses focus, focus goes back to the opener."

@cixzhang

cixzhang commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

/approve-spec 9dc6fb9

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

Recommending approval: an open Lightbox that unmounts now returns focus to its opener while that opener is still on the page, as component:Lightbox AR3 asks, and both earlier blocking points (one shared focus-return path, and release-note wording that names the connected-opener condition) are resolved. The new unmount and StrictMode tests fail on main and pass here, and Dialog's suites still pass on the shared helper.

Optional follow-ups, none blocking:

  • prepareFocusReturnOnRemoval reaches focusForLastInput, which expects its caller to keep modality tracking active; Lightbox doesn't call useInteractionModalityTracking(), so either state that precondition in the helper's JSDoc or add the call as Dialog does.
  • A few new comments could be shorter and name the browser focus return they stand in for (see the inline suggestions), and the focusReturn.ts file header could list the new export.

[Automated review]

Comment on lines +163 to +166
* Once the commit has finished, focus moves to the opener only when the
* surface really left the document (StrictMode's simulated unmount keeps it),
* the opener is still on the page, and the removal lost focus, both in the
* surface's document and in the document that hosts its frame. A control the

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 (comment style): this is one 48-word sentence with three conditions. Suggested:

 * The returned function checks after the commit. A StrictMode replay keeps
 * the surface in the document, so only a real removal passes. Focus then
 * moves to a connected opener if focus is lost in the surface's document and
 * in the frame's host document.

return;
}
if (isFocusLost(surface) && (host == null || isFocusLost(host))) {
focusForLastInput(opener as HTMLElement);

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: focusForLastInput asks its caller to keep modality tracking active (lines 119-120). Lightbox never calls useInteractionModalityTracking(), so on a page where nothing else has started the tracker the keyboard default applies and this is a plain focus(). Consider stating that precondition in this helper's JSDoc, or adding the tracking call to Lightbox as Dialog does.

Comment on lines +449 to +452
// A viewer that unmounts while open (rendered only while open, or removed
// with the rest of a view) never runs the close branch above. Focus the
// removal lost goes back to the opener while the opener is still on the
// page.

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 (comment style): the note no longer names the browser focus return it stands in for, and the last clause repeats the helper's check. Suggested:

  // Removing an open dialog runs no close and no browser focus return. If the
  // removal loses document focus, focus goes back to the opener.

Comment on lines 640 to +642
// 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.
// with the rest of a view) never runs the close branch above. Focus the
// removal lost 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 (comment style): same suggestion as in Lightbox, so the Effect note names the browser focus return it stands in for:

  // Removing an open dialog runs no close and no browser focus return. If the
  // removal loses document focus, focus goes back to the opener.

@cixzhang
cixzhang marked this pull request as ready for review November 3, 2026 14:56
@cixzhang
cixzhang merged commit 709a286 into main Nov 3, 2026
107 checks passed
@github-actions
github-actions Bot deleted the fix/lightbox-unmount-returns-focus branch November 4, 2026 06:04

This branch was successfully deployed

1 active deployment
Preview — 9dc6fb9e 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