Skip to content

[Bug] Dialog, Lightbox, BottomSheet, Drawer: unmounting while open drops focus to the body #8963

Closed

Description

@duytbp

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:

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>
      )}
    </>
  );
}
  1. Tab to "Edit" and press Enter.
  2. Tab to "Save" and press Enter.
  3. document.activeElement is <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

  1. added
    bug
    component
    on Nov 2, 2026
  2. cixzhang commented on Nov 9, 2026

    @cixzhang
    Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    accessibilityarea:corebugcomponent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions