mazey
    Preparing search index...

    Function listenMediaQueryChanges

    • Listen for media-query changes with the standard browser event API.

      A missing media query or unavailable standard event methods return an inert cleanup function.

      Usage:

      import { listenMediaQueryChanges } from "mazey";

      const media = window.matchMedia("(prefers-color-scheme: dark)");
      const stop = listenMediaQueryChanges(media, event => {
      console.log(event.matches);
      });

      stop();
      stop();

      Parameters

      • media: MediaQueryList | null

        Media-query result to observe, or null when unavailable.

      • listener: (event: MediaQueryListEvent) => void

        Callback invoked for media-query change events.

      Returns () => void

      An idempotent function that removes the registered listener.

      If listener is not a function or media is neither an object nor null.

      This helper does not call matchMedia, invoke the listener immediately, or modify the DOM. It is safe during SSR when null is supplied.