BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/shadcn-hooks/use-event-listener

use-event-listener

shadcn-hooks/use-event-listener
FreeHook

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.json

Source

registry/hooks/use-event-listener.tsshadcn-hooks.com/r/use-event-listener.json
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'
5
6type noop = (...p: any) => void
7
8export type Target = BasicTarget<HTMLElement | Element | Window | Document>
9
10interface Options<T extends Target = Target> {
11 target?: T
12 capture?: boolean
13 once?: boolean
14 passive?: boolean
15 enable?: boolean
16}
17
18function useEventListener<K extends keyof HTMLElementEventMap>(
19 eventName: K,
20 handler: (ev: HTMLElementEventMap[K]) => void,
21 options?: Options<HTMLElement>,
22): void
23function useEventListener<K extends keyof ElementEventMap>(
24 eventName: K,
25 handler: (ev: ElementEventMap[K]) => void,
26 options?: Options<Element>,
27): void
28function useEventListener<K extends keyof DocumentEventMap>(
29 eventName: K,
30 handler: (ev: DocumentEventMap[K]) => void,
31 options?: Options<Document>,
32): void
33function useEventListener<K extends keyof WindowEventMap>(
34 eventName: K,
35 handler: (ev: WindowEventMap[K]) => void,
36 options?: Options<Window>,
37): void
38function useEventListener(
39 eventName: string | string[],
40 handler: (event: Event) => void,
41 options?: Options<Window>,
42): void
43function useEventListener(
44 eventName: string | string[],
45 handler: noop,
46 options: Options,
47): void
48
49function useEventListener(
50 eventName: string | string[],
51 handler: noop,
52 options: Options = {},
53) {
54 const { enable = true } = options
55
56 const handlerRef = useLatest(handler)
57
58 useEffectWithTarget(
59 () => {
60 if (!enable) {
61 return
62 }
63
64 const targetElement = getTargetElement(options.target, window)
65 if (!targetElement?.addEventListener) {
66 return
67 }
68
69 const eventListener = (event: Event) => {
70 return handlerRef.current(event)
71 }
72
73 const eventNameArray = Array.isArray(eventName) ? eventName : [eventName]
74
75 eventNameArray.forEach((event) => {
76 targetElement.addEventListener(event, eventListener, {
77 capture: options.capture,
78 once: options.once,
79 passive: options.passive,
80 })
81 })
82
83 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

  • @shadcnhooks/use-latest
  • @shadcnhooks/use-effect-with-target
  • @shadcnhooks/create-effect-with-target

Files it writes

  • registry/hooks/use-event-listener.ts

Facts

Registry
shadcn-hooks
Type
registry:hook
Access
Free
Files written
1
Licence
the repository states none
In the index
at or before 2026-08-13
Last confirmed
today

Go to the source

shadcn-hooks.com Debbl/shadcn-hooks on GitHub Raw registry item This item as JSON

More from shadcn-hooks

All 58 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
use-active-elementuse-active-elementshadcn-hooksHooksFreeno deps
use-batteryuse-batteryshadcn-hooksHooksFreeno deps
use-booleanuse-booleanshadcn-hooksHooksFreeno deps
use-click-any-whereuse-click-any-whereshadcn-hooksHooksFreeno deps
use-click-awayuse-click-awayshadcn-hooksHooksFreeno deps
use-clipboarduse-clipboardshadcn-hooksHooksFreeno deps
use-controllable-valueuse-controllable-valueshadcn-hooksHooksFree1 dep
use-counteruse-countershadcn-hooksHooksFreeno deps

Also from Kynth Studios

Built for the same person as BlockDex

SkillWorks

Claude Code skills and plugins, scored

skillworks.kynth.studio

RuleStack

AGENTS.md gallery and agent-config comparison

rulestack.kynth.studio

ToolDrift

What the AI coding tools changed last night

tooldrift.kynth.studio

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex

The studio list

One product, taken apart, once a month

Kynth Studios pulls one shipped product open every month — what it does, what it cost to build, what the pipeline behind it looks like, and what the numbers did. One email a month, nothing in between.

Double opt-in — we send one confirmation link and nothing else until you click it.

BlockDex

Built by

Kynth Studios

the studio behind SkillWorks, RuleStack and ToolDrift

part of Toolproof, the measurement layer for AI agent tooling

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

© 2026 BlockDex. A Kynth Studios product. Changelog

BlockDex