use-event-listener
shadcn-hooks/use-event-listenerFreeHook
A hook to attach event listeners to DOM elements, the window, or media query lists.
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-event-listener.jsonSource
1import { useEffectWithTarget } from '@/registry/hooks/use-effect-with-target'2import { useLatest } from '@/registry/hooks/use-latest'3import { getTargetElement } from '@/registry/lib/create-effect-with-target'4import type { BasicTarget } from '@/registry/lib/create-effect-with-target'56type noop = (...p: any) => void78export type Target = BasicTarget<HTMLElement | Element | Window | Document>910interface Options<T extends Target = Target> {11 target?: T12 capture?: boolean13 once?: boolean14 passive?: boolean15 enable?: boolean16}1718function useEventListener<K extends keyof HTMLElementEventMap>(19 eventName: K,20 handler: (ev: HTMLElementEventMap[K]) => void,21 options?: Options<HTMLElement>,22): void23function useEventListener<K extends keyof ElementEventMap>(24 eventName: K,25 handler: (ev: ElementEventMap[K]) => void,26 options?: Options<Element>,27): void28function useEventListener<K extends keyof DocumentEventMap>(29 eventName: K,30 handler: (ev: DocumentEventMap[K]) => void,31 options?: Options<Document>,32): void33function useEventListener<K extends keyof WindowEventMap>(34 eventName: K,35 handler: (ev: WindowEventMap[K]) => void,36 options?: Options<Window>,37): void38function useEventListener(39 eventName: string | string[],40 handler: (event: Event) => void,41 options?: Options<Window>,42): void43function useEventListener(44 eventName: string | string[],45 handler: noop,46 options: Options,47): void4849function useEventListener(50 eventName: string | string[],51 handler: noop,52 options: Options = {},53) {54 const { enable = true } = options5556 const handlerRef = useLatest(handler)5758 useEffectWithTarget(59 () => {60 if (!enable) {61 return62 }6364 const targetElement = getTargetElement(options.target, window)65 if (!targetElement?.addEventListener) {66 return67 }6869 const eventListener = (event: Event) => {70 return handlerRef.current(event)71 }7273 const eventNameArray = Array.isArray(eventName) ? eventName : [eventName]7475 eventNameArray.forEach((event) => {76 targetElement.addEventListener(event, eventListener, {77 capture: options.capture,78 once: options.once,79 passive: options.passive,80 })81 })8283 return () => {84 eventNameArray.forEach((event) => {85 targetElement.removeEventListener(event, eventListener, {86 capture: options.capture,87 })88 })89 }90 },91// … truncated
What it pulls in
Other registry items
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
