Docs
useIdle

Detect user inactivity with useIdle.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-idle

Usage

"use client"
 
import { useIdle } from "@/hooks/use-idle"
 
export function Component() {
  const isIdle = useIdle(3000) // 3 seconds idle time
 
  return (
    <div>
      <h2>User Status: {isIdle ? "⏸️ Idle" : "▶️ Active"}</h2>
      <p>Move your mouse or press any key to reset the idle timer</p>
      <div>
        <div
          style={{
            height: "6px",
            width: "100%",
            borderRadius: "9999px",
            transition: "background-color 0.3s",
            backgroundColor: isIdle ? "yellow" : "green",
          }}
        />
      </div>
    </div>
  )
}

API Reference

Parameters

NameTypeDescriptionDefault ValueOptional
msnumberThis is the duration of idle time (in milliseconds) after which the idle state will be set to true. The default value is 20 * 1000 (20 seconds).60 * 1000 (60 seconds)Yes

Return Values

NameTypeDescription
idlebooleanA boolean indicating if the user is idle. It is true if the user has been idle for at least ms milliseconds.