media.ts
This file
Path
apps/web/src/features/code/media.ts
33 lines · 1.1 KiB · TypeScript
Last changed by
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334/** * Whether the screen is at least a width, kept current as the window * changes. The server and the page's first render say no, so the markup * matches; the browser then says what the screen is. */import { useMemo, useSyncExternalStore } from "react";
const subscribe = (query: string) => (changed: () => void) => { const list = globalThis.matchMedia(query);
list.addEventListener("change", changed);
return () => { list.removeEventListener("change", changed); };};
/** Whether `query` matches now, in the browser; never on the server. */export const useMedia = (query: string): boolean => useSyncExternalStore( useMemo(() => subscribe(query), [query]), () => globalThis.matchMedia(query).matches, () => false );
/** The shell's width, from which the rail holds the tree. */export const SHELL_QUERY = "(min-width: 70rem)";
/** The width from which a diff may set its sides apart. */export const SPLIT_QUERY = "(min-width: 48rem)";
/** A tablet's width and wider, where a sheet comes from the side rather than filling the screen. */export const TABLET_QUERY = "(min-width: 40rem)";