Skip to main content
Vuetify0 is now in alpha!
Vuetify0 Logo
Theme
Mode
Palettes
Accessibility
Vuetify One
Sign in to Vuetify One

Access premium tools across the Vuetify ecosystem — Bin, Play, Studio, and more.

Not a subscriber? See what's included

useMediaQuery

A composable for reactive CSS media query matching with automatic cleanup.


IntermediateApr 5, 2026

Usage

The useMediaQuery composable wraps the browser’s matchMedia API, providing reactive updates when the media query state changes. It supports static strings, refs, and getter functions for dynamic queries.

Tip

Why wrap matchMedia? The native matchMedia API has no awareness of Vue’s effectScope lifecycle — change listeners you add won’t be removed when the scope is disposed. useMediaQuery integrates onScopeDispose for automatic cleanup, defers evaluation until after hydration for SSR safety, and supports reactive query strings that re-evaluate on change.

vue
<script setup lang="ts">
  import { useMediaQuery } from '@vuetify/v0'
  import { shallowRef } from 'vue'

  const { matches: prefersDark } = useMediaQuery('(prefers-color-scheme: dark)')
  const { matches: isMobile } = useMediaQuery('(max-width: 768px)')

  // Dynamic query with a reactive value
  const breakpoint = shallowRef(768)
  const { matches: isWide } = useMediaQuery(
    () => `(min-width: ${breakpoint.value}px)`
  )
</script>

<template>
  <div>
    <p>Dark mode: {{ prefersDark }}</p>
    <p>Mobile: {{ isMobile }}</p>
    <p>Wide (>= {{ breakpoint }}px): {{ isWide }}</p>
  </div>
</template>

Architecture

useMediaQuery wraps the browser’s matchMedia API with Vue reactivity and SSR safety:

Media Query Hierarchy

Use controls to zoom and pan. Click outside or press Escape to close.

Media Query Hierarchy

Reactivity

Property/MethodReactiveNotes
matchesShallowRef, readonly
queryComputed, accepts MaybeRefOrGetter
mediaQueryListShallowRef, readonly (MediaQueryList or null)
Tip

Dynamic queries Pass a ref or getter to useMediaQuery for dynamic query updates. The composable re-evaluates when the query changes.

Examples

Dynamic Min-Width Detection

Reactively detects landscape orientation and a slider-adjustable min-width threshold, showing how media queries update live as conditions change.

Landscape orientation: false
Width >= 768px: false
768px

API Reference

The following API details are for the useMediaQuery composable.

Functions

useMediaQuery

(query: MaybeRefOrGetter<string>) => MediaQueryContext

Reactive media query matching.

usePrefersDark

() => MediaQueryContext

Check if the user prefers dark color scheme.

usePrefersReducedMotion

() => MediaQueryContext

Check if the user prefers reduced motion.

usePrefersContrast

() => MediaQueryContext

Check if the user prefers more contrast.

Properties

matches

Readonly<ShallowRef<boolean>>

Whether the media query currently matches

query

Readonly<Ref<string, string>>

The current media query string

mediaQueryList

Readonly<ShallowRef<MediaQueryList | null>>

The underlying MediaQueryList (null on server)

Methods

stop

() => void

Stop listening and clean up

Was this page helpful?

© 2016-1970 Vuetify, LLC
Ctrl+/