Repository navigation
Description
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
- Open the Playground/WP Compat Overlay Slot story (
Inside Components Popover). - Type in the
Autocompleteinput until its popup opens. - 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'suseDismissEscape handler),useDialoghas already calledonClose()and stopped propagation. Modaldoesn't have this issue because it handles Escape via a ReactonKeyDownprop on its overlay (packages/components/src/modal/index.tsx), which naturally runs after React descendants' handlers — so a descendant callingevent.stopPropagation()correctly prevents Modal's handler from firing.
Other minor issues in the same callback:
- Uses the deprecated
event.keyCodeinstead ofevent.key === 'Escape'. - The ref callback registers a
keydownlistener every time React calls the ref but never removes it (addEventListenerwithout a matchingremoveEventListener).
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
useDialog: handle Escape via React onKeyDown so cascade works through…
useDialog: handle Escape via React `onKeyDown` so cascade works throu…