Skip to content

Reset focus when closing a dialog with no previously focused element #13118

Open

Description

@hjanuschka

The close-the-dialog algorithm restores previously focused element if non-null, but does nothing with focus when it is null. If focus is inside the dialog when it closes, this leaves the focused area inside a closed dialog until the asynchronous focus fixup rule runs. In the meantime, keyboard events can be lost because the active element is no longer focusable.

Reproduction:

<dialog><input></dialog>
<script>
  const dialog = document.querySelector('dialog');
  const input = document.querySelector('input');
  dialog.showModal();
  input.focus();
  dialog.close();
  console.log(document.activeElement === input); // true
  setTimeout(() => console.log(document.activeElement === document.body), 150); // true
</script>

Chromium, Firefox, and WebKit all leave the input active immediately after close(), then change document.activeElement to body later. A Chromium change adds a synchronous regression test for this case.

Could the dialog-close steps run the focusing steps for the document's viewport when previously focused element is null and the focused area's DOM anchor is a shadow-including inclusive descendant of the dialog? This uses the existing focus model rather than introducing a separate notion of "clearing" focus, and preserves focus when it has already moved elsewhere.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions