BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/guarahooks/use-click-outside

UseClickOutside

guarahooks/use-click-outside
FreeHook

Detects clicks outside of a referenced element.

Live preview

live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://guarahooks.com/r/use-click-outside.json

Source

registry/hooks/use-click-outside.tsxguarahooks.com/r/use-click-outside.json
1'use client';
2
3import { useEffect, useRef } from 'react';
4
5export function useClickOutside<T extends HTMLElement>(
6 ref: React.RefObject<T | null>,
7 onClickOutside: (event: MouseEvent | TouchEvent) => void,
8) {
9 const callbackRef = useRef(onClickOutside);
10 useEffect(() => {
11 callbackRef.current = onClickOutside;
12 }, [onClickOutside]);
13
14 useEffect(() => {
15 function handleEvent(event: MouseEvent | TouchEvent) {
16 const el = ref.current;
17 if (!el || !(event.target instanceof Node) || el.contains(event.target))
18 return;
19 callbackRef.current(event);
20 }
21
22 document.addEventListener('mousedown', handleEvent, true);
23 document.addEventListener('touchstart', handleEvent, true);
24 return () => {
25 document.removeEventListener('mousedown', handleEvent, true);
26 document.removeEventListener('touchstart', handleEvent, true);
27 };
28 }, []);
29}

Files it writes

  • registry/hooks/use-click-outside.tsx

Facts

Registry
guarahooks
Type
registry:hook
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-03
Last confirmed
today

Go to the source

Docs on guarahooks guarahooks.com h3rmel/guarahooks on GitHub Raw registry item This item as JSON

More from guarahooks

All 100 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
UseAbortControlleruse-abort-controllerguarahooksHooksFreeno deps
UseArrayStateuse-array-stateguarahooksHooksFreeno deps
UseAxiosuse-axiosguarahooksHooksFreeno deps
UseBatteryStatususe-battery-statusguarahooksHooksFreeno deps
UseBetterAuthuse-better-authguarahooksHooksFreeno deps
UseConfirmuse-confirmguarahooksHooksFreeno deps
UseCookieuse-cookieguarahooksHooksFreeno deps
UseCopyToClipboarduse-copy-to-clipboardguarahooksHooksFreeno deps
BlockDex

Built by

Kynth Studio

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

BlockDex

BlockDex

Built by

Kynth Studio

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

BlockDex

BlockDex

Built by

Kynth Studio

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

BlockDex