Skip to content

useMediaQuery

Whether a media query matches now, and again whenever it changes: the width of the screen, less motion, a coarse pointer.

import { useMediaQuery } from '@sypnia/ui/react'

On this page
  1. The screen and motion
  2. When to use it
  3. For everyone

The screen and motion

Wider than a phoneMotion on
Code
import { Badge, useMediaQuery } from '@sypnia/ui/react';

// Resize the window. False while rendering on the server, so render the wide version first and let CSS do most work.
export default function Width() {
  const phone = useMediaQuery('(max-width: 40rem)');
  const reduce = useMediaQuery('(prefers-reduced-motion: reduce)');
  return <div className="sy-row"><Badge tone={phone ? 'info' : 'neutral'}>{phone ? 'Phone' : 'Wider than a phone'}</Badge><Badge>{reduce ? 'Less motion' : 'Motion on'}</Badge></div>;
}

When to use it

Use it when

  • Behaviour that must change with the screen, beyond what CSS can do.

Do not use it when

  • Layout: CSS does it before the first paint; this only knows after.

For everyone

  • Read prefers-reduced-motion before animating from code.
  1. The screen and motion
  2. When to use it
  3. For everyone

    Search pages, sections and terms