A buttons widget: a grid of presses on one message

Several stateless instructions where a single button is one — six presets are
otherwise six tiles to place, six titles to read, and the message they share
repeated six times. Nothing is read back, as for a single button: what these
send is an instruction, and the last one sent is not a state to draw.

Auto-fit columns rather than a configured count: a tile is resized in the
editor and scaled again to whatever panel it hangs on, so how many fit is not
something the document can know.

The editor reads `cfg.buttons` raw rather than through `buttonsOf`, which
drops the blanks — a row being typed into is blank until the first keystroke.

Also carried along by the hooks: the generated SDK was stale (it had no
`RunsReadMetricNames`) and one pre-existing block in `test_panels.py` was
unformatted.
This commit is contained in:
2026-09-04 08:44:16 +02:00
parent 8e62c7726b
commit 10fe2fd760
11 changed files with 252 additions and 10 deletions
+83 -1
View File
@@ -59,7 +59,13 @@ import {
settingOf,
THEME_CHOICES,
} from "./settings"
import { type BarRow, MAX_ROWS, rowsOf, showTitle } from "./ui/core/config"
import {
type BarRow,
type ButtonEntry,
MAX_ROWS,
rowsOf,
showTitle,
} from "./ui/core/config"
import { parsePalette, roleLabel } from "./ui/core/theme"
import {
acceptsDtype,
@@ -482,6 +488,12 @@ export function WidgetPanel({
// Nothing wrote these until now, so a dropdown's choices were uneditable.
const options = (cfg.options ?? []) as { label?: string; value?: unknown }[]
const setOptions = (next: typeof options) => set({ options: next })
// A grid's presses, read raw rather than through `buttonsOf`: that drops the
// blanks, and a row being typed into is blank until the first keystroke. An
// empty row stands in for none, so a fresh grid offers the fields.
const written = (cfg.buttons ?? []) as ButtonEntry[]
const buttons: ButtonEntry[] = written.length ? written : [{}]
const setButtons = (next: ButtonEntry[]) => set({ buttons: next })
const querying = cfg.source === "query"
const pinnedRuns = cfg.source === "runs"
// What this chart would refresh at with nothing configured. The viewer can
@@ -1054,6 +1066,76 @@ export function WidgetPanel({
</div>
) : null}
{widget.type === "buttons" ? (
<div className="grid gap-2">
<span className={PANEL_SECTION}>Buttons</span>
<InfoTip label="Buttons">
One press per row, all publishing to the message above. The grid
fits as many columns as the tile is wide; a row with no value
sends its own label.
</InfoTip>
{buttons.map((button, index) => (
<div
// Position is the only identity a press has.
key={`press-${index}`}
className="flex items-end gap-1.5"
>
<Input
className="min-w-0 flex-1"
value={str(button.label)}
placeholder="Label"
aria-label="Button label"
onChange={(event) =>
setButtons(
buttons.map((other, at) =>
at === index
? { ...other, label: event.target.value }
: other,
),
)
}
/>
<Input
className="w-28"
value={str(button.value)}
placeholder="Value"
aria-label="Button value"
onChange={(event) =>
setButtons(
buttons.map((other, at) =>
at === index
? { ...other, value: coerce(event.target.value) }
: other,
),
)
}
/>
<Button
variant="ghost"
size="icon-sm"
className="text-muted-foreground"
aria-label="Remove button"
onClick={() =>
setButtons(buttons.filter((_, at) => at !== index))
}
>
<X />
</Button>
</div>
))}
<Button
variant="outline"
size="sm"
className="h-8 justify-self-start"
onClick={() => setButtons([...buttons, {}])}
data-testid="add-button"
>
<Plus />
Add button
</Button>
</div>
) : null}
{widget.type === "dropdown" ? (
<div className="grid gap-2">
<span className={PANEL_SECTION}>Options</span>