Skip to main content
cngx-src documentation

observeMediaQuery

Functioncore/utils/media-queryv0.3.0

projects/core/utils/media-query-signal.ts

Description#

Subscribes apply to a media query's match state on host.

Seeds synchronously from MediaQueryList.matches, re-applies on every change event, and returns the teardown that removes the listener. On a host without matchMedia (SSR, jsdom) nothing is wired, apply never fires, and the returned teardown is a no-op.

Reach for this low-level form when the query itself is reactive and the subscription must follow it (re-wire per effect run via onCleanup); for a static query, createMediaQuerySignal owns the teardown via DestroyRef.

Signature#

observeMediaQuery(host, query: string, apply)

Parameters#

@paramhost

The window-like object to read matchMedia from.

@paramquerystring

A CSS media query string, e.g. (max-width: 640px).

@paramapply

Receives the current match state, synchronously on subscribe and on every change.

Returns#

void

Teardown that removes the change listener.