Skip to content

overlay: Leave Escape and back to a dialog opened above an overlay - #4277

Open
RebelBIrd wants to merge 1 commit into
longbridge:mainfrom
RebelBIrd:overlay-escape-under-modal
Open

RebelBIrd wants to merge 1 commit into
longbridge:mainfrom
RebelBIrd:overlay-escape-under-modal

Conversation

@RebelBIrd

Copy link
Copy Markdown
Contributor

Description

A page can keep an Overlay listening to onCancel in its root view, for example a non-modal side drawer that closes on Escape. While it is mounted, a Dialog (nsh/base) opened above it never hears Escape: the core binds Escape for the root overlay (ESCAPE_BINDING, When::Always) and hands it to that overlay's onCancel, so the dialog's own close (onRequestClose → onOpenChange with reason cancel) never runs and the dialog stays open. A keymap.bind('escape', …, { when: 'always' }) made while the dialog is open is not reached either, because the overlay's binding comes last and wins. Android back takes the same path (BackPress calls overlay_cancel() first).

<View class="flex-1 p-4 gap-2">
  <Overlay class="w-0 h-0" onCancel={() => log.set('overlay cancel')} />
  <Button testID="open" onPress={() => open.set(true)}><Text>Open</Text></Button>
  <Text testID="log">{log()}</Text>
  <Dialog open={open()} onOpenChange={(e) => { log.set(`dialog ${e.reason}`); open.set(e.open) }}>
    <Text testID="body">Dialog</Text>
  </Dialog>
</View>

Press open, then Escape: before, the log shows overlay cancel and the dialog stays open. After, the dialog hears cancel and closes; a second Escape gives overlay cancel.

The cause is in crates/nsh-core/src/core/overlay.rs: whether Escape is bound (overlay_state.cancel, computed in sync_overlay_listeners), the top candidate in overlay_cancel() and the one focus_takes_escape() checks were all chosen across every root view, ignoring a dialog shown above the overlay's root view.

Now the newest layer wins, as on the Web, where a modal <dialog> above an open popover takes Escape first:

  • Core::cancel_host finds the top shown dialog: a created, displayed Kind::Modal that is not one of the app's own windows, the one opened last, or a dialog opened inside it.
  • While there is one, only the overlays hosted by that dialog (OverlayState::host) can take Escape and back. With none listening inside it, no Escape binding is made and the root does not take back, so the key reaches the dialog natively. With no dialog shown, behavior is unchanged.
  • overlay_cancel() and focus_takes_escape() share the new top_cancel_overlay(). The binding is recomputed when a dialog opens or closes, since inserting, removing and making views already mark the overlays dirty.

The platforms only consume the root keymap and the root's back listener, so they need no change.

Docs: the onCancel event and prop docs (ops.rs, js/host.rs, Overlay::on_cancel), docs/dev/elements.md and the English and zh-CN Overlay pages now say that a dialog above the overlay takes the key first.

Public API

No new or changed signatures. Behavior of Overlay onCancel (nsh) / Overlay::on_cancel (nsh_core::element): while a Dialog is shown above the overlay's root view, the dialog takes Escape and back first. The overlay hears them once no dialog is above it.

How to Test

  • cargo test -p nsh-core: all pass, including the new core::tests::a_dialog_above_takes_escape_and_back_from_the_overlays_under_it. It covers the binding going away while a dialog opens, back reaching no overlay under it, an overlay inside the dialog taking Escape and back, a nested dialog, and the root overlay getting Escape again once the dialogs close. It fails on main.
  • cargo test -p nsh-base: all pass, including the new headless dialog::escape_closes_a_dialog_before_the_overlay_under_it (the TSX case above in Rust). It fails on main (dialog cancel never shown).
  • The TSX reproduction above as an nsh test file (open, Escape → dialog cancel and the dialog gone, Escape → overlay cancel) passes headless as ios, android, macos and web. On main it shows ["Open", "overlay cancel", "Dialog"].
  • cargo clippy -p nsh-core -p nsh-base --all-targets, cargo +nightly fmt, ./target/debug/nsh test examples/counter/App.tsx --trace (no overlays: unchanged).
  • Not run: native windows. The change is in the core's keymap and back listener, which every backend consumes as is.

Checklist

  • I have read the CONTRIBUTING document and followed the guidelines.
  • Reviewed the changes in this PR and confirmed AI generated code (if any) is accurate.
  • Passed targeted cargo test -p nsh-core -p nsh-base, cargo +nightly fmt and clippy for the changed crates.
  • Changes to what apps use keep what released versions shipped (cargo xtask compat).

🤖 Generated with Claude Code

An overlay listening to `onCancel` in the root view kept the Escape
binding and the back listener while a dialog was shown above it, so the
dialog never heard its close and stayed open. While a dialog is shown,
only the overlays the top dialog hosts take Escape and back; with none
listening there, the key reaches the dialog.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

huacnlee commented Dec 4, 2026

Copy link
Copy Markdown
Member

💡 The fix is correct and matches the Web top-layer rule, but it changes Overlay onCancel behaviour, so it needs a maintainer call; I recommend accepting.

A shown Dialog now takes Escape and back before overlays in the roots below it (like a modal <dialog> above a popover). No new signatures.

  • Nit — crates/nsh-core/src/core/overlay.rs:379 cancel_host scans roots and window_ids on every sync_overlay_listeners / overlay_cancel / focus_takes_escape; fine while lists are tiny.
  • Nit — overlay.rs:389: "opened last" follows roots order; re-inserting an already-open dialog root would make it the top one. Not covered by the test.
  • Not verified on native windows (as the description says); the keymap/back path is shared by all backends.

Reviewed head 917b9a1 (diff review); CI still running.

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

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants