Field
A labeled row for one form control, with a required or optional marker, a counter, and a helper or error line. ChoiceGroup and CheckboxField cover radios and checkboxes. FieldsDisabled locks a whole form while it saves.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/field.jsonCopies Field and the components it uses (Badge, Radio Group, and Separator) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/field. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/react @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, Radio Group, and Separator.
Copy and paste the following code into your project.
'use client'; import { createContext, useContext, useId, type AriaAttributes, type ReactElement, type ReactNode } from 'react'; import { Checkbox as BaseCheckbox } from '@base-ui/react/checkbox'; import { CheckIcon } from '@/lib/sprout/icons/check-icon'; import { cn } from '@/lib/sprout/classes'; import { Badge } from '@/components/ui/badge'; import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import { Separator, type SeparatorLabelProps } from '@/components/ui/separator'; export const fieldLabelVisibilityNames = ['visible', 'screen-reader'] as const; export const choiceGroupTypeNames = ['checkbox', 'radio'] as const; export const choiceGroupLayoutNames = ['list', 'columns'] as const; export const checkboxFieldVariantNames = ['default', 'neutral'] as const; export type FieldLabelVisibility = (typeof fieldLabelVisibilityNames)[number]; export type ChoiceGroupType = (typeof choiceGroupTypeNames)[number]; export type ChoiceGroupLayout = (typeof choiceGroupLayoutNames)[number]; export type CheckboxFieldVariant = (typeof checkboxFieldVariantNames)[number]; export type Requirement = { required: true; optional?: never } | { optional: true; required?: never }; export const FieldsDisabled = createContext(false); interface FieldControl { id: string; describedBy: string | undefined; invalid: boolean; required: boolean; disabled: boolean; } const FieldControlContext = createContext<FieldControl | null>(null); export interface FieldControlProps { id?: string | undefined; disabled?: boolean | undefined; 'aria-describedby'?: string | undefined; 'aria-invalid'?: AriaAttributes['aria-invalid']; 'aria-required'?: AriaAttributes['aria-required']; 'aria-label'?: string | undefined; 'aria-labelledby'?: string | undefined; } export function useFieldControl<P extends FieldControlProps>(props: P): P { const field = useContext(FieldControlContext); if (!field || props['aria-label'] !== undefined || props['aria-labelledby'] !== undefined) return props; return { ...props, id: field.id, disabled: field.disabled || props.disabled, 'aria-describedby': ids(field.describedBy, props['aria-describedby']), 'aria-invalid': field.invalid || props['aria-invalid'], 'aria-required': field.required || props['aria-required'], }; } function ids(...values: (string | false | undefined)[]) { const tokens = values.flatMap(value => (value ? value.split(/\s+/) : [])); return [...new Set(tokens.filter(Boolean))].join(' ') || undefined; } function RequirementMarker({ required, id }: { required: boolean; id: string | undefined }) { if (!required) return null; return ( <> <span aria-hidden data-slot="requirement" className="sprout-field-requirement"> * </span> {id && ( <span id={id} className="sprout-field-sr-only"> Required </span> )} </> ); } export type FieldProps = Requirement & { label: string; labelVisibility?: FieldLabelVisibility | undefined; helper?: ReactNode; error?: string | undefined; counter?: string | undefined; id?: string | undefined; disabled?: boolean | undefined; className?: string | undefined; children: ReactElement; }; export function Field({ label, labelVisibility = 'visible', helper, error, counter, id, disabled, required, className, children, }: FieldProps) { const generatedId = useId(); const controlId = id ?? generatedId; const locked = useContext(FieldsDisabled) || disabled === true; const lineId = error ? `${controlId}-error` : helper ? `${controlId}-helper` : undefined; const counterId = counter ? `${controlId}-counter` : undefined; const control: FieldControl = { id: controlId, describedBy: ids(lineId, counterId), invalid: Boolean(error), required: required === true, disabled: locked, }; return ( <div data-slot="field" data-disabled={locked || undefined} data-invalid={error ? true : undefined} className={cn('sprout-field', className)} > <div data-slot="field-header" className={labelVisibility === 'screen-reader' ? 'sprout-field-sr-only' : 'sprout-field-header'} > <div className="sprout-field-heading"> <label data-slot="label" htmlFor={controlId} className="sprout-field-label"> {label} </label> <RequirementMarker required={required === true} id={undefined} /> </div> {counter && ( <p id={counterId} data-slot="field-counter" className="sprout-field-counter"> {counter} </p> )} </div> <FieldControlContext value={control}>{children}</FieldControlContext> {error ? ( <p id={lineId} data-slot="field-error" className="sprout-field-message sprout-field-error"> {error} </p> ) : helper ? ( <p id={lineId} data-slot="field-helper" className="sprout-field-message"> {helper} </p> ) : null} </div> ); } function FieldCheckbox({ variant = 'default', className, ...props }: BaseCheckbox.Root.Props & { variant?: CheckboxFieldVariant | undefined }) { return ( <BaseCheckbox.Root data-slot="checkbox" data-variant={variant} className={cn('sprout-field-checkbox', className)} {...props} > <BaseCheckbox.Indicator data-slot="checkbox-indicator" className="sprout-field-checkbox-indicator"> <CheckIcon aria-hidden="true" /> </BaseCheckbox.Indicator> </BaseCheckbox.Root> ); } export interface ChoiceOption<V extends string> { value: V; label: string; description?: string | undefined; disabled?: boolean | undefined; } export type ChoiceGroupProps<V extends string> = Requirement & { legend: string; legendVisibility?: FieldLabelVisibility | undefined; helper?: ReactNode; error?: string | undefined; warning?: string | undefined; options: readonly ChoiceOption<V>[]; layout?: ChoiceGroupLayout | undefined; disabled?: boolean | undefined; id?: string | undefined; className?: string | undefined; } & ( | { type: 'checkbox'; value: readonly V[]; onValueChange: (value: V[]) => void; } | { type: 'radio'; value: V; onValueChange: (value: V) => void } ); export function ChoiceGroup<V extends string>(props: ChoiceGroupProps<V>) { const { legend, legendVisibility = 'visible', helper, error, warning, options, layout = 'list', required, className, } = props; const generatedId = useId(); const groupId = props.id ?? generatedId; const locked = useContext(FieldsDisabled) || props.disabled === true; const radio = props.type === 'radio'; const legendId = `${groupId}-legend`; const errorId = error ? `${groupId}-error` : undefined; const helperId = helper ? `${groupId}-helper` : undefined; const markerId = !radio && required ? `${groupId}-requirement` : undefined; const labelling = { id: groupId, 'aria-labelledby': legendId, 'aria-describedby': ids(errorId, helperId, markerId), }; const rows = options.map((option, index) => { const checked = props.type === 'checkbox' ? props.value.includes(option.value) : props.value === option.value; const disabled = locked || option.disabled === true; const descriptionId = option.description ? `${groupId}-option-${index}-description` : undefined; let control: ReactElement; if (props.type === 'checkbox') { const { value, onValueChange } = props; control = ( <FieldCheckbox checked={checked} disabled={disabled} aria-describedby={descriptionId} aria-invalid={error ? true : undefined} onCheckedChange={next => onValueChange(next ? [...value, option.value] : value.filter(item => item !== option.value)) } /> ); } else { control = ( <RadioGroupItem className="sprout-field-radio" value={option.value} disabled={disabled} aria-describedby={descriptionId} /> ); } return ( <div key={option.value} data-slot="choice-option" data-checked={checked || undefined} data-disabled={disabled || undefined} className="sprout-choice-option" > <label className="sprout-choice-option-label"> {control} <span className="sprout-choice-option-text">{option.label}</span> </label> {option.description && ( <p id={descriptionId} className="sprout-choice-option-description"> {option.description} </p> )} </div> ); }); const content = ( <> <div className={legendVisibility === 'screen-reader' ? 'sprout-field-sr-only' : 'sprout-choice-group-legend'}> <p id={legendId} className="sprout-choice-group-legend-text"> {legend} </p> <RequirementMarker required={required === true} id={markerId} /> {warning && <Badge variant="warning">{warning}</Badge>} </div> {helper && ( <p id={helperId} className="sprout-field-message"> {helper} </p> )} <div data-slot="choice-group-options" data-layout={layout} className="sprout-choice-group-options"> {rows} </div> {error && ( <p id={errorId} className="sprout-field-message sprout-field-error"> {error} </p> )} </> ); const classes = cn('sprout-choice-group', className); if (props.type === 'radio') { const { onValueChange } = props; return ( <RadioGroup data-slot="choice-group" data-type="radio" className={classes} value={props.value} disabled={locked} aria-required={required || undefined} aria-invalid={error ? true : undefined} {...labelling} onValueChange={next => { const option = options.find(item => item.value === next); if (option) onValueChange(option.value); }} > {content} </RadioGroup> ); } return ( <div role="group" data-slot="choice-group" data-type="checkbox" data-disabled={locked || undefined} className={classes} {...labelling} > {content} </div> ); } export interface CheckboxFieldProps { label: string; helper?: ReactNode; error?: string | undefined; checked: boolean; onCheckedChange: (checked: boolean) => void; disabled?: boolean | undefined; variant?: CheckboxFieldVariant | undefined; id?: string | undefined; className?: string | undefined; } export function CheckboxField({ label, helper, error, checked, onCheckedChange, disabled, id, variant, className, }: CheckboxFieldProps) { const generatedId = useId(); const fieldId = id ?? generatedId; const locked = useContext(FieldsDisabled) || disabled === true; const lineId = error ? `${fieldId}-error` : helper ? `${fieldId}-helper` : undefined; return ( <div id={fieldId} data-slot="checkbox-field" data-disabled={locked || undefined} className={cn('sprout-checkbox-field', className)} > <label className="sprout-checkbox-field-label"> <FieldCheckbox variant={variant} checked={checked} disabled={locked} aria-describedby={lineId} aria-invalid={error ? true : undefined} onCheckedChange={next => onCheckedChange(next)} /> <span className="sprout-checkbox-field-text">{label}</span> </label> {error ? ( <p id={lineId} className="sprout-field-message sprout-field-error sprout-checkbox-field-message"> {error} </p> ) : helper ? ( <p id={lineId} className="sprout-field-message sprout-checkbox-field-message"> {helper} </p> ) : null} </div> ); } export type FieldSeparatorProps = Omit<SeparatorLabelProps, 'variant' | 'children'> & { children?: SeparatorLabelProps['children']; }; export function FieldSeparator({ className, children = null, ...props }: FieldSeparatorProps) { return ( <Separator data-slot="field-separator" className={cn('sprout-field-separator', className)} {...props}> {children} </Separator> ); }And the files it imports:
lib/sprout/icons/check-icon.tsxlib/sprout/icons/create-icon.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-field { align-content: start; gap: 6px; min-width: 0; display: grid; } .sprout-field-header { justify-content: space-between; align-items: center; gap: 12px; min-width: 0; display: flex; } .sprout-field-heading, .sprout-choice-group-legend { flex-wrap: wrap; align-items: center; gap: 4px; min-width: 0; display: flex; } .sprout-field-label, .sprout-choice-group-legend-text { min-width: 0; color: var(--sprout-text); font-size: var(--sprout-text-sm); overflow-wrap: anywhere; margin: 0; font-weight: 500; line-height: 1rem; } .sprout-field-label { user-select: none; } .sprout-field[data-disabled] .sprout-field-label { cursor: not-allowed; opacity: .5; } .sprout-choice-group-legend-text { line-height: 1.25rem; } .sprout-choice-group[data-disabled] .sprout-choice-group-legend-text { opacity: .5; } .sprout-field-requirement { color: var(--sprout-danger); font-weight: 500; } .sprout-field-counter { color: var(--sprout-muted); font-size: var(--sprout-text-xs); font-variant-numeric: tabular-nums; flex-shrink: 0; margin: 0; line-height: 1rem; } .sprout-field-message { min-width: 0; color: var(--sprout-muted); font-size: var(--sprout-text-sm); overflow-wrap: anywhere; margin: 0; line-height: 1.25rem; } .sprout-field-error { color: var(--sprout-danger); } .sprout-field-sr-only { clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; width: 1px; height: 1px; margin: -1px; padding: 0; position: absolute; overflow: hidden; } .sprout-field-checkbox, .sprout-field-radio { box-sizing: border-box; border: 1px solid var(--sprout-border); background-color: var(--sprout-surface); width: 16px; height: 16px; color: var(--sprout-on-primary); cursor: pointer; outline-offset: 0; outline: 0 solid #0000; flex-shrink: 0; justify-content: center; align-items: center; margin-top: 2px; padding: 0; display: flex; position: relative; } .sprout-field-checkbox { border-radius: 4px; } .sprout-field-radio { border-radius: 999px; } .sprout-field-checkbox:after, .sprout-field-radio:after { content: ""; position: absolute; inset: -8px -12px; } .sprout-field-checkbox:focus-visible, .sprout-field-radio:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); } .sprout-field-checkbox[data-checked], .sprout-field-radio[data-checked] { border-color: var(--sprout-primary); background-color: var(--sprout-primary); } .sprout-field-checkbox[data-variant="neutral"][data-checked] { border-color: var(--sprout-text); background-color: var(--sprout-text); color: var(--sprout-surface); } .sprout-field-checkbox[aria-invalid="true"]:not([data-checked]) { border-color: var(--sprout-danger); box-shadow: 0 0 0 3px color-mix(in oklab, var(--sprout-danger) 20%, transparent); } .sprout-field-checkbox[aria-invalid="true"]:focus-visible { outline-color: var(--sprout-danger); } .sprout-field-checkbox[data-disabled], .sprout-field-radio[data-disabled] { cursor: not-allowed; opacity: .5; } .sprout-field-checkbox-indicator { color: currentColor; place-content: center; display: grid; } .sprout-field-checkbox-indicator[data-unchecked] { display: none; } .sprout-field-checkbox-indicator svg { width: 14px; height: 14px; } .sprout-checkbox-field { min-width: 0; color: var(--sprout-text); align-content: start; gap: 4px; display: grid; } .sprout-checkbox-field[data-disabled] { color: var(--sprout-muted); } .sprout-checkbox-field-label { width: fit-content; max-width: 100%; font-size: var(--sprout-text-sm); cursor: pointer; align-items: flex-start; gap: 8px; font-weight: 500; line-height: 1.25rem; display: flex; } .sprout-checkbox-field[data-disabled] .sprout-checkbox-field-label { cursor: not-allowed; } .sprout-checkbox-field-text, .sprout-choice-option-text { overflow-wrap: anywhere; min-width: 0; } .sprout-checkbox-field-message { padding-left: 24px; } .sprout-choice-group { align-content: start; gap: 8px; min-width: 0; display: grid; } .sprout-choice-group-options { gap: 4px; min-width: 0; display: grid; } .sprout-choice-group[data-type="radio"] .sprout-choice-group-options { gap: 8px; } @media (width >= 40rem) { .sprout-choice-group-options[data-layout="columns"] { grid-template-columns: repeat(2, minmax(0, 1fr)); } } .sprout-choice-option { isolation: isolate; box-sizing: border-box; border-radius: var(--sprout-radius-control); min-width: 0; min-height: 36px; color: var(--sprout-text); align-content: center; gap: 2px; padding: 8px 10px; display: grid; position: relative; } @media (hover: hover) { .sprout-choice-option:not([data-disabled], [data-checked]):hover { background-color: var(--sprout-hover); } .sprout-choice-group[data-type="radio"] .sprout-choice-option:not([data-disabled], [data-checked]):hover { background-color: var(--sprout-hover); } .sprout-checkbox-field:not([data-disabled]) .sprout-checkbox-field-label:hover .sprout-field-checkbox:not([data-checked]) { border-color: var(--sprout-border-strong); } } .sprout-choice-option[data-checked] { background-color: var(--sprout-hover); } .sprout-choice-option[data-disabled] { color: var(--sprout-muted); } .sprout-choice-group[data-type="radio"] .sprout-choice-option { border: 1px solid var(--sprout-input); background-color: var(--sprout-surface); } .sprout-choice-group[data-type="radio"] .sprout-choice-option[data-checked] { border-color: color-mix(in oklab, var(--sprout-text) 35%, transparent); background-color: var(--sprout-hover); } .sprout-choice-option-label { width: 100%; font-size: var(--sprout-text-sm); cursor: pointer; align-items: flex-start; gap: 8px; line-height: 1.25rem; display: flex; } .sprout-choice-option[data-disabled] .sprout-choice-option-label { cursor: not-allowed; } .sprout-choice-option-label:after { content: ""; z-index: -1; border-radius: inherit; position: absolute; inset: 0; } .sprout-choice-option .sprout-field-checkbox, .sprout-choice-option .sprout-field-radio { position: static; } .sprout-choice-option .sprout-field-checkbox:after, .sprout-choice-option .sprout-field-radio:after { content: none; } .sprout-choice-option-description { color: var(--sprout-muted); font-size: var(--sprout-text-xs); overflow-wrap: anywhere; pointer-events: none; margin: 0; padding-left: 24px; line-height: 1rem; } @media (pointer: coarse) { .sprout-choice-option { min-height: 44px; } .sprout-checkbox-field-label { align-items: center; min-height: 44px; } .sprout-checkbox-field-label .sprout-field-checkbox { margin-top: 0; } } .sprout-field-separator { min-height: 20px; margin-block: -8px; } @media (prefers-reduced-motion: no-preference) { .sprout-field-label, .sprout-choice-option, .sprout-field-checkbox, .sprout-field-radio { transition: color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out), background-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out), border-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out), opacity var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out); } } @media (forced-colors: active) { .sprout-field-checkbox, .sprout-field-radio { border-color: canvastext; } .sprout-field-checkbox[data-checked], .sprout-field-radio[data-checked] { color: highlighttext; background-color: highlight; border-color: highlight; } .sprout-field-checkbox:focus-visible, .sprout-field-radio:focus-visible { outline-color: highlight; } .sprout-choice-option[data-checked] { outline: 1px solid highlight; } } }Import it from
@/components/ui/field. 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/field.
Agent details
Required and optional markers, a helper, a counter, a Select, and errors that appear after submit and move focus to the first problem.
Choice groups
Radios pick one value and checkboxes pick many, in a list or two columns from 640px. Options can carry descriptions and be disabled one at a time.
Reply mode
Repeat
Tools
Changes dataSkills to import
RequiredRun on
Checkbox fields
One checkbox with its label and a helper or error line. The neutral variant fills with the text color for filter lists.
Calendars
Sign in
Large inputs for auth and onboarding, with a "or" separator before the other sign-in option.
Disabled and locked
FieldsDisabled locks every field below it while a form saves. A single Field can also be disabled with a reason in its helper.
Screen reader labels
When a nearby heading already names the rows, labels and legends can be read by screen readers only. The required or optional marker stays in the description.
Invite by email
GitHub knowledge
Repositories
RequiredCustom and wrapped controls
Any control that calls useFieldControl joins the field, even inside a wrapper. Fields without an id generate one.
58 / 2,000
182 / 2,000
Usage
import {
CheckboxField,
ChoiceGroup,
Field,
FieldSeparator,
FieldsDisabled,
} from "@tulipfarm/sprout/field";
import { Input } from "@tulipfarm/sprout/input";
<FieldsDisabled value={saving}>
<form noValidate onSubmit={save}>
<Field label="Name" required error={errors.name}>
<Input value={name} onChange={(event) => setName(event.target.value)} />
</Field>
<Field
label="Instructions"
required
counter="120 / 8,000"
helper="Tell the agent what to do."
>
<Textarea rows={4} maxLength={8000} />
</Field>
<ChoiceGroup
type="radio"
legend="Repeat"
required
layout="columns"
options={[
{ value: "daily", label: "Every day", description: "Runs at 9:00 AM" },
{ value: "weekly", label: "Every week" },
]}
value={repeat}
onValueChange={setRepeat}
/>
<CheckboxField
label="Notify me when it runs"
checked={notify}
onCheckedChange={setNotify}
/>
<FieldSeparator>or</FieldSeparator>
</form>
</FieldsDisabled>Every Field, ChoiceGroup, and CheckboxField says whether it isrequired or optional; there is no default. The marker sits next to the label. Pass id to name the control, or leave it out to generate one.
Field
label names the control. labelVisibility is"visible" (default) or "screen-reader", which keeps the label, marker, and counter for assistive tech only.helper is a line of help below the control.error replaces it and marks the control invalid.counter is short status at the end of the label row, such as"120 / 8,000". disabled dims the label and disables the control.
The child is one control, or one element that wraps it. SprõutInput and SelectTrigger join the field on their own. For any other control, call useFieldControl(props) and spread the result on the focusable element:
function Textarea(rest: ComponentProps<"textarea">) {
const props = useFieldControl(rest);
return <textarea {...props} className="my-textarea" />;
}It adds the field's id, disabled,aria-describedby, aria-invalid, andaria-required. Outside a Field, or when the control has its ownaria-label or aria-labelledby, props pass through unchanged.
ChoiceGroup
type="radio" picks one value and type="checkbox"picks many. options are { value, label, description?, disabled? }. layout is "list" (default) or"columns", two columns from 640px. It also takeslegend, legendVisibility, helper,error, disabled, id, andwarning, a badge after the legend such as "Changes data".
CheckboxField
One checkbox with a label and a helper or error line. variantis "default", filled with the primary color, or"neutral", filled with the text color for filter lists. Theid goes on the wrapper, so a form can scroll to it and focus the checkbox inside.
FieldSeparator and FieldsDisabled
FieldSeparator is a labeled Separator that pulls in the stack gap, so the line sits evenly between fields. Text such as "or" is optional. FieldsDisabled is a context provider:<FieldsDisabled value={saving}> disables every field below it.
Accessibility
Labels point at their control with for. The control is described by the error or helper, then the optional marker, then the counter. Required controls get aria-required; invalid ones getaria-invalid. A checkbox group is a labelledrole="group"; a radio group is a Base UI radiogroup with arrow key movement. Each option row is clickable, and option descriptions describe their checkbox or radio.
Checkboxes and radios show a 3px focus ring. Rows are at least 32px tall, and 44px on touch screens. Colors change in 120ms and stop when reduced motion is requested. Forced colors mode uses system colors.
Theme tokens
Labels use --sprout-text; markers, counters, and helpers use--sprout-muted; errors use --sprout-danger. Checked boxes use --sprout-primary and--sprout-on-primary, and checked rows use--sprout-primary-subtle. Focus rings use--sprout-focus.