Checkbox
Also called check box or tick box
A small square you tick to turn an option on or select an item, with a dash in a parent box when only some items are checked.
import { useState } from 'react';
import { Checkbox } from '@base-ui/react/checkbox';
import { CheckboxGroup } from '@base-ui/react/checkbox-group';
const FILES = ['Q3 report.pdf', 'Brand guidelines.fig', 'Launch plan.docx'];
const BOX =
'flex size-5 items-center justify-center rounded-md border bg-background text-white outline-none focus-visible:ring-2 focus-visible:ring-ring data-checked:border-blue-600 data-checked:bg-blue-600 data-indeterminate:border-blue-600 data-indeterminate:bg-blue-600';
// pathLength={1} makes a dash offset of 1 hide the stroke and 0 draw all of it.
const DRAW =
'[stroke-dasharray:1] [stroke-dashoffset:1] transition-[stroke-dashoffset] duration-180 ease-out group-data-checked:[stroke-dashoffset:0] group-data-indeterminate:hidden';
export function FileSelection() {
const [selected, setSelected] = useState<string[]>([]);
return (
<CheckboxGroup
value={selected}
onValueChange={setSelected}
allValues={FILES}
aria-label='Files'
className='w-80 overflow-hidden rounded-xl border text-sm *:flex *:items-center *:gap-3 *:px-4 *:py-2.5 *:not-first:has-[[data-checked]]:bg-blue-500/10'
>
<label className='border-b bg-muted font-medium'>
{/* `parent` ticks every file, and shows a dash when only some are ticked. */}
<Box parent />
{selected.length ? `${selected.length} selected` : 'Select all'}
</label>
{FILES.map((file) => (
<label key={file}>
<Box value={file} />
{file}
</label>
))}
</CheckboxGroup>
);
}
function Box(props: Checkbox.Root.Props) {
return (
<Checkbox.Root {...props} className={BOX}>
<Checkbox.Indicator keepMounted className='group'>
<svg
viewBox='0 0 16 16'
className='size-3.5 fill-none stroke-current stroke-2'
>
<path d='M4 8h8' className='hidden group-data-indeterminate:block' />
<path d='m3.5 8.5 3 3 6-7' pathLength={1} className={DRAW} />
</svg>
</Checkbox.Indicator>
</Checkbox.Root>
);
}Add a checkbox to each file row and a Select all checkbox in the header that shows a dash when only some rows are checked. Draw the checkmark in with an SVG stroke animation over 180ms with an ease-out curve, tint checked rows light blue, show an 'N selected' count, and make the whole row clickable with a visible focus ring.
Paste into Layerwise or any AI coding tool.
What is Checkbox?
A checkbox toggles one independent choice: accept the terms, select a file, include weekends. Each box works on its own, so people can check none, one, or all of them. A Select all box above a list shows a third, indeterminate state, drawn as a dash, when only some rows are checked.
Use checkboxes for selecting several items and for options that take effect when someone presses Submit or Save. For a single setting that applies instantly, a toggle switch is clearer, and for picking exactly one option, use radio buttons.
Make the label clickable along with the box, keep the target at least 24px, and show a clear focus ring. The indeterminate state can only be set from JavaScript, through the indeterminate property. A checkmark that draws itself in over about 150 to 200ms, using an SVG stroke animation, makes ticking feel satisfying without slowing anyone down.
Often confused with
- Toggle switch
- A toggle switch applies one setting the moment you flip it. Checkboxes suit selecting several items or choices that apply when a form is submitted.
- Radio button
- Radio buttons allow exactly one choice in a group. Checkboxes allow any number, including none.
Related terms
All termsToggle switch
An on/off control shaped like a small track with a sliding knob, used for settings that take effect immediately.
Radio button
A round option in a group where choosing one clears the others, so people pick exactly one answer, like a plan or a shipping speed.
SVG line drawing
An animation that makes an SVG outline look like it is being drawn by hand, by revealing its stroke from start to end.
Data table
A table built for working with records: click a column header to sort, type to filter, and check rows to act on several at once.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.