Skip to content

matchesMedia

Whether the document currently matches a CSS media query.

Outside a browser (no matchMedia, e.g. during server-side rendering), it returns false.

Available since v3.1.5

import { matchesMedia } from '@helpers4/browser';
// or, from the all-in-one package (same code, one install):
import { matchesMedia } from 'helpers4/browser';
matchesMedia(query: string): boolean
ParameterTypeDescription
querystringA media query, e.g. `‘(min-width: 600px)‘`

boolean — true if the query matches now

Returns whether the document matches the query right now.

const dark = matchesMedia('(prefers-color-scheme: dark)');

Without matchMedia (Node.js, a build step), it returns false instead of throwing.

// on the server
matchesMedia('(min-width: 600px)') // => false

View source on GitHub