refactor(web): controls, toggles and segments as utilities

look.ts builds each control's class list from its whole state, so no
two utilities fight over a property. The contextual overrides from rows,
trays, page heads and the removal panel become props.

Refs #280

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Miguel Palhas
2026-11-11 17:40:50 +00:00
co-authored by Claude Opus 5.5
parent 288d4b26ad
commit e07db5a042
15 changed files with 277 additions and 408 deletions
+58 -23
View File
@@ -16,13 +16,11 @@ import {
useState,
} from "react";
import { Icon, type IconName, useArmed } from "./icons";
import { type ControlLook, control, cx, toggle, toggleLamp, toggleState } from "./look";
export type Tone = "fault" | "warn";
/** Joins class names, dropping the falsy ones. */
export function cx(...names: (string | false | null | undefined)[]): string {
return names.filter(Boolean).join(" ");
}
export { cx } from "./look";
/** A boolean data attribute: present (empty) or absent. */
export function flag(on: boolean | undefined): "" | undefined {
@@ -40,6 +38,10 @@ export interface ControlProps extends ComponentProps<"button"> {
glyph?: IconName;
/** Fixed width, so a label that changes (GRAB → SENDING → SENT) never moves the row. */
fixed?: boolean;
/** Full width, content at the start: a phone tray or a settings panel. */
fill?: boolean;
danger?: boolean;
state?: ControlLook["state"];
busy?: boolean;
done?: boolean;
failed?: boolean;
@@ -51,6 +53,9 @@ export function Control({
quiet,
glyph,
fixed,
fill,
danger,
state,
busy,
done,
failed,
@@ -60,14 +65,23 @@ export function Control({
return (
<button
type="button"
className={cx("control", quiet && "control-quiet", fixed && "control-fixed", className)}
className={cx(
control({
look: quiet ? "quiet" : "primary",
shape: fixed ? "fixed" : "word",
fill,
danger,
state,
}),
className,
)}
data-busy={flag(busy)}
data-done={flag(done)}
data-failed={flag(failed)}
{...rest}
>
{glyph && <Icon name={glyph} />}
<span className="control-word">{text}</span>
{glyph && <Icon name={glyph} className={fill ? "mr-1" : undefined} />}
<span>{text}</span>
</button>
);
}
@@ -76,13 +90,20 @@ export function Control({
export function IconControl({
glyph,
label,
look,
shape = "icon",
className,
...rest
}: ComponentProps<"button"> & { glyph: IconName; label: string }) {
}: ComponentProps<"button"> & {
glyph: IconName;
label: string;
look?: ControlLook["look"];
shape?: "icon" | "chip";
}) {
return (
<button
type="button"
className={cx("control control-icon", className)}
className={cx(control({ look, shape }), className)}
aria-label={label}
{...rest}
>
@@ -95,17 +116,17 @@ export function IconControl({
export function IconLink({
label,
href,
className,
shape = "icon",
children,
}: {
label: string;
href: string;
className?: string;
shape?: "icon" | "trailer";
children: ReactNode;
}) {
return (
<a
className={cx("control control-icon", className)}
className={control({ shape })}
href={href}
target="_blank"
rel="noreferrer"
@@ -124,17 +145,20 @@ export function IconLink({
export function LampToggle({
label,
pressed,
fill = false,
...rest
}: ComponentProps<"button"> & {
label: string;
pressed: boolean;
/** Full width, content at the start: a phone tray or a settings panel. */
fill?: boolean;
"data-focus-id"?: string;
}) {
return (
<button type="button" className="toggle" aria-pressed={pressed} {...rest}>
<span className="toggle-lamp" aria-hidden="true" />
<span className="toggle-label">{label}</span>
<span className="toggle-state" aria-hidden="true">
<button type="button" className={toggle(pressed, fill)} aria-pressed={pressed} {...rest}>
<span className={toggleLamp(pressed)} aria-hidden="true" />
<span>{label}</span>
<span className={toggleState(pressed)} aria-hidden="true">
{pressed ? "on" : "off"}
</span>
</button>
@@ -159,14 +183,18 @@ export function Disclosure({
return (
<button
type="button"
className="control control-quiet disclose ml-auto"
className={cx(control({ look: "quiet", shape: "disclose" }), "ml-auto")}
aria-expanded={open}
onClick={onToggle}
>
<span className="control-word">
{`${open ? "hide" : "show"} ${count}${noun === "" ? "" : ` ${noun}`}`}
</span>
<Icon name="chevronDown" />
<span>{`${open ? "hide" : "show"} ${count}${noun === "" ? "" : ` ${noun}`}`}</span>
<Icon
name="chevronDown"
className={cx(
"[transition:transform_200ms_var(--ease-out)]",
open && "[transform:rotate(180deg)]",
)}
/>
</button>
);
}
@@ -188,6 +216,9 @@ export function ArmedDeleteWord({
text={armed ? `tap again to ${label}` : label}
glyph={armed ? "check" : "trash"}
quiet
fill
danger
state={armed ? "armed" : null}
data-kind="delete"
data-armed={armed ? "true" : undefined}
disabled={spent}
@@ -355,6 +386,8 @@ export interface ActionSlot {
wide?: boolean;
/** Shown below 40rem only — an empty cell that keeps a phone column aligned. */
narrow?: boolean;
/** A word or toggle control fills its column; an input keeps its own width. */
fill?: boolean;
}
export interface RowProps {
@@ -447,7 +480,8 @@ export function Row(props: RowProps) {
// the column is positional: a slot is its index
// biome-ignore lint/suspicious/noArrayIndexKey: see above
key={index}
className="r-slot"
// a word or a toggle fills its column, so every row's plates line up
className={cx("r-slot", slot.width !== "icon" && slot.fill !== false && "*:w-full")}
data-width={slot.width}
data-wide={flag(slot.wide)}
data-narrow={flag(slot.narrow)}
@@ -695,7 +729,8 @@ export function GroupNote({ short, more }: { short: string; more?: string }) {
label="more about this list"
aria-expanded={open}
onClick={() => setOpen(!open)}
className="size-chip-h min-w-chip-h min-h-chip-h border-transparent bg-none bg-transparent [box-shadow:none] text-ink-faint aria-expanded:border-accent aria-expanded:text-accent-bright"
look="faint"
shape="chip"
/>
<p className="m-0 basis-full max-w-measure text-xs" hidden={!open}>
{more}