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-idle

UseIdle

guarahooks/use-idle
FreeHook

Detects if the user is idle.

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

Source

registry/hooks/use-idle.tsxguarahooks.com/r/use-idle.json
1'use client';
2
3import { useCallback, useEffect, useRef, useState } from 'react';
4
5const DEFAULT_EVENTS = [
6 'keydown',
7 'mousemove',
8 'touchmove',
9 'click',
10 'scroll',
11 'wheel',
12] as const;
13
14interface UseIdleOptions {
15 events?: ReadonlyArray<(typeof DEFAULT_EVENTS)[number]>;
16 initialState?: boolean;
17}
18
19export function useIdle(
20 timeout: number = 1000,
21 options?: UseIdleOptions,
22): [boolean, () => void] {
23 const { events = DEFAULT_EVENTS, initialState = true } = options || {};
24 const [idle, setIdle] = useState<boolean>(initialState);
25 const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
26
27 const reset = useCallback(() => {
28 if (!idle) return; // No need to reset if already idle
29 setIdle(false);
30 if (timer.current) {
31 clearTimeout(timer.current);
32 }
33 timer.current = setTimeout(() => {
34 setIdle(true);
35 }, timeout);
36 }, [timeout, idle]);
37
38 useEffect(() => {
39 // Stable handler for listeners
40 const handleEvents = () => {
41 if (!idle) {
42 // Only reset the timer, don't setIdle(false) if already active
43 if (timer.current) {
44 clearTimeout(timer.current);
45 }
46 timer.current = setTimeout(() => {
47 setIdle(true);
48 }, timeout);
49 return;
50 }
51 setIdle(false);
52 if (timer.current) {
53 clearTimeout(timer.current);
54 }
55 timer.current = setTimeout(() => {
56 setIdle(true);
57 }, timeout);
58 };
59
60 events.forEach((event) => document.addEventListener(event, handleEvents));
61
62 // Initialize timer
63 if (timer.current) {
64 clearTimeout(timer.current);
65 }
66 timer.current = setTimeout(() => {
67 setIdle(true);
68 }, timeout);
69
70 return () => {
71 events.forEach((event) =>
72 document.removeEventListener(event, handleEvents),
73 );
74 if (timer.current) {
75 clearTimeout(timer.current);
76 }
77 };
78 }, [timeout, events.join(','), idle]);
79
80 return [idle, reset];
81}

Files it writes

  • registry/hooks/use-idle.tsx

Facts

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

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
UseClickOutsideuse-click-outsideguarahooksHooksFreeno deps
UseConfirmuse-confirmguarahooksHooksFreeno deps
UseCookieuse-cookieguarahooksHooksFreeno 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