-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
101 lines (89 loc) · 3.15 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
96
97
98
99
100
101
import type { ComputedRef, Ref } from 'vue-demi'
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { increaseWithUnit, toValue } from '@vueuse/shared'
import { computed } from 'vue-demi'
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, MaybeRefOrGetter<number | string>>
/**
* Reactively viewport breakpoints
*
* @see https://vueuse.org/useBreakpoints
*/
export function useBreakpoints<K extends string>(breakpoints: Breakpoints<K>, options: ConfigurableWindow = {}) {
function getValue(k: K, delta?: number) {
let v = toValue(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 Object.assign(shortcutMethods, {
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)})`)
},
current() {
const points = Object.keys(breakpoints).map(i => [i, greaterOrEqual(i as K)] as const)
return computed(() => points.filter(([, v]) => v.value).map(([k]) => k))
},
})
}
export type UseBreakpointsReturn<K extends string = string> = {
greater: (k: K) => ComputedRef<boolean>
greaterOrEqual: (k: K) => ComputedRef<boolean>
smaller: (k: K) => ComputedRef<boolean>
smallerOrEqual: (k: K) => ComputedRef<boolean>
between: (a: K, b: K) => ComputedRef<boolean>
isGreater: (k: K) => boolean
isGreaterOrEqual: (k: K) => boolean
isSmaller: (k: K) => boolean
isSmallerOrEqual: (k: K) => boolean
isInBetween: (a: K, b: K) => boolean
current: () => ComputedRef<string[]>
} & Record<K, ComputedRef<boolean>>