UNPKG

@yamada-ui/react

Version:

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

47 lines (43 loc) 1.25 kB
"use client"; const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs'); let react = require("react"); react = require_rolldown_runtime.__toESM(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] = (0, react.useState)(initialState); const timeoutId = (0, react.useRef)(void 0); (0, react.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 exports.useIdle = useIdle; //# sourceMappingURL=index.cjs.map