forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
95 lines (84 loc) · 2.69 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 type { Ref } from 'vue-demi'
import { increaseWithUnit } from '@vueuse/shared'
import { useMediaQuery } from '../useMediaQuery'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
export * from './breakpoints'
export type Breakpoints<K extends string = string> = Record<K, number | string>
/**
* Reactively viewport breakpoints
*
* @see https://vueuse.org/useBreakpoints
* @param options
*/
export function useBreakpoints<K extends string>(breakpoints: Breakpoints<K>, options: ConfigurableWindow = {}) {
function getValue(k: K, delta?: number) {
let v = breakpoints[k]
if (delta != null)
v = increaseWithUnit(v, delta)
if (typeof v === 'number')
v = `${v}px`
return v
}
const { window = defaultWindow } = options
function match(query: string): boolean {
if (!window)
return false
return window.matchMedia(query).matches
}
const greaterOrEqual = (k: K) => {
return useMediaQuery(`(min-width: ${getValue(k)})`, options)
}
const shortcutMethods = Object.keys(breakpoints)
.reduce((shortcuts, k) => {
Object.defineProperty(shortcuts, k, {
get: () => greaterOrEqual(k as K),
enumerable: true,
configurable: true,
})
return shortcuts
}, {} as Record<K, Ref<boolean>>)
return {
greater(k: K) {
return useMediaQuery(`(min-width: ${getValue(k, 0.1)})`, options)
},
greaterOrEqual,
smaller(k: K) {
return useMediaQuery(`(max-width: ${getValue(k, -0.1)})`, options)
},
smallerOrEqual(k: K) {
return useMediaQuery(`(max-width: ${getValue(k)})`, options)
},
between(a: K, b: K) {
return useMediaQuery(`(min-width: ${getValue(a)}) and (max-width: ${getValue(b, -0.1)})`, options)
},
isGreater(k: K) {
return match(`(min-width: ${getValue(k, 0.1)})`)
},
isGreaterOrEqual(k: K) {
return match(`(min-width: ${getValue(k)})`)
},
isSmaller(k: K) {
return match(`(max-width: ${getValue(k, -0.1)})`)
},
isSmallerOrEqual(k: K) {
return match(`(max-width: ${getValue(k)})`)
},
isInBetween(a: K, b: K) {
return match(`(min-width: ${getValue(a)}) and (max-width: ${getValue(b, -0.1)})`)
},
...shortcutMethods,
}
}
export type UseBreakpointsReturn<K extends string = string> = {
greater: (k: K) => Ref<boolean>
greaterOrEqual: (k: K) => Ref<boolean>
smaller(k: K): Ref<boolean>
smallerOrEqual: (k: K) => Ref<boolean>
between(a: K, b: K): Ref<boolean>
isGreater(k: K): boolean
isGreaterOrEqual(k: K): boolean
isSmaller(k: K): boolean
isSmallerOrEqual(k: K): boolean
isInBetween(a: K, b: K): boolean
} & Record<K, Ref<boolean>>