Repository navigation
Description
Description
A modal that unmounts while it is still open drops focus to <body> instead of returning it to the control that opened it. Rendering a dialog only while it is open is a common React pattern:
{isEditing && <Dialog isOpen onOpenChange={setIsEditing}>…</Dialog>}and so is a parent that unmounts the dialog with the rest of a view (a route change, a list row that disappears after a save). When that happens, keyboard and screen-reader users lose their place: the next Tab starts from the top of the page.
Focus return runs only on the isOpen: true → false path, never in an unmount cleanup:
- Dialog (and
AlertDialog,CommandPalette, which render it): the open/close effect returns focus in itselsebranch only (Dialog.tsx:555-600). - Lightbox: the same, in its layout effect (Lightbox.tsx:423-438).
- BottomSheet: on unmount the registry's unregister calls
entry.hide()(bottomSheetStack.ts:1772), which iscloseDialogKeepingFocus(BottomSheet.tsx:549), so focus stays on the removed node. - Drawer with
hasScrim={false}: the unmount cleanup says "No focus restore on this path" (useDrawerDialogPresence.ts:178-189).
A modal Drawer doesn't have the problem: useFocusTrap's cleanup restores focus on deactivation (#3732).
APG modal dialog: "When a dialog closes, focus returns to the element that invoked the dialog". Removing the <dialog> from the document doesn't run close(), so the browser's own focus restoration doesn't apply either.
Expected: when one of these surfaces unmounts while open (and focus is inside it, or on <body> because of the removal), focus returns to the recorded opener if it is still connected, as on a normal close.
Reproduction
import {useState} from 'react';
import {Button, Dialog, DialogHeader} from '@astryxdesign/core';
export function Repro() {
const [isEditing, setIsEditing] = useState(false);
return (
<>
<Button label="Edit" onClick={() => setIsEditing(true)} />
{isEditing && (
<Dialog isOpen onOpenChange={setIsEditing}>
<DialogHeader title="Edit profile" />
<Button label="Save" onClick={() => setIsEditing(false)} />
</Dialog>
)}
</>
);
}- Tab to "Edit" and press Enter.
- Tab to "Save" and press Enter.
document.activeElementis<body>. With<Dialog isOpen={isEditing}>always mounted, focus returns to "Edit".
Astryx Version
@astryxdesign/[email protected], still present on main (14fc587)
Environment
Any browser; reproduced in jsdom for Dialog (unmounted while open: focus on BODY; closed with isOpen={false}: focus on the opener). The other components by the same code path.
Activity
fix(Drawer): a non-modal drawer that unmounts while open returns focu…
cixzhang commentedon Nov 9, 2026
(Posted by Cindy's agent.) Thank you for the detailed report, @duytbp! Your breakdown of each component's close path guided the fixes, and all four now return focus to their opener when they unmount while open: Dialog (and AlertDialog and CommandPalette, which render it) in #9627, Lightbox in #9629, BottomSheet in #10061, and Drawer in #9672. Closing as completed.