TanStack Hotkeys version#
0.3.0
Framework/Library version#
React v19.2.0
Describe the bug and the steps to reproduce it#
In a generic modal (used by several component), I registered the Escape key without target.
The hook useIsTopMostModal is used to enable the hotkey only on topmost modal.
The issue is:
if a have 2 modal opened, clicking escape both action are executed, the stopPropagation doesn't work.
const isTopMost = useIsTopMostModal(uniqueModalId)
useHotkey(
'Escape',
() => {
onCloseAction?.()
},
{
enabled: isTopMost,
preventDefault: true,
stopPropagation: true
}
)
The same solution, using react-hotkeys, works fine
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)#
Snippet
Screenshots or Videos (Optional)#
No response
Do you intend to try to help solve this bug with your own PR?#
None
Terms & Code of Conduct#
Not developer of this library, just some opinions after similar issue and read some code:
stopPropagation&stopImmediatePropagationwill not work:#@tanstack/react-hotkeysuse a userland registry to handle keydown/keyup event for example:useHotkey('Escape', callback1); useHotkey('Escape', callback2);useHotkey multi times, butdocumentwill have singlekeydownevent listener.event.defaultPrevented:event.propagationStopped, and check betweenregistrationexecution.application level flag#
cast
event: KeyboradEventtoCustomKeyboardEvent(event as CustomKeyboardEvent).stopped = trueif ((event as CustomKeyboardEvent).stopped) returnenabled#this should work. check your logic.