|
1 | 1 | --- |
2 | 2 | title: useIdleTimeout |
3 | | -description: Plugin to track user activity and manage it |
| 3 | +description: A Vue 3 composable that provides an easy way to track user inactivity on your website and execute a callback function when the user becomes idle. |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | # {{ $frontmatter.title }} |
7 | 7 |
|
8 | 8 | {{ $frontmatter.description }} |
9 | 9 |
|
10 | | -::: tip |
| 10 | +::: info |
11 | 11 | A plugin to know the amount of time a user has spent on your website |
12 | 12 | ::: |
13 | 13 |
|
14 | 14 | ## Demo |
15 | 15 |
|
16 | | -<br /> |
17 | | - |
18 | | -<div class="flex items-start gap-05 items-center flex-wrap"> |
19 | | - <MazBtn @click="idle.start()" color="info"> |
20 | | - Start |
21 | | - </MazBtn> |
22 | | - <MazBtn @click="idle.pause()" color="warning"> |
23 | | - Pause |
24 | | - </MazBtn> |
25 | | - <MazBtn @click="idle.resume()"> |
26 | | - Resume |
27 | | - </MazBtn> |
28 | | - <MazBtn @click="idle.reset()" color="secondary"> |
29 | | - Reset |
30 | | - </MazBtn> |
31 | | - <MazBtn @click="idle.destroy()" color="danger"> |
32 | | - Destroy |
33 | | - </MazBtn> |
34 | | -</div> |
35 | | - |
36 | | -<br /> |
37 | | - |
38 | | -<MazCard overflow-hidden style="width: 100%;"> |
39 | | - <div style="display: flex;"> |
40 | | - <div style="flex: 1;">isIdle: {{event.isIdle ?? false}}</div> |
41 | | - <div v-if="event.eventType" style="flex: 1; padding-left: 10px;">eventType: {{event.eventType ?? '-' }}</div> |
| 16 | +<ComponentDemo> |
| 17 | + <div class="flex items-start gap-05 items-center flex-wrap"> |
| 18 | + <MazBtn @click="idleTimeout.start()" color="info"> |
| 19 | + Start |
| 20 | + </MazBtn> |
| 21 | + <MazBtn @click="idleTimeout.pause()" color="warning"> |
| 22 | + Pause |
| 23 | + </MazBtn> |
| 24 | + <MazBtn @click="idleTimeout.resume()"> |
| 25 | + Resume |
| 26 | + </MazBtn> |
| 27 | + <MazBtn @click="idleTimeout.reset()" color="secondary"> |
| 28 | + Reset |
| 29 | + </MazBtn> |
| 30 | + <MazBtn @click="idleTimeout.destroy()" color="danger"> |
| 31 | + Destroy |
| 32 | + </MazBtn> |
42 | 33 | </div> |
43 | | -</MazCard> |
44 | | - |
45 | | -**Wait 3 seconds without any actions to see the idle change to true** |
46 | | - |
47 | | -## How to use it? |
48 | | - |
49 | | -```vue |
50 | | -<template> |
51 | | - <MazBtn @click="idle.start()" color="info"> |
52 | | - Start |
53 | | - </MazBtn> |
54 | | - <MazBtn @click="idle.pause()" color="warning"> |
55 | | - Pause |
56 | | - </MazBtn> |
57 | | - <MazBtn @click="idle.resume()"> |
58 | | - Resume |
59 | | - </MazBtn> |
60 | | - <MazBtn @click="idle.reset()" color="secondary"> |
61 | | - Reset |
62 | | - </MazBtn> |
63 | | - <MazBtn @click="idle.destroy()" color="danger"> |
64 | | - Destroy |
65 | | - </MazBtn> |
66 | | -
|
67 | | - <MazCard overflow-hidden style="width: 100%;"> |
| 34 | + |
| 35 | + <br /> |
| 36 | + |
| 37 | + <MazCard block> |
68 | 38 | <div style="display: flex;"> |
69 | | - <div style="flex: 1;"> |
70 | | - isIdle: {{event.isIdle}} |
71 | | - </div> |
72 | | - <div v-if="event.eventType" style="flex: 1; padding-left: 10px;"> |
73 | | - eventType: {{event.eventType}} |
74 | | - </div> |
| 39 | + <div style="flex: 1;">isIdle: {{event.isIdle ?? false}}</div> |
| 40 | + <div v-if="event.eventType" style="flex: 1; padding-left: 10px;">eventType: {{event.eventType ?? '-' }}</div> |
75 | 41 | </div> |
76 | 42 | </MazCard> |
77 | | -</template> |
78 | 43 |
|
79 | | -<script lang="ts" setup> |
80 | | - import MazBtn from 'maz-ui/components/MazBtn' |
81 | | - import MazCard from 'maz-ui/components/MazCard' |
| 44 | + <br /> |
| 45 | + <br /> |
| 46 | + |
| 47 | + <p class="maz-text-warning">Wait 5 seconds without any actions to see the dialog popup</p> |
| 48 | + |
| 49 | + <template #code> |
| 50 | + |
| 51 | + ```vue |
| 52 | + <template> |
| 53 | + <MazBtn @click="idle.start()" color="info"> |
| 54 | + Start |
| 55 | + </MazBtn> |
| 56 | + <MazBtn @click="idle.pause()" color="warning"> |
| 57 | + Pause |
| 58 | + </MazBtn> |
| 59 | + <MazBtn @click="idle.resume()"> |
| 60 | + Resume |
| 61 | + </MazBtn> |
| 62 | + <MazBtn @click="idle.reset()" color="secondary"> |
| 63 | + Reset |
| 64 | + </MazBtn> |
| 65 | + <MazBtn @click="idle.destroy()" color="danger"> |
| 66 | + Destroy |
| 67 | + </MazBtn> |
| 68 | +
|
| 69 | + <MazCard block> |
| 70 | + <div style="display: flex;"> |
| 71 | + <div style="flex: 1;"> |
| 72 | + isIdle: {{event.isIdle}} |
| 73 | + </div> |
| 74 | + <div v-if="event.eventType" style="flex: 1; padding-left: 10px;"> |
| 75 | + eventType: {{event.eventType}} |
| 76 | + </div> |
| 77 | + </div> |
| 78 | + </MazCard> |
| 79 | + </template> |
| 80 | +
|
| 81 | + <script lang="ts" setup> |
| 82 | + import MazBtn from 'maz-ui/components/MazBtn' |
| 83 | + import MazCard from 'maz-ui/components/MazCard' |
| 84 | +
|
| 85 | + import { onMounted, ref, onBeforeUnmount } from 'vue' |
| 86 | +
|
| 87 | + import { useIdleTimeout, useDialog } from 'maz-ui' |
| 88 | +
|
| 89 | + const dialog = useDialog() |
| 90 | +
|
| 91 | + const event = ref({}) |
| 92 | +
|
| 93 | + const timeout = 5000 |
| 94 | +
|
| 95 | + const idleTimeout = useIdleTimeout({ |
| 96 | + callback, |
| 97 | + options: { |
| 98 | + timeout, |
| 99 | + immediate: false, |
| 100 | + once: false, |
| 101 | + }, |
| 102 | + }) |
| 103 | +
|
| 104 | + async function callback({ isIdle, eventType, instance }) { |
| 105 | + console.log({ isIdle, eventType }) |
| 106 | + event.value = { isIdle, eventType } |
| 107 | +
|
| 108 | + if (isIdle) { |
| 109 | + try { |
| 110 | + instance.destroy() |
| 111 | + await dialog.open({ |
| 112 | + title: 'Are you still here?', |
| 113 | + message: `You have been inactive for ${timeout / 1000} secondes, do you want to continue?`, |
| 114 | + cancelText: 'No', |
| 115 | + confirmText: 'Yes', |
| 116 | + }).promise |
| 117 | + instance.start() |
| 118 | + } catch (e) { |
| 119 | + // do something like logout the user |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +
|
| 124 | + onMounted(() => { |
| 125 | + // should be executed on client side |
| 126 | + idleTimeout.start() |
| 127 | + }) |
| 128 | +
|
| 129 | + onBeforeUnmount(() => { |
| 130 | + // Destroy the instance when the component is destroyed to avoid memory leaks |
| 131 | + idleTimeout.destroy() |
| 132 | + }) |
| 133 | + </script> |
| 134 | + ``` |
| 135 | + |
| 136 | + </template> |
| 137 | +</ComponentDemo> |
82 | 138 |
|
| 139 | +<script lang="ts" setup> |
83 | 140 | import { onMounted, ref, onBeforeUnmount } from 'vue' |
84 | 141 |
|
85 | | - import { useIdleTimeout } from 'maz-ui' |
| 142 | + import { useIdleTimeout, useDialog } from 'maz-ui' |
86 | 143 |
|
87 | | - const idleTimeout = useIdleTimeout({ |
88 | | - callback, |
89 | | - options, |
90 | | - }) |
| 144 | + const dialog = useDialog() |
91 | 145 |
|
92 | 146 | const event = ref({}) |
93 | 147 |
|
94 | | - const callback = ({ isIdle, eventType }) => { |
95 | | - console.log({ isIdle, eventType }) |
96 | | - event.value = { isIdle, eventType } |
97 | | - } |
98 | | -
|
99 | | - const options = { |
100 | | - timeout: 3000, |
101 | | - immediate: false, |
102 | | - once: false, |
103 | | - } |
104 | | -
|
105 | | - // should be executed on client |
106 | | - const callback = ({ isIdle, event }) => { |
107 | | - console.log({ isIdle, event }) |
108 | | - event.value = { isIdle, event } |
109 | | - } |
110 | | -
|
111 | | - onMounted(() => { |
112 | | - idleTimeout.start() |
113 | | - }) |
| 148 | + const timeout = 5000 |
114 | 149 |
|
115 | | - onBeforeUnmount(() => { |
116 | | - idleTimeout.destroy() |
| 150 | + const idleTimeout = useIdleTimeout({ |
| 151 | + callback, |
| 152 | + options: { |
| 153 | + timeout, |
| 154 | + immediate: false, |
| 155 | + once: false, |
| 156 | + }, |
117 | 157 | }) |
118 | | -</script> |
119 | | -``` |
120 | 158 |
|
121 | | -<script lang="ts" setup> |
122 | | - import { onMounted, ref, onBeforeUnmount } from 'vue' |
123 | | - |
124 | | - import { useIdleTimeout } from 'maz-ui' |
125 | | - |
126 | | - const event = ref({}) |
127 | | - |
128 | | - const callback = ({ isIdle, eventType }) => { |
| 159 | + async function callback({ isIdle, eventType, instance }) { |
129 | 160 | console.log({ isIdle, eventType }) |
130 | 161 | event.value = { isIdle, eventType } |
131 | | - } |
132 | 162 |
|
133 | | - const options = { |
134 | | - timeout: 3000, |
135 | | - immediate: false, |
136 | | - once: false, |
| 163 | + if (isIdle) { |
| 164 | + try { |
| 165 | + instance.destroy() |
| 166 | + await dialog.open({ |
| 167 | + title: 'Are you still here?', |
| 168 | + message: `You have been inactive for ${timeout / 1000} secondes, do you want to continue?`, |
| 169 | + data: { |
| 170 | + cancelText: 'No', |
| 171 | + confirmText: 'Yes', |
| 172 | + } |
| 173 | + }).promise |
| 174 | + instance.start() |
| 175 | + } catch (e) { |
| 176 | + instance.destroy() |
| 177 | + } |
| 178 | + } |
137 | 179 | } |
138 | 180 |
|
139 | | - const idleTimeout = useIdleTimeout({ |
140 | | - callback, |
141 | | - options, |
142 | | - }) |
143 | | - |
144 | 181 | onMounted(() => { |
145 | 182 | // should be executed on client |
146 | 183 | idleTimeout.start() |
@@ -182,37 +219,37 @@ interface IdleTimeoutStrictOption { |
182 | 219 | Start tracking user - needed for SSR when `immediate` option is set to false (execute it on client side) |
183 | 220 |
|
184 | 221 | ```ts |
185 | | -idle.start() |
| 222 | +idleTimeout.start() |
186 | 223 | ``` |
187 | 224 |
|
188 | 225 | ### Pause |
189 | 226 |
|
190 | | -Will pause the timeout and events |
| 227 | +Will pause the timeout and events, can't be executed when the timeout has expired |
191 | 228 |
|
192 | 229 | ```ts |
193 | | -idle.pause() |
| 230 | +idleTimeout.pause() |
194 | 231 | ``` |
195 | 232 |
|
196 | 233 | ### Resume |
197 | 234 |
|
198 | 235 | Resume the instance will reinit the timeout |
199 | 236 |
|
200 | 237 | ```ts |
201 | | -idle.resume() |
| 238 | +idleTimeout.resume() |
202 | 239 | ``` |
203 | 240 |
|
204 | 241 | ### Reset |
205 | 242 |
|
206 | 243 | Reset the timeout of the instance like a restart |
207 | 244 |
|
208 | 245 | ```ts |
209 | | -idle.reset() |
| 246 | +idleTimeout.reset() |
210 | 247 | ``` |
211 | 248 |
|
212 | 249 | ### Destroy |
213 | 250 |
|
214 | 251 | Will destroy the instance and stop tracking |
215 | 252 |
|
216 | 253 | ```ts |
217 | | -idle.destroy() |
| 254 | +idleTimeout.destroy() |
218 | 255 | ``` |
0 commit comments