forked from vueuse/vueuse
/
index.ts
95 lines (76 loc) · 2.86 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
86
87
88
89
90
91
92
93
94
95
import { ref } from 'vue-demi'
import { until } from '@vueuse/shared'
import { useEventListener } from '../useEventListener'
import { useSupported } from '../useSupported'
import { unrefElement } from '../unrefElement'
import type { MaybeElementRef } from '../unrefElement'
import type { ConfigurableDocument } from '../_configurable'
import { defaultDocument } from '../_configurable'
declare global {
interface PointerLockOptions {
unadjustedMovement?: boolean
}
interface Element {
requestPointerLock(options?: PointerLockOptions): Promise<void> | void
}
}
type MaybeHTMLElement = HTMLElement | undefined | null
export interface UsePointerLockOptions extends ConfigurableDocument {
pointerLockOptions?: PointerLockOptions
}
/**
* Reactive pointer lock.
*
* @see https://vueuse.org/usePointerLock
* @param target
* @param options
*/
export function usePointerLock(target?: MaybeElementRef<MaybeHTMLElement>, options: UsePointerLockOptions = {}) {
const { document = defaultDocument, pointerLockOptions } = options
const isSupported = useSupported(() => document && 'pointerLockElement' in document)
const element = ref<MaybeHTMLElement>()
const triggerElement = ref<MaybeHTMLElement>()
let targetElement: MaybeHTMLElement
if (isSupported.value) {
useEventListener(document, 'pointerlockchange', () => {
const currentElement = document!.pointerLockElement ?? element.value
if (targetElement && currentElement === targetElement) {
element.value = document!.pointerLockElement as MaybeHTMLElement
if (!element.value)
targetElement = triggerElement.value = null
}
})
useEventListener(document, 'pointerlockerror', () => {
const currentElement = document!.pointerLockElement ?? element.value
if (targetElement && currentElement === targetElement) {
const action = document!.pointerLockElement ? 'release' : 'acquire'
throw new Error(`Failed to ${action} pointer lock.`)
}
})
}
async function lock(e: MaybeElementRef<MaybeHTMLElement> | Event, options?: PointerLockOptions) {
if (!isSupported.value)
throw new Error('Pointer Lock API is not supported by your browser.')
triggerElement.value = e instanceof Event ? <HTMLElement>e.currentTarget : null
targetElement = e instanceof Event ? unrefElement(target) ?? triggerElement.value : unrefElement(e)
if (!targetElement)
throw new Error('Target element undefined.')
targetElement.requestPointerLock(options ?? pointerLockOptions)
return await until(element).toBe(targetElement)
}
async function unlock() {
if (!element.value)
return false
document!.exitPointerLock()
await until(element).toBeNull()
return true
}
return {
isSupported,
element,
triggerElement,
lock,
unlock,
}
}
export type UsePointerLockReturn = ReturnType<typeof usePointerLock>