hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { 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 | */ |
| 8 | export 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 | } |