Updated
Checkbox
NewCustomize
Medium
Duration0.25s
Installation
npx shadcn@latest add https://uibrik.com/r/checkbox.jsonInstall the packages
npm install motionAdd the cn helper
Imports
cnfrom@/lib/utils, whichshadcn initcreates. Add it by hand without shadcn.Copy the source into your project
components/uibrik/checkbox/checkbox.tsx"use client"; import { useId, useRef, useState, type MouseEvent, type ReactNode } from "react"; import { MotionConfig, motion } from "motion/react"; import { cn } from "@/lib/utils"; // Checkbox and checkbox group. Ticking fills the box and draws the check on // stroke by stroke; a box that's partly ticked shows a dash, and the check // morphs into the dash and back rather than swapping. Unticking runs the // stroke backwards before the fill lets go. // // The group can carry a "select all" box that goes indeterminate when only // some are ticked. Shift-click ticks (or unticks) every box between the last // one you clicked and this one, like a mail client. export type CheckedState = boolean | "indeterminate"; const SIZES = { sm: { box: 16, radius: 4.5, text: "text-[13px]", gap: "gap-2.5" }, md: { box: 18, radius: 5, text: "text-sm", gap: "gap-3" }, lg: { box: 20, radius: 6, text: "text-[15px]", gap: "gap-3" }, } as const; // Both marks have three points, so the path can morph between them.
Adds the files to
components/uibrik/checkbox and installs motion. With the @uibrik namespace set up: npx shadcn@latest add @uibrik/checkbox.Usage
import { CheckboxGroup } from "@/components/uibrik/checkbox/checkbox";const options = [ { value: "comments", label: "Comments", description: "Replies to your posts and threads." }, { value: "mentions", label: "Mentions", description: "When someone tags you by name." }, // …2 more];<CheckboxGroup options={options} size="md" rangeSelect duration={0.25} disabled={false} defaultValue={["comments", "mentions"]} selectAll="Email notifications" label="Email notifications" // value={["…"]} // onChange={(value) => {}} // name="…" // Checkbox={…}/>Where to use Checkbox
Forms and settings
A check that draws itself on gives clear, satisfying feedback.
Pairs with Radio Group
Table row selection
A dash for partly ticked select-all and shift-click range select suit tables.
Pairs with Data Table
Terms and consent
An accessible, clear checkbox fits legal agreements.
Pairs with Cookie Consent
Props
CheckboxGroup- size
- Type "sm" | "md" | "lg"
- "md"
- Box and text size.
- rangeSelect
- Type boolean
- true
- Shift-click ticks every box between the last click and this one.
- duration
- Type number
- 0.25
- How long the check takes to draw.
- disabled
- Type boolean
- false
- Grey the group out and ignore clicks.
- optionsrequired
- Type (string | { value; label; description?; disabled? })[]
- –
- Boxes in order.
- value / onChange
- Type string[] / (value) => void
- –
- Controlled ticked values, in the options' order.
- defaultValue
- Type string[]
- –
- Ticked values on mount when uncontrolled.
- selectAll
- Type ReactNode
- –
- Label for a parent box that ticks them all and goes indeterminate when only some are ticked.
- name
- Type string
- –
- Submits each ticked value under this name with a surrounding form.
- label
- Type string
- –
- Names the group for screen readers.
- className
- Type string
- –
- Classes for the group.
- Checkbox
- Type component
- –
- A single box: checked / defaultChecked / onCheckedChange (true, false or "indeterminate"), label, description, size, disabled, name.