Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useMediaQuery.md
1---2category: Browser3---45# useMediaQuery67Reactive [Media Query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Testing_media_queries). Once you've created a MediaQueryList object, you can check the result of the query or receive notifications when the result changes.89## Usage1011```ts12import { useMediaQuery } from '@vueuse/core'1314const isLargeScreen = useMediaQuery('(min-width: 1024px)')15const isPreferredDark = useMediaQuery('(prefers-color-scheme: dark)')16```1718#### Server Side Rendering and Nuxt1920If you are using `useMediaQuery` with SSR enabled, then you need to specify which screen size you would like to render on the server and before hydration to avoid an hydration mismatch2122```ts23import { useMediaQuery } from '@vueuse/core'2425const isLarge = useMediaQuery('(min-width: 1024px)', {26ssrWidth: 768 // Will enable SSR mode and render like if the screen was 768px wide27})2829console.log(isLarge.value) // always false because ssrWidth of 768px is smaller than 1024px30onMounted(() => {31console.log(isLarge.value) // false if screen is smaller than 1024px, true if larger than 1024px32})33```3435Alternatively you can set this up globally for your app using [`provideSSRWidth`](../useSSRWidth/index.md)3637## Type Declarations3839```ts40/**41* Reactive Media Query.42*43* @see https://vueuse.org/useMediaQuery44* @param query45* @param options46*/47export declare function useMediaQuery(48query: MaybeRefOrGetter<string>,49options?: ConfigurableWindow & {50ssrWidth?: number51},52): ComputedRef<boolean>53```54