Skip to main content

@rtorcato/browser-common / mediaquery

mediaquery

Functions​

isMediaQueryAvailable()​

isMediaQueryAvailable(): boolean

Defined in: mediaquery/index.ts:10

Checks if window.matchMedia is available in the current environment.

Example​

import { isMediaQueryAvailable } from '@rtorcato/browser-common/mediaquery'
if (isMediaQueryAvailable()) applyResponsiveLayout()

Returns​

boolean

True if matchMedia exists, false otherwise.


matchesMedia()​

matchesMedia(query): boolean | null

Defined in: mediaquery/index.ts:24

Checks whether a CSS media query currently matches.

Example​

import { matchesMedia } from '@rtorcato/browser-common/mediaquery'
if (matchesMedia('(min-width: 768px)')) showSidebar()

Parameters​

query​

string

The media query, e.g. '(min-width: 768px)'.

Returns​

boolean | null

True or false, or null where matchMedia is unavailable.


onMediaQueryChange()​

onMediaQueryChange(query, callback): () => void

Defined in: mediaquery/index.ts:40

Listens for a CSS media query to start or stop matching.

Example​

import { onMediaQueryChange } from '@rtorcato/browser-common/mediaquery'
const off = onMediaQueryChange('(min-width: 768px)', (matches) => setWide(matches))
off()

Parameters​

query​

string

The media query to watch.

callback​

(matches) => void

Called with the new matches value on each change.

Returns​

A function that removes the listener (a no-op where unsupported).

() => void


prefersDarkMode()​

prefersDarkMode(): boolean | null

Defined in: mediaquery/index.ts:65

Checks whether the user prefers a dark color scheme.

Example​

import { prefersDarkMode } from '@rtorcato/browser-common/mediaquery'
if (prefersDarkMode()) document.documentElement.classList.add('dark')

Returns​

boolean | null

True or false, or null where matchMedia is unavailable.


prefersReducedMotion()​

prefersReducedMotion(): boolean | null

Defined in: mediaquery/index.ts:78

Checks whether the user has asked for reduced motion.

Example​

import { prefersReducedMotion } from '@rtorcato/browser-common/mediaquery'
if (!prefersReducedMotion()) playIntroAnimation()

Returns​

boolean | null

True or false, or null where matchMedia is unavailable.