{"slug":"checkbox","name":"Checkbox","description":"Form choice control with a draw-on checkmark, spring press feedback and indeterminate state support.","category":"motion","source_url":"https://beui.dev/r/checkbox/raw","detail_url":"https://beui.dev/r/checkbox","raw_url":"https://beui.dev/r/checkbox/raw","page_url":"https://beui.dev/components/motion/checkbox","markdown_url":"https://beui.dev/components/motion/checkbox.md","published_at":"2026-06-23","updated_at":"2026-11-10","dependencies":["clsx","motion","react","tailwind-merge"],"internal":["@/components/motion/checkbox","@/lib/ease","@/lib/i18n","@/lib/utils"],"install_slugs":["checkbox"],"requires_theme":false,"props":[{"displayName":"Checkbox","props":[{"name":"checked","type":"boolean","required":true,"defaultValue":null,"description":"Whether the box is checked."},{"name":"onCheckedChange","type":"(checked: boolean) => void","required":true,"defaultValue":null,"description":"Called with the new state when the box is toggled."},{"name":"disabled","type":"boolean","required":false,"defaultValue":null,"description":"Blocks toggling and dims the control."},{"name":"indeterminate","type":"boolean","required":false,"defaultValue":null,"description":"Shows a dash for a partly selected group. Announced as mixed."},{"name":"label","type":"string","required":false,"defaultValue":null,"description":"Visible text beside the box, also its accessible name."},{"name":"description","type":"string","required":false,"defaultValue":null,"description":"Supporting text under the label, announced as the description rather than the name."},{"name":"className","type":"string","required":false,"defaultValue":null,"description":"Classes merged onto the root label element."},{"name":"id","type":"string","required":false,"defaultValue":null,"description":"Id for the checkbox button. Generated when omitted."},{"name":"aria-label","type":"string","required":false,"defaultValue":null,"description":"Accessible name when there is no visible label."},{"name":"aria-describedby","type":"string","required":false,"defaultValue":null,"description":"Associates an external message (e.g. a form error) with the control."},{"name":"labels","type":"Partial<{ checked: string; indeterminate: string; }>","required":false,"defaultValue":null,"description":"Override any built-in string."}]}],"types":[],"files":[{"path":"components/motion/checkbox.tsx","type":"component","content":"\"use client\";\n// beui.dev/components/motion/checkbox\n\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { useId } from \"react\";\nimport { EASE_OUT, SPRING_PRESS } from \"@/lib/ease\";\nimport { useMessages } from \"@/lib/i18n\";\nimport { cn } from \"@/lib/utils\";\n\n/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */\nexport const checkboxMessages = {\n  checked: \"Selected\",\n  indeterminate: \"Partially selected\",\n};\n\nexport type CheckboxMessages = typeof checkboxMessages;\n\nconst CHECK_PATH = \"M5 13l4 4L19 7\";\nconst INDETERMINATE_PATH = \"M6 12h12\";\n\nexport interface CheckboxProps {\n  /** Whether the box is checked. */\n  checked: boolean;\n  /** Called with the new state when the box is toggled. */\n  onCheckedChange: (checked: boolean) => void;\n  /** Blocks toggling and dims the control. */\n  disabled?: boolean;\n  /** Shows a dash for a partly selected group. Announced as mixed. */\n  indeterminate?: boolean;\n  /** Visible text beside the box, also its accessible name. */\n  label?: string;\n  /** Supporting text under the label, announced as the description rather than the name. */\n  description?: string;\n  /** Classes merged onto the root label element. */\n  className?: string;\n  /** Id for the checkbox button. Generated when omitted. */\n  id?: string;\n  /** Accessible name when there is no visible label. */\n  \"aria-label\"?: string;\n  /** Associates an external message (e.g. a form error) with the control. */\n  \"aria-describedby\"?: string;\n  /** Override any built-in string. */\n  labels?: Partial<CheckboxMessages>;\n}\n\nexport function Checkbox({\n  checked,\n  onCheckedChange,\n  disabled,\n  indeterminate,\n  label,\n  description,\n  className,\n  id: idProp,\n  \"aria-label\": ariaLabel,\n  \"aria-describedby\": ariaDescribedBy,\n  labels,\n}: CheckboxProps) {\n  const t = useMessages(\"checkbox\", checkboxMessages, labels);\n  const autoId = useId();\n  const id = idProp ?? autoId;\n  const reduce = useReducedMotion();\n  const showMark = checked || indeterminate;\n  const path = indeterminate ? INDETERMINATE_PATH : CHECK_PATH;\n  const descriptionId = description ? `${id}-description` : undefined;\n  // The wrapping <label> would fold the description into the name, so name from the label text alone.\n  const labelId = description && label && !ariaLabel ? `${id}-label` : undefined;\n  const describedBy =\n    [ariaDescribedBy, descriptionId].filter(Boolean).join(\" \") || undefined;\n\n  return (\n    <label\n      htmlFor={id}\n      className={cn(\n        \"inline-flex gap-3\",\n        description ? \"items-start\" : \"items-center\",\n        disabled ? \"cursor-not-allowed\" : \"cursor-pointer\",\n        className,\n      )}\n    >\n      <motion.button\n        id={id}\n        type=\"button\"\n        role=\"checkbox\"\n        aria-checked={indeterminate ? \"mixed\" : checked}\n        aria-label={ariaLabel}\n        aria-labelledby={labelId}\n        aria-describedby={describedBy}\n        disabled={disabled}\n        onClick={() => !disabled && onCheckedChange(!checked)}\n        whileTap={reduce || disabled ? undefined : { scale: 0.92 }}\n        transition={SPRING_PRESS}\n        data-state={\n          checked ? \"checked\" : indeterminate ? \"indeterminate\" : \"unchecked\"\n        }\n        className={cn(\n          \"inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-md border-2 outline-none transition-colors duration-200\",\n          \"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n          \"disabled:cursor-not-allowed disabled:opacity-60\",\n          showMark\n            ? \"border-primary bg-primary text-primary-foreground\"\n            : \"border-muted-foreground/50 bg-background hover:border-muted-foreground\",\n        )}\n      >\n        <AnimatePresence initial={false}>\n          {showMark ? (\n            <motion.svg\n              key={indeterminate ? \"indeterminate\" : \"checked\"}\n              width=\"12\"\n              height=\"12\"\n              viewBox=\"0 0 24 24\"\n              fill=\"none\"\n              stroke=\"currentColor\"\n              strokeWidth={3}\n              strokeLinecap=\"round\"\n              strokeLinejoin=\"round\"\n              initial={reduce ? { opacity: 1 } : { opacity: 0, scale: 0.5 }}\n              animate={reduce ? { opacity: 1 } : { opacity: 1, scale: 1 }}\n              exit={\n                reduce\n                  ? { opacity: 0 }\n                  : { opacity: 0, scale: 0.5, filter: \"blur(4px)\" }\n              }\n              transition={\n                reduce ? { duration: 0 } : { duration: 0.16, ease: EASE_OUT }\n              }\n              aria-hidden\n            >\n              <title>{t(indeterminate ? \"indeterminate\" : \"checked\")}</title>\n              <motion.path\n                d={path}\n                initial={reduce ? { pathLength: 1 } : { pathLength: 0 }}\n                animate={{ pathLength: 1 }}\n                transition={\n                  reduce\n                    ? { duration: 0 }\n                    : {\n                        duration: indeterminate ? 0.2 : 0.3,\n                        ease: EASE_OUT,\n                        delay: 0.04,\n                      }\n                }\n              />\n            </motion.svg>\n          ) : null}\n        </AnimatePresence>\n      </motion.button>\n      {label || description ? (\n        <span className={cn(\"flex min-w-0 flex-col gap-0.5 select-none\", disabled && \"opacity-60\")}>\n          {label ? (\n            <span\n              id={labelId}\n              // Geist sits low in a 20px line; a 16px line lifts the text onto the control.\n              className={cn(\"text-sm text-foreground\", description && \"leading-4\")}\n            >\n              {label}\n            </span>\n          ) : null}\n          {description ? (\n            <span id={descriptionId} className=\"text-xs text-muted-foreground\">\n              {description}\n            </span>\n          ) : null}\n        </span>\n      ) : null}\n    </label>\n  );\n}\n"},{"path":"lib/ease.ts","type":"util","content":"// Shared motion tokens. Easing curves mirror the CSS custom properties in\n// globals.css; springs are the canonical physics used across components.\n// Strong custom variants - defaults like `ease-in`/`ease-out` feel weak.\n\nexport const EASE_OUT = [0.16, 1, 0.3, 1] as const;\n/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */\nexport const EASE_IN = [0.55, 0, 1, 0.45] as const;\nexport const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;\nexport const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;\n\n/** CSS string form of EASE_OUT for inline style transitions. */\nexport const EASE_OUT_CSS = \"cubic-bezier(0.16, 1, 0.3, 1)\";\n\n/** Press feedback on buttons and other tappable surfaces. */\nexport const SPRING_PRESS = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 30,\n  mass: 0.6,\n} as const;\n\n/** Content swaps - label/icon slots trading places inside a control. */\nexport const SPRING_SWAP = {\n  type: \"spring\",\n  stiffness: 460,\n  damping: 30,\n  mass: 0.55,\n} as const;\n\n/** Overlay panel entrances - modals and sheets summoned by pointer. */\nexport const SPRING_PANEL = {\n  type: \"spring\",\n  stiffness: 420,\n  damping: 40,\n  mass: 0.5,\n} as const;\n\n/** Shared-layout glides - pills, indicators and panels morphing between positions. */\nexport const SPRING_LAYOUT = {\n  type: \"spring\",\n  stiffness: 360,\n  damping: 32,\n  mass: 0.6,\n} as const;\n\n/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */\nexport const SPRING_MOUSE = {\n  stiffness: 200,\n  damping: 15,\n  mass: 0.3,\n} as const;\n\n/** Dragged handles and fills (sliders) - critically damped `useSpring` config,\n * so the value follows the pointer butterily and never rebounds off an end. */\nexport const SPRING_GLIDE = {\n  stiffness: 700,\n  damping: 50,\n  mass: 0.5,\n} as const;\n\n/** Shared-surface morphs: a trigger growing into its panel and back on one\n * weighted spring, so opening and closing travel the same path. */\nexport const SPRING_MORPH = {\n  type: \"spring\",\n  duration: 0.5,\n  bounce: 0.22,\n} as const;\n"},{"path":"lib/i18n.tsx","type":"util","content":"\"use client\";\n\nimport {\n  createContext,\n  type ReactNode,\n  useCallback,\n  useContext,\n  useMemo,\n} from \"react\";\n\n/**\n * A component's messages. Plain strings with `{name}` placeholders, so a\n * dictionary stays JSON-serializable and can be passed from a server layout.\n * There are no plural forms: write messages that read correctly for any count.\n */\nexport type Messages = Record<string, string>;\n\n/** A language file: component namespace → any subset of that component's messages. */\nexport type LocaleMessages = Record<string, Partial<Messages>>;\n\ntype LocaleContextValue = {\n  locale?: string;\n  messages: LocaleMessages;\n};\n\nconst NO_MESSAGES: LocaleMessages = {};\n\nconst LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });\n\n/** Without a locale, digits stay plain so server and client render the same text. */\nexport function formatNumber(value: number, locale?: string) {\n  return locale ? new Intl.NumberFormat(locale).format(value) : String(value);\n}\n\nexport interface LocaleProviderProps {\n  /** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */\n  locale: string;\n  /**\n   * Component namespace → translated strings. Missing keys fall back to English.\n   * Pass a stable object (a module constant, imported JSON or a memoized value):\n   * a new object each render gives every translated component a new `t`.\n   */\n  messages?: LocaleMessages;\n  children: ReactNode;\n}\n\nexport function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {\n  const value = useMemo(() => ({ locale, messages }), [locale, messages]);\n  return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;\n}\n\nexport function useLocale(): { locale?: string } {\n  return { locale: useContext(LocaleContext).locale };\n}\n\n/**\n * Resolves a component's strings: `labels` prop, then the provider's\n * messages for `namespace`, then the component's English `defaults`.\n * Numbers passed as values are formatted for the provider's locale.\n */\nexport function useMessages<T extends Messages>(\n  namespace: string,\n  defaults: T,\n  labels?: Partial<T>,\n) {\n  const { locale, messages } = useContext(LocaleContext);\n  const provided = messages[namespace];\n  // Inline `labels={{ … }}` is a new object every render. Keying it by content\n  // keeps `t` one identity until a label changes, so effects and memos that\n  // depend on `t` don't re-run per render, and render-time reads never lag.\n  const labelsKey = labels ? JSON.stringify(labels) : \"\";\n  const stableLabels = useMemo(\n    () => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),\n    [labelsKey],\n  );\n\n  return useCallback(\n    (key: keyof T & string, values?: Record<string, string | number>) => {\n      const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];\n      if (!values) return template;\n      return template.replace(/\\{(\\w+)\\}/g, (match, name: string) => {\n        const value = values[name];\n        if (value === undefined) return match;\n        return typeof value === \"number\" ? formatNumber(value, locale) : value;\n      });\n    },\n    [defaults, stableLabels, locale, provided],\n  );\n}\n"},{"path":"lib/utils.ts","type":"util","content":"import { clsx, type ClassValue } from \"clsx\"\nimport { twMerge } from \"tailwind-merge\"\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs))\n}\n"},{"path":"components/previews/motion/checkbox.preview.tsx","type":"preview","content":"\"use client\";\n\nimport { useState } from \"react\";\nimport { Checkbox } from \"@/components/motion/checkbox\";\n\nexport function CheckboxPreview() {\n  const [terms, setTerms] = useState(true);\n  const [updates, setUpdates] = useState(false);\n\n  return (\n    <div className=\"flex flex-col gap-3\">\n      <Checkbox\n        checked={terms}\n        onCheckedChange={setTerms}\n        label=\"Accept terms and conditions\"\n      />\n      <Checkbox\n        checked={updates}\n        onCheckedChange={setUpdates}\n        label=\"Email me product updates\"\n      />\n      <Checkbox checked indeterminate onCheckedChange={() => {}} label=\"Select all (partial)\" />\n      <Checkbox checked disabled onCheckedChange={() => {}} label=\"Disabled\" />\n    </div>\n  );\n}\n"}]}