Styled checkbox
Five checkbox treatments: a fill, a bounce, a drawn check, a glow and a rounded box. Each is sized, token-driven and keyboard accessible with role checkbox.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/checkbox-styledDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/checkbox-styled.tsx"use client"
import * as React from "react"
import { Check } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Checkbox family: 5 decorative selection boxes. role="checkbox", keyboard accessible. Colour comes ONLY from tokens. Size = the box scale. Used uncontrolled (defaultChecked) or controlled. framer-motion switches instantly under reduced-motion. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const boxSize: Record<StyledSize, string> = {
sm: "size-4",
md: "size-5",
lg: "size-6",
xl: "size-7",
}
const iconSize: Record<StyledSize, string> = {
sm: "size-3",
md: "size-3.5",
lg: "size-4",
xl: "size-5",
}
const boxBase =
"relative inline-flex shrink-0 cursor-pointer items-center justify-center outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:cursor-not-allowed disabled:opacity-50 after:absolute after:-inset-1.5"
type CheckboxProps = Omit<React.ComponentProps<"button">, "onChange"> & {
size?: StyledSize
checked?: boolean
defaultChecked?: boolean
onCheckedChange?: (checked: boolean) => void
}
function useCheckbox(props: CheckboxProps) {
const { checked, defaultChecked, onCheckedChange } = props
const reduce = useReducedMotion()
const [internal, setInternal] = React.useState(defaultChecked ?? false)
const on = checked ?? internal
const toggle = () => {
if (checked === undefined) setInternal((v) => !v)
onCheckedChange?.(!on)
}
const spring = reduce
? { duration: 0 }
: ({ type: "spring", stiffness: 500, damping: 30 } as const)
return { on, toggle, spring, reduce }
}
/* Fill: kutu merkezden yay ile dolar, isaret olceklenerek girer. */
export function FillCheckbox({
className,
size = "md",
checked,
defaultChecked,
onCheckedChange,
...props
}: CheckboxProps) {
const { on, toggle, spring } = useCheckbox({ checked, defaultChecked, onCheckedChange })
return (
<button
type="button"
role="checkbox"
aria-checked={on}
data-slot="styled-checkbox"
onClick={toggle}
className={cn(
boxBase,
boxSize[size],
"overflow-hidden rounded-md border transition-colors",
on ? "border-primary text-primary-foreground" : "border-field-border text-transparent",
className
)}
{...props}
>
<motion.span
initial={false}
animate={{ scale: on ? 1 : 0 }}
transition={spring}
className="absolute inset-0 origin-center bg-primary"
/>
<motion.span
initial={false}
animate={{ scale: on ? 1 : 0.4, opacity: on ? 1 : 0 }}
transition={spring}
className="relative flex items-center justify-center"
>
<Check className={cn(iconSize[size], "stroke-[3]")} />
</motion.span>
</button>
)
}
/* Bounce: isaret yay ile ziplayarak girer. */
export function BounceCheckbox({
className,
size = "md",
checked,
defaultChecked,
onCheckedChange,
...props
}: CheckboxProps) {
const { on, toggle, reduce } = useCheckbox({ checked, defaultChecked, onCheckedChange })
const bounce = reduce
? { duration: 0 }
: ({ type: "spring", stiffness: 700, damping: 12 } as const)
return (
<button
type="button"
role="checkbox"
aria-checked={on}
data-slot="styled-checkbox"
onClick={toggle}
className={cn(
boxBase,
boxSize[size],
"rounded-md border transition-colors",
on ? "border-primary bg-primary text-primary-foreground" : "border-field-border text-transparent",
className
)}
{...props}
>
<motion.span
initial={false}
animate={{ scale: on ? 1 : 0, opacity: on ? 1 : 0 }}
transition={bounce}
className="flex items-center justify-center"
>
<Check className={cn(iconSize[size], "stroke-[3]")} />
</motion.span>
</button>
)
}
/* Draw: isaret yolu cizilir (pathLength). */
export function DrawCheckbox({
className,
size = "md",
checked,
defaultChecked,
onCheckedChange,
...props
}: CheckboxProps) {
const { on, toggle, reduce } = useCheckbox({ checked, defaultChecked, onCheckedChange })
return (
<button
type="button"
role="checkbox"
aria-checked={on}
data-slot="styled-checkbox"
onClick={toggle}
className={cn(
boxBase,
boxSize[size],
"rounded-md border transition-colors",
on ? "border-primary bg-primary text-primary-foreground" : "border-field-border text-transparent",
className
)}
{...props}
>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth={3}
strokeLinecap="round"
strokeLinejoin="round"
className={cn(iconSize[size])}
aria-hidden="true"
>
<motion.path
d="M5 12.5l4.5 4.5L19 7"
initial={false}
animate={{ pathLength: on ? 1 : 0, opacity: on ? 1 : 0 }}
transition={reduce ? { duration: 0 } : { duration: 0.28, ease: "easeInOut" }}
style={{ pathLength: 0 }}
/>
</svg>
</button>
)
}
/* Glow: acikken kutu token isima yayar. */
export function GlowCheckbox({
className,
size = "md",
checked,
defaultChecked,
onCheckedChange,
...props
}: CheckboxProps) {
const { on, toggle, spring } = useCheckbox({ checked, defaultChecked, onCheckedChange })
return (
<button
type="button"
role="checkbox"
aria-checked={on}
data-slot="styled-checkbox"
onClick={toggle}
className={cn(
boxBase,
boxSize[size],
"rounded-md border transition-all",
on
? "border-primary bg-primary text-primary-foreground shadow-[0_0_12px_2px_var(--primary)]"
: "border-field-border text-transparent",
className
)}
{...props}
>
<motion.span
initial={false}
animate={{ scale: on ? 1 : 0, opacity: on ? 1 : 0 }}
transition={spring}
className="flex items-center justify-center"
>
<Check className={cn(iconSize[size], "stroke-[3]")} />
</motion.span>
</button>
)
}
/* Rounded: tam yuvarlak kutu, yumusak token dolgusu. */
export function RoundedCheckbox({
className,
size = "md",
checked,
defaultChecked,
onCheckedChange,
...props
}: CheckboxProps) {
const { on, toggle, spring } = useCheckbox({ checked, defaultChecked, onCheckedChange })
return (
<button
type="button"
role="checkbox"
aria-checked={on}
data-slot="styled-checkbox"
onClick={toggle}
className={cn(
boxBase,
boxSize[size],
"rounded-full border transition-colors",
on ? "border-success bg-success text-success-foreground" : "border-field-border text-transparent",
className
)}
{...props}
>
<motion.span
initial={false}
animate={{ scale: on ? 1 : 0, opacity: on ? 1 : 0 }}
transition={spring}
className="flex items-center justify-center"
>
<Check className={cn(iconSize[size], "stroke-[3]")} />
</motion.span>
</button>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Fill
The box fills from the center as the check scales in.
import { FillCheckbox } from "@/components/ui/checkbox-styled"
<FillCheckbox defaultChecked={true} />Bounce
The checkmark bounces in with a spring.
import { BounceCheckbox } from "@/components/ui/checkbox-styled"
<BounceCheckbox defaultChecked={true} />Draw
The checkmark path draws itself in.
import { DrawCheckbox } from "@/components/ui/checkbox-styled"
<DrawCheckbox defaultChecked={true} />Glow
The checked box gains a token glow.
import { GlowCheckbox } from "@/components/ui/checkbox-styled"
<GlowCheckbox defaultChecked={true} />Rounded
A fully rounded checkbox with a smooth token fill.
import { RoundedCheckbox } from "@/components/ui/checkbox-styled"
<RoundedCheckbox defaultChecked={true} />ai2 Styled checkbox: 5 styled variations on the token system
The ai2 Styled checkbox are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around checkbox check animations. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion fills the box, bounces and draws the checkmark. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the check appears instantly with no animation.
What is in the ai2 Styled checkbox?
5 exports in one file: Fill, Bounce, Draw, Glow and Rounded. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion fills the box, bounces and draws the checkmark.
- Reduced-motion aware: Under prefers-reduced-motion, the check appears instantly with no animation.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Styled checkbox sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.