'use client'
import { Button, SizableText, XStack } from '@hanzo/gui'
import type { ReactElement, ReactNode } from 'react'
import { Caret } from './caret'
import { CARET, type Size } from './coin'
import { paint } from '../material/paint'
import { reach } from '../material/reach'
import { weight } from '../material/weight'
/**
* The control at the end of a field that says what has been chosen, and opens
* the chooser.
*
* The list it opens belongs to whoever calls this — a venue's tokens, a
* network, a delegate — and is a different list in every product. What is the
* same everywhere is the closed control: four boxes, each measured, each with
* its own ground, its own corner and its own answer to what a mark on it is
* drawn at. Written per screen those four came apart at all of it.
*
* It is a button, and the primitive is the button one rather than a stack with
* a button substituted underneath: a press is made one way in this family, and
* the two spellings do not stay identical — a stack handed somebody else's
* element forwards DOM props to it and leaves the engine's own press behind.
* A caller wiring this to a dialog substitutes that dialog's own trigger with
* `render`, which is how this engine swaps an element everywhere else.
*/
const { plain, surface } = paint
/**
* Where the closed control sits, which is the whole of what differs between the
* four of them — the box, its height, its corner, its inset and its ground.
*
* Beside a figure it is a pill on that field's own line, sized to the symbol it
* carries: an ordinary control, standing where every other one in the row
* stands. Under a panel it is the width of the card and stands at the height of
* the action below it, which is the only other thing on the card as wide as
* this; the name and the mark that says it opens go to opposite ends. One word
* at the call site, because the alternative is five style props repeated
* wherever the second box is wanted, and five props are five chances for two
* rows to disagree.
*
* The fourth is a name and its mark and no box at all, which is how the asset a
* limit price is quoted in is drawn: that control states the unit of the figure
* beside it rather than offering a second decision, and a filled pill with a
* caret beside a price reads as the other side of the order. It presses, so it
* stands at the rung an inline control takes — the same one the prices beside
* it are drawn at, where the design reference draws twenty-one and no target
* may go under twenty-four.
*/
export type Form = 'pill' | 'row' | 'pair' | 'bare'
/**
* `ground` is what the control is painted once it carries something and `empty`
* before it does.
*
* They differ on the pair and only there. Two of those sit side by side, and
* the one still to be answered is painted solid while the one already answered
* goes quiet, swapping the moment either is filled — so the eye lands on the
* question rather than on the answer already given. Quiet is a shade off the
* card and not the card itself: the answered half stands a tenth of white above
* the page it is on, and a pill painted in the card's own fill has no edge left
* to read.
*
* A control standing alone has no second one to be the unanswered half of, so
* its empty state is drawn on the card's own ground.
*/
const FORM = {
pill: {
width: undefined,
justifyContent: 'center',
minHeight: reach.plain,
paddingHorizontal: '$3',
borderRadius: '$12',
ground: surface,
empty: surface,
// A pill sits on a card painted the same shade, so the fill alone leaves no
// edge to read and the control disappears into the panel behind it. The
// hairline is what separates them: one pixel of the ink at a tenth, around
// the filled pill and the empty one alike. Only this form carries it — a
// row and a pair stand on the page rather than on a card, and the fourth
// has no box at all.
borderWidth: 1,
borderColor: paint.line,
// Beside a figure, after the symbol: the pill is sized to the pair and the
// caret is the second half of the control, drawn in the same ink as the
// ticker beside it.
caret: { to: 'down', size: CARET, quiet: false },
scale: 'pill',
},
row: {
width: '100%',
justifyContent: 'space-between',
minHeight: reach.major,
paddingHorizontal: '$3',
borderRadius: paint.roundest,
borderWidth: 0,
borderColor: 'transparent',
ground: surface,
empty: surface,
// Across a card it stands at the far end and leads on. A step under the
// pill's, and under the words rather than level with them: `mark` is the
// rung a glyph that is not itself a target is drawn at, which is what a
// sign saying "next" is — it is not half of the control the way a
// chooser's caret is.
caret: { to: 'right', size: reach.mark, quiet: true },
scale: 'pill',
},
pair: {
width: '100%',
justifyContent: 'space-between',
minHeight: reach.major,
// Sixteen, where the other three take twelve: both halves of a pair are
// inset 16 and the mark stands there, so a twelve puts the disc and the
// ticker four pixels inside where a reader met them.
paddingHorizontal: '$4',
borderRadius: paint.roundest,
borderWidth: 0,
borderColor: 'transparent',
ground: paint.subtle,
empty: paint.solid,
caret: { to: 'down', size: CARET, quiet: false },
scale: 'pill',
},
bare: {
width: undefined,
justifyContent: 'flex-end',
minHeight: reach.dense,
paddingHorizontal: '$3',
borderRadius: '$12',
borderWidth: 0,
borderColor: 'transparent',
ground: 'transparent',
empty: 'transparent',
// A control that states the unit of the figure beside it opens quietly, and
// wears the smallest mark for the same reason: it is naming what the figure
// is in, not offering a second thing to choose.
caret: null,
scale: 'unit',
},
} as const
export function Picker({
form = 'pill',
chosen,
empty,
label,
open = false,
render = ,
onPress,
}: {
/** Which of the four boxes this control is drawn as. */
form?: Form
/**
* What has been chosen, drawn at the rung this form pairs a mark with.
*
* A function rather than a node, so the pairing cannot drift: the control
* knows how large a mark on it stands and hands that out, and no call site is
* left holding a number it would have to keep in step. Absent is nothing
* chosen, which is the state the control arrives in.
*/
chosen?: (scale: Size) => ReactNode
/** The words with nothing chosen. */
empty?: ReactNode
/**
* What the control is read out as, where its own contents will not do.
*
* Usually they will: a chosen thing draws its mark and its name, and nothing
* chosen draws the sentence asking for one. A screen passes one only where
* two of these sit in the same form and their contents do not tell them
* apart.
*/
label?: string
/**
* Whether the chooser it opens is open.
*
* Defaulted rather than optional, so the attribute is always on the element.
* A control that says it opens a dialog and never says whether the dialog is
* open has told a screen reader half of the fact — and the half it withheld
* is the one that changes.
*/
open?: boolean
/**
* The element it is drawn as. A dialog's own trigger goes here; left out it
* is an ordinary button, which is what it is the rest of the time.
*
* Stated rather than left to the primitive. Handing the engine the key with
* nothing in it drops the element to a `div` wearing `role="button"` — the
* words a screen reader gets are the same and the tag a keyboard needs is
* gone. Naming the element is one line and it cannot come out that way.
*/
render?: ReactElement
onPress?: () => void
}) {
/** The box this control is drawn as, and the ground it sits on. Split apart
* because the rest of the row is style props and the ground is not one. */
const { ground: full, empty: bare, caret: opens, scale, ...shaped } = FORM[form]
/** What it is painted now, and therefore what the words and the mark on it
* have to be legible against. */
const box = chosen ? full : bare
const on = box === paint.solid ? paint.ink : plain
return (
)
}