useIdle

An SSR-safe hook to monitor user inactivity by tracking browser interaction events and exposing real-time idle status based on a custom timeout.

Tip

You can customize the events array to exclude specific interactions—for example, removing scroll to ignore page movement as an "active" state.

Note

This hook is a state reporter only; it does not perform automated actions like session timeouts or logouts.

Installation

bash

Description

The useIdle observes a customizable set of browser events (such as mousemove, keydown, or scroll). Each detected interaction resets an internal timer; if no activity occurs before the timeout expires, the user is marked as idle. This hook is purely reactive, allowing developers to trigger side effects like session timeouts, pausing expensive computations, or UI state changes.

Return values

NameTypeDescription
isIdlebooleanReturns true if the timeout has been reached without user interaction.
lastActiveAtnumberThe timestamp (ms) of the most recent user activity detected.

Demo

Idle

Status: User is Active
Last Activity: 1:16:11 AM

Source code

tsx