Vue User Inactivity Detection

2.0.5 · maintenance · verified Sun Apr 19

idle-vue is a Vue.js plugin designed to detect user inactivity within a Vue 2 application. It leverages the underlying `idle-js` library to provide a reactive and integrated solution for monitoring when a user has not interacted with the application for a specified duration. The package is currently at version 2.0.5 and, as a Vue 2-specific plugin, its release cadence is likely stable, focused on maintenance rather than frequent new features, aligning with the lifecycle of Vue 2 itself. It offers three primary mechanisms for handling idle states: `onIdle` and `onActive` lifecycle hooks accessible in any Vue component, a computed property `isAppIdle` for reactive state management, and an optional `IdleView` component for a default idle overlay. A key differentiator is its flexible integration, allowing users to either provide an `eventEmitter` (a Vue instance) for hook-based responses or a Vuex `store` for state-driven reactivity, providing developers with robust options for building responsive user experiences around inactivity detection without boilerplate DOM event handling.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `idle-vue` using an `eventEmitter` to detect user inactivity and respond via `onIdle` and `onActive` hooks, updating the UI accordingly.

import Vue from 'vue';
import IdleVue from 'idle-vue';

// Create a new Vue instance to act as the event hub for idle-vue
const eventsHub = new new Vue();

// Install the idle-vue plugin with the event hub and an idle time of 10 seconds
Vue.use(IdleVue, {
  eventEmitter: eventsHub,
  idleTime: 10000 // 10 seconds (in milliseconds)
});

// Create the main Vue application instance
const vm = new Vue({
  el: '#app',
  data () {
    return {
      messageStr: 'Application is active.'
    };
  },
  // Define the onIdle hook to be called when the app goes idle
  onIdle() {
    this.messageStr = 'Application is idle. ZZZ...';
    console.log('User is idle!');
  },
  // Define the onActive hook to be called when the app becomes active again
  onActive() {
    this.messageStr = 'Application is active again!';
    console.log('User is active!');
  },
  template: `
    <div style="font-family: sans-serif; text-align: center; margin-top: 50px;">
      <h1>idle-vue Example</h1>
      <p>{{ messageStr }}</p>
      <p>Move your mouse or type to stay active. It will go idle after 10 seconds of inactivity.</p>
    </div>
  `
});

view raw JSON →