import { useDevice } from '../DeviceToggle'; /** * How big each role is, at the width being edited. * * ── The same four roles as Fonts ── * * A template asks for a role and gets both the face and the size, so this section is the one above it * with numbers instead of names. What that costs is worth knowing: a role used at two sizes on purpose * collapses to one — `font-sub` was three sizes across the site and is now a single choice. * * ── One value per field ── * * The tabs at the top of the panel say which width. Unset inherits the smaller one, so "the same * everywhere" is one choice; the option names what it is inheriting rather than leaving it to be * worked out. */ export type FontSizes = Partial>>>; const BREAKPOINT = { desktop: 'lg', tablet: 'md', mobile: 'base' } as const; /** The order they are read in: biggest first, which is how a type scale is judged. */ const ROLES = [ { key: 'display' as const, label: 'Display' }, { key: 'title' as const, label: 'Headings' }, { key: 'body' as const, label: 'Body' }, { key: 'sub' as const, label: 'Menu' }, ]; const inherited = ( steps: Partial> | undefined, key: 'base' | 'md' | 'lg', ): string | null => { const below = { base: [], md: ['base'], lg: ['md', 'base'] } as const; for (const smaller of below[key]) { const step = steps?.[smaller]; if (step) return step; } return null; }; export const FontSizesTab = ({ sizes, steps, onChange, }: { sizes: FontSizes; /** The scale, sent by the API so this file holds no copy of it. */ steps: string[]; onChange: (sizes: FontSizes) => void; }) => { const [device] = useDevice(); const key = BREAKPOINT[device]; const set = (role: string, value: string) => { const at = { ...((sizes as Record>>)[role] ?? {}) }; // Removed rather than stored empty: an absence is what "inherit" means, all the way up. if (value === '') delete at[key]; else at[key] = value; const next = { ...(sizes as Record) }; if (Object.keys(at).length) next[role] = at; else delete next[role]; onChange(next as FontSizes); }; return (
{ROLES.map((role) => { const current = sizes[role.key]; const own = current?.[key]; const from = inherited(current, key); return (
); })}
); };