import { Dialog, DialogContent, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, Input, SizableText, Tabs, VisuallyHidden, XStack, YStack, } from '@hanzo/gui' import { useEffect, useId, useRef, type ReactNode } from 'react' import { Code } from './code' import { Mark } from './mark' import { paint } from '../material/paint' import { reach } from '../material/reach' import { weight } from '../material/weight' /** * The one field that reaches everything a surface holds. * * It is not a picker. A picker fills a field and answers one question — which * of these is the one — so it lists what can be chosen and returns a choice. * This answers a different one, what is here, and every row leads to the screen * that holds it: an asset, the market it trades in, a proposal, the person who * filed it, an identifier somebody pasted. The two share a dialog exactly once, * and the result is a search that opens by asking you to choose something. * * The shell is here and the answer is not. What rows there are, in what order, * under what heading, and what a press does with one are facts about a venue * and its routes; drawing them is this file's work and knowing them is not. So * everything that reads — the query, which list is open, whether the dialog is * up — is the caller's state, and the rows arrive as children. * * **A row is an object, not a line of text.** Every one wears the mark of what * it is about, and then says what it is called, what it is tickered, and the * key it is filed under. Reading is slower than recognising, and a list of * forty strings is forty readings. * * **What a row does not carry is a figure.** A heading names the ordering and * that is the whole of what an ordering is for here; a number printed beside * each row turns a list somebody is scanning for a name into a column of * figures, and a `Table` is what a column of figures is. * * **One per screen.** Each of these binds the slash to its own `onOpen`, so two * on a page answer the key twice and stack two dialogs. The finder belongs to * the application's band, where there is one of it. */ /** What this surface paints with. A text primitive left uncoloured resolves * near-black on a black ground, so everything readable here names its * colour. */ const { plain, quiet, faint, line, surface, subtle: tile } = paint /** * What stands for the characters an abbreviation leaves out. * * Three periods and not an ellipsis. A character that looks alike at a glance * is a different character on the page and in every string a reader copies out, * and the one thing somebody does with an identifier is copy it. */ const ELIDED = '...' /** How much of each end survives. Six leading characters is what tells two * addresses minted in one block apart; four trailing is what somebody checks * against a key already in hand. */ const HEAD = 6 const TAIL = 4 /** * An identifier, short enough for a row and long enough to check against one * somebody already has. * * Drawn whole unless abbreviating it actually shortens it, which is why the * threshold is the length of the abbreviation rather than a number written * beside it: at thirteen characters the two are the same length, and replacing * a key with something no shorter loses the middle for nothing. */ export const short = (at: string): string => at.length > HEAD + ELIDED.length + TAIL ? `${at.slice(0, HEAD)}${ELIDED}${at.slice(-TAIL)}` : at /** * The magnifier. A circle and a stroke, drawn here rather than pulled from an * icon set, and it names its colour for the same reason every string above * does. */ export function Lens() { return ( ) } /** * A run of rows under a heading, and whatever action belongs to that run. * * The title comes from the caller's reading rather than from this file: where * nothing stated an ordering there is no ordering to name, and the title says * so by not claiming one. * * Beside it goes an action and nothing else — no count, no pill, no word about * where the rows came from. A count changes on every keystroke with focus * nowhere near it, and a pill beside a heading reads as a state the list is in. */ export function List({ title, action, children, }: { title: ReactNode action?: ReactNode children: ReactNode }) { return ( {title} {action} {children} ) } /** * The action that empties a remembered list. * * A word rather than a mark, because it is the only thing on the panel that * takes something away and a glyph for that is a glyph a reader has to guess * at. */ export function Clear({ say, go }: { say: string; go: () => void }) { return ( } alignItems="center" minHeight={reach.dense} paddingHorizontal="$2" borderRadius="$4" cursor="pointer" flexShrink={0} backgroundColor="transparent" borderWidth={0} hoverStyle={{ backgroundColor: tile }} focusVisibleStyle={paint.focus} > {say} ) } /** * One result. * * One shape for every list, because they are the same row asking the same * thing: what this is, what it is called, what it is tickered, and the key it * is filed under. Three components would be three chances for the lists to * disagree about what a result looks like. * * A button rather than something wrapped in one — `render` puts a real `button` * element under the row, so Tab reaches it, Enter and Space press it, and the * focus ring is the browser's. The key is abbreviated on screen and read out * whole, since it is the only field that tells two things sharing three letters * apart. */ export function Result({ face, name, said, mark, note, at, go, }: { /** The mark of whatever the row is about. */ face: ReactNode /** What it is called. */ name: string /** What it is called when it is read out, where that is not what is drawn. A * row named by an abbreviated identifier identifies nobody. */ said?: string /** A word qualifying the name, where the row has one. */ mark?: string /** What it is tickered, where it is tickered anything. */ note?: string /** The key it is filed under, drawn short and read whole. */ at?: string go: () => void }) { return ( p !== undefined).join(', ')} /> } alignItems="center" gap="$3" width="100%" minWidth={0} paddingHorizontal="$2" paddingVertical="$2" borderRadius={paint.roundest} cursor="pointer" // A button carries the browser's own grey and its own two-pixel outset // border unless it is told otherwise, and a list of them reads as a stack // of chips rather than as rows. backgroundColor="transparent" borderWidth={0} hoverStyle={{ backgroundColor: tile }} focusVisibleStyle={paint.focus} // Everything the row says is a string the venue produced, and by shape // those are indistinguishable from prose. The part that draws them says // which it is drawing. data-value="" > {face} {/* Two lines, and the name on top of them. What a reader scans here is a name, so the name leads and the ticker and the key qualify it underneath — which is the opposite of a table's row, where the identifier leads and the name follows. */} {name} {mark === undefined ? null : {mark}} {note === undefined ? null : ( {note} )} {at === undefined ? null : ( {short(at)} )} ) } /** One of the lists, as a control. */ function Word({ value, on, label }: { value: string; on: string; label: ReactNode }) { return ( {label} ) } export interface SearchProps { /** * What the field asks for, in the reader's words. * * Worn by the trigger, by the dialog's own name and by the field's label and * placeholder, so a reader hearing the control and a reader seeing it are * told the same thing. */ say: string open: boolean onOpen: (next: boolean) => void query: string onQuery: (next: string) => void /** * The lists on offer, where there is more than one kind of answer. * * Absent, there is one list and no row of words over it — a control with a * single option is a control that does nothing. */ lists?: readonly { key: string; label: ReactNode }[] /** Which of them is open. */ list?: string onList?: (key: string) => void /** What stands at the end of the field: a venue picker, a readout, nothing. */ beside?: ReactNode /** * Where Enter goes, and only where exactly one row answers. * * Pasting an identifier leaves one, which is the whole reason this is here — * paste, Enter, done, without reaching for a pointer or tabbing past the list * to find the row. Null where there are several, because Enter into an * ambiguity has to pick, and picking for somebody is guessing. */ enter?: (() => void) | null /** Whether the trigger carries its words and its keycap, or is the mark * alone. A header narrow enough to drop the words still opens the same * dialog. */ wide?: boolean /** The answer: `List`s of `Result`s, and whatever the caller says about a * reading that has not landed or did not come back. */ children: ReactNode } export function Search({ say, open, onOpen, query, onQuery, lists, list, onList, beside, enter, wide = true, children, }: SearchProps) { const field = useId() /** Kept current in a ref, so the listener below is added once rather than * torn down and rebound on every render a caller passes a fresh arrow. */ const opening = useRef(onOpen) useEffect(() => { opening.current = onOpen }) /** * The slash, from anywhere on the page. * * Not while somebody is typing: a slash typed into an amount, an address or * this dialog's own field is a slash. Not with a modifier either, which is * where a browser's own shortcuts live. */ useEffect(() => { const press = (e: KeyboardEvent) => { if (e.key !== '/' || e.metaKey || e.ctrlKey || e.altKey) return const on = e.target if ( on instanceof HTMLElement && (on.isContentEditable || /^(?:INPUT|TEXTAREA|SELECT)$/.test(on.tagName)) ) { return } e.preventDefault() opening.current(true) } window.addEventListener('keydown', press) return () => window.removeEventListener('keydown', press) }, []) return ( {/* The dialog's own trigger, which is what returns the keyboard: the dialog holds a reference to whatever opened it and puts focus back there on close. A button of ours beside it holds no such reference, and Tab would restart at the top of the document. */} {wide ? ( <> {say} {/* The key that opens it, on the control it opens. Heavier than the words beside it: a keycap is a glyph standing for a key rather than a word being read, and at this size the light end goes to nothing inside its border. */} / ) : null} {/* The portal's own padding insets the panel from the edge of a phone. At 390 the padding is the whole margin; at 1920 the width cap takes over and the padding never comes up. */} {/* The dialog is named for a reader and not for the eye: the field under it carries the same words in a placeholder, and a heading repeating them is a line of chrome between a person and the thing they pressed a key to reach. */} {say} {/* The field, closed by the rule under it. The rule is under the field and not under the words: what it separates is the one thing being typed into from everything that answers, and the words are part of the answer. */} onQuery(e.target.value)} onKeyDown={(e) => { if (e.key !== 'Enter' || !enter) return e.preventDefault() enter() }} /> {beside} {/* The lists, as words. No rule under them and no underline on the one that is open — ink alone is what keeps the row reading as a choice of list rather than as a second band of chrome under the first. */} {lists && list !== undefined && onList ? ( {lists.map((l) => ( ))} ) : null} {/* Everything below the field scrolls, so the field being typed into never leaves the screen — and the scroll is capped so a venue with forty things in it draws a dialog rather than a page. On a short window the content's own cap takes over first. */} {children} ) }