Repository navigation
web: Return the focus into the dialog under a closed nested dialog - #4244
Merged
Merged
Conversation
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
approved these changes
Dec 3, 2026
huacnlee
left a comment
Member
There was a problem hiding this comment.
✅ 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 thatscopeFocusleaves, so the focus can never land behind an inert layer.this.dialogsexists only indialogs: "viewport"mode (nsh.js:2286). Otherwise the code falls back totopModal(), 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 acontains: () => trueroot 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (
dismisschecked!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'sbodyinstead of that button; with a single dialog the focus came back as expected.Minimal reproduction:
Tab, Enter (opens the dialog, focus on "Delete"), Enter (opens the confirmation), Escape:
document.activeElementisBODYChange
dismissrestores the focus when no modal or dialog is left, or when the one now on top contains the element to restore (the same topscopeFocusmakes the only non-inert layer). A dialog under another that closes first still leaves the focus in the top one.docs/dev/web.mddescribes 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 onmainand passes here.main, after on this branch).