Skip to Content
ohooks

useIdle

Tracks whether the user has been inactive for a given duration by listening to window interaction events.

Loading preview...

The useIdle hook monitors user activity across mouse, pointer, touch, keyboard, wheel, and visibility events and returns true when no interaction has occurred for timeout milliseconds.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useIdle.json

Usage

Examples

Auto-Locking a Session After Inactivity

Detect when the user has been inactive for 60 seconds and show a lock screen or warning banner.

Custom Activity Events and Initial State

Customize the list of DOM events that reset the timer or start in the idle state using initialState.

API Reference

UseIdleOptions

Configuration options for initial idle state and monitored DOM events.

Property
Specification
initialState?
uniondefault:false
Initial idle state before the first timeout elapses
events?
array
Window events that reset the idle timer
throttleMs?
numberdefault:200
Throttle interval in milliseconds for activity event listeners