Components · Forms
Field
A label, a control, a line of help and the error message, wired with aria-describedby and aria-invalid; a fieldset for a set of checkboxes.
47% of the 2,189 real sites we studied have a form (1,019 sites).
Lowercase letters, numbers and dashes.
We send the receipt here.
Enter an email address with an @, like ada@example.com.
Your plan has no seats left.
Lowercase letters, numbers and dashes.
We send the receipt here.
Enter an email address with an @, like ada@example.com.
Your plan has no seats left.
Both previews render the same markup; the HTML tab below is that markup without a framework.
Install
- React
- npx shadcn@
latest add https:// www.themes.dev/ ui/ r/ field.json - Astro
- npx shadcn@
latest add https:// www.themes.dev/ ui/ r/ astro/ field.json
Or copy the files below into your project. They need clsx, tailwind-merge, and the themes.dev UI theme. The HTML needs only the compiled CSS.
import type { ReactNode } from 'react';
import { cn } from '@/lib/utils';
import { labelClasses } from '../label/label.variants';
import { describedBy, field, fieldControl, type FieldTexts } from './field.variants';
export interface FieldProps extends FieldTexts {
label: string;
/** Adds "(optional)" to the label. Mark the few optional fields, not the many required ones. */
optional?: boolean;
/** A fieldset with a legend, for a set of checkboxes or switches; `id` then only names the texts. */
group?: boolean;
/** The control: `<Input {...fieldControl({ id, description, error })} />`. */
children: ReactNode;
className?: string;
}
/** A label, a control, a line of help and the error, wired together. */
export function Field({ id, label, description, error, optional, group, children, className }: FieldProps) {
const invalid = error ? '' : undefined;
if (group)
return (
<fieldset aria-describedby={describedBy({ id, description, error })} data-invalid={invalid} className={cn(field.group, className)}>
<legend className={field.legend}>{label}{optional && <span className={field.optional}> (optional)</span>}</legend>
<div className={field.options}>{children}</div>
{description && <p id={`${id}-description`} className={field.description}>{description}</p>}
{error && <p id={`${id}-error`} className={field.error}>{error}</p>}
</fieldset>
);
return (
<div data-invalid={invalid} className={cn(field.root, className)}>
<label htmlFor={id} className={labelClasses}>{label}{optional && <span className={field.optional}>(optional)</span>}</label>
{children}
{description && <p id={`${id}-description`} className={field.description}>{description}</p>}
{error && <p id={`${id}-error`} className={field.error}>{error}</p>}
</div>
);
}
export { field as fieldClasses, fieldControl };/** A field: the label, the control, a line of help and the error, wired together. The control is yours (Input,
* Textarea, Select, a date field); spread `fieldControl()` on it so its id, aria-describedby and aria-invalid match
* the texts. `group` makes it a fieldset with a legend, for a set of checkboxes or switches. No JavaScript. */
export const field = {
root: 'grid min-w-0 gap-2',
// "(optional)" is part of the label, so it is read with it; the minority is marked, in words.
optional: 'font-normal text-muted-foreground',
description: 'text-sm text-pretty text-muted-foreground',
// The message is the error; the colour only repeats it.
error: 'text-sm font-medium text-pretty text-destructive',
group: 'm-0 grid min-w-0 gap-2 border-0 p-0',
legend: 'mb-3 p-0 text-sm leading-none font-medium',
options: 'grid gap-3',
};
export type FieldTexts = {
/** The control's id; the texts get `<id>-description` and `<id>-error`. Unique on the page. */
id: string;
description?: string;
/** The error message. With it the control is invalid. */
error?: string;
};
/** The ids of the texts that describe the control: the error first, then the help. */
export const describedBy = ({ id, description, error }: FieldTexts) =>
[error && `${id}-error`, description && `${id}-description`].filter(Boolean).join(' ') || undefined;
/** Props for the control inside a Field: `<Input {...fieldControl({ id, description, error })} />`. */
export const fieldControl = (texts: FieldTexts) => ({
id: texts.id,
'aria-describedby': describedBy(texts),
'aria-invalid': texts.error ? ('true' as const) : undefined,
});---
import { cn } from '@/lib/utils';
import { labelClasses } from '../label/label.variants';
import { describedBy, field, type FieldTexts } from './field.variants';
type Props = FieldTexts & {
label: string;
/** Adds "(optional)" to the label. Mark the few optional fields, not the many required ones. */
optional?: boolean;
/** A fieldset with a legend, for a set of checkboxes or switches; `id` then only names the texts. */
group?: boolean;
class?: string;
};
const { id, label, description, error, optional, group, class: className } = Astro.props;
const invalid = error ? '' : undefined;
---
{
group ? (
<fieldset aria-describedby={describedBy({ id, description, error })} data-invalid={invalid} class={cn(field.group, className)}>
<legend class={field.legend}>{label}{optional && <span class={field.optional}> (optional)</span>}</legend>
<div class={field.options}><slot /></div>
{description && <p id={`${id}-description`} class={field.description}>{description}</p>}
{error && <p id={`${id}-error`} class={field.error}>{error}</p>}
</fieldset>
) : (
<div data-invalid={invalid} class={cn(field.root, className)}>
<label for={id} class={labelClasses}>{label}{optional && <span class={field.optional}>(optional)</span>}</label>
<slot />
{description && <p id={`${id}-description`} class={field.description}>{description}</p>}
{error && <p id={`${id}-error`} class={field.error}>{error}</p>}
</div>
)
}/** A field: the label, the control, a line of help and the error, wired together. The control is yours (Input,
* Textarea, Select, a date field); spread `fieldControl()` on it so its id, aria-describedby and aria-invalid match
* the texts. `group` makes it a fieldset with a legend, for a set of checkboxes or switches. No JavaScript. */
export const field = {
root: 'grid min-w-0 gap-2',
// "(optional)" is part of the label, so it is read with it; the minority is marked, in words.
optional: 'font-normal text-muted-foreground',
description: 'text-sm text-pretty text-muted-foreground',
// The message is the error; the colour only repeats it.
error: 'text-sm font-medium text-pretty text-destructive',
group: 'm-0 grid min-w-0 gap-2 border-0 p-0',
legend: 'mb-3 p-0 text-sm leading-none font-medium',
options: 'grid gap-3',
};
export type FieldTexts = {
/** The control's id; the texts get `<id>-description` and `<id>-error`. Unique on the page. */
id: string;
description?: string;
/** The error message. With it the control is invalid. */
error?: string;
};
/** The ids of the texts that describe the control: the error first, then the help. */
export const describedBy = ({ id, description, error }: FieldTexts) =>
[error && `${id}-error`, description && `${id}-description`].filter(Boolean).join(' ') || undefined;
/** Props for the control inside a Field: `<Input {...fieldControl({ id, description, error })} />`. */
export const fieldControl = (texts: FieldTexts) => ({
id: texts.id,
'aria-describedby': describedBy(texts),
'aria-invalid': texts.error ? ('true' as const) : undefined,
});<div class="grid w-full max-w-sm gap-6">
<div class="grid min-w-0 gap-2">
<label for="ex-field-slug" class="flex items-center gap-2 text-sm leading-none font-medium has-[input]:items-start has-[input]:leading-snug has-[input]:[&>*:first-child]:mt-px select-none peer-disabled:cursor-not-allowed peer-disabled:opacity-50">
Project slug
</label>
<input type="text" class="w-full min-w-0 rounded-md border border-input bg-background px-3 text-sm text-foreground transition-colors placeholder:text-muted-foreground enabled:hover:border-foreground/85 enabled:active:bg-muted aria-invalid:enabled:hover:border-destructive aria-invalid:enabled:hover:bg-[color-mix(in_oklab,var(--destructive)_6%,var(--background))] aria-invalid:enabled:active:bg-[color-mix(in_oklab,var(--destructive)_10%,var(--background))] focus-visible:border-ring focus-visible:outline-2 focus-visible:outline-offset-0 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:outline-destructive h-9 py-1 file:mr-3 file:border-0 file:bg-transparent file:text-sm file:font-medium" id="ex-field-slug" aria-describedby="ex-field-slug-description" name="slug" value="caseroom-docs" autocomplete="off">
<p id="ex-field-slug-description" class="text-sm text-pretty text-muted-foreground">Lowercase letters, numbers and dashes.</p>
</div>
<div data-invalid class="grid min-w-0 gap-2">
<label for="ex-field-email" class="flex items-center gap-2 text-sm leading-none font-medium has-[input]:items-start has-[input]:leading-snug has-[input]:[&>*:first-child]:mt-px select-none peer-disabled:cursor-not-allowed peer-disabled:opacity-50">
Email
</label>
<input type="email" class="w-full min-w-0 rounded-md border border-input bg-background px-3 text-sm text-foreground transition-colors placeholder:text-muted-foreground enabled:hover:border-foreground/85 enabled:active:bg-muted aria-invalid:enabled:hover:border-destructive aria-invalid:enabled:hover:bg-[color-mix(in_oklab,var(--destructive)_6%,var(--background))] aria-invalid:enabled:active:bg-[color-mix(in_oklab,var(--destructive)_10%,var(--background))] focus-visible:border-ring focus-visible:outline-2 focus-visible:outline-offset-0 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:outline-destructive h-9 py-1 file:mr-3 file:border-0 file:bg-transparent file:text-sm file:font-medium" id="ex-field-email" aria-describedby="ex-field-email-error ex-field-email-description" aria-invalid="true" name="email" value="ada.example.com" autocomplete="email">
<p id="ex-field-email-description" class="text-sm text-pretty text-muted-foreground">We send the receipt here.</p>
<p id="ex-field-email-error" class="text-sm font-medium text-pretty text-destructive">Enter an email address with an @, like ada@example.com.</p>
</div>
<div class="grid min-w-0 gap-2">
<label for="ex-field-note" class="flex items-center gap-2 text-sm leading-none font-medium has-[input]:items-start has-[input]:leading-snug has-[input]:[&>*:first-child]:mt-px select-none peer-disabled:cursor-not-allowed peer-disabled:opacity-50">
Note for the team
<span class="font-normal text-muted-foreground">(optional)</span>
</label>
<textarea class="w-full min-w-0 rounded-md border border-input bg-background px-3 text-sm text-foreground transition-colors placeholder:text-muted-foreground enabled:hover:border-foreground/85 enabled:active:bg-muted aria-invalid:enabled:hover:border-destructive aria-invalid:enabled:hover:bg-[color-mix(in_oklab,var(--destructive)_6%,var(--background))] aria-invalid:enabled:active:bg-[color-mix(in_oklab,var(--destructive)_10%,var(--background))] focus-visible:border-ring focus-visible:outline-2 focus-visible:outline-offset-0 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:outline-destructive min-h-20 py-2 [field-sizing:content]" id="ex-field-note" name="note" rows="3">
</textarea>
</div>
<div class="grid min-w-0 gap-2">
<label for="ex-field-seats" class="flex items-center gap-2 text-sm leading-none font-medium has-[input]:items-start has-[input]:leading-snug has-[input]:[&>*:first-child]:mt-px select-none peer-disabled:cursor-not-allowed peer-disabled:opacity-50">
Seats
</label>
<input type="number" class="w-full min-w-0 rounded-md border border-input bg-background px-3 text-sm text-foreground transition-colors placeholder:text-muted-foreground enabled:hover:border-foreground/85 enabled:active:bg-muted aria-invalid:enabled:hover:border-destructive aria-invalid:enabled:hover:bg-[color-mix(in_oklab,var(--destructive)_6%,var(--background))] aria-invalid:enabled:active:bg-[color-mix(in_oklab,var(--destructive)_10%,var(--background))] focus-visible:border-ring focus-visible:outline-2 focus-visible:outline-offset-0 disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive aria-invalid:outline-destructive h-9 py-1 file:mr-3 file:border-0 file:bg-transparent file:text-sm file:font-medium" id="ex-field-seats" aria-describedby="ex-field-seats-description" name="seats" value="5" disabled>
<p id="ex-field-seats-description" class="text-sm text-pretty text-muted-foreground">Your plan has no seats left.</p>
</div>
<fieldset aria-describedby="ex-field-updates-description" class="m-0 grid min-w-0 gap-2 border-0 p-0">
<legend class="mb-3 p-0 text-sm leading-none font-medium">Email me about</legend>
<div class="grid gap-3">
<label class="flex items-center gap-2 text-sm leading-none font-medium has-[input]:items-start has-[input]:leading-snug has-[input]:[&>*:first-child]:mt-px select-none peer-disabled:cursor-not-allowed peer-disabled:opacity-50">
<span class="relative inline-flex size-4 shrink-0">
<input type="checkbox" class="peer size-4 shrink-0 cursor-pointer appearance-none rounded-[4px] border border-input bg-background transition-[color,background-color,border-color,transform] enabled:not-checked:hover:border-foreground enabled:not-checked:hover:bg-accent enabled:active:scale-90 enabled:not-checked:active:bg-pressed checked:border-primary checked:enabled:hover:border-[color-mix(in_oklab,var(--primary)_var(--hover-amount),var(--background))] checked:enabled:hover:bg-[color-mix(in_oklab,var(--primary)_var(--hover-amount),var(--background))] checked:enabled:active:border-[color-mix(in_oklab,var(--primary)_var(--press-amount),var(--press-mix))] checked:enabled:active:bg-[color-mix(in_oklab,var(--primary)_var(--press-amount),var(--press-mix))] checked:bg-primary indeterminate:border-primary indeterminate:bg-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive" name="updates" value="releases" checked>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="pointer-events-none absolute inset-0 hidden size-4 text-primary-foreground peer-checked:block"><path d="M3.5 8.5l3 3 6-7"></path></svg>
</span>
New releases
</label>
<label class="flex items-center gap-2 text-sm leading-none font-medium has-[input]:items-start has-[input]:leading-snug has-[input]:[&>*:first-child]:mt-px select-none peer-disabled:cursor-not-allowed peer-disabled:opacity-50">
<span class="relative inline-flex size-4 shrink-0">
<input type="checkbox" class="peer size-4 shrink-0 cursor-pointer appearance-none rounded-[4px] border border-input bg-background transition-[color,background-color,border-color,transform] enabled:not-checked:hover:border-foreground enabled:not-checked:hover:bg-accent enabled:active:scale-90 enabled:not-checked:active:bg-pressed checked:border-primary checked:enabled:hover:border-[color-mix(in_oklab,var(--primary)_var(--hover-amount),var(--background))] checked:enabled:hover:bg-[color-mix(in_oklab,var(--primary)_var(--hover-amount),var(--background))] checked:enabled:active:border-[color-mix(in_oklab,var(--primary)_var(--press-amount),var(--press-mix))] checked:enabled:active:bg-[color-mix(in_oklab,var(--primary)_var(--press-amount),var(--press-mix))] checked:bg-primary indeterminate:border-primary indeterminate:bg-primary disabled:cursor-not-allowed disabled:opacity-50 aria-invalid:border-destructive" name="updates" value="security">
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class="pointer-events-none absolute inset-0 hidden size-4 text-primary-foreground peer-checked:block"><path d="M3.5 8.5l3 3 6-7"></path></svg>
</span>
Security notices
</label>
</div>
<p id="ex-field-updates-description" class="text-sm text-pretty text-muted-foreground">One email at most per week.</p>
</fieldset>
</div>