Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Remove unnecessary ref in useMediaQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Nov 14, 2022
1 parent 503521d commit a56d375
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/composables/use-media-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
which, in turn, is ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
import { computed, ref, watchEffect } from '@nuxtjs/composition-api'

import { resolveRef } from '@vueuse/core'
import { resolveUnref } from '@vueuse/core'

import { SCREEN_SIZES, Breakpoint } from '~/constants/screens'
import { defaultWindow } from '~/constants/window'
Expand Down Expand Up @@ -53,7 +53,7 @@ export function useMediaQuery(

cleanup()

mediaQuery = window.matchMedia(resolveRef(query).value)
mediaQuery = window.matchMedia(resolveUnref(query))
matches.value = mediaQuery.matches

if ('addEventListener' in mediaQuery) {
Expand All @@ -70,7 +70,9 @@ export function useMediaQuery(

return matches
}
type BreakpointWithoutXs = Exclude<Breakpoint, 'xs'>

const isBpXs = (bp: Breakpoint): bp is 'xs' => bp === 'xs'

/**
* Check whether the current screen meets
* or exceeds the provided breakpoint size.
Expand All @@ -79,15 +81,15 @@ export const isMinScreen = (
breakpointName: MaybeComputedRef<Breakpoint>,
options?: Options
) => {
if (breakpointName === 'xs') {
const resolvedBp = resolveUnref(breakpointName)

if (isBpXs(resolvedBp)) {
// `xs` is the "minimum" so it is always true
return ref(true)
}

const query = computed(() => {
const sizeInPx = SCREEN_SIZES.get(
resolveRef(breakpointName as BreakpointWithoutXs).value
)
const sizeInPx = SCREEN_SIZES.get(resolvedBp)
return `(min-width: ${sizeInPx}px)`
})

Expand Down

0 comments on commit a56d375

Please sign in to comment.