Skip to content

useIdle

Hook that defines the logic when the user is idle

sensors
low
test coverage
Last changed: 12 days ago

Installation

Library
CLI
Manual
typescript
import { useIdle } from '@siberiacancode/reactuse';

Usage

typescript
const { idle, lastActive } = useIdle();
// or
const { idle, lastActive } = useIdle(1000, (idle) => console.log(idle));
// or
const { idle, lastActive } = useIdle(1000, { onChange: (idle) => console.log(idle) });

Demo

Api

Returns

UseIdleReturn

Parameters

NameTypeDefaultNote
milliseconds?numberONE_MINUTEThe idle time in milliseconds
callback(idle: boolean) => void-The callback to execute when idle state changes

Returns

UseIdleReturn

Parameters

NameTypeDefaultNote
milliseconds?numberONE_MINUTEThe idle time in milliseconds
options.onChange?(idle: boolean) => void-The callback to execute when idle state changes
options.initialValue?booleanfalseThe options for the hook
options.events?Array<keyof WindowEventMap>IDLE_EVENTS

Returns

UseIdleReturn

Type declaration

typescript
export type UseIdleCallback = (idle: boolean) => void;

export interface UseIdleOptions {
  /** The idle events */
  events?: Array<keyof DocumentEventMap>;
  /** The idle state */
  initialValue?: boolean;
  /** The callback to execute when idle state changes */
  onChange?: UseIdleCallback;
}

export interface UseIdleReturn {
  /** The idle state */
  idle: boolean;
  /** The last active time */
  lastActive: number;
}

export interface UseIdle {
  (): UseIdleReturn;

  (milliseconds: number, callback: UseIdleCallback): UseIdleReturn;

  (milliseconds: number, options?: UseIdleOptions): UseIdleReturn;
}

Source

SourceDemo

Contributors

D
debabin
debabin
H
hywax
hywax

Released under the MIT License.