useIdle
function useIdle(ms: number): boolean;
Returns true once ms milliseconds pass with no activity on the page, and false again as
soon as the user is back.
- Activity is any pointer movement or press, key press, wheel, touch or scroll (of the page or any element). The tab becoming visible again also counts; the tab being hidden does not, so a user who switched tabs goes idle as usual.
- It starts
false, and isfalseduring server rendering. - Changing
msrestarts the wait fromfalse. A delay that isn't a positive, finite number never goes idle.
Frequent events such as pointermove only record the time, so listening is cheap.
Live demo
Active
Keep your hands off the mouse and keyboard for 3 seconds.
Example
import { useIdle } from "react-honey-hooks";
function Presence() {
const idle = useIdle(5 * 60_000);
return <span>{idle ? "Away" : "Online"}</span>;
}