UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

45 lines (41 loc) 1.1 kB
"use client"; import { useEffect, useRef, useState } from "react"; //#region src/hooks/use-idle/index.ts const DEFAULT_OPTIONS = { events: [ "keypress", "mousemove", "touchmove", "click", "scroll" ], initialState: true }; /** * `useIdle` is a custom hook that detects whether the user has been idle for a certain amount of time in milliseconds. * * @see https://yamada-ui.com/docs/hooks/use-idle */ const useIdle = (timeout, options) => { const { events, initialState } = { ...DEFAULT_OPTIONS, ...options }; const [idle, setIdle] = useState(initialState); const timeoutId = useRef(void 0); useEffect(() => { const handleEvent = () => { setIdle(false); if (timeoutId.current) clearTimeout(timeoutId.current); timeoutId.current = setTimeout(() => setIdle(true), timeout); }; events.forEach((event) => document.addEventListener(event, handleEvent)); return () => { events.forEach((event) => document.removeEventListener(event, handleEvent)); }; }, [events, timeout]); return idle; }; //#endregion export { useIdle }; //# sourceMappingURL=index.js.map