Checkbox
Choices that wait to be submitted: single boxes, whole cards, to-do items and compact chips.
Standard
A choice that waits to be submitted. The check draws in, and a parent can show a partial selection.
Installation
$npx shadcn@latest add @stillui/checkboxOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/checkbox
import { motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const PRESS = { type: "spring", stiffness: 700, damping: 40, mass: 0.6 } as const;
const DRAW = { duration: 0.2, ease: [0.16, 1, 0.3, 1] } as const;
export type CheckedState = boolean | "indeterminate";
type CheckboxProps = {
checked?: CheckedState;
defaultChecked?: CheckedState;
onCheckedChange?: (checked: boolean) => void;
disabled?: boolean;
label?: ReactNode;
description?: ReactNode;
className?: string;
id?: string;
};
export function Checkbox({
checked,
defaultChecked = false,
onCheckedChange,
disabled,
label,
description,
className,
id,
}: CheckboxProps) {
const reduce = useReducedMotion();
const [internal, setInternal] = useState<CheckedState>(defaultChecked);
const controlled = checked !== undefined;
const state = controlled ? checked : internal;
const filled = state !== false;
const autoId = useId();
const boxId = id ?? autoId;
const descId = description ? `${boxId}-desc` : undefined;
// Indeterminate moves to checked, like native checkboxes.
const toggle = () => {
if (disabled) return;
const next = state !== true;
if (!controlled) setInternal(next);
onCheckedChange?.(next);
};
return (
<div className={cn("flex items-start gap-3", disabled && "opacity-50", className)}>
<motion.button
id={boxId}
type="button"
role="checkbox"
aria-checked={state === "indeterminate" ? "mixed" : state}
aria-describedby={descId}
disabled={disabled}
onClick={toggle}
whileTap={reduce || disabled ? undefined : { scale: 0.88 }}
transition={PRESS}
className={cn(
"mt-0.5 grid size-[18px] shrink-0 place-items-center rounded-[5px] transition-colors duration-150",
"disabled:cursor-not-allowed",
filled ? "bg-primary text-primary-foreground" : "bg-card shadow-[inset_0_0_0_1.5px_var(--input)]",
)}
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="size-3.5" aria-hidden>
{state === "indeterminate" ? (
<motion.path d="M4 8h8" initial={{ pathLength: reduce ? 1 : 0 }} animate={{ pathLength: 1 }} transition={DRAW} />
) : (
<motion.path
d="M3.5 8.5l3 3 6-7"
initial={false}
animate={{ pathLength: state ? 1 : 0, opacity: state ? 1 : 0 }}
transition={reduce ? { duration: 0 } : DRAW}
/>
)}
</svg>
</motion.button>
{label || description ? (
<div className="grid gap-0.5">
{label ? (
<label htmlFor={boxId} className="cursor-pointer text-sm font-medium leading-6">
{label}
</label>
) : null}
{description ? (
<p id={descId} className="text-[13px] leading-5 text-muted-foreground">
{description}
</p>
) : null}
</div>
) : null}
</div>
);
}States
- Unchecked
- An outlined box on the card surface.
- Checked
- Fills with the primary color while the check draws in.
- Indeterminate
- A dash for “some selected”. Announced as mixed. Clicking selects all.
- Pressed
- Box shrinks slightly under the pointer.
- Focus
- Outline from the ring token. Space toggles.
- Disabled
- Box and label dim together.
Notes
Use a checkbox when the choice is applied later by a Save or Submit button. For settings that apply at once, use a switch.
Card
A whole card that toggles, for choices that need an icon and a sentence of explanation.
Installation
$npx shadcn@latest add @stillui/checkbox-cardOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/checkbox#card
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const PRESS = { type: "spring", stiffness: 600, damping: 38 } as const;
type CheckboxCardProps = {
title: ReactNode;
description?: ReactNode;
icon?: ReactNode;
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
disabled?: boolean;
className?: string;
};
/** A whole card that toggles, for choices that need a sentence of explanation. */
export function CheckboxCard({
title,
description,
icon,
checked,
defaultChecked = false,
onCheckedChange,
disabled,
className,
}: CheckboxCardProps) {
const reduce = useReducedMotion();
const [internal, setInternal] = useState(defaultChecked);
const on = checked ?? internal;
const toggle = () => {
if (checked === undefined) setInternal(!on);
onCheckedChange?.(!on);
};
return (
<motion.button
type="button"
role="checkbox"
aria-checked={on}
disabled={disabled}
onClick={toggle}
whileTap={reduce || disabled ? undefined : { scale: 0.985 }}
transition={PRESS}
className={cn(
"relative flex w-full items-start gap-3 rounded-xl bg-card p-4 text-left transition-shadow duration-200",
"disabled:cursor-not-allowed disabled:opacity-50",
on ? "shadow-[inset_0_0_0_2px_var(--primary)]" : "shadow-raised hover:shadow-[inset_0_0_0_1px_var(--input)]",
className,
)}
>
{icon ? (
<span className="grid size-9 shrink-0 place-items-center rounded-lg bg-secondary text-foreground [&_svg]:size-[18px]" aria-hidden>
{icon}
</span>
) : null}
<span className="grid min-w-0 flex-1 gap-0.5 pr-7">
<span className="text-sm font-medium leading-5">{title}</span>
{description ? <span className="text-[13px] leading-5 text-muted-foreground">{description}</span> : null}
</span>
<span
className={cn(
"absolute right-4 top-4 grid size-5 place-items-center rounded-full transition-colors duration-150",
on ? "bg-primary text-primary-foreground" : "shadow-[inset_0_0_0_1.5px_var(--input)]",
)}
aria-hidden
>
<AnimatePresence initial={false}>
{on ? (
<motion.svg
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="size-3"
initial={{ scale: reduce ? 1 : 0.4, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: reduce ? 1 : 0.4, opacity: 0 }}
transition={reduce ? { duration: 0 } : PRESS}
>
<path d="M3.5 8.5l3 3 6-7" />
</motion.svg>
) : null}
</AnimatePresence>
</span>
</motion.button>
);
}States
- Unchecked
- A raised card with an empty circle in the corner.
- Hover
- A thin outline shows the whole card is clickable.
- Checked
- A primary outline and a filled circle with a check that pops in.
- Press
- The card dips slightly so the tap feels received.
- Disabled
- Faded and not clickable.
- Keyboard
- One tab stop per card, toggled with Space, announced as a checkbox.
Notes
Group cards in a fieldset with a legend so screen readers hear what the choices are for.
Todo
A round checkbox for task lists. Checking it draws a line through the text, which then steps back.
Installation
$npx shadcn@latest add @stillui/checkbox-todoOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/checkbox#todo
import { motion, useReducedMotion } from "motion/react";
import { useId, useState, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const PRESS = { type: "spring", stiffness: 700, damping: 40, mass: 0.6 } as const;
const STRIKE = { duration: 0.28, ease: [0.65, 0, 0.35, 1] } as const;
type CheckboxTodoProps = {
children: ReactNode;
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (checked: boolean) => void;
className?: string;
};
/** A to-do item. Checking it draws a line through the text, which then steps back. */
export function CheckboxTodo({ children, checked, defaultChecked = false, onCheckedChange, className }: CheckboxTodoProps) {
const reduce = useReducedMotion();
const [internal, setInternal] = useState(defaultChecked);
const on = checked ?? internal;
const id = useId();
const toggle = () => {
if (checked === undefined) setInternal(!on);
onCheckedChange?.(!on);
};
return (
<div className={cn("flex items-center gap-3", className)}>
<motion.button
id={id}
type="button"
role="checkbox"
aria-checked={on}
onClick={toggle}
whileTap={reduce ? undefined : { scale: 0.85 }}
transition={PRESS}
className={cn(
"grid size-5 shrink-0 place-items-center rounded-full transition-colors duration-200",
on ? "bg-primary text-primary-foreground" : "shadow-[inset_0_0_0_1.5px_var(--input)] hover:shadow-[inset_0_0_0_1.5px_var(--muted-foreground)]",
)}
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="size-3" aria-hidden>
<motion.path
d="M3.5 8.5l3 3 6-7"
initial={false}
animate={{ pathLength: on ? 1 : 0, opacity: on ? 1 : 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.2 }}
/>
</svg>
</motion.button>
<label htmlFor={id} className="relative cursor-pointer text-sm leading-6">
<span className={cn("transition-colors duration-300", on ? "text-muted-foreground" : "text-foreground")}>{children}</span>
{/* The strike is drawn from left to right, then the text fades. */}
<motion.span
aria-hidden
className="absolute inset-x-0 top-1/2 h-px bg-muted-foreground"
style={{ originX: 0 }}
initial={false}
animate={{ scaleX: on ? 1 : 0 }}
transition={reduce ? { duration: 0 } : STRIKE}
/>
</label>
</div>
);
}States
- Open
- An empty circle and the task in full color.
- Done
- The circle fills, a check draws in, and a line is drawn through the task from left to right.
- Undo
- Unchecking erases the line in reverse and brings the text back.
- Keyboard
- Tab to the circle and press Space. The task text is its label.
- Reduced motion
- The check and line appear without drawing.
Notes
The line runs through the middle of the label, so keep each task to a single line.
Chips
Several choices as compact chips, for filters and preferences. A check slides in on the ones that are on.
Cuisines
Installation
$npx shadcn@latest add @stillui/checkbox-chipsOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/checkbox#chips
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useState } from "react";
import { cn } from "@/lib/utils";
const SPRING = { type: "spring", stiffness: 500, damping: 36 } as const;
export type ChipOption = { value: string; label: string };
type CheckboxChipsProps = {
/** Names the group for screen readers. */
label: string;
options: ChipOption[];
value?: string[];
defaultValue?: string[];
onValueChange?: (value: string[]) => void;
className?: string;
};
/** Several choices as compact chips. A check slides in on the ones that are on. */
export function CheckboxChips({ label, options, value, defaultValue = [], onValueChange, className }: CheckboxChipsProps) {
const reduce = useReducedMotion();
const [internal, setInternal] = useState(defaultValue);
const selected = value ?? internal;
const transition = reduce ? { duration: 0 } : SPRING;
const toggle = (option: string) => {
const next = selected.includes(option) ? selected.filter((v) => v !== option) : [...selected, option];
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
return (
<div role="group" aria-label={label} className={cn("flex flex-wrap gap-2", className)}>
{options.map((option) => {
const on = selected.includes(option.value);
return (
<motion.button
key={option.value}
type="button"
role="checkbox"
aria-checked={on}
onClick={() => toggle(option.value)}
layout={!reduce}
transition={transition}
style={{ borderRadius: 999 }}
className={cn(
"inline-flex h-8 items-center px-3 text-[13px] font-medium transition-[color,background-color,box-shadow,scale] active:scale-[0.95] duration-200",
on ? "bg-primary text-primary-foreground" : "bg-secondary text-secondary-foreground hover:bg-secondary/70",
)}
>
<AnimatePresence initial={false}>
{on ? (
<motion.span
className="overflow-hidden"
initial={{ width: 0, opacity: 0 }}
animate={{ width: "auto", opacity: 1 }}
exit={{ width: 0, opacity: 0 }}
transition={transition}
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" className="mr-1.5 size-3.5" aria-hidden>
<path d="M3.5 8.5l3 3 6-7" />
</svg>
</motion.span>
) : null}
</AnimatePresence>
<motion.span layout="position" transition={transition}>
{option.label}
</motion.span>
</motion.button>
);
})}
</div>
);
}States
- Off
- A quiet chip with only the label.
- On
- Fills with the primary color and a check slides in; neighbours shift to make room.
- Keyboard
- Each chip is its own tab stop with role checkbox, toggled with Space.
- Screen readers
- The chips sit in a labelled group, so the topic is announced once.
- Reduced motion
- The check appears without sliding and nothing shifts.
Notes
The check is there so the state doesn’t rely on color alone.