BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/zyeon/use-event-listener

useEventListener

zyeon/use-event-listener
FreeHook

A type-safe window/document/element event listener hook with a latest-ref handler — zero rebinds on handler change, never stale.

Install it

npx shadcn@latest add https://ui.zyeon.ai/r/use-event-listener.json

Source

src/registry/hooks/use-event-listener.tsxui.zyeon.ai/r/use-event-listener.json
1"use client"
2
3import * as React from "react"
4
5export interface UseEventListenerOptions {
6 /** Passed straight through as the native addEventListener `passive`. */
7 passive?: boolean
8 /** Passed straight through as the native addEventListener/removeEventListener `capture`. */
9 capture?: boolean
10}
11
12type TargetRef<T extends HTMLElement> = React.RefObject<T | null>
13
14// Overload 1: no target, or window passed explicitly — event names map to WindowEventMap.
15export function useEventListener<K extends keyof WindowEventMap>(
16 eventName: K,
17 handler: (event: WindowEventMap[K]) => void,
18 options?: UseEventListenerOptions & { target?: Window },
19): void
20
21// Overload 2: document as the target — event names map to DocumentEventMap.
22export function useEventListener<K extends keyof DocumentEventMap>(
23 eventName: K,
24 handler: (event: DocumentEventMap[K]) => void,
25 options: UseEventListenerOptions & { target: Document },
26): void
27
28// Overload 3: an element RefObject as the target — event names map to
29// HTMLElementEventMap, and T is inferred from the ref at the call site (say
30// RefObject<HTMLDivElement>).
31// Note: the effect depends on the ref object itself (stable identity). If .current
32// is null when the effect runs, nothing is bound this round and nothing re-binds
33// later — for a conditionally rendered target, remount the consumer with a key or
34// switch to the callback-ref pattern (see use-hover). This fits targets that live as
35// long as the component: window / document / an element always in the tree.
36export function useEventListener<K extends keyof HTMLElementEventMap, T extends HTMLElement = HTMLElement>(
37 eventName: K,
38 handler: (event: HTMLElementEventMap[K]) => void,
39 options: UseEventListenerOptions & { target: TargetRef<T> },
40): void
41
42/**
43 * Typed event listening against three kinds of target — window (the default),
44 * document, or any element ref — narrowing `eventName` / `handler` to the native
45 * event map that matches.
46 *
47 * The core is the latest-ref for the handler: `handler` is synced into a ref after
48 * every render, while the effect that actually calls `addEventListener` depends only
49 * on `eventName` / `target` / the primitives pulled out of `options`
50 * (`passive` / `capture`) — never on the `options` object, and never on `handler`.
51 * So a consumer passing a freshly built `handler` on every render (closing over the
52 * latest state, which is the common case) does not thrash the listener; when it
53// … truncated

Files it writes

  • src/registry/hooks/use-event-listener.tsx

Facts

Registry
zyeon
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
In the index
at or before 2026-08-12
Last confirmed
today

Go to the source

ui.zyeon.ai Zyeon-AI/zyeon-ui-components on GitHub Raw registry item This item as JSON

More from zyeon

All 893 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
useAsyncuse-asynczyeonHooksFreeno deps
useBatteryuse-batteryzyeonHooksFreeno deps
useBroadcastChanneluse-broadcast-channelzyeonHooksFreeno deps
useClickOutsideuse-click-outsidezyeonHooksFreeno deps
useClipboardPasteuse-clipboard-pastezyeonHooksFreeno deps
useControllableStateuse-controllable-statezyeonHooksFreeno deps
useCookieuse-cookiezyeonHooksFreeno deps
useCopyToClipboarduse-copy-to-clipboardzyeonHooksFreeno 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