forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

create-media-query.ts16 lines, 693 bytesHistoryRawDownload
1import { createSignal, onCleanup } from "solid-js";
2
3/**
4 * A signal that follows a CSS media query. False where `matchMedia` is
5 * missing (tests, very old browsers), so callers fall back to the desktop
6 * layout.
7 */
8export function createMediaQuery(query: string): () => boolean {
9 if (typeof window === "undefined" || typeof window.matchMedia !== "function") return () => false;
10 const list = window.matchMedia(query);
11 const [matches, setMatches] = createSignal(list.matches);
12 const onChange = (event: MediaQueryListEvent) => setMatches(event.matches);
13 list.addEventListener("change", onChange);
14 onCleanup(() => list.removeEventListener("change", onChange));
15 return matches;
16}