fn

useIdle

v0.0.14testeddemo

Track whether the user has been inactive for a given duration.

Examples

ts
const { idle, lastActive, reset } = useIdle(5 * 60_000); // 5 minutes
ts
const { idle } = useIdle(10_000, { events: ['keydown'] });

Demo

Loading demo…

Signature

ts
export function useIdle(
  timeout: number = ONE_MINUTE,
  options: UseIdleOptions ={ ... }

Parameters

ParameterTypeDescription
timeout?numberIdle threshold in milliseconds
options?UseIdleOptionsOptions

Returns

UseIdleReturn{ idle, lastActive, isPending, reset, start, stop }
PropertyTypeDescription
idleShallowRef<boolean>Whether the user is currently idle
lastActiveShallowRef<number>Timestamp (ms) of the last detected user activity
isPendingReadonly<ShallowRef<boolean>>Whether the idle tracker is currently running
reset() => voidManually mark the user as active and restart the idle timer
start() => voidBegin (or resume) tracking. Restarts the idle timer unless initialState is true
stop() => voidStop tracking. Resets idle to initialState and clears the pending timer