forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (44 loc) · 1.51 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
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
import { ref, unref, watchEffect } from 'vue-demi'
import type { MaybeRef } from '@vueuse/shared'
import { tryOnBeforeMount, tryOnScopeDispose } from '@vueuse/shared'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
import { useSupported } from '../useSupported'
/**
* Reactive Media Query.
*
* @see https://vueuse.org/useMediaQuery
* @param query
* @param options
*/
export function useMediaQuery(query: MaybeRef<string>, options: ConfigurableWindow = {}) {
const { window = defaultWindow } = options
const isSupported = useSupported(() => window && 'matchMedia' in window && typeof window!.matchMedia === 'function')
let mediaQuery: MediaQueryList | undefined
const matches = ref(false)
const update = () => {
if (!isSupported.value)
return
if (!mediaQuery)
mediaQuery = window!.matchMedia(unref(query))
matches.value = mediaQuery.matches
}
watchEffect(update)
tryOnBeforeMount(() => {
if (!mediaQuery)
return
if ('addEventListener' in mediaQuery)
mediaQuery.addEventListener('change', update)
else
// @ts-expect-error deprecated API
mediaQuery.addListener(update)
tryOnScopeDispose(() => {
if ('removeEventListener' in mediaQuery!)
mediaQuery!.removeEventListener('change', update)
else
mediaQuery!.removeListener(update)
})
})
return matches
}