@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
45 lines (44 loc) • 1.1 kB
JavaScript
"use client";
let react = require("react");
//#region packages/@mantine/hooks/src/use-idle/use-idle.ts
const DEFAULT_OPTIONS = {
events: [
"keydown",
"mousemove",
"touchmove",
"click",
"scroll",
"wheel"
],
initialState: true
};
function useIdle(timeout, options) {
const { events, initialState } = {
...DEFAULT_OPTIONS,
...options
};
const [idle, setIdle] = (0, react.useState)(initialState);
const timer = (0, react.useRef)(-1);
(0, react.useEffect)(() => {
const handleEvents = () => {
setIdle(false);
if (timer.current) window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => {
setIdle(true);
}, timeout);
};
events.forEach((event) => document.addEventListener(event, handleEvents));
timer.current = window.setTimeout(() => {
setIdle(true);
}, timeout);
return () => {
events.forEach((event) => document.removeEventListener(event, handleEvents));
window.clearTimeout(timer.current);
timer.current = -1;
};
}, [timeout]);
return idle;
}
//#endregion
exports.useIdle = useIdle;
//# sourceMappingURL=use-idle.cjs.map