createMediaQuerySignal
projects/core/utils/media-query-signal.ts
Description#
Creates a reactive Signal<boolean> that reflects whether host
currently matches a CSS media query.
Seeds from MediaQueryList.matches, updates on the change event, and
removes the listener when destroyRef is destroyed. On a host without
matchMedia (SSR, jsdom) the signal stays false and no listener is
wired, so it never throws off the browser.
This is the shared kernel behind injectMediaQuery, CngxMediaQuery,
CngxSkeleton, CngxReducedMotion, and the stepper's mobile-viewport
signal; consumers that hand-roll a matchMedia listener should route
through it instead.
const compact = createMediaQuerySignal(
'(max-width: 640px)',
inject(DestroyRef),
inject(DOCUMENT).defaultView,
);Signature#
createMediaQuerySignal(query: string, destroyRef: DestroyRef, host)Parameters#
A CSS media query string, e.g. (max-width: 640px).
Scope whose destruction removes the change listener.
The window-like object to read matchMedia from.
Returns#
Signal A readonly Signal<boolean> tracking the query's match state.