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(useMediaQuery): allow query to be a ref #2178

Merged
merged 3 commits into from Sep 7, 2022
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
42 changes: 23 additions & 19 deletions packages/core/useMediaQuery/index.ts
@@ -1,7 +1,8 @@
/* this implementation is original ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */

import { ref } from 'vue-demi'
import { tryOnBeforeMount, tryOnScopeDispose } from '@vueuse/shared'
import { ref, watchEffect } from 'vue-demi'
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveRef, tryOnScopeDispose } from '@vueuse/shared'
import type { ConfigurableWindow } from '../_configurable'
import { defaultWindow } from '../_configurable'
import { useSupported } from '../useSupported'
Expand All @@ -13,40 +14,43 @@ import { useSupported } from '../useSupported'
* @param query
* @param options
*/
export function useMediaQuery(query: string, options: ConfigurableWindow = {}) {
export function useMediaQuery(query: MaybeComputedRef<string>, options: ConfigurableWindow = {}) {
const { window = defaultWindow } = options
const isSupported = useSupported(() => window && 'matchMedia' in window && typeof window!.matchMedia === 'function')
const isSupported = useSupported(() => window && 'matchMedia' in window && typeof window.matchMedia === 'function')

let mediaQuery: MediaQueryList | undefined
const matches = ref(false)

const cleanup = () => {
if (!mediaQuery)
return
if ('removeEventListener' in mediaQuery)
// eslint-disable-next-line @typescript-eslint/no-use-before-define
mediaQuery.removeEventListener('change', update)
else
// @ts-expect-error deprecated API
// eslint-disable-next-line @typescript-eslint/no-use-before-define
mediaQuery.removeListener(update)
}

const update = () => {
if (!isSupported.value)
return
if (!mediaQuery)
mediaQuery = window!.matchMedia(query)
matches.value = mediaQuery.matches
}

tryOnBeforeMount(() => {
update()
cleanup()

if (!mediaQuery)
return
mediaQuery = window!.matchMedia(resolveRef(query).value)
matches.value = mediaQuery.matches

sxzz marked this conversation as resolved.
Show resolved Hide resolved
if ('addEventListener' in mediaQuery)
mediaQuery.addEventListener('change', update)
else
// @ts-expect-error deprecated API
mediaQuery.addListener(update)
}
watchEffect(update)

tryOnScopeDispose(() => {
if ('removeEventListener' in mediaQuery!)
mediaQuery!.removeEventListener('change', update)
else
mediaQuery!.removeListener(update)
})
})
tryOnScopeDispose(() => cleanup())

return matches
}