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
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-motionbefore animating from code.