{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spring-checkbox","type":"registry:block","title":"Spring checkbox","description":"A task checklist where every box springs, the check draws itself and a thin bar tracks what is done.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/spring-checkbox/spring-checkbox.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useRef, useState } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\nconst BAR_SPRING = { type: \"spring\", stiffness: 260, damping: 32 } as const;\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type SpringCheckboxItem = {\n  id: string;\n  label: string;\n  /** Optional small line under the label, e.g. an owner or a due day. */\n  detail?: string;\n  defaultDone?: boolean;\n};\n\nexport type SpringCheckboxProps = {\n  className?: string;\n  title?: string;\n  subtitle?: string;\n  items?: SpringCheckboxItem[];\n  /** Called with the item id and its new state. */\n  onChange?: (id: string, done: boolean) => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_ITEMS: SpringCheckboxItem[] = [\n  {\n    id: \"dns\",\n    label: \"Move DNS to the new nameservers\",\n    detail: \"Kite & Co · due Monday\",\n    defaultDone: true,\n  },\n  {\n    id: \"beta\",\n    label: \"Invite the 12 beta accounts\",\n    detail: \"Ama Mensah · due Tuesday\",\n    defaultDone: true,\n  },\n  {\n    id: \"tour\",\n    label: \"Record the 90-second product tour\",\n    detail: \"Lucas Ferreira · due Wednesday\",\n  },\n  {\n    id: \"rollback\",\n    label: \"Write the rollback note\",\n    detail: \"Jonas Weber · due Thursday\",\n  },\n  {\n    id: \"rehearsal\",\n    label: \"Schedule the status-page rehearsal\",\n    detail: \"Priya Nair · due Friday\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* row                                                                 */\n/* ------------------------------------------------------------------ */\n\ntype RowProps = {\n  item: SpringCheckboxItem;\n  index: number;\n  done: boolean;\n  show: boolean;\n  reduce: boolean;\n  onToggle: (item: SpringCheckboxItem) => void;\n};\n\nfunction CheckRow({ item, index, done, show, reduce, onToggle }: RowProps) {\n  return (\n    <motion.li\n      initial={reduce ? false : { opacity: 0, y: 8 }}\n      animate={show ? { opacity: 1, y: 0 } : { opacity: 0, y: reduce ? 0 : 8 }}\n      transition={{\n        duration: reduce ? 0 : 0.42,\n        ease: EASE,\n        delay: reduce ? 0 : index * 0.045,\n      }}\n    >\n      <button\n        type=\"button\"\n        role=\"checkbox\"\n        aria-checked={done}\n        onClick={() => onToggle(item)}\n        className=\"group flex w-full items-start gap-3 rounded-lg px-2 py-3 text-left transition-colors hover:bg-muted/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n      >\n        <span\n          aria-hidden=\"true\"\n          className={cn(\n            \"mt-0.5 grid h-[18px] w-[18px] shrink-0 place-items-center rounded-[5px] border transition-[background-color,border-color,transform] duration-200 group-active:scale-90\",\n            done\n              ? \"border-foreground bg-foreground text-background\"\n              : \"border-border bg-background text-transparent group-hover:border-muted-foreground/50\"\n          )}\n        >\n          <svg viewBox=\"0 0 12 12\" fill=\"none\" className=\"h-3 w-3\">\n            <motion.path\n              d=\"M2.4 6.2 4.9 8.6 9.6 3.4\"\n              stroke=\"currentColor\"\n              strokeWidth={1.8}\n              strokeLinecap=\"round\"\n              strokeLinejoin=\"round\"\n              initial={false}\n              animate={{ pathLength: done ? 1 : 0, opacity: done ? 1 : 0 }}\n              transition={\n                reduce\n                  ? { duration: 0 }\n                  : { duration: 0.28, ease: EASE }\n              }\n            />\n          </svg>\n        </span>\n\n        <span className=\"min-w-0 flex-1\">\n          <span\n            className={cn(\n              \"text-[14px] leading-5 tracking-[-0.01em] transition-colors duration-200\",\n              done\n                ? \"text-muted-foreground line-through decoration-muted-foreground/50\"\n                : \"text-foreground\"\n            )}\n          >\n            {item.label}\n          </span>\n          {item.detail ? (\n            <span\n              className={cn(\n                \"mt-0.5 block text-[12px] leading-4 transition-colors duration-200\",\n                done ? \"text-muted-foreground/60\" : \"text-muted-foreground\"\n              )}\n            >\n              {item.detail}\n            </span>\n          ) : null}\n        </span>\n      </button>\n    </motion.li>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function SpringCheckbox({\n  className,\n  title = \"Launch checklist\",\n  subtitle = \"Release 2.4 · ships Friday at 14:00.\",\n  items = DEFAULT_ITEMS,\n  onChange,\n}: SpringCheckboxProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const inView = useInView(rootRef, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const show = reduce || inView;\n\n  const uid = useId().replace(/:/g, \"\");\n\n  const [state, setState] = useState<Record<string, boolean>>(() =>\n    Object.fromEntries(items.map((item) => [item.id, item.defaultDone ?? false]))\n  );\n\n  const isDone = (item: SpringCheckboxItem) => state[item.id] ?? false;\n  const doneCount = items.filter(isDone).length;\n  const ratio = items.length === 0 ? 0 : doneCount / items.length;\n\n  const toggle = (item: SpringCheckboxItem) => {\n    const next = !isDone(item);\n    setState((current) => ({ ...current, [item.id]: next }));\n    onChange?.(item.id, next);\n  };\n\n  const clearCompleted = () => {\n    setState(\n      Object.fromEntries(items.map((item) => [item.id, false]))\n    );\n  };\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={rootRef}\n        initial={reduce ? false : { opacity: 0, y: 16 }}\n        animate={\n          show ? { opacity: 1, y: 0 } : { opacity: 0, y: reduce ? 0 : 16 }\n        }\n        transition={{ duration: reduce ? 0 : 0.55, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card text-card-foreground shadow-sm\",\n          className\n        )}\n      >\n        <div className=\"p-5 sm:p-6\">\n          <div className=\"flex flex-wrap items-end justify-between gap-3\">\n            <div className=\"min-w-0\">\n              <h2\n                id={`${uid}-title`}\n                className=\"text-[15px] font-semibold tracking-tight\"\n              >\n                {title}\n              </h2>\n              <p className=\"mt-0.5 text-[13px] text-muted-foreground\">\n                {subtitle}\n              </p>\n            </div>\n            <p className=\"shrink-0 text-[12px] tabular-nums text-muted-foreground\">\n              <span className=\"text-[15px] font-semibold text-foreground\">\n                {doneCount}\n              </span>{\" \"}\n              / {items.length} done\n            </p>\n          </div>\n\n          <div\n            aria-hidden=\"true\"\n            className=\"mt-3 h-1 w-full overflow-hidden rounded-full bg-muted\"\n          >\n            <motion.span\n              initial={false}\n              animate={{ scaleX: ratio }}\n              transition={reduce ? { duration: 0 } : BAR_SPRING}\n              className=\"block h-full w-full origin-left rounded-full bg-foreground/80\"\n            />\n          </div>\n\n          {items.length > 0 ? (\n            <ul className=\"mt-2\" aria-labelledby={`${uid}-title`}>\n              {items.map((item, index) => (\n                <CheckRow\n                  key={item.id}\n                  item={item}\n                  index={index}\n                  done={isDone(item)}\n                  show={show}\n                  reduce={reduce}\n                  onToggle={toggle}\n                />\n              ))}\n            </ul>\n          ) : (\n            <p className=\"mt-5 text-[13px] text-muted-foreground\">\n              Nothing left to check off.\n            </p>\n          )}\n\n          <AnimatePresence initial={false}>\n            {doneCount > 0 ? (\n              <motion.div\n                initial={reduce ? false : { opacity: 0, height: 0 }}\n                animate={{ opacity: 1, height: \"auto\" }}\n                exit={reduce ? { opacity: 0 } : { opacity: 0, height: 0 }}\n                transition={{ duration: reduce ? 0 : 0.25, ease: EASE }}\n                className=\"overflow-hidden\"\n              >\n                <div className=\"flex justify-end border-t border-border pt-3\">\n                  <button\n                    type=\"button\"\n                    onClick={clearCompleted}\n                    className=\"rounded-sm text-[12.5px] font-medium text-muted-foreground underline-offset-4 transition-colors hover:text-foreground hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                  >\n                    Clear completed\n                  </button>\n                </div>\n              </motion.div>\n            ) : null}\n          </AnimatePresence>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default SpringCheckbox;\n"}]}