forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
75 lines (65 loc) · 1.77 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
import { MaybeRef, objectPick, toRefs } from '@vueuse/shared'
import { ref, Ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { ConfigurableWindow, defaultWindow } from '../_configurable'
import { PointerType, Position } from '../_types'
export interface UsePointerState extends Position {
pressure: number
pointerId: number
tiltX: number
tiltY: number
width: number
height: number
twist: number
pointerType: PointerType | null
}
export interface UsePointerOptions extends ConfigurableWindow {
/**
* Pointer types that listen to.
*
* @default ['mouse', 'touch', 'pen']
*/
pointerTypes?: PointerType[]
/**
* Initial values
*/
initialValue?: MaybeRef<Partial<UsePointerState>>
}
const defaultState: UsePointerState = /* #__PURE__ */ {
x: 0,
y: 0,
pointerId: 0,
pressure: 0,
tiltX: 0,
tiltY: 0,
width: 0,
height: 0,
twist: 0,
pointerType: null,
}
const keys = /* #__PURE__ */ Object.keys(defaultState) as (keyof UsePointerState)[]
/**
* Reactive pointer state.
*
* @see https://vueuse.org/usePointer
* @param options
*/
export function usePointer(options: UsePointerOptions = {}) {
const {
window = defaultWindow,
} = options
const state = ref(options.initialValue || {}) as unknown as Ref<UsePointerState>
Object.assign(state.value, defaultState, state.value)
const handler = (event: PointerEvent) => {
if (options.pointerTypes && !options.pointerTypes.includes(event.pointerType as PointerType))
return
state.value = objectPick(event, keys, false) as UsePointerState
}
if (window) {
useEventListener(window, 'pointerdown', handler, { passive: true })
useEventListener(window, 'pointermove', handler, { passive: true })
}
return {
...toRefs(state),
}
}