MCPcopy Create free account
hub / github.com/TanStack/hotkeys / isEventForTarget

Function isEventForTarget

packages/hotkeys/src/manager.utils.ts:146–192  ·  view source on GitHub ↗
(
  event: KeyboardEvent,
  target: HTMLElement | Document | Window,
)

Source from the content-addressed store, hash-verified

144 * instead of document when listeners are attached to document.
145 */
146export function isEventForTarget(
147 event: KeyboardEvent,
148 target: HTMLElement | Document | Window,
149): boolean {
150 // For Document and Window, verify that our handler was indeed called for this target.
151 //
152 // Browser compatibility note:
153 // Per the DOM spec, event.currentTarget should equal the element the listener was
154 // attached to. However, some Chromium-based browsers (notably Brave) exhibit
155 // non-standard behavior where event.currentTarget is set to document.documentElement
156 // (<html>) instead of document when a listener is attached to document.
157 // This may be related to privacy/fingerprinting protections.
158 //
159 // To ensure cross-browser compatibility, we accept both the expected target
160 // and document.documentElement as valid currentTarget values.
161 // See: https://dom.spec.whatwg.org/#dom-event-currenttarget
162 if (target === document || target === window) {
163 return (
164 event.currentTarget === target ||
165 event.currentTarget === document.documentElement
166 )
167 }
168
169 // For Window, accept window, document, or document.documentElement (browser quirks)
170 if (target === window) {
171 return (
172 event.currentTarget === window ||
173 event.currentTarget === document ||
174 event.currentTarget === document.documentElement
175 )
176 }
177
178 // For HTMLElement, check if event originated from or bubbled to the element
179 if (target instanceof HTMLElement) {
180 // Check if the event's currentTarget is the target (capturing/bubbling)
181 if (event.currentTarget === target) {
182 return true
183 }
184
185 // Check if the event's target is a descendant of our target
186 if (event.target instanceof Node && target.contains(event.target)) {
187 return true
188 }
189 }
190
191 return false
192}
193
194/**
195 * Handles conflicts between registrations based on conflict behavior.

Callers 3

#processTargetEventMethod · 0.90
#processTargetEventMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…