/
index.ts
85 lines (76 loc) · 1.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { ref } from 'vue-demi'
import type { MaybeRef, Pausable } from '@vueuse/shared'
import { useIntervalFn } from '@vueuse/shared'
import type { ConfigurableNavigator } from '../_configurable'
import { defaultNavigator } from '../_configurable'
export interface UseVibrateOptions extends ConfigurableNavigator {
/**
*
* Vibration Pattern
*
* An array of values describes alternating periods in which the
* device is vibrating and not vibrating. Each value in the array
* is converted to an integer, then interpreted alternately as
* the number of milliseconds the device should vibrate and the
* number of milliseconds it should not be vibrating
*
* @default []
*
*/
pattern?: MaybeRef<number[] | number>
/**
* Interval to run a persistent vibration, in ms
*
* Pass `0` to disable
*
* @default 0
*
*/
interval?: number
}
/**
* Reactive vibrate
*
* @see https://vueuse.org/useVibrate
* @see https://developer.mozilla.org/en-US/docs/Web/API/Vibration_API
* @param options
*/
export function useVibrate(options?: UseVibrateOptions) {
const {
pattern = [],
interval = 0,
navigator = defaultNavigator,
} = options || {}
const isSupported = typeof navigator !== 'undefined' && 'vibrate' in navigator
const patternRef = ref(pattern)
let intervalControls: Pausable | undefined
const vibrate = (pattern = patternRef.value) => {
if (isSupported)
navigator.vibrate(pattern)
}
// Attempt to stop the vibration:
const stop = () => {
// Stope the vibration if we need to:
if (isSupported)
navigator.vibrate(0)
intervalControls?.pause()
}
if (interval > 0) {
intervalControls = useIntervalFn(
vibrate,
interval,
{
immediate: false,
immediateCallback: false,
},
)
}
return {
isSupported,
pattern,
intervalControls,
vibrate,
stop,
}
}
export type UseVibrateReturn = ReturnType<typeof useVibrate>