Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions app/src/components/decisions/decision-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ const COLLAPSE_SECONDS = 0.22;
* and is named for it, so it always says what pressing it will do. Where the Bot can take words
* instead of a choice, the last row is a field for them.
*
* ONE SURFACE. The card is the only box: no line between the rows, none above the button, no fill
* behind the picked row. A radio's filled dot is what says which one is picked, as in any radio
* group, and the rows are set apart by their own space. Lines and a tint on every row made a card
* of two answers read as a table of three.
*
* THE KEYBOARD. 1 to 9 pick a row and Enter sends it, as in T3 Code, but only on the newest card
* that is waiting (`useNewestDecision`), so two cards never both take a key, and never while the
* person is typing somewhere (TanStack's single-key hotkeys skip inputs and contenteditable, so
Expand Down Expand Up @@ -179,17 +184,14 @@ export function DecisionCard({
style={{ overflow: "hidden" }}
transition={fold}
>
{children ? <div className="px-4 pb-3">{children}</div> : null}
{children ? <div className="px-4 pb-2">{children}</div> : null}
{/*
* Native radios, one group per card, styled as rows: the browser gives them the radio
* group's keys (Tab to the picked row, Up and Down to move) and screen readers name
* them as choices.
*/}
{/* A fieldset is as wide as its longest word unless told otherwise, which clipped a long link. */}
<fieldset
aria-label={title}
className="min-w-0 border-border border-t"
>
<fieldset aria-label={title} className="min-w-0 px-2">
{/*
* ONLY A CLICK OR A KEY PICKS, AND ONLY THE PICKED ROW LOOKS PICKED. A row under the
* pointer used to take a tint all but the picked row's, so moving the pointer across
Expand All @@ -198,10 +200,7 @@ export function DecisionCard({
*/}
{options.map((option, index) => (
<label
className={cn(
"group flex w-full cursor-pointer items-start gap-3 border-border border-b px-4 py-2.5 transition-colors duration-150 has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-ring/50 has-[:focus-visible]:ring-inset",
picked === option.value && "bg-primary/[0.06]",
)}
className={ROW}
data-picked={picked === option.value ? "" : undefined}
key={option.value}
>
Expand Down Expand Up @@ -238,10 +237,8 @@ export function DecisionCard({
))}
{other ? (
<div
className={cn(
"flex w-full items-center gap-3 border-border border-b px-4 py-2.5 transition-colors duration-150 has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-ring/50 has-[:focus-visible]:ring-inset",
picked === OTHER && "bg-primary/[0.06]",
)}
className={cn(ROW, "cursor-default items-center")}
data-picked={picked === OTHER ? "" : undefined}
>
<input
aria-label={other.placeholder}
Expand Down Expand Up @@ -275,11 +272,11 @@ export function DecisionCard({
) : null}
</fieldset>
{error ? (
<p className="px-4 pt-2 text-destructive text-sm" role="alert">
<p className="px-4 pt-1 text-destructive text-sm" role="alert">
{failureSaid(error)}
</p>
) : null}
<div className="flex justify-end px-4 py-2.5">
<div className="flex justify-end px-4 pt-1 pb-3">
<Button disabled={!canSend} onClick={send}>
{submitting
? "Sending…"
Expand All @@ -301,7 +298,7 @@ export function DecisionCard({
style={{ overflow: "hidden" }}
transition={{ ...fold, delay: reduceMotion ? 0 : 0.06 }}
>
<p className="flex min-w-0 items-center gap-2 border-border border-t px-4 py-2.5 text-sm">
<p className="flex min-w-0 items-center gap-2 px-4 pb-3 text-sm">
<IconCheck
aria-hidden
className="size-4 shrink-0 text-muted-foreground"
Expand All @@ -322,6 +319,13 @@ export function DecisionCard({
);
}

/**
* One answer's row. Inset from the card's edge by the fieldset, so the focus ring, drawn round the
* row's own corners, has room inside the card; the row's text still lines up with the title.
*/
const ROW =
"group flex w-full cursor-pointer items-start gap-3 rounded-md px-2 py-2 has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-ring/50";

/** The radio's dot: a ring, filled in on the picked row. */
function Dot({ on }: { on: boolean }) {
return (
Expand Down
35 changes: 12 additions & 23 deletions app/src/components/gallery/cards.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import type { GalleryComponent } from "@/lib/copilot/gallery-registry";
import { Fact, Facts } from "./facts";
import { Badge, FigureLink, GalleryFrame, type Tone } from "./frame";
import { GalleryMarkdown } from "./markdown";
import { byPosition } from "./positions";
Expand Down Expand Up @@ -44,18 +45,14 @@ export function RecordCard({
caption={subtitle}
title={title}
>
<dl className="grid grid-cols-[minmax(0,10rem)_1fr] gap-x-4 gap-y-2 text-sm">
<Facts>
{byPosition(fields).map(({ item: field, key }) => (
// Keyed by position: a label is still being typed while the card draws.
<div className="contents" key={key}>
<dt className="truncate text-muted-foreground">{field.label}</dt>
{/* Field values wrap because truncation can hide the reported data. */}
<dd className="min-w-0 break-words">
<GalleryMarkdown>{field.value}</GalleryMarkdown>
</dd>
</div>
<Fact key={key} label={field.label}>
<GalleryMarkdown>{field.value}</GalleryMarkdown>
</Fact>
))}
</dl>
</Facts>
</GalleryFrame>
);
}
Expand Down Expand Up @@ -147,21 +144,13 @@ export function ChecklistCard({
items: given,
}: Partial<z.infer<typeof ChecklistCardProps>>) {
const items = given ?? [];
const done = items.filter((item) => item.done).length;
/*
* No "2 of 3" beside the title. The ticks are the count, a few lines below it, and a badge that
* only said them again was one more filled shape on the card; a screen reader hears "Done" or "To
* do" before each item.
*/
return (
<GalleryFrame
action={
<Badge
tone={
done === items.length && items.length > 0 ? "positive" : "neutral"
}
>
{done} of {items.length}
</Badge>
}
caption={caption}
title={title}
>
<GalleryFrame caption={caption} title={title}>
<ul className="space-y-2">
{byPosition(items).map(({ item, key }) => (
<li className="flex items-start gap-2.5 text-sm" key={key}>
Expand Down
23 changes: 9 additions & 14 deletions app/src/components/gallery/decisions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useState } from "react";
import { z } from "zod";
import { DecisionCard, OTHER } from "@/components/decisions/decision-card";
import type { GalleryComponent } from "@/lib/copilot/gallery-registry";
import { Fact, Facts } from "./facts";
import { GalleryFrame } from "./frame";
import { GalleryMarkdown } from "./markdown";
import { byPosition } from "./positions";
Expand Down Expand Up @@ -112,24 +113,18 @@ export function ApprovalCard(props: Waiting<ApprovalArgs> & { name?: string }) {
>
<GalleryMarkdown className="text-sm">{args.summary}</GalleryMarkdown>
{command || details.length ? (
<dl className="mt-3 grid grid-cols-[minmax(0,9rem)_1fr] gap-x-4 gap-y-1.5 rounded-lg bg-muted/60 px-3 py-2 text-sm">
<Facts className="mt-3">
{command ? (
<div className="contents">
<dt className="truncate text-muted-foreground">Command</dt>
<dd className="min-w-0">
<code className="break-all font-mono text-xs">{command}</code>
</dd>
</div>
<Fact label="Command">
<code className="break-all font-mono text-xs">{command}</code>
</Fact>
) : null}
{byPosition(details).map(({ item: detail, key }) => (
<div className="contents" key={key}>
<dt className="truncate text-muted-foreground">{detail.label}</dt>
<dd className="min-w-0 break-words">
<GalleryMarkdown>{detail.value}</GalleryMarkdown>
</dd>
</div>
<Fact key={key} label={detail.label}>
<GalleryMarkdown>{detail.value}</GalleryMarkdown>
</Fact>
))}
</dl>
</Facts>
) : null}
</DecisionCard>
);
Expand Down
47 changes: 47 additions & 0 deletions app/src/components/gallery/facts.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { ReactNode } from "react";
import { cn } from "@/lib/utils";

/**
* Facts read as label and value, one pair a line: a record's fields, what an approval is about, a
* form's answers once sent.
*
* Plain rows on the card that holds them, with no box or fill of their own. An approval's facts used
* to sit in a grey panel inside the card, a box in a box, while a record's same facts sat bare; now
* every card a Bot draws shows them one way. A label wraps rather than being cut off, since a form's
* question can be a sentence and the value means little without it.
*/
export function Facts({
className,
children,
}: {
className?: string;
children: ReactNode;
}) {
return (
<dl
className={cn(
"grid grid-cols-[minmax(0,10rem)_1fr] gap-x-4 gap-y-2 text-sm",
className,
)}
data-slot="gallery-facts"
>
{children}
</dl>
);
}

export function Fact({
label,
children,
}: {
label: ReactNode;
children: ReactNode;
}) {
return (
<div className="contents" data-slot="gallery-fact">
<dt className="break-words text-muted-foreground">{label}</dt>
{/* A value wraps: cut off, it could hide the very thing being reported. */}
<dd className="min-w-0 break-words">{children}</dd>
</div>
);
}
84 changes: 57 additions & 27 deletions app/src/components/gallery/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { useCallback, useEffect, useId, useRef, useState } from "react";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import type { GalleryComponent } from "@/lib/copilot/gallery-registry";
import { Fact, Facts } from "./facts";
import { Badge, GalleryFrame } from "./frame";

const Field = z
Expand Down Expand Up @@ -207,8 +215,11 @@ function FormEntry({
setErrors(problems);
if (problems.size) {
const firstId = problems.keys().next().value;
const control = firstId ? form.elements.namedItem(firstId) : null;
if (control instanceof HTMLElement) control.focus();
// By its id, not its name: a select's name is on a hidden input, and its id on the button.
const control = firstId
? document.getElementById(`${prefix}-${firstId}`)
: null;
control?.focus();
return;
}
sendingRef.current = true;
Expand Down Expand Up @@ -242,21 +253,19 @@ function FormEntry({
{finished ? (
<div role="status">
{answer ? (
<dl className="space-y-3">
<Facts>
{args.fields.map((field) => (
<div
key={field.id}
className="grid grid-cols-[minmax(0,1fr)_minmax(0,2fr)] gap-4 text-sm"
>
<dt className="text-muted-foreground">{field.label}</dt>
<dd className="whitespace-pre-wrap break-words font-medium">
{Object.hasOwn(answer.values, field.id)
? answer.values[field.id]
: "Not provided"}
</dd>
</div>
<Fact key={field.id} label={field.label}>
{Object.hasOwn(answer.values, field.id) ? (
<span className="whitespace-pre-wrap">
{answer.values[field.id]}
</span>
) : (
<span className="text-muted-foreground">Not provided</span>
)}
</Fact>
))}
</dl>
</Facts>
) : (
<p className="text-sm text-muted-foreground">
This form has been completed. Its saved answers are unavailable.
Expand Down Expand Up @@ -321,18 +330,39 @@ function FormEntry({
onChange={(event) => change(event.target.value)}
/>
) : field.type === "select" ? (
<select
{...shared}
className="h-9 w-full rounded-lg border border-input bg-background px-2.5 text-sm outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50 disabled:opacity-50"
onChange={(event) => change(event.target.value)}
/*
* The app's Select, as every other choice in OpenBot is drawn, not the browser's
* own, whose closed box and open list were the system's rather than the app's.
* The value goes into the same map the other fields use, so checking it and what
* is sent to the Bot are unchanged.
*/
<Select
disabled={shared.disabled}
items={field.options?.map((option) => ({
value: option,
label: option,
}))}
name={shared.name}
onValueChange={(value) => change(value ?? "")}
required={shared.required}
value={shared.value || null}
>
<option value="">Choose an option</option>
{field.options?.map((option) => (
<option key={option} value={option}>
{option}
</option>
))}
</select>
<SelectTrigger
aria-describedby={shared["aria-describedby"]}
aria-invalid={shared["aria-invalid"]}
className="w-full"
id={id}
>
<SelectValue placeholder="Choose an option" />
</SelectTrigger>
<SelectContent>
{field.options?.map((option) => (
<SelectItem key={option} value={option}>
{option}
</SelectItem>
))}
</SelectContent>
</Select>
) : (
<Input
{...shared}
Expand Down Expand Up @@ -369,7 +399,7 @@ function FormEntry({
Couldn't send your answers. Try again.
</p>
)}
<div className="flex justify-end border-t border-border pt-3">
<div className="flex justify-end">
<Button type="submit" size="sm" disabled={sending || !respond}>
{sending ? "Sending…" : (args.submitLabel ?? "Submit answers")}
</Button>
Expand Down
Loading
Loading