Modal layer keyboard events
The modal stage is the first stop in the key pipeline: before global keys, regular layers, and the screen stack. Three rules define it:
- Only the modal layer with the highest
zIndexreceives keyboard events. - The top modal broadcasts the event to all of its active elements.
- Keys the top modal does not handle are swallowed by the barrier and go no further.
Prerequisites
| Chapter | Why you need it |
|---|---|
| Modal Layers | Opening, mounting, and closing |
| Keyboard Events Between Layers | Regular-layer propagation as the contrast |
Top modal exclusivity
With several modals open, only the topmost one participates; the others stay mounted but receive no events. When the topmost closes, the next one takes over.
openModalLayer('modal-1', 100)
openModalLayer('modal-2', 200) // only this one receives keysA key modal-2 does not handle is not passed to modal-1; the barrier swallows it.
The keyboard barrier
Regular layers bubble a miss and eventually reach the screen stack. A modal does the opposite: while one is open, an unhandled key stops at the top modal and never reaches regular layers or the screen.
function Dialog() {
const { boundKeyboard } = useKeyboard()
useEffect(() => boundKeyboard('return', () => confirm()), [boundKeyboard])
}return is handled by the dialog; every other key is swallowed, so the screen's bindings do not fire.
Broadcast within the top layer
Inside the top modal, behavior matches regular layers: the event is broadcast to every active element, so several elements can respond to the same key. As in a regular layer it is handled in two passes — priority handlers (auto-tab, wildcard, multi-key sequences) run before ordinary bindings, so a sequence takes the key without also firing a sibling's plain binding. The barrier still blocks propagation once the layer is done.
Passing through and listening
allowModalputs selected keys on an allowlist so they cross the barrier when the modal does not handle them.useModalMissListenerreports keys swallowed by the barrier.
Neither changes the "modal handles first" order.
Boundaries
- The modal stage runs before global keys; use
allowModalwhen a global shortcut must still fire while a modal is open. - A modal with no active elements still has its barrier.
- Equal
zIndexvalues are ordered bycreatedAt; the later modal is on top. - When all modals close, the keyboard returns to regular layers and the screen stack.
