{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-idle",
  "title": "UseIdle",
  "description": "Detects if the user is idle.",
  "files": [
    {
      "path": "registry/hooks/use-idle.tsx",
      "content": "'use client';\n\nimport { useCallback, useEffect, useRef, useState } from 'react';\n\nconst DEFAULT_EVENTS = [\n  'keydown',\n  'mousemove',\n  'touchmove',\n  'click',\n  'scroll',\n  'wheel',\n] as const;\n\ninterface UseIdleOptions {\n  events?: ReadonlyArray<(typeof DEFAULT_EVENTS)[number]>;\n  initialState?: boolean;\n}\n\nexport function useIdle(\n  timeout: number = 1000,\n  options?: UseIdleOptions,\n): [boolean, () => void] {\n  const { events = DEFAULT_EVENTS, initialState = true } = options || {};\n  const [idle, setIdle] = useState<boolean>(initialState);\n  const timer = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n  const reset = useCallback(() => {\n    if (!idle) return; // No need to reset if already idle\n    setIdle(false);\n    if (timer.current) {\n      clearTimeout(timer.current);\n    }\n    timer.current = setTimeout(() => {\n      setIdle(true);\n    }, timeout);\n  }, [timeout, idle]);\n\n  useEffect(() => {\n    // Stable handler for listeners\n    const handleEvents = () => {\n      if (!idle) {\n        // Only reset the timer, don't setIdle(false) if already active\n        if (timer.current) {\n          clearTimeout(timer.current);\n        }\n        timer.current = setTimeout(() => {\n          setIdle(true);\n        }, timeout);\n        return;\n      }\n      setIdle(false);\n      if (timer.current) {\n        clearTimeout(timer.current);\n      }\n      timer.current = setTimeout(() => {\n        setIdle(true);\n      }, timeout);\n    };\n\n    events.forEach((event) => document.addEventListener(event, handleEvents));\n\n    // Initialize timer\n    if (timer.current) {\n      clearTimeout(timer.current);\n    }\n    timer.current = setTimeout(() => {\n      setIdle(true);\n    }, timeout);\n\n    return () => {\n      events.forEach((event) =>\n        document.removeEventListener(event, handleEvents),\n      );\n      if (timer.current) {\n        clearTimeout(timer.current);\n      }\n    };\n  }, [timeout, events.join(','), idle]);\n\n  return [idle, reset];\n}\n",
      "type": "registry:hook",
      "target": "hooks/guarahooks/use-idle.tsx"
    }
  ],
  "categories": [
    "utilities"
  ],
  "type": "registry:hook"
}