'use client' import { SizableText, Tabs, XStack, YStack } from '@hanzo/gui' import type { KeyboardEvent, ReactNode } from 'react' import { paint } from '../material/paint' import { reach } from '../material/reach' import { weight } from '../material/weight' import { press } from './press' /** * One widget under several names, and the column it stands over. * * Swapping, resting a limit, buying and selling are four things to do with the * same two fields, and nothing on four separate screens says so: a person who * wants a price of their own instead of a crossing has to already know the * limit screen exists before they can go looking for it. The row here is that * relation put where it can be seen and pressed. * * Each mode keeps its own address. A row that only moved a variable would leave * one link for four screens, no way to send somebody the one you meant, and a * back button that walks out of the widget rather than back through it — so the * tabs are anchors and moving between them is a change of address. * * The screens themselves are untouched by it. What they render is what they * rendered; this decides where the column sits and what stands above it. */ /** What the row is painted with, in the token layer's own names. A text * primitive given no colour resolves near-black on a black page, so every * readable thing here names one. */ const { plain, quiet, subtle: fill } = paint /** * What the ring needs outside the pill, and how far the row is inset. * * The halo is what keeps a focused tab's ring off the top and bottom edges when * the row scrolls sideways: five either side around a 32 pill makes the 42 the * row measures, which also puts the column's own top edge directly under it * with no gap between them. The inset is the four between the column's edge and * the first tab, and again between the trailing control and the other edge. */ const HALO = 5 const INSET = 4 /** How far apart the modes sit. */ const APART = 12 /** * How much deeper than an ordinary page this one starts. * * A page's first row sits 24 below the band and this row sits 60 below it. The * workspace already spends the 24 that every screen gets, so this is the * difference, and it belongs to the desk because the modes are the only screens * that take it. */ const CREST = 36 /** One thing to do with the fields below, and where it lives. */ export interface Mode { /** The address the mode has to itself. */ to: string /** What the tab says, in the reader's language. */ name: string } export function Desk({ modes = [], here = '', walk, label, act, width = 464, children, }: { modes?: readonly Mode[] /** Which mode the row is standing over. The address, so nothing here is asked * to remember it and no state can drift from the address bar. */ here?: string /** What moving to a mode means. Absent, the tabs are left to the browser as * ordinary links. */ walk?: (to: string) => void /** What the row of tabs is called, for a reader who cannot see it sits over a * ticket. */ label?: string /** The one control that belongs to the row rather than to the screen — what * the current mode lets a person adjust. Drawn at the trailing edge, and * drawn only where a caller passes one. */ act?: ReactNode /** How wide the column under the row runs. The modes share it, so the row * lines up with the card beneath and every screen agrees on where its * leading edge is. */ width?: number /** The screen the current mode is showing. */ children?: ReactNode }) { /** * Enter and space on a tab. * * The row is a roving-focus group and it cancels the key before the anchor * can turn it into a click, so the move that click would have made is made * here. The same cancel is what stops the browser fetching the document again * over the top of it. */ const key = (to: string) => (e: KeyboardEvent) => { if (e.key !== 'Enter' && e.key !== ' ') return e.preventDefault() walk?.(to) } return ( {/* Given less room than the words need, the row scrolls inside its own box rather than widening the document. It needs no focus stop of its own the way a table does: every tab in it is focusable, and focus brings its own tab into view. */} {modes.map((m) => { const on = here === m.to return ( } flexDirection="row" alignItems="center" justifyContent="center" flexShrink={0} minHeight={reach.card} paddingHorizontal="$3" borderRadius="$12" // The one mark of which mode you are in. Nothing else on the // row is filled, so the fill is unambiguous — a pointer moves // the word's colour instead, which is what a link does // everywhere else. backgroundColor={on ? fill : 'transparent'} cursor="pointer" focusVisibleStyle={paint.focus} > {/* Every word carries the same emphasis, on and off. The fill alone says which one is current; a heavier current tab would say it twice. */} {m.name} ) })} {act} {children} ) }