Skip to content

web: Return the focus into the dialog under a closed nested dialog - #4244

Merged
huacnlee merged 1 commit into
mainfrom
web-nested-modal-focus-return
Dec 3, 2026
Merged

huacnlee merged 1 commit into
mainfrom
web-nested-modal-focus-return

Conversation

@madcodelife

Copy link
Copy Markdown
Member

Problem

On the web, closing a dialog gives the focus back to what had it when the dialog came in, but only when no other dialog is left (dismiss checked !this.topModal()). Closing a dialog opened from a control inside another dialog (a confirmation opened by a button in a dialog) left the focus on the page's body instead of that button; with a single dialog the focus came back as expected.

Minimal reproduction:

import { mount, signal, Text, View } from "nsh";
import { Dialog } from "nsh/base";

function Button(props: { label: string; onPress: () => void }) {
  return (
    <View accessibilityLabel={props.label} onPress={props.onPress} style={{ padding: 12 }}>
      <Text>{props.label}</Text>
    </View>
  );
}

function App() {
  const outer = signal(false);
  const inner = signal(false);
  return (
    <View style={{ flex: 1, padding: 24 }}>
      <Button label="Open dialog" onPress={() => outer.set(true)} />
      <Dialog open={outer()} accessibilityLabel="Outer" onOpenChange={({ open }) => !open && outer.set(false)}>
        <Button label="Delete" onPress={() => inner.set(true)} />
      </Dialog>
      <Dialog open={inner()} accessibilityLabel="Confirm" onOpenChange={({ open }) => !open && inner.set(false)}>
        <Button label="Cancel" onPress={() => inner.set(false)} />
      </Dialog>
    </View>
  );
}

mount(App);

Tab, Enter (opens the dialog, focus on "Delete"), Enter (opens the confirmation), Escape:

  • before: document.activeElement is BODY
  • after: "Delete"; a second Escape returns it to "Open dialog", as before.

Change

dismiss restores the focus when no modal or dialog is left, or when the one now on top contains the element to restore (the same top scopeFocus makes the only non-inert layer). A dialog under another that closes first still leaves the focus in the top one. docs/dev/web.md describes the focus return.

Tests

  • node --test crates/nsh-platform-web/web/nsh.test.mjs: new case "closing a modal opened from another gives the focus back to what opened it in that one" presents two modals and dismisses them in both orders; it fails on main and passes here.
  • Checked in headless Chromium with the reproduction above (before on main, after on this branch).

Closing a dialog gave the focus back to what had it only when no other
dialog was left, so closing a confirmation opened from a button in a
dialog left the focus on the page's body instead of that button.

The focus now goes back to what had it when the dialog still on top
holds it (or when none is left), and stays put while a dialog over it
keeps it out of reach.

@huacnlee huacnlee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Closing a nested dialog now returns the focus to the control in the dialog underneath, instead of leaving it on body. A new unit test covers both dismissal orders.

  • crates/nsh-platform-web/web/nsh.js:7537 — the focus is restored when no layer is left, or when the new top layer contains the restore target. This matches the single non-inert layer that scopeFocus leaves, so the focus can never land behind an inert layer.
  • this.dialogs exists only in dialogs: "viewport" mode (nsh.js:2286). Otherwise the code falls back to topModal(), so both hosting modes are covered. When the bottom dialog closes first, the top dialog keeps the focus, and the test asserts that.
  • The test (nsh.test.mjs:2665) uses a contains: () => true root and fake modals only. Real-browser behavior is claimed from a headless Chromium run. That is acceptable, but no viewport-dialog (popover) case is unit-tested.
  • No public API change. Docs updated (docs/dev/web.md:898). The description is clear.

CI: compat passed. check (macos/ubuntu/windows), web and build are pending. Not a pass yet.

Reviewed head 1c09894.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee huacnlee added the Approved Review approved label Dec 3, 2026
@huacnlee
huacnlee merged commit d6da37f into main Dec 3, 2026
5 of 9 checks passed
@huacnlee
huacnlee deleted the web-nested-modal-focus-return branch December 3, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved Review approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants