labelindeterminatedisabledreadOnlyimport { Checkbox } from "@/components/ui/checkbox"
export function TermsCheckbox() {
return (
<div className="flex items-center space-x-2">
<Checkbox id="terms" />
<label
htmlFor="terms"
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
>
Accept terms and conditions
</label>
</div>
)
}Usage
import { Checkbox } from "@/components/ui/checkbox"
export function TermsCheckbox() {
return (
<div className="flex items-center gap-2">
<Checkbox id="terms" name="terms" />
<label htmlFor="terms" className="text-sm">
Accept terms and conditions
</label>
</div>
)
}Examples
import { Checkbox } from '@/components/ui/checkbox';
export function Example() {
return (
<div className="flex items-center space-x-2">
<Checkbox id="terms" />
<label htmlFor="terms" className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
Accept terms and conditions
</label>
</div>
);
}import React from 'react';
import { Checkbox } from '@/components/ui/checkbox';
export function Example() {
const [checked, setChecked] = React.useState(false);
const [indeterminate, setIndeterminate] = React.useState('indeterminate');
return (
<div className="space-y-4">
<div className="flex items-center space-x-2">
<Checkbox checked={checked} onCheckedChange={setChecked} />
<label className="text-sm font-medium">Controlled checkbox</label>
</div>
<div className="flex items-center space-x-2">
<Checkbox checked={indeterminate} onCheckedChange={setIndeterminate} />
<label className="text-sm font-medium">Indeterminate checkbox</label>
</div>
<div className="flex items-center space-x-2">
<Checkbox disabled />
<label className="text-sm font-medium text-muted-foreground">Disabled checkbox</label>
</div>
</div>
);
}Notes
Labels and peer classes
The box sits in a wrapper span with the hidden input. A <label htmlFor> beside it works, but Tailwind peer-* classes on that label cannot see the box: the label is a sibling of the wrapper, not the box.
In a group
Inside a CheckboxGroup, give each Checkbox a value. The group's value decides which are ticked, and changes go through the group's onValueChange instead of onCheckedChange.
API
details.cancel() refuses the change.checked as "mixed".CheckboxGroup; submitted when ticked.CheckboxGroup. Needs allValues on the group.<label htmlFor> points at.Agent
Inside an AgentProvider, it appears in agent.snapshot() as checkbox.
Reads
labelThe label pointing at it, or its aria-label.
checkedtrue, false, or "mixed" when indeterminate.
disabledTrue when the checkbox is blocked.
Actions
toggle()Ticks it, or clears it when ticked. A mixed box becomes ticked.
setChecked(true)Sets the state. Setting the current state succeeds as a no-op.
An agent's change runs through onCheckedChange like anyone's. isAgentChange(details) is true for it; details.cancel() refuses it and the agent is told the app refused. In a group it goes through the group's onValueChange instead. A disabled or readOnly checkbox refuses on its own.
await agent.act({
steps: [{ ref: "Accept terms", action: "setChecked", args: [true] }],
})Accessibility
- The box has role="checkbox" with aria-checked set to true, false or mixed.
- Name it with a
<label htmlFor>on its id, or aria-label. The agent reads it too. - A hidden input submits the value with a native form.