Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useBreakpoints): make parameters reactivity #3592

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/core/useBreakpoints/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import type { ComputedRef, Ref } from 'vue-demi'
import { increaseWithUnit } from '@vueuse/shared'
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, number | string>
export type Breakpoints<K extends string = string> = Record<K, MaybeRefOrGetter<number | string>>

/**
* Reactively viewport breakpoints
Expand All @@ -16,7 +17,7 @@ export type Breakpoints<K extends string = string> = Record<K, number | string>
*/
export function useBreakpoints<K extends string>(breakpoints: Breakpoints<K>, options: ConfigurableWindow = {}) {
function getValue(k: K, delta?: number) {
let v = breakpoints[k]
let v = toValue(breakpoints[k])

if (delta != null)
v = increaseWithUnit(v, delta)
Expand All @@ -36,7 +37,7 @@ export function useBreakpoints<K extends string>(breakpoints: Breakpoints<K>, op
}

const greaterOrEqual = (k: K) => {
return useMediaQuery(`(min-width: ${getValue(k)})`, options)
return useMediaQuery(() => `(min-width: ${getValue(k)})`, options)
}

const shortcutMethods = Object.keys(breakpoints)
Expand All @@ -51,17 +52,17 @@ export function useBreakpoints<K extends string>(breakpoints: Breakpoints<K>, op

return Object.assign(shortcutMethods, {
greater(k: K) {
return useMediaQuery(`(min-width: ${getValue(k, 0.1)})`, options)
return useMediaQuery(() => `(min-width: ${getValue(k, 0.1)})`, options)
},
greaterOrEqual,
smaller(k: K) {
return useMediaQuery(`(max-width: ${getValue(k, -0.1)})`, options)
return useMediaQuery(() => `(max-width: ${getValue(k, -0.1)})`, options)
},
smallerOrEqual(k: K) {
return useMediaQuery(`(max-width: ${getValue(k)})`, options)
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)
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)})`)
Expand Down