feat(widgets): redesign gate widgets — readable hierarchy, prominent select-all, styled controls
Titles + intro read as a clear prompt (comfortable line length), options as spaced choice rows, Other/Back/Exit as discreet secondary controls under a divider. Multiselect gets a prominent Select all/Deselect all at the head of the list with a live count. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,27 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import type { WidgetProps } from "./types";
|
import type { WidgetProps } from "./types";
|
||||||
|
|
||||||
export function FreetextWidget({ descriptor, onRespond }: WidgetProps) {
|
export function FreetextWidget({ descriptor, onRespond }: WidgetProps) {
|
||||||
const [text, setText] = useState("");
|
const [text, setText] = useState("");
|
||||||
return (
|
return (
|
||||||
<div className="space-y-2">
|
<div className="space-y-3">
|
||||||
{descriptor.title && <p className="font-medium">{descriptor.title}</p>}
|
{descriptor.title && (
|
||||||
<textarea className="w-full border rounded p-2" value={text} onChange={(e) => setText(e.target.value)} />
|
<p className="text-[0.95rem] font-semibold leading-snug text-foreground">
|
||||||
<button className="border rounded px-3 py-1" onClick={() => onRespond({ id: descriptor.id, kind: "freetext", text })}>Send</button>
|
{descriptor.title}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
<textarea
|
||||||
|
className="min-h-24 w-full resize-y rounded-lg border border-input bg-card p-3 text-sm outline-none transition-colors focus:border-primary/40 focus:ring-2 focus:ring-ring/20"
|
||||||
|
value={text}
|
||||||
|
onChange={(e) => setText(e.target.value)}
|
||||||
|
placeholder="Type your answer…"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
className="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-[oklch(var(--primary-hover))]"
|
||||||
|
onClick={() => onRespond({ id: descriptor.id, kind: "freetext", text })}
|
||||||
|
>
|
||||||
|
Send
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,43 +18,56 @@ export function MultiselectWidget({ descriptor, onRespond }: WidgetProps) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectAll() {
|
|
||||||
setChecked(new Set(options.map((o) => o.id)));
|
|
||||||
}
|
|
||||||
|
|
||||||
function deselectAll() {
|
|
||||||
setChecked(new Set());
|
|
||||||
}
|
|
||||||
|
|
||||||
const isDisabled = descriptor.allow_empty === false && checked.size === 0;
|
const isDisabled = descriptor.allow_empty === false && checked.size === 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-2">
|
<div className="space-y-4">
|
||||||
{descriptor.title && <p className="font-medium">{descriptor.title}</p>}
|
{descriptor.title && (
|
||||||
<div className="flex gap-2">
|
<p className="text-[0.95rem] font-semibold leading-snug text-foreground">
|
||||||
<button className="text-sm border rounded px-2 py-1" onClick={allChecked ? deselectAll : selectAll}>
|
{descriptor.title}
|
||||||
{allChecked ? "Deselect all" : "Select all"}
|
</p>
|
||||||
|
)}
|
||||||
|
<div className="flex flex-col">
|
||||||
|
{/* Prominent select-all control at the head of the list. */}
|
||||||
|
<div className="flex items-center justify-between border-b border-border/60 pb-2">
|
||||||
|
<button
|
||||||
|
className="text-sm font-semibold text-primary transition-colors hover:text-[oklch(var(--primary-hover))]"
|
||||||
|
onClick={() =>
|
||||||
|
setChecked(allChecked ? new Set() : new Set(options.map((o) => o.id)))
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{allChecked ? "Deselect all" : "Select all"}
|
||||||
|
</button>
|
||||||
|
<span className="text-xs tabular-nums text-muted-foreground">
|
||||||
|
{checked.size} / {options.length} selected
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col gap-1 pt-2">
|
||||||
|
{options.map((o) => (
|
||||||
|
<label
|
||||||
|
key={o.id}
|
||||||
|
className="flex cursor-pointer items-center gap-2.5 rounded-lg border border-transparent px-2.5 py-2 text-sm text-foreground transition-colors hover:bg-accent"
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
className="size-4 shrink-0 accent-[oklch(var(--primary))]"
|
||||||
|
checked={checked.has(o.id)}
|
||||||
|
onChange={() => toggle(o.id)}
|
||||||
|
/>
|
||||||
|
<span>{o.label}</span>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<button
|
||||||
|
className="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition-colors hover:bg-[oklch(var(--primary-hover))] disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
disabled={isDisabled}
|
||||||
|
onClick={() => onRespond({ id: descriptor.id, kind: "multiselect", choices: Array.from(checked) })}
|
||||||
|
>
|
||||||
|
Confirm
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col gap-1">
|
|
||||||
{options.map((o) => (
|
|
||||||
<label key={o.id} className="flex items-center gap-2 cursor-pointer">
|
|
||||||
<input
|
|
||||||
type="checkbox"
|
|
||||||
checked={checked.has(o.id)}
|
|
||||||
onChange={() => toggle(o.id)}
|
|
||||||
/>
|
|
||||||
{o.label}
|
|
||||||
</label>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
className="border rounded px-3 py-1 disabled:opacity-50 disabled:cursor-not-allowed"
|
|
||||||
disabled={isDisabled}
|
|
||||||
onClick={() => onRespond({ id: descriptor.id, kind: "multiselect", choices: Array.from(checked) })}
|
|
||||||
>
|
|
||||||
Confirm
|
|
||||||
</button>
|
|
||||||
<ReservedControls
|
<ReservedControls
|
||||||
reserved={descriptor.reserved}
|
reserved={descriptor.reserved}
|
||||||
onControl={(c, t) => onRespond({ id: descriptor.id, control: c, ...(t !== undefined ? { text: t } : {}) })}
|
onControl={(c, t) => onRespond({ id: descriptor.id, control: c, ...(t !== undefined ? { text: t } : {}) })}
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ export function ReservedControls({
|
|||||||
const [text, setText] = useState("");
|
const [text, setText] = useState("");
|
||||||
if (!reserved?.length) return null;
|
if (!reserved?.length) return null;
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-2 pt-2">
|
<div className="flex flex-col gap-2 border-t border-border/60 pt-3">
|
||||||
<div className="flex gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
{reserved.map((c) => (
|
{reserved.map((c) => (
|
||||||
<button
|
<button
|
||||||
key={c}
|
key={c}
|
||||||
className="text-sm border rounded px-2 py-1"
|
className="rounded-md border border-border/60 px-2.5 py-1 text-xs text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
|
||||||
onClick={() => (c === "other" ? setOtherOpen(true) : onControl(c))}
|
onClick={() => (c === "other" ? setOtherOpen(true) : onControl(c))}
|
||||||
>
|
>
|
||||||
{LABELS[c] ?? c}
|
{LABELS[c] ?? c}
|
||||||
@@ -26,14 +26,15 @@ export function ReservedControls({
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
{otherOpen && (
|
{otherOpen && (
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-2">
|
||||||
<textarea
|
<textarea
|
||||||
className="w-full border rounded p-2"
|
className="min-h-20 w-full resize-y rounded-lg border border-input bg-card p-3 text-sm outline-none transition-colors focus:border-primary/40 focus:ring-2 focus:ring-ring/20"
|
||||||
value={text}
|
value={text}
|
||||||
onChange={(e) => setText(e.target.value)}
|
onChange={(e) => setText(e.target.value)}
|
||||||
|
placeholder="Describe your alternative…"
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
className="text-sm border rounded px-2 py-1 self-start disabled:opacity-50 disabled:cursor-not-allowed"
|
className="self-start rounded-lg bg-primary px-3 py-1.5 text-sm font-medium text-primary-foreground transition-colors hover:bg-[oklch(var(--primary-hover))] disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
disabled={!text.trim()}
|
disabled={!text.trim()}
|
||||||
onClick={() => onControl("freetext", text)}
|
onClick={() => onControl("freetext", text)}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,19 +1,43 @@
|
|||||||
import type { WidgetProps } from "./types";
|
import type { WidgetProps } from "./types";
|
||||||
import { ReservedControls } from "./ReservedControls";
|
import { ReservedControls } from "./ReservedControls";
|
||||||
|
|
||||||
export function SelectWidget({ descriptor, onRespond }: WidgetProps) {
|
export function SelectWidget({ descriptor, onRespond }: WidgetProps) {
|
||||||
return (
|
return (
|
||||||
<div className="space-y-2">
|
<div className="space-y-4">
|
||||||
{descriptor.title && <p className="font-medium">{descriptor.title}</p>}
|
{(descriptor.title || descriptor.intro) && (
|
||||||
{descriptor.intro && <p className="text-sm text-muted-foreground">{descriptor.intro}</p>}
|
<div className="space-y-1.5">
|
||||||
|
{descriptor.title && (
|
||||||
|
<p className="text-[0.95rem] font-semibold leading-snug text-foreground">
|
||||||
|
{descriptor.title}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{descriptor.intro && (
|
||||||
|
<p className="max-w-[68ch] whitespace-pre-wrap text-sm leading-relaxed text-muted-foreground">
|
||||||
|
{descriptor.intro}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
{descriptor.options?.map((o) => (
|
{descriptor.options?.map((o) => (
|
||||||
<button key={o.id} className="border rounded px-3 py-2 text-left hover:bg-accent"
|
<button
|
||||||
onClick={() => onRespond({ id: descriptor.id, kind: "select", choices: [o.id] })}>
|
key={o.id}
|
||||||
{o.label}{o.recommended && <span className="ml-2 text-xs text-green-600">(consigliato)</span>}
|
className="flex items-center justify-between gap-3 rounded-lg border border-border bg-card px-4 py-3 text-left text-sm text-foreground transition-colors hover:border-primary/40 hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/40"
|
||||||
|
onClick={() => onRespond({ id: descriptor.id, kind: "select", choices: [o.id] })}
|
||||||
|
>
|
||||||
|
<span>{o.label}</span>
|
||||||
|
{o.recommended && (
|
||||||
|
<span className="shrink-0 rounded-full bg-[oklch(var(--success)/0.16)] px-2 py-0.5 text-[0.65rem] font-medium text-[oklch(0.5_0.13_165)]">
|
||||||
|
consigliato
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
</button>
|
</button>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
<ReservedControls reserved={descriptor.reserved} onControl={(c, t) => onRespond({ id: descriptor.id, control: c, ...(t !== undefined ? { text: t } : {}) })} />
|
<ReservedControls
|
||||||
|
reserved={descriptor.reserved}
|
||||||
|
onControl={(c, t) => onRespond({ id: descriptor.id, control: c, ...(t !== undefined ? { text: t } : {}) })}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user