Skip to content
Source
Sign inSign up
TanStack/hotkeys

stopPropagation with multiple modal #42

Closedluirzy(GitHub profile, opens in a new tab) opened this issue 1 comment

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

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

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
  1. magicdawn(GitHub profile, opens in a new tab)commented

    Not developer of this library, just some opinions after similar issue and read some code:

    stopPropagation & stopImmediatePropagation will not work:

    • @tanstack/react-hotkeys use a userland registry to handle keydown/keyup event for example: useHotkey('Escape', callback1); useHotkey('Escape', callback2); useHotkey multi times, but document will have single keydown event listener.
    • the registry did not check some kind of flag. I suggest add a flag similar to native event.defaultPrevented: event.propagationStopped, and check between registration execution.
    • or switch to multi native event listeners, don't know current a manual registry will help for performance?

    application level flag

    cast event: KeyboradEvent to CustomKeyboardEvent

    interface CustomKeyboardEvent extends KeyboardEvent {
      stopped?: boolean
    }
    • stop with (event as CustomKeyboardEvent).stopped = true
    • check in your handler if ((event as CustomKeyboardEvent).stopped) return

    enabled

    this should work. check your logic.

Sign in to join this conversation.