shadcn.io is not affiliated with official shadcn/ui
Checkbox examples · Standard
Todo Style Checkbox for React
A to-do item whose label is struck through and muted once its checkbox is checked.
To-do lists live on one interaction: tick a task, see it done. The checkbox here is controlled, and its checked state drives the label's classes: once checked, line-through and text-muted-foreground strike the task out and fade it, with transition-colors easing the fade. The task stays in place, so people can see what they finished and untick it. Use it for checklists, task lists and shopping lists.
Preview
Installation
FAQ
How does the strikethrough styling work?
The checked value lives in state, and cn() adds line-through text-muted-foreground to the FieldLabel when it's true. Ticking the box updates the state, and the label restyles on the next render.
The state is updated in onCheckedChange.
Can I customize the completion animation?
transition-colors fades the color; change the speed with duration-300 or duration-75. The line itself can't animate with a plain class, so keep the effect short: completion should feel instant.
Does the task text stay when the box is checked?
Yes. The label stays in place. While checked is true, line-through and text-muted-foreground apply. Clearing the box drops those classes, because cn() adds them only for that state.
Why is this checkbox controlled?
The label has to read the same value as the box. This demo keeps checked in state, starts it at false, and passes it through. An uncontrolled box would update itself and the label would not know.
Related
- Simple CheckboxA checkbox with a label for a single
- Vertical List CheckboxA stacked list of programming language
- Nested List CheckboxA select-all checkbox over three indented
- Indeterminate CheckboxA checkbox that starts indeterminate, the
- Disabled CheckboxTwo disabled checkboxes, one unchecked
- With Description CheckboxA checkbox with a label and a line of