Checkbox

One yes or no, ticked by a person or an agent.

Source
$npx @dinachi/cli@latest add checkbox
label
indeterminate
disabled
readOnly

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

A checkbox named by its label.
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>
  );
}

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

checkedboolean
Controlled checked state.
defaultCheckedboolean = false
Starting state when uncontrolled.
onCheckedChange
agent
(checked, details) => void
Runs for people and agents. details.cancel() refuses the change.
indeterminate
agent
boolean = false
Shows the mixed state. The agent reads checked as "mixed".
disabled
agent
boolean = false
Blocks people and agents.
readOnly
agent
boolean = false
Shows the state, refuses any change.
requiredboolean = false
Must be ticked before the form submits.
namestring
Submits the checkbox with a form.
value
agent
string
Identifies it in a CheckboxGroup; submitted when ticked.
parent
agent
boolean = false
Ticks or clears every checkbox in its CheckboxGroup. Needs allValues on the group.
idstring
The id a <label htmlFor> points at.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as checkbox.

Reads

label

The label pointing at it, or its aria-label.

checked

true, false, or "mixed" when indeterminate.

disabled

True 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.