import { useEffect, useState } from 'react'; import { throttle } from 'throttle-debounce'; import { off, on } from './misc/util'; var defaultEvents = ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel']; var oneMinute = 60e3; var useIdle = function (ms, initialState, events) { if (ms === void 0) { ms = oneMinute; } if (initialState === void 0) { initialState = false; } if (events === void 0) { events = defaultEvents; } var _a = useState(initialState), state = _a[0], setState = _a[1]; useEffect(function () { var mounted = true; var timeout; var localState = state; var set = function (newState) { if (mounted) { localState = newState; setState(newState); } }; var onEvent = throttle(50, function () { if (localState) { set(false); } clearTimeout(timeout); timeout = setTimeout(function () { return set(true); }, ms); }); var onVisibility = function () { if (!document.hidden) { onEvent(); } }; for (var i = 0; i < events.length; i++) { on(window, events[i], onEvent); } on(document, 'visibilitychange', onVisibility); timeout = setTimeout(function () { return set(true); }, ms); return function () { mounted = false; for (var i = 0; i < events.length; i++) { off(window, events[i], onEvent); } off(document, 'visibilitychange', onVisibility); }; }, [ms, events]); return state; }; export default useIdle;