'use client' import { Dialog, DialogClose, DialogContent, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, SizableText, XStack, YStack, } from '@hanzo/gui' import { useLayoutEffect, useRef, useState, type ComponentRef, type ReactNode, } from 'react' import { paint } from '../material/paint' import { reach } from '../material/reach' import { press } from './press' /** * The band across the top of every screen: the mark, the places, the field in * the middle, and everything about this session at the trailing edge. * * It measures rather than guesses. Given less room than the row wants, the * places move into a sheet — decided by the row's own overflow, so nothing here * names a width or a device, and a longer word or a larger type scale folds it * at a wider window without anyone changing a number. * * Nothing in it reads. The places arrive named in the reader's language, the * path being read arrives as a string, and moving between screens is a call the * caller owns — so the same band sits over a router, over a static site, and * over a preview with no navigation at all. */ /** * What the band is painted with, in the token layer's own names. * * A text primitive given no colour resolves the engine's `$color`, which the * installed theme row publishes as near-black — on a black page, invisible. So * every readable string here names its colour, and `plain` is the one the body * of the page is set in. */ const { plain, quiet, line, surface, body: ground } = paint /** What a stack hands back on the web when you hold on to it. Asked of the * component rather than assumed to be a div: it answers for two platforms and * the half with a box to measure is the half named here. */ type Box = Extract, HTMLElement> /** The space between the three groups the row holds. */ const between = 16 /** * What a place is inset by, and how far the places sit apart. * * Each stands in a box eight wider either side than its word with twelve * between the boxes, so the words stand twenty apart and the target is the word * plus a thumb's worth of margin. Written as the two numbers rather than as the * twenty, because the pair is what a reader gets: the twenty is air and the * sixteen is press-able. */ const INSET = 8 const APART = 12 /** The air above and below the row. */ const AIR = 16 /** * How tall the band stands, so a workspace can fill what is left of the glass. * * An ordinary control at forty, sixteen of air either side, and the row the * closing rule spends. A screen with little on it would otherwise end where its * content ends and pull the page's foot up into the first frame. */ export const tall = reach.plain + AIR * 2 + 1 /** The gutters. The band is not symmetrical: the mark holds twenty off the * leading edge and the session holds twelve off the trailing one. They sit on * the two ends rather than on the band, so the field in the middle centres on * the glass rather than on what is left between them. */ const GUTTER = 20 const CLOSE = 12 /** The flyout's own measures: a row 172 wide and 48 tall, four of air around * them inside the panel, twelve between a row's edge and what it holds, and * eight of fall under the word that opens it. */ const ENTRY = 172 const ROW = 48 const PAD = 4 const NEAR = 12 const FALL = 8 /** * A quiet outline — the frame a control on this surface wears when it is not * the point of the screen. Written once so the folded trigger and anything * beside it agree without anyone checking. */ const outline = { paddingHorizontal: '$3', borderRadius: '$12', borderWidth: 1, borderColor: line, backgroundColor: surface, flexShrink: 0, hoverStyle: { borderColor: plain, backgroundColor: surface }, pressStyle: { borderColor: plain, backgroundColor: surface }, } as const /** One screen under a place. */ export interface Screen { /** What the row says, in the reader's language. */ name: string /** Where it goes. */ to: string /** The drawing beside it, where the place has one. */ mark?: ReactNode } /** One quarter of a product, and the screens inside it. */ export interface Place { /** What the link says, in the reader's language. */ name: string /** * Where the link goes, then every path it answers for. * * The rule reads the whole list and not only the destination, so a reader * three screens deep is still told which quarter of the product they walked * into. */ at: readonly [string, ...string[]] /** The drawing beside it in the sheet, where there is room for one. */ mark?: ReactNode /** The screens under it, in the order they should be met. */ under?: readonly Screen[] } /** * One place, in the row or in the sheet. * * `sheet` is the same link given a row of its own: full width and deep enough * for a thumb rather than a cursor. It is one component either way because "am * I the screen you are on" is one rule, and a second copy of it is a second * answer waiting to disagree. */ function Destination({ place, here, walk, sheet, done, }: { place: Place here: string walk?: (to: string) => void sheet?: boolean done?: () => void }) { const to = place.at[0] const on = place.at.some((p) => here.startsWith(p)) const [open, setOpen] = useState(false) const link = useRef(null) // In the sheet a place is a row with its mark. Nothing hangs off it there: // the sheet lists the places and not the screens under them, and a panel // opening inside a panel on a phone is a second layer over a first. if (sheet) return ( } alignItems="center" gap={NEAR} minHeight={reach.plain} width="100%" borderRadius="$4" cursor="pointer" focusVisibleStyle={paint.focus} > {place.mark} {place.name} ) const word = ( } display="inline-flex" alignItems="center" minHeight={reach.dense} paddingHorizontal={INSET} flexShrink={0} whiteSpace="nowrap" size="$4" color={on ? plain : quiet} hoverStyle={{ color: plain }} > {place.name} ) if (!place.under?.length) return {word} return ( setOpen(true)} onMouseLeave={() => setOpen(false)} onFocusCapture={() => setOpen(true)} onBlurCapture={(e) => { if (!e.currentTarget.contains(e.relatedTarget)) setOpen(false) }} onKeyDown={(e) => { if (e.key !== 'Escape' || !open) return setOpen(false) link.current?.focus() }} /> } position="relative" flexShrink={0} > {word} {open ? ( setOpen(false)} /> ) : null} ) } /** * The screens under one place, hanging off it. * * Inside the place rather than portalled out of it, which decides three * behaviours at once and costs nothing. The pointer crossing the gap between * the word and the panel is still inside the box that opened it, so the panel * does not blink shut halfway down. Tab reaches the rows in the order they are * drawn, because the order they are drawn in is the order they are in. And * focus leaving the whole place is one question asked of one element rather * than a search across a portal for where focus went. */ function Flyout({ place, here, walk, shut, }: { place: Place here: string walk?: (to: string) => void shut: () => void }) { return ( {(place.under ?? []).map((u) => { const on = here === u.to return ( } alignItems="center" gap={NEAR} height={ROW} paddingHorizontal={NEAR} borderRadius="$6" backgroundColor={surface} cursor="pointer" focusVisibleStyle={paint.focus} > {u.mark} {u.name} ) })} ) } export function Band({ mark, places = [], here = '', walk, find, more = 'More', close = 'Close', children, }: { /** The brand lockup at the leading edge. */ mark?: ReactNode /** The quarters of the product. */ places?: readonly Place[] /** The path being read, against which a place says whether it is current. */ here?: string /** What moving to an address means. Absent, every destination is left to the * browser as an ordinary link. */ walk?: (to: string) => void /** The middle of the row. A field that collapses rather than folding: a * control that fits has no business behind something to open. */ find?: ReactNode /** The word on the folded trigger, and the sheet's own title. */ more?: string /** The word that shuts the sheet. */ close?: string /** The trailing edge — everything about this session. It holds the edge at * every width, folded or not. */ children?: ReactNode }) { const [folded, setFolded] = useState(false) const [open, setOpen] = useState(false) const row = useRef(null) const cluster = useRef(null) const wants = useRef(0) /** * Whether the row fits, measured — there is no width in this file and no * device in it. * * The number is the row's own `scrollWidth` against its `clientWidth`, and * what makes that pair mean anything is that every box in the row is floored * at its own content. A flex item's `min-width` starts at `auto`, which is * the content-based minimum, and each leaf here is a single unbreakable run — * one word per link, button frames that are `nowrap`. So the line cannot * squash: given less room than it wants it hangs off the end instead, and the * overhang is exactly the shortfall, gaps counted by the same layout that * draws them. * * Which is why the row is a stack and not a grid. A grid promises the * opposite on purpose — it floors its children at zero so no child can widen * its own track — and under that floor the tracks collapse quietly, the row * reports that it fits, and the labels sit on top of each other. * * A pixel of tolerance, because both figures are integers rounded off a * fractional box and a row that fits exactly can report one more than it has. * * The reading can only be taken while the row is open — folded, that content * is in the sheet — so the one taken as it overflowed is what decides when to * open again. */ useLayoutEffect(() => { const box = row.current if (!box) return const fit = () => { if (!folded) wants.current = box.scrollWidth const over = wants.current > box.clientWidth + 1 setFolded(over) // A sheet left open across an unfold would be a set of controls that are // already on screen behind it. if (!over) setOpen(false) } fit() // The row's own box answers the window. The cluster answers its own // content, which changes without the window moving — an account that // connects, a longer name, a larger type scale — and none of that moves the // box being watched for the first reason. const watch = new ResizeObserver(fit) watch.observe(box) if (cluster.current) watch.observe(cluster.current) return () => watch.disconnect() }, [folded]) const shut = () => setOpen(false) return ( {/* Three groups. What leads is the mark with the places beside it, the middle is the field, and what trails is everything about this session. The band around the field is the only elastic thing in the row and it can only grow, which is what keeps the row's own width the question and the overhang the answer. */} {/* The two ends take equal shares of whatever the field leaves, which is what puts the field on the middle of the glass rather than on the middle of the space between them. Both can still only grow — a flex item's minimum is its own content — so the row's overhang is still exactly the shortfall the fold reads. */} {mark} {folded ? null : ( {places.map((p) => ( ))} )} {find} {folded && places.length ? ( /* Everything that did not fit, in one sheet, and only while the row is folded — unfolded, every control in it is already on screen. */ {/* The dialog's own trigger, which is what makes the keyboard come back: the dialog holds a reference to whatever opened it and returns focus there on close, and a button of our own beside it holds no such reference — Tab restarts at the top of the document. It also carries the popup, expanded and controls attributes without being told. */} {more} {/* The portal's own padding is what insets the sheet from the edge of a phone, and putting it at the top of the trailing edge is where a hand that just pressed the trigger already is. */} {more} {close} {places.map((p) => ( ))} ) : null} {children} ) }