Vue Idle User Detector
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
-
TypeError: Vue.use is not a function
cause Attempting to install `v-idle` as a plugin using `Vue.use()` after v1.0.0.fixRemove `Vue.use(Vidle)` calls. `v-idle` is now a component; import it directly (`import Vidle from 'v-idle'`) and register it in your component's `components` option. -
[Vue warn]: Failed to resolve component: Vidle
cause The `Vidle` component has not been correctly registered in the parent Vue component's `components` option, or the import path is incorrect.fixEnsure `Vidle` is imported correctly (`import Vidle from 'v-idle'`) and explicitly registered within the `components` option of the Vue component where it's being used, e.g., `{ components: { Vidle } }`. -
TypeError: Cannot read properties of undefined (reading 'use') at <anonymous>:1:5
cause When using `v-idle` in Vue 2.6+ with Composition API, `@vue/composition-api` has not been correctly installed and registered globally via `Vue.use(VueCompositionAPI)`.fixFor Vue 2.6 projects, install `@vue/composition-api` (`npm install @vue/composition-api`) and ensure `Vue.use(VueCompositionAPI)` is called in your application's entry file before mounting your root Vue instance.
Warnings
- breaking Prior to v1.0.0, `v-idle` was installed as a Vue plugin via `Vue.use(Vidle)`. As of v1.0.0, `v-idle` is no longer a plugin and must be imported directly as a component (`import Vidle from 'v-idle'`) and registered in your component's `components` option.
- gotcha For Vue 2.6 projects, utilizing the Composition API (and therefore `v-idle`'s cross-version compatibility) requires explicit installation of the `@vue/composition-api` package and registering it with `Vue.use(VueCompositionAPI)` in your main Vue entry file.
- gotcha The `syncKey` feature, introduced in v1.0.1 for synchronizing the refresh event across browser tabs, relies on the browser's `BroadcastChannel` API. This API might not be supported in older browsers, preventing the synchronization feature from working as expected in those environments.
Install
-
npm install v-idle -
yarn add v-idle -
pnpm add v-idle
Imports
- Vidle
import { Vidle } from 'v-idle'import Vidle from 'v-idle'
- defineComponent
import { defineComponent } from '@vue/composition-api'import { defineComponent } from 'vue' - defineComponent
import { defineComponent } from 'vue'import { defineComponent } from '@vue/composition-api' - VueCompositionAPI
import VueCompositionAPI from '@vue/composition-api'
Quickstart
<!-- 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>