'use client' import { Card, Paragraph, SizableText, XStack, YStack } from '@hanzo/gui' import { Grid } from '@hanzo/ui/grid' import { useId, type KeyboardEvent, type ReactNode } from 'react' import { paint } from '../material/paint' /** * One answer out of a few named ones. * * Every field on this surface takes text, which is what `field.tsx` says about * itself and why it owns an input. A fee tier, a range and a hook are not text: * each is a choice among a handful of values the screen already knows, where * the whole of what a person needs is the values side by side with what is true * about each. Typed into a box that would be a number to look up rather than a * decision to read. * * The set is the reason it is a radio group and not a menu. A menu shows one * answer and hides the rest, and here the rest are the argument — a tier with * no market beside a tier with one is the fact somebody is deciding on. So they * are all on screen, each carrying its own sentence, and none of them is * preselected: a default here would be this software choosing the fee somebody * pays for as long as their position exists. * * Focus moves the way a radio group's does. Tab reaches the group once and the * arrows move within it, so a keyboard does not have to walk past four options * to leave a question it has answered. */ /** The page's own colours, named where they are used. */ const { plain, quiet, line } = paint /** * The ring that says which answer is chosen, and the dot inside it. * * The part's own geometry rather than a rung off the ladder. `reach` says how * tall a control stands and this is not a control — it is a mark drawn inside * one, and the whole of what it has to do is read as filled or empty beside * words set at 16. Named here because it is decided here: three anonymous * sixteens and two eights in the middle of a render are five numbers a reader * has to match up by eye to see that they are two. */ const RING = 16 const DOT = RING / 2 export interface Option { value: T /** What this answer is, in a word or two. */ label: ReactNode /** What is true about it — a spacing, a verdict from the registry, a price. */ note?: ReactNode } /** * How the question is named, which is one of two ways and never neither. * * A group standing under its own heading points at the heading; a group with no * heading draws the words itself. Both are stated as one alternative rather than * two optional props, because the arrangement a pair of optionals also admits — * neither — is a `radiogroup` that a screen reader announces with no question * attached, and it draws nothing, so nobody looking at it sees what is missing. * Passing both is refused for the reason the second half of this exists: two * copies of a question are two questions waiting to disagree. */ export type Asked = { label: ReactNode; labelledBy?: never } | { labelledBy: string; label?: never } /** Where an arrow key lands, or null for a key that is not ours. */ export function lands(key: string, at: number, of: number): number | null { switch (key) { case 'ArrowDown': case 'ArrowRight': return (at + 1) % of case 'ArrowUp': case 'ArrowLeft': return (at - 1 + of) % of case 'Home': return 0 case 'End': return of - 1 default: return null } } export function Choice({ label, labelledBy, help, options, chosen, onChoose, columns = 2, }: Asked & { /** What a person needs to know before answering. */ help?: ReactNode options: readonly Option[] /** Null until answered, which is the state this arrives in. */ chosen: T | null onChoose: (value: T) => void /** At most this many side by side, and fewer where there is no room. The * grid drops a column rather than letting one go under 180. */ columns?: number }) { const named = useId() // One of the two is always there, so this always points at words that exist. const by = labelledBy ?? named const on = options.findIndex((o) => o.value === chosen) /** * Which option Tab reaches. The chosen one, or the first while nothing is * chosen — one stop for the group either way, which is what makes the arrows * the way through it. */ const stop = on === -1 ? 0 : on const press = (key: string, at: number) => { const to = lands(key, at, options.length) if (to === null) return onChoose(options[to]!.value) // Selection and focus move together, which is what a radio group promises. // Reached through the document rather than a ref because the id is already // written on the element for the label to point at, and a focus move is a // document operation whichever way it is spelled. document.getElementById(`${named}-${to}`)?.focus() } return ( {label ? ( {label} ) : null} {help ? ( {help} ) : null} {/* A grid, because the options are a set and a set reads as columns that line up. Wrapped in a flex row they did not: three under a cap of two put two on the first line and stretched the third across the second, so the same answer was a different size depending on how many other answers there were. The track list floors every column at `minmax(0, 1fr)`, which is what holds the widths equal, and states the narrowest a column may get before one is dropped. */} {options.map((o, at) => ( { if (lands(e.key, at, options.length) === null) return e.preventDefault() press(e.key, at) }} /> } role="radio" aria-checked={o.value === chosen} tabIndex={at === stop ? 0 : -1} onPress={() => onChoose(o.value)} // The width belongs to the grid above. Stated here as a basis and a // floor it was stated twice, and the two disagreed the moment a set // did not divide by the cap. height="100%" gap="$1" paddingHorizontal="$4" paddingVertical="$3" // The rung by its name. `$6` is the engine's own radius scale and // lands on the same twelve today, which is the whole problem: two // ladders agreeing is not one ladder, and the brand's rung is the // one the appearance panel moves. borderRadius={paint.round} {...paint.card} outlineStyle="solid" outlineWidth={1} outlineColor={o.value === chosen ? paint.plain : 'transparent'} cursor="pointer" hoverStyle={{ outlineColor: paint.plain }} focusVisibleStyle={paint.focus} > {/* The mark, so the answer is not carried by an edge colour alone. It is drawn from two boxes rather than an icon: one ring, one dot, and nothing to import. */} {o.value === chosen ? ( ) : null} {o.label} {o.note ? ( {o.note} ) : null} ))} ) }