'use client'; import * as React from 'react'; // ONE viewport hook for the whole design system. // // Three components used to carry their own copy of the same `matchMedia` // dance at three different breakpoints, so "is this phone-sized?" had three // answers. It has one now, and the breakpoints are named values rather than // magic numbers buried in a component. // // SSR-safe by construction: the first render is always `false` on both the // server and the client (there is no viewport during prerender, and guessing // one produces a hydration mismatch), then the real answer arrives in an // effect before paint. /** Below this the UI is a phone: menus become sheets, drawers drag. */ export const NARROW_PX = 640; /** Below this the pointer is a finger: hover-only affordances are suppressed. */ export const TOUCH_PX = 1024; /** * Below this a horizontal application nav is folded into its drawer. * * Separate from NARROW_PX because the two answer different questions. A phone * is a phone at 640; a row of brand, links, settings and account needs about * 780 before it fits, so between those two numbers the nav rendered wide and * pushed the document past the viewport — worst at 640 itself, where the page * ran 139px wider than the screen showing it. * * A fixed number is a compromise here and worth naming as one: the width a nav * needs is a function of how many links it carries and how long their labels * are, so this value holds for the navs in this system and would need * revisiting for one materially larger. The drawer is the safe side of the * trade — folding a nav that would have fitted costs a tap, and not folding * one that does not costs a sideways scroll on every screen. */ export const NAV_PX = 800; /** True while the viewport is narrower than `px`. */ export function useBelow(px: number): boolean { const [ below, setBelow ] = React.useState(false); React.useEffect(() => { const mql = window.matchMedia(`(max-width: ${ px - 1 }px)`); const apply = () => setBelow(mql.matches); apply(); mql.addEventListener('change', apply); return () => mql.removeEventListener('change', apply); }, [ px ]); return below; } /** Phone-sized: present menus as sheets. */ export const useIsNarrow = (): boolean => useBelow(NARROW_PX); /** Touch-sized: no hover, so hover-only affordances must not be the only path. */ export const useIsTouch = (): boolean => useBelow(TOUCH_PX); /** Too narrow for a horizontal nav row: present the links as a drawer. */ export const useNavFolds = (): boolean => useBelow(NAV_PX);