-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
43 lines (37 loc) · 1.26 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
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
import type { Ref } from 'vue-demi'
import { ref } from 'vue-demi'
import { useEventListener } from '../useEventListener'
import { useSupported } from '../useSupported'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
/**
* Reactive DeviceOrientationEvent.
*
* @see https://vueuse.org/useDeviceOrientation
* @param options
*/
export function useDeviceOrientation(options: ConfigurableWindow = {}) {
const { window = defaultWindow } = options
const isSupported = useSupported(() => window && 'DeviceOrientationEvent' in window)
const isAbsolute = ref(false)
const alpha: Ref<number | null> = ref(null)
const beta: Ref<number | null> = ref(null)
const gamma: Ref<number | null> = ref(null)
if (window && isSupported.value) {
useEventListener(window, 'deviceorientation', (event) => {
isAbsolute.value = event.absolute
alpha.value = event.alpha
beta.value = event.beta
gamma.value = event.gamma
})
}
return {
isSupported,
isAbsolute,
alpha,
beta,
gamma,
}
}
export type UseDeviceOrientationReturn = ReturnType<typeof useDeviceOrientation>