| 1 | import { useSyncExternalStore } from 'react'; |
| 2 | |
| 3 | /** |
| 4 | * Too small for the table. Four strips of --depth around a centre square need |
| 5 | * roughly 770px in *both* directions before the middle is worth looking at and |
| 6 | * the tiles stop shrinking — a phone has that in neither direction, and a |
| 7 | * tablet held upright has it in only one. Under this the table switches to the |
| 8 | * compact layout: one viewer, nothing rotated, and the hand allowed to wrap. |
| 9 | * |
| 10 | * The class this drives (`compact`, set on <html>) is what styles.css keys off, |
| 11 | * so this string is the only place the breakpoint is written down. |
| 12 | */ |
| 13 | export const COMPACT_QUERY = '(max-height: 620px), (max-width: 820px)'; |
| 14 | |
| 15 | const mql = typeof window === 'undefined' ? null : window.matchMedia(COMPACT_QUERY); |
| 16 | |
| 17 | const subscribe = (cb: () => void) => { |
| 18 | mql?.addEventListener('change', cb); |
| 19 | return () => mql?.removeEventListener('change', cb); |
| 20 | }; |
| 21 | |
| 22 | export function useCompact(): boolean { |
| 23 | return useSyncExternalStore( |
| 24 | subscribe, |
| 25 | () => mql?.matches ?? false, |
| 26 | () => false, |
| 27 | ); |
| 28 | } |