{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"checkbox-group","type":"registry:component","title":"Checkbox group","description":"Check marks draw themselves in, and Shift-click fills a range.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/checkbox-group.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { motion } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\ntype Item = { id: string; label: string; description?: string };\ntype Mark = \"checked\" | \"mixed\" | \"none\";\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\n// Drawing is the confirmation, so it gets the time to be seen; undrawing is\n// just getting out of the way, so it's twice as quick.\nconst DRAW = { duration: 0.2, ease: EASE_OUT } as const;\nconst UNDRAW = { duration: 0.1, ease: EASE_OUT } as const;\n// A short spring with a little give, so the fill lands and settles instead\n// of just appearing.\nconst FILL_IN = { type: \"spring\", duration: 0.25, bounce: 0.3 } as const;\nconst FILL_OUT = { duration: 0.12, ease: EASE_OUT } as const;\nconst FADE = { duration: 0.15, ease: EASE_OUT } as const;\n\nexport function CheckboxGroup({\n  label,\n  items,\n  value,\n  onChange,\n  className,\n}: {\n  label: string;\n  items: Item[];\n  value: string[];\n  onChange: (value: string[]) => void;\n  className?: string;\n}) {\n  const id = useId();\n  const reduceMotion = Boolean(useReducedMotion());\n  const parentRef = useRef<HTMLInputElement>(null);\n  // Where the last plain click landed; a shift-click fills from here.\n  const anchor = useRef<number | null>(null);\n  const shift = useRef(false);\n\n  const selected = new Set(value);\n  const count = items.filter((i) => selected.has(i.id)).length;\n  const all = count === items.length;\n  const mixed = count > 0 && !all;\n\n  // indeterminate only exists as a DOM property, and it's what makes\n  // screen readers announce the parent as mixed.\n  useEffect(() => {\n    if (parentRef.current) parentRef.current.indeterminate = mixed;\n  }, [mixed]);\n\n  const emit = (next: Set<string>) =>\n    onChange(items.filter((i) => next.has(i.id)).map((i) => i.id));\n\n  const toggle = (index: number) => {\n    const next = new Set(selected);\n    const checked = !next.has(items[index].id);\n    const from = shift.current && anchor.current !== null ? anchor.current : index;\n    shift.current = false;\n    // The clicked box decides the direction; everything between follows it.\n    const [lo, hi] = from < index ? [from, index] : [index, from];\n    for (let i = lo; i <= hi; i++) {\n      if (checked) next.add(items[i].id);\n      else next.delete(items[i].id);\n    }\n    anchor.current = index;\n    emit(next);\n  };\n\n  // A label click forwards a second click to its input, and browsers don't\n  // agree on whether that one carries the modifier, so remember it from\n  // whichever event saw it.\n  const rememberShift = (e: React.MouseEvent) => {\n    if (e.shiftKey) shift.current = true;\n  };\n\n  return (\n    // 20px outer radius around 14px rows with 6px padding: concentric.\n    <div\n      className={cn(\n        \"w-[min(360px,100%)] rounded-[20px] bg-background p-1.5 shadow-raised\",\n        className,\n      )}\n    >\n      <Row\n        mark={all ? \"checked\" : mixed ? \"mixed\" : \"none\"}\n        reduceMotion={reduceMotion}\n        inputRef={parentRef}\n        labelId={`${id}-all`}\n        label={label}\n        aria-controls={items.map((i) => `${id}-${i.id}`).join(\" \")}\n        trailing={\n          <span className=\"text-[13px] text-muted-foreground tabular-nums\">\n            {count} of {items.length}\n          </span>\n        }\n        onToggle={() => {\n          anchor.current = null;\n          shift.current = false;\n          emit(all ? new Set() : new Set(items.map((i) => i.id)));\n        }}\n        bold\n      />\n      <div className=\"mx-3 my-1 h-px bg-border\" />\n      <div role=\"group\" aria-labelledby={`${id}-all`}>\n        {items.map((item, index) => (\n          <Row\n            key={item.id}\n            inputId={`${id}-${item.id}`}\n            mark={selected.has(item.id) ? \"checked\" : \"none\"}\n            reduceMotion={reduceMotion}\n            label={item.label}\n            description={item.description}\n            onRememberShift={rememberShift}\n            onToggle={() => toggle(index)}\n          />\n        ))}\n      </div>\n      <p className=\"px-3 pt-1.5 pb-2 text-xs text-muted-foreground select-none\">\n        <kbd className=\"rounded-[4px] border border-border px-1 py-px font-sans text-xs\">\n          Shift\n        </kbd>{\" \"}\n        click to select a range\n      </p>\n    </div>\n  );\n}\n\nfunction Row({\n  mark,\n  reduceMotion,\n  label,\n  description,\n  trailing,\n  bold,\n  inputId,\n  inputRef,\n  labelId,\n  onToggle,\n  onRememberShift,\n  \"aria-controls\": controls,\n}: {\n  mark: Mark;\n  reduceMotion: boolean;\n  label: string;\n  description?: string;\n  trailing?: React.ReactNode;\n  bold?: boolean;\n  inputId?: string;\n  inputRef?: React.Ref<HTMLInputElement>;\n  labelId?: string;\n  onToggle: () => void;\n  onRememberShift?: (e: React.MouseEvent) => void;\n  \"aria-controls\"?: string;\n}) {\n  return (\n    <label\n      onClick={(e) => {\n        if (!(e.target instanceof HTMLInputElement)) onRememberShift?.(e);\n      }}\n      className={cn(\n        \"group relative flex min-h-11 cursor-pointer touch-manipulation gap-3 rounded-[14px] px-3 py-2.5 select-none\",\n        description ? \"items-start\" : \"items-center\",\n        \"transition-[background-color] duration-150 ease-out hover:bg-muted\",\n        \"has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-solid has-[:focus-visible]:-outline-offset-2 has-[:focus-visible]:outline-foreground\",\n      )}\n    >\n      <input\n        ref={inputRef}\n        id={inputId}\n        type=\"checkbox\"\n        checked={mark === \"checked\"}\n        aria-controls={controls}\n        onClick={onRememberShift}\n        onChange={onToggle}\n        className=\"sr-only\"\n      />\n      <Box mark={mark} reduceMotion={reduceMotion} offset={Boolean(description)} />\n      <span className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n        <span\n          id={labelId}\n          className={cn(\n            \"text-[15px] text-foreground\",\n            bold ? \"font-semibold\" : \"font-medium\",\n          )}\n        >\n          {label}\n        </span>\n        {description && (\n          <span className=\"text-sm text-muted-foreground\">{description}</span>\n        )}\n      </span>\n      {trailing}\n    </label>\n  );\n}\n\nfunction Box({\n  mark,\n  reduceMotion,\n  offset,\n}: {\n  mark: Mark;\n  reduceMotion: boolean;\n  offset: boolean;\n}) {\n  const filled = mark !== \"none\";\n  return (\n    <span\n      aria-hidden\n      className={cn(\n        \"relative grid size-5 shrink-0 place-items-center rounded-md border-[1.5px] transition-[border-color,scale] duration-150 ease-out group-active:scale-[0.96] motion-reduce:transition-[border-color]\",\n        // Centers the box on the first line of 15px text (22px line box).\n        offset && \"mt-px\",\n        filled\n          ? \"border-foreground\"\n          : \"border-foreground/25 group-hover:border-foreground/45\",\n      )}\n    >\n      {/* Covers the border too, so a filled box is exactly 20px. */}\n      <motion.span\n        className=\"absolute -inset-[1.5px] rounded-md bg-foreground\"\n        initial={false}\n        animate={\n          filled\n            ? { scale: 1, opacity: 1 }\n            : { scale: reduceMotion ? 1 : 0.8, opacity: 0 }\n        }\n        transition={filled && !reduceMotion ? FILL_IN : FILL_OUT}\n      />\n      <svg\n        viewBox=\"0 0 16 16\"\n        className=\"relative size-3.5 text-background\"\n        fill=\"none\"\n        stroke=\"currentColor\"\n        // Heavier than the lab's 1.5: at 14px on a solid fill, a thinner\n        // mark reads as a hairline.\n        strokeWidth={2.25}\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      >\n        <Stroke d=\"m3.25 8.5 3 3 6.5-7\" shown={mark === \"checked\"} reduceMotion={reduceMotion} />\n        <Stroke d=\"M4 8h8\" shown={mark === \"mixed\"} reduceMotion={reduceMotion} />\n      </svg>\n    </span>\n  );\n}\n\nfunction Stroke({\n  d,\n  shown,\n  reduceMotion,\n}: {\n  d: string;\n  shown: boolean;\n  reduceMotion: boolean;\n}) {\n  if (reduceMotion)\n    return (\n      <motion.path\n        d={d}\n        initial={false}\n        animate={{ opacity: shown ? 1 : 0 }}\n        transition={FADE}\n      />\n    );\n  return (\n    <motion.path\n      d={d}\n      initial={false}\n      // Opacity rides along because a round cap at pathLength 0 still\n      // leaves a dot behind. It snaps on at the start of a draw and off at\n      // the end of an undraw.\n      animate={\n        shown ? { pathLength: 1, opacity: 1 } : { pathLength: 0, opacity: 0 }\n      }\n      transition={\n        shown\n          ? { pathLength: DRAW, opacity: { duration: 0 } }\n          : { pathLength: UNDRAW, opacity: { duration: 0, delay: UNDRAW.duration } }\n      }\n    />\n  );\n}\n\nconst NOTIFICATIONS: Item[] = [\n  { id: \"comments\", label: \"Comments\", description: \"Replies to your posts and threads\" },\n  { id: \"mentions\", label: \"Mentions\", description: \"When someone mentions you by name\" },\n  { id: \"followers\", label: \"New followers\", description: \"When someone starts following you\" },\n  { id: \"updates\", label: \"Product updates\", description: \"New features, about once a month\" },\n  { id: \"digest\", label: \"Weekly digest\", description: \"A short summary of what you missed\" },\n];\n\nexport default function CheckboxGroupDemo() {\n  const [value, setValue] = useState([\"comments\", \"mentions\"]);\n  return (\n    <CheckboxGroup\n      label=\"All notifications\"\n      items={NOTIFICATIONS}\n      value={value}\n      onChange={setValue}\n    />\n  );\n}\n"},{"path":"hooks/use-reduced-motion.ts","type":"registry:hook","target":"hooks/use-reduced-motion.ts","content":"\"use client\";\n\nimport { useSyncExternalStore } from \"react\";\n\n// Motion's useReducedMotion reads the media query on the very first client\n// render, but the server can't know it, so every component that renders\n// differently under reduced motion broke hydration for those users. This\n// reports false during hydration, matching the server HTML, then the real\n// preference straight after, and follows it if it changes.\nconst query = \"(prefers-reduced-motion: reduce)\";\n\nfunction subscribe(onChange: () => void) {\n  const media = window.matchMedia(query);\n  media.addEventListener(\"change\", onChange);\n  return () => media.removeEventListener(\"change\", onChange);\n}\n\nexport function useReducedMotion() {\n  return useSyncExternalStore(\n    subscribe,\n    () => window.matchMedia(query).matches,\n    () => false,\n  );\n}\n"}],"cssVars":{"light":{"shadow-raised":"0 0 0 1px oklch(0 0 0 / 0.06), 0 1px 2px oklch(0 0 0 / 0.06), 0 6px 16px -6px oklch(0 0 0 / 0.12)"},"dark":{"shadow-raised":"0 0 0 1px oklch(1 0 0 / 0.08), 0 1px 2px oklch(0 0 0 / 0.4), 0 6px 16px -6px oklch(0 0 0 / 0.6)"},"theme":{"shadow-raised":"var(--shadow-raised)"}},"categories":["inputs"],"docs":"From ui lab: https://lab.xevrion.dev/lab/checkbox-group"}