Vue Idle User Detector

1.0.3 · active · verified Sun Apr 19

V-idle is a universal Vue plugin (now component-based) designed to detect inactive users across both Vue 2 and Vue 3 applications. The current stable version is v1.0.3, with recent updates indicating an active maintenance cadence focused on bug fixes and compatibility. Key differentiators include its seamless support for both Vue 2 and Vue 3 environments via `vue-demi`, eliminating the need for `Vue.use()` for plugin installation since v1.0.0. It offers flexible idle event detection, configurable reminder timings, and a unique `syncKey` feature for synchronizing activity across multiple browser tabs using the BroadcastChannel API, ensuring consistent idle state management in complex applications. This library ships with TypeScript definitions for improved developer experience.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates basic integration of the `Vidle` component within a Vue 3 setup, showcasing how to handle `@idle` and `@refresh` events, configure `reminders`, `events` to track, and use `syncKey` for cross-tab synchronization. The `wait` prop is set to a short duration for quick testing.

<!-- App.vue template example -->
<template>
  <div>
    <h1>v-idle Demo</h1>
    <p>Idle Count: {{ idleCount }}</p>
    <p>Refresh Count: {{ refreshCount }}</p>
    <p>Last Activity: {{ lastActivity || 'None yet' }}</p>
    <Vidle
      @idle="onIdle"
      @refresh="onRefresh"
      :reminders="[5, 10]"
      :events="['mousemove', 'keypress', 'click']"
      :wait="30" 
      syncKey="my-app-idle-sync"
    />
    <p>Move mouse, click, or press keys to reset the timer.</p>
    <p>An idle event will fire after 30 seconds of inactivity, with reminders at 5 and 10 seconds remaining.</p>
    <p>The `syncKey` prop will synchronize activity across tabs sharing the same key.</p>
  </div>
</template>

<script lang="typescript">
import { defineComponent, ref } from 'vue';
import Vidle from 'v-idle';

export default defineComponent({
  components: {
    Vidle,
  },
  setup() {
    const idleCount = ref(0);
    const refreshCount = ref(0);
    const lastActivity = ref('');

    const onIdle = () => {
      idleCount.value++;
      console.log('User is idle!');
    };

    const onRefresh = (event: { type: string; key: string | undefined }) => {
      refreshCount.value++;
      lastActivity.value = event.type + (event.key ? ` (${event.key})` : '');
      console.log('User activity detected:', event.type, event.key);
    };

    return {
      idleCount,
      refreshCount,
      lastActivity,
      onIdle,
      onRefresh,
    };
  },
});
</script>

view raw JSON →