Skip to content

Components/Compose: useDialog Escape handler runs before React, breaking cascade in portaled descendants #78432

Closed

Description

@ciampo

Summary

useDialog (used by Popover, EntitiesSavedStates, the dataviews panel dropdown, and others) attaches its Escape handler as a native keydown listener directly on the dialog DOM node. That short-circuits any React onKeyDown handlers in portaled descendants, so a descendant that handles Escape (via React's synthetic events) can't prevent the dialog from closing.

The most visible symptom: a @wordpress/ui Autocomplete (or any base-ui combobox/listbox) inside a @wordpress/components Popover — pressing Escape always closes the Popover, regardless of whether the Autocomplete listbox is open and would otherwise handle the Escape itself.

Steps to reproduce

  1. Open the Playground/WP Compat Overlay Slot story (Inside Components Popover).
  2. Type in the Autocomplete input until its popup opens.
  3. Press Escape.

Expected: the Autocomplete listbox closes; the host Popover stays open. (This is what happens when the same Autocomplete is rendered inside a @wordpress/components Modal — see the Inside Components Modal story.)
Actual: the host Popover closes immediately, regardless of the Autocomplete state.

Root cause
// packages/compose/src/hooks/use-dialog/index.ts
const closeOnEscapeRef = useCallback( ( node: HTMLElement ) => {
	if ( ! node ) return;
	node.addEventListener( 'keydown', ( event: KeyboardEvent ) => {
		if (
			event.keyCode === ESCAPE &&
			! event.defaultPrevented &&
			currentOptions.current?.onClose
		) {
			event.preventDefault();
			event.stopPropagation();
			currentOptions.current.onClose();
		}
	} );
}, [] );
  • The listener is native and attached to the dialog node directly, so it runs while the native event bubbles through the DOM — before React dispatches synthetic events through the React tree.
  • By the time React would dispatch the synthetic event to a portaled descendant's onKeyDown (e.g. base-ui's useDismiss Escape handler), useDialog has already called onClose() and stopped propagation.
  • Modal doesn't have this issue because it handles Escape via a React onKeyDown prop on its overlay (packages/components/src/modal/index.tsx), which naturally runs after React descendants' handlers — so a descendant calling event.stopPropagation() correctly prevents Modal's handler from firing.

Other minor issues in the same callback:

  • Uses the deprecated event.keyCode instead of event.key === 'Escape'.
  • The ref callback registers a keydown listener every time React calls the ref but never removes it (addEventListener without a matching removeEventListener).
Expected Esc cascade (what the user sees with Autocomplete inside Modal or base-ui Popover today)
Scenario Expected
Autocomplete listbox open Esc closes only the listbox; host stays open
Autocomplete listbox closed, empty input Esc closes the host
Autocomplete listbox closed, free-text input Esc closes the host
Autocomplete listbox closed, valid value selected First Esc clears value; second Esc closes the host

Proposed fix

Switch useDialog's Escape handling from a ref callback that attaches a native listener to an onKeyDown prop returned alongside focusOutsideProps. Same logic, just via React — so portaled descendants that handle Escape via React onKeyDown (and stop propagation) will correctly prevent the dialog from closing, exactly as they already do for Modal.

Out of scope: any deeper API redesign of useDialog's Escape behavior.

Activity

  1. added
    [Type] Bug
    [Package] Compose
    [Package] Components
    on May 19, 2026
  2. added
    [Status] In Progress
    on May 19, 2026
  3. added a commit that references this issue on May 19, 2026
    7b5b38b
  4. added a commit that references this issue on May 19, 2026
    200f6f8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

[Package] Components[Package] Compose[Status] In Progress[Type] Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions