useIdleDetectionApi
About
Hook to detect when user is idle using the native Idle Detection API with polyfill fallback. This hook provides comprehensive idle detection capabilities including user activity state, screen lock state, and permission management.
Examples
Basic idle detection
With state change callbacks
Manual control with custom threshold
Arguments
Argument | Type | Description | Default value |
---|---|---|---|
options | object | Configuration options for idle detection | {} |
Options
Option key | Type | Description | Default value |
---|---|---|---|
threshold | number | Minimum time in milliseconds before user is considered idle. Must be at least 60000ms (60 seconds) per W3C specification | 60000 |
autoStart | boolean | Whether to automatically start idle detection when the hook mounts | false |
requestPermission | boolean | Whether to automatically request permission for the Idle Detection API (required for native API usage) | false |
onIdleChange | function | Callback function called when idle state changes. Receives object with {isIdle, userState, screenState} | undefined |
onError | function | Callback function called when an error occurs during idle detection setup or operation. Receives Error object | undefined |
Return value
Return value | Type | Description |
---|---|---|
isIdle | boolean | Whether the user is currently idle |
userState | string | Current user activity state: "active" or "idle" |
screenState | string | Current screen state: "locked" or "unlocked" |
isSupported | boolean | Whether the native Idle Detection API is supported in the current browser |
isPermissionGranted | boolean | Whether permission has been granted for the Idle Detection API |
start | function | Function to manually start idle detection. Returns a Promise that resolves when detection starts |
stop | function | Function to manually stop idle detection and reset states |