Repository navigation
fix(Lightbox): a viewer that unmounts while open returns focus to its opener - #9629
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.
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 itsisFocusLostinstead of the inline check atLightbox.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]
| const doc = trigger.ownerDocument; | ||
| const active = doc.activeElement; | ||
| if (active == null || active === doc.body || dialog.contains(active)) { | ||
| trigger.focus(); |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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` | |
There was a problem hiding this comment.
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."
| // 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. |
There was a problem hiding this comment.
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."
|
/approve-spec 9dc6fb9 |
There was a problem hiding this comment.
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:
prepareFocusReturnOnRemovalreachesfocusForLastInput, which expects its caller to keep modality tracking active; Lightbox doesn't calluseInteractionModalityTracking(), 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.tsfile header could list the new export.
[Automated review]
| * 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 |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
| // 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. |
There was a problem hiding this comment.
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.| // 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. |
There was a problem hiding this comment.
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.
Part of #8963 (Lightbox; Dialog merged as #9627, BottomSheet and Drawer follow from their owners).
User impact
A
Lightboxthat 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:LightboxAR3: focus "on close returns to the element that opened the viewer when that element is still connected and can receive focus" (family:overlay-dismissalAV1). Removing a<dialog>from the document doesn't runclose(), so the browser's own focus return doesn't apply.Change
utils/focusReturn.ts: new internalprepareFocusReturnOnRemoval(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.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.[fix].Verification
Unit, red first: two new tests fail on
mainand 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, andcheck-knowledgeis aligned.Real Chromium 154: focus "View photos", Enter, and the page removes the viewer 1.5 s later.
mainWriting 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.