-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
127 lines (112 loc) · 4.33 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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>>
export interface UseBreakpointsOptions extends ConfigurableWindow {
/**
* The query strategy to use for the generated shortcut methods like `.lg`
*
* 'min-width' - .lg will be true when the viewport is greater than or equal to the lg breakpoint (mobile-first)
* 'max-width' - .lg will be true when the viewport is smaller than the xl breakpoint (desktop-first)
*
* @default "min-width"
*/
strategy?: 'min-width' | 'max-width'
}
/**
* Reactively viewport breakpoints
*
* @see https://vueuse.org/useBreakpoints
*/
export function useBreakpoints<K extends string>(
breakpoints: Breakpoints<K>,
options: UseBreakpointsOptions = {},
) {
function getValue(k: MaybeRefOrGetter<K>, delta?: number) {
let v = toValue(breakpoints[toValue(k)])
if (delta != null)
v = increaseWithUnit(v, delta)
if (typeof v === 'number')
v = `${v}px`
return v
}
const { window = defaultWindow, strategy = 'min-width' } = options
function match(query: string): boolean {
if (!window)
return false
return window.matchMedia(query).matches
}
const greaterOrEqual = (k: MaybeRefOrGetter<K>) => {
return useMediaQuery(() => `(min-width: ${getValue(k)})`, options)
}
const smallerOrEqual = (k: MaybeRefOrGetter<K>) => {
return useMediaQuery(() => `(max-width: ${getValue(k)})`, options)
}
const shortcutMethods = Object.keys(breakpoints)
.reduce((shortcuts, k) => {
Object.defineProperty(shortcuts, k, {
get: () => strategy === 'min-width'
? greaterOrEqual(k as K)
: smallerOrEqual(k as K),
enumerable: true,
configurable: true,
})
return shortcuts
}, {} as Record<K, Ref<boolean>>)
function 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))
}
return Object.assign(shortcutMethods, {
greaterOrEqual,
smallerOrEqual,
greater(k: MaybeRefOrGetter<K>) {
return useMediaQuery(() => `(min-width: ${getValue(k, 0.1)})`, options)
},
smaller(k: MaybeRefOrGetter<K>) {
return useMediaQuery(() => `(max-width: ${getValue(k, -0.1)})`, options)
},
between(a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>) {
return useMediaQuery(() => `(min-width: ${getValue(a)}) and (max-width: ${getValue(b, -0.1)})`, options)
},
isGreater(k: MaybeRefOrGetter<K>) {
return match(`(min-width: ${getValue(k, 0.1)})`)
},
isGreaterOrEqual(k: MaybeRefOrGetter<K>) {
return match(`(min-width: ${getValue(k)})`)
},
isSmaller(k: MaybeRefOrGetter<K>) {
return match(`(max-width: ${getValue(k, -0.1)})`)
},
isSmallerOrEqual(k: MaybeRefOrGetter<K>) {
return match(`(max-width: ${getValue(k)})`)
},
isInBetween(a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>) {
return match(`(min-width: ${getValue(a)}) and (max-width: ${getValue(b, -0.1)})`)
},
current,
active() {
const bps = current()
return computed(() => bps.value.length === 0 ? '' : bps.value.at(-1))
},
})
}
export type UseBreakpointsReturn<K extends string = string> = {
greater: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>
greaterOrEqual: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>
smaller: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>
smallerOrEqual: (k: MaybeRefOrGetter<K>) => ComputedRef<boolean>
between: (a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>) => ComputedRef<boolean>
isGreater: (k: MaybeRefOrGetter<K>) => boolean
isGreaterOrEqual: (k: MaybeRefOrGetter<K>) => boolean
isSmaller: (k: MaybeRefOrGetter<K>) => boolean
isSmallerOrEqual: (k: MaybeRefOrGetter<K>) => boolean
isInBetween: (a: MaybeRefOrGetter<K>, b: MaybeRefOrGetter<K>) => boolean
current: () => ComputedRef<string[]>
active: ComputedRef<string>
} & Record<K, ComputedRef<boolean>>