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
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 1–33why, one step back1/**2 * Whether the screen is at least a width, kept current as the window3 * changes. The server and the page's first render say no, so the markup4 * matches; the browser then says what the screen is.5 */6import { useMemo, useSyncExternalStore } from "react";78const subscribe = (query: string) => (changed: () => void) => {9 const list = globalThis.matchMedia(query);1011 list.addEventListener("change", changed);1213 return () => {14 list.removeEventListener("change", changed);15 };16};1718/** Whether `query` matches now, in the browser; never on the server. */19export const useMedia = (query: string): boolean =>20 useSyncExternalStore(21 useMemo(() => subscribe(query), [query]),22 () => globalThis.matchMedia(query).matches,23 () => false24 );2526/** The shell's width, from which the rail holds the tree. */27export const SHELL_QUERY = "(min-width: 70rem)";2829/** The width from which a diff may set its sides apart. */30export const SPLIT_QUERY = "(min-width: 48rem)";3132/** A tablet's width and wider, where a sheet comes from the side rather than filling the screen. */33export const TABLET_QUERY = "(min-width: 40rem)";