Checkbox and radio
130 lines · 1 file · MIT
A checkbox with a dash for partial choices, and radios in a group you can move through with arrow keys.
import type { ReactNode } from "react";
import { Checkbox as BaseCheckbox } from "@base-ui/react/checkbox";
import { Radio as BaseRadio } from "@base-ui/react/radio";
import { RadioGroup as BaseRadioGroup } from "@base-ui/react/radio-group";
import { Check, Minus } from "./icons";
import { cn } from "./cn";
// The box and the dot share the Switch's colours: surface-2 with a border
// when off, the accent when on. The ::after reaches the press target to
// 28px without moving the 16px box.
const control =
"relative inline-flex size-4 shrink-0 cursor-pointer items-center justify-center border border-border bg-surface-2 text-bg transition-colors after:absolute after:-inset-1.5 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent data-[disabled]:cursor-default data-[disabled]:opacity-50";
/** A checkbox with its label beside it. `indeterminate` shows a dash, for a
* "select all" over a partly chosen list. */
export function Checkbox({
checked,
onCheckedChange,
label,
hint,
indeterminate,
disabled,
className,
}: {
checked: boolean;
onCheckedChange: (checked: boolean) => void;
label: ReactNode;
/** One quiet line under the label. */
hint?: ReactNode;
indeterminate?: boolean;
disabled?: boolean;
className?: string;
}) {
return (
<label
className={cn(
"flex cursor-pointer items-start gap-2.5 text-sm",
disabled && "cursor-default opacity-50",
className,
)}
>
<BaseCheckbox.Root
checked={checked}
indeterminate={indeterminate}
onCheckedChange={onCheckedChange}
disabled={disabled}
className={cn(
control,
"mt-0.5 rounded-[5px] data-[checked]:border-accent data-[checked]:bg-accent data-[indeterminate]:border-accent data-[indeterminate]:bg-accent",
)}
>
<BaseCheckbox.Indicator className="flex data-[unchecked]:hidden">
{indeterminate ? <Minus size={12} stroke={3} /> : <Check size={12} stroke={3} />}
</BaseCheckbox.Indicator>
</BaseCheckbox.Root>
<span className="min-w-0">
<span className="block">{label}</span>
{hint && <span className="mt-0.5 block text-xs text-muted">{hint}</span>}
</span>
</label>
);
}
/** A group of exclusive choices, one radio each. Arrow keys move through it. */
export function RadioGroup({
value,
onValueChange,
label,
disabled,
className,
children,
}: {
value: string;
onValueChange: (value: string) => void;
/** Names the group for a screen reader. */
label: string;
disabled?: boolean;
className?: string;
children: ReactNode;
}) {
return (
<BaseRadioGroup
value={value}
onValueChange={(v) => onValueChange(String(v))}
aria-label={label}
disabled={disabled}
className={cn("grid gap-2.5", className)}
>
{children}
</BaseRadioGroup>
);
}
/** One choice in a RadioGroup, with its label beside it. */
export function RadioItem({
value,
label,
hint,
disabled,
}: {
value: string;
label: ReactNode;
hint?: ReactNode;
disabled?: boolean;
}) {
return (
<label
className={cn(
"flex cursor-pointer items-start gap-2.5 text-sm",
disabled && "cursor-default opacity-50",
)}
>
<BaseRadio.Root
value={value}
disabled={disabled}
className={cn(
control,
"mt-0.5 rounded-full data-[checked]:border-accent data-[checked]:bg-accent",
)}
>
<BaseRadio.Indicator className="block size-1.5 rounded-full bg-bg data-[unchecked]:hidden" />
</BaseRadio.Root>
<span className="min-w-0">
<span className="block">{label}</span>
{hint && <span className="mt-0.5 block text-xs text-muted">{hint}</span>}
</span>
</label>
);
}
Install
One command. The files land in your repo, with everything they import.
$ bunx shadcn@latest add @blocco/checkbox- Category
- Forms
- Files
- checkbox.tsx
- Brings in
- @base-ui/react