Choice List
A list of answers to pick from, with an optional typed answer. Use it for preference questions and for questions an assistant asks in a chat. When the question is done, show the answer with Choice Summary.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/choice-list.jsonCopies Choice List and the components it uses (Badge) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/choice-list. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @hugeicons/core-free-icons @hugeicons/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: Badge.
Copy and paste the following code into your project.
'use client'; import { useId, type ReactNode } from 'react'; import { CheckIcon } from '@/lib/sprout/icons/check-icon'; import { HelpCircleIcon } from '@/lib/sprout/icons/help-circle-icon'; import { cn } from '@/lib/sprout/classes'; import { Badge } from '@/components/ui/badge'; export const choiceListLayoutNames = ['stacked', 'compact'] as const; export type ChoiceListLayout = (typeof choiceListLayoutNames)[number]; export interface ChoiceListOption { id: string; label: string; recommended?: boolean | undefined; } export interface ChoiceListTextOption { value: string; active: boolean; onChange: (value: string) => void; onFocus: () => void; id?: string | undefined; error: string | null; label?: string | undefined; placeholder?: string | undefined; } export interface ChoiceListProps { legend: string; options: readonly ChoiceListOption[]; value: readonly string[]; onChange: (value: string[]) => void; multiple?: boolean | undefined; disabled?: boolean | undefined; error?: string | undefined; layout?: ChoiceListLayout | undefined; textOption?: ChoiceListTextOption | undefined; className?: string | undefined; } const recommendedSuffix = /\s*\(recommended\)\s*$/i; export function ChoiceList({ legend, options, value, onChange, multiple = false, disabled = false, error, layout = 'stacked', textOption, className, }: ChoiceListProps) { const name = useId(); const compact = layout === 'compact'; const errorId = `${name}-choices-error`; const textId = `${name}-text`; const textErrorId = `${name}-text-error`; return ( <fieldset data-slot="choice-list" data-layout={layout} data-multiple={multiple || undefined} disabled={disabled} className={cn('sprout-choice-list', className)} > <legend className={compact && !multiple ? 'sprout-choice-list-sr-only' : 'sprout-choice-list-legend'}> {legend} </legend> <div data-slot="choice-list-options" className="sprout-choice-list-options"> {options.map((option, index) => { const checked = value.includes(option.id); const recommended = option.recommended === true || recommendedSuffix.test(option.label); const label = option.label.replace(recommendedSuffix, ''); return ( <label key={option.id} data-slot="choice-list-option" data-checked={checked || undefined} data-disabled={disabled || undefined} data-recommended={recommended || undefined} className="sprout-choice-list-option" > <input type={multiple ? 'checkbox' : 'radio'} name={name} value={option.id} checked={checked} aria-invalid={error ? true : undefined} aria-describedby={error ? errorId : undefined} onChange={event => onChange( multiple ? event.target.checked ? [...value, option.id] : value.filter(id => id !== option.id) : [option.id], ) } className="sprout-choice-list-input" /> <span aria-hidden="true" className="sprout-choice-list-mark"> {checked ? <CheckIcon /> : compact ? index + 1 : null} </span> <span className="sprout-choice-list-label"> {label} {recommended && ( <Badge variant="success" className="sprout-choice-list-badge"> Recommended </Badge> )} </span> </label> ); })} </div> {error && ( <p id={errorId} className="sprout-choice-list-error"> {error} </p> )} {textOption && (!compact || textOption.active) && ( <div id={textOption.id ?? `${name}-custom`} data-slot="choice-list-text" data-checked={textOption.active || undefined} data-invalid={textOption.error ? true : undefined} className="sprout-choice-list-text" > <span aria-hidden="true" className="sprout-choice-list-mark"> {textOption.active && <CheckIcon />} </span> <div className="sprout-choice-list-text-body"> <label htmlFor={textId} className="sprout-choice-list-text-label"> {textOption.label ?? 'Your answer'} </label> <textarea id={textId} autoFocus={compact} rows={1} value={textOption.value} disabled={disabled} placeholder={textOption.placeholder ?? 'Type a different answer…'} aria-describedby={textOption.error ? textErrorId : undefined} aria-invalid={textOption.error ? true : undefined} onFocus={textOption.onFocus} onChange={event => textOption.onChange(event.target.value)} className="sprout-choice-list-textarea" /> {textOption.error && ( <p id={textErrorId} role="alert" className="sprout-choice-list-error"> {textOption.error} </p> )} </div> </div> )} </fieldset> ); } export interface ChoiceSummaryProps { prompt: string; answer?: string | undefined; className?: string | undefined; children?: ReactNode; } export function ChoiceSummary({ prompt, answer, className, children }: ChoiceSummaryProps) { return ( <div data-slot="choice-summary" className={cn('sprout-choice-summary', className)}> <HelpCircleIcon aria-hidden="true" className="sprout-choice-summary-icon" /> <span className="sprout-choice-summary-prompt">{prompt}</span> {answer && ( <span className="sprout-choice-summary-answer"> <CheckIcon aria-hidden="true" className="sprout-choice-summary-check" /> <span className="sprout-choice-summary-answer-text">{answer}</span> </span> )} {children} </div> ); }And the files it imports:
lib/sprout/icons/check-icon.tsxlib/sprout/icons/create-icon.tsxlib/sprout/icons/help-circle-icon.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-choice-list { min-width: 0; color: var(--sprout-text); border: 0; gap: 6px; margin: 0; padding: 0; font-family: inherit; display: grid; } .sprout-choice-list-legend { font-size: var(--sprout-text-xs); color: var(--sprout-muted); margin-bottom: 2px; padding: 0; line-height: 1rem; } .sprout-choice-list-sr-only { clip-path: inset(50%); white-space: nowrap; border: 0; width: 1px; height: 1px; margin: -1px; padding: 0; position: absolute; overflow: hidden; } .sprout-choice-list-options { gap: 6px; min-width: 0; display: grid; } .sprout-choice-list-option, .sprout-choice-list-text { box-sizing: border-box; border: 1px solid var(--sprout-input); border-radius: var(--sprout-radius-panel); min-width: 0; min-height: 44px; font-size: var(--sprout-text-sm); outline-offset: 0; outline: 0 solid #0000; gap: 12px; padding: 8px 12px; line-height: 1.25rem; display: flex; position: relative; } .sprout-choice-list-option { cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; align-items: center; } .sprout-choice-list-text { align-items: flex-start; } @media (hover: hover) { .sprout-choice-list-option:not([data-disabled]):hover, .sprout-choice-list-text:hover { background: color-mix(in oklab, var(--sprout-hover) 60%, transparent); } } .sprout-choice-list-option:has(.sprout-choice-list-input:focus-visible), .sprout-choice-list-text:focus-within { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); } .sprout-choice-list-option[data-checked], .sprout-choice-list-text[data-checked] { border-color: color-mix(in oklab, var(--sprout-text) 35%, transparent); background: var(--sprout-hover); } .sprout-choice-list-option[data-disabled] { cursor: not-allowed; opacity: .5; } .sprout-choice-list-option[data-disabled][data-checked] { border-color: var(--sprout-input); background: var(--sprout-secondary); } .sprout-choice-list-text:has(.sprout-choice-list-textarea:disabled) { opacity: .5; } .sprout-choice-list-text[data-invalid] { border-color: var(--sprout-danger); } .sprout-choice-list-text[data-invalid]:focus-within { outline-color: var(--sprout-danger); } .sprout-choice-list-input { z-index: 1; opacity: 0; width: calc(100% + 2px); height: calc(100% + 2px); cursor: inherit; margin: 0; position: absolute; inset: -1px; } .sprout-choice-list-mark { box-sizing: border-box; border: 1px solid var(--sprout-border); background: var(--sprout-surface); width: 20px; height: 20px; color: var(--sprout-primary); border-radius: 999px; flex-shrink: 0; place-items: center; display: grid; } .sprout-choice-list[data-multiple] .sprout-choice-list-mark { border-radius: 4px; } .sprout-choice-list-text .sprout-choice-list-mark { border-radius: 999px; } .sprout-choice-list-mark svg { width: 13px; height: 13px; } :is(.sprout-choice-list-option, .sprout-choice-list-text)[data-checked] .sprout-choice-list-mark { border-color: var(--sprout-primary); background: var(--sprout-primary); color: var(--sprout-primary-foreground, #fff); } .sprout-choice-list:not([data-multiple]) .sprout-choice-list-option[data-checked] .sprout-choice-list-mark svg { display: none; } .sprout-choice-list:not([data-multiple]) .sprout-choice-list-option[data-checked] .sprout-choice-list-mark:after { content: ""; background: var(--sprout-primary-foreground, #fff); border-radius: 999px; width: 8px; height: 8px; } .sprout-choice-list-option[data-disabled] .sprout-choice-list-mark { border-color: var(--sprout-border); background: var(--sprout-secondary); } .sprout-choice-list-label { overflow-wrap: anywhere; flex: 1; min-width: 0; } .sprout-choice-list-badge { margin-inline-start: 8px; } .sprout-choice-list-error { font-size: var(--sprout-text-xs); color: var(--sprout-danger); margin: 0; line-height: 1rem; } .sprout-choice-list-text-body { flex: 1; min-width: 0; } .sprout-choice-list-text-label { font-size: var(--sprout-text-xs); color: var(--sprout-muted); font-weight: 500; line-height: 1rem; display: block; } .sprout-choice-list-textarea { box-sizing: border-box; width: 100%; min-height: 24px; max-height: 160px; font: inherit; color: inherit; resize: vertical; field-sizing: content; background: none; border: 0; outline: none; margin: 0; padding: 2px 0; display: block; } .sprout-choice-list-textarea::placeholder { color: var(--sprout-muted); } .sprout-choice-list-textarea:disabled { cursor: not-allowed; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-options { overscroll-behavior: contain; grid-auto-rows: max-content; gap: 0; max-height: max(76px, min(224px, 34dvh - 96px)); padding: 2px; overflow-y: auto; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option { border-radius: var(--sprout-radius-control); border-color: #0000; width: 100%; min-height: 36px; padding: 4px 8px; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option[data-checked] { border-color: #0000; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option[data-disabled][data-checked] { border-color: var(--sprout-input); } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option + .sprout-choice-list-option:before { content: ""; background: color-mix(in oklab, var(--sprout-text) 8%, transparent); height: 1px; position: absolute; top: -1px; left: 8px; right: 8px; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option[data-checked]:before, .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option[data-checked] + .sprout-choice-list-option:before { visibility: hidden; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option .sprout-choice-list-mark { border-radius: var(--sprout-radius-control); background: var(--sprout-secondary); width: 24px; height: 24px; font-size: var(--sprout-text-xs); font-variant-numeric: tabular-nums; color: var(--sprout-muted); border: 0; line-height: 1rem; } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option[data-checked] .sprout-choice-list-mark { background: var(--sprout-primary); color: var(--sprout-primary-foreground, #fff); } .sprout-choice-list[data-layout="compact"]:not([data-multiple]) .sprout-choice-list-option[data-checked] .sprout-choice-list-mark svg { display: block; } .sprout-choice-list[data-layout="compact"]:not([data-multiple]) .sprout-choice-list-option[data-checked] .sprout-choice-list-mark:after { content: none; } @media (any-pointer: coarse) { .sprout-choice-list[data-layout="compact"] .sprout-choice-list-options { max-height: max(94px, min(224px, 40dvh - 128px)); } .sprout-choice-list[data-layout="compact"] .sprout-choice-list-option { min-height: 44px; } } .sprout-choice-summary { min-width: 0; font-size: var(--sprout-text-sm); color: var(--sprout-text); flex-wrap: wrap; align-items: center; gap: 4px 8px; padding-inline-start: 24px; line-height: 1.25rem; display: flex; position: relative; } .sprout-choice-summary-icon { top: 3px; width: 16px; height: 16px; color: var(--sprout-muted); position: absolute; inset-inline-start: 0; } .sprout-choice-summary-prompt { overflow-wrap: anywhere; min-width: 0; color: var(--sprout-muted); } .sprout-choice-summary-answer { border: 1px solid var(--sprout-line); border-radius: var(--sprout-radius-control); background: var(--sprout-secondary); align-items: center; gap: 4px; min-width: 0; padding: 2px 8px; font-weight: 500; display: inline-flex; } .sprout-choice-summary-check { width: 14px; height: 14px; color: var(--sprout-success); flex-shrink: 0; } .sprout-choice-summary-answer-text { white-space: pre-wrap; overflow-wrap: anywhere; min-width: 0; } @media (prefers-reduced-motion: no-preference) { .sprout-choice-list-option, .sprout-choice-list-text, .sprout-choice-list-mark { transition: background-color var(--sprout-motion-normal, .18s) var(--sprout-motion-ease, ease-out), border-color var(--sprout-motion-normal, .18s) var(--sprout-motion-ease, ease-out), color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out); } .sprout-choice-list-mark svg, .sprout-choice-summary-check { animation: sprout-choice-pop var(--sprout-motion-normal, .15s) ease-out both; } } @keyframes sprout-choice-pop { from { opacity: 0; transform: scale(.6); } } @media (forced-colors: active) { .sprout-choice-list-option[data-checked], .sprout-choice-list-text[data-checked], .sprout-choice-list-option[data-checked] .sprout-choice-list-mark { border-color: highlight; } .sprout-choice-list-option:has(.sprout-choice-list-input:focus-visible), .sprout-choice-list-text:focus-within { outline-color: highlight; } } }Import it from
@/components/ui/choice-list. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/choice-list.
Layouts
Stacked rows for forms and settings. Compact, numbered rows for questions inside a chat. Each works as a single (radio) or multiple (checkbox) list.
Stacked · single (radio)
Stacked · multiple (checkbox)
Compact · single (radio)
Which repository should I open the issue in?
Compact · multiple (checkbox), scrolls past ~6 rows
Which Slack channels should I summarize each morning?
Bud preference
A stacked reply-style list inside a form. Custom reveals a text field. Saving locks the list.
How should I write my replies to you?
Agent question
Compact lists inside a question card. Other answer opens a typed answer. Continue with nothing chosen shows an error; Continue or Skip shows the summary.
Which repository should I open the issue in?
Which Slack channels should I summarize each morning?
Should I send the weekly digest now?
Recommended choice and custom answer
A label ending in (Recommended) gets a badge. A stacked list can always show a typed answer after its options.
States
Disabled, busy, and error states for both layouts and the typed answer.
Preselected and disabled
Stacked error
Compact error
Typed answer error
Choice summary
The answered, expired, and cancelled lines a question collapses into. Long and multi-line answers wrap.
Your answer was recorded.
Your answer was recorded.
Your answer was recorded.
This request expired without an answer.
This request was cancelled without an answer.
Usage
import { ChoiceList, ChoiceSummary } from "@tulipfarm/sprout/choice-list";
const [style, setStyle] = useState<string[]>([]);
<ChoiceList
legend="Reply style"
options={[
{ id: "short", label: "Short answers (Recommended)" },
{ id: "detailed", label: "Detailed answers" },
]}
value={style}
onChange={setStyle}
/>
<ChoiceList
layout="compact"
multiple
legend="Choose one or more answers"
options={channels}
value={choices}
onChange={setChoices}
error={attempted && !choices.length ? "Choose an answer." : undefined}
textOption={{
id: "custom-answer",
value: text,
active: typed,
onChange: setText,
onFocus: () => setTyped(true),
error: null,
}}
/>
<ChoiceSummary prompt="Reply style" answer="Short answers" />value is always an array of option ids, for one answer or many.onChange gets the next array, so a state setter can be passed straight in.
Props
layout is "stacked" (bordered 44px rows with radio or checkbox marks) or "compact" (36px numbered rows with dividers, in a list that scrolls after about six rows). There is no separate size: each layout has one row height, and compact rows grow to 44px on touch screens.
multiple turns radios into checkboxes. In a compact single list the legend is for screen readers only, because the question above already names it. disabled locks every row, anderror shows a message under the list and marks each input invalid.
An option with recommended, or a label ending in(Recommended), gets a Recommended badge.textOption adds a typed answer after the options. A stacked list always shows it. A compact list shows it only whileactive, and moves focus into it, so pair it with an "Other answer" button whose aria-controls is thetextOption.id.
ChoiceSummary takes a prompt, an optionalanswer (leave it out for an expired or cancelled question), and optional children after the answer.
Accessibility
The list is a <fieldset> with a<legend>. Each row is a label around a real radio or checkbox input, so Tab, arrow keys, and Space work as they do for native controls. A focused row shows a 2px focus ring. Errors are linked witharia-describedby, and a typed-answer error is announced.
Colors change in 120–180ms, and check marks pop in. Both stop when reduced motion is requested. In forced colors mode checked rows and focus use system highlight colors.
Theme tokens
Rows use --sprout-input borders. Checked rows use--sprout-primary-subtle with a --sprout-focusborder, and check marks use --sprout-primary, so the opt-in TulipFarm theme tints them green. Compact numbers sit on--sprout-secondary. Errors use --sprout-danger, and the summary check uses --sprout-success.