{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spring-switch","type":"registry:block","title":"Spring switch","description":"Notification rows with a physical switch: the knob glides on a spring, the track fills and a small live count follows.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/spring-switch/spring-switch.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useRef, useState } from \"react\";\nimport {\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 KNOB_SPRING = { type: \"spring\", stiffness: 520, damping: 40 } as const;\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type SpringSwitchItem = {\n  id: string;\n  title: string;\n  description: string;\n  defaultOn?: boolean;\n};\n\nexport type SpringSwitchProps = {\n  className?: string;\n  title?: string;\n  subtitle?: string;\n  items?: SpringSwitchItem[];\n  /** Called with the row id and its new state. */\n  onChange?: (id: string, on: boolean) => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_ITEMS: SpringSwitchItem[] = [\n  {\n    id: \"digest\",\n    title: \"Weekly digest\",\n    description:\n      \"A Monday summary of mentions, replies and threads still waiting on you.\",\n    defaultOn: true,\n  },\n  {\n    id: \"mentions\",\n    title: \"Mentions and reviews\",\n    description:\n      \"Skip everything except direct mentions and review requests assigned to you.\",\n    defaultOn: true,\n  },\n  {\n    id: \"updates\",\n    title: \"Product updates\",\n    description:\n      \"New blocks, changelog entries and the occasional note from the team.\",\n    defaultOn: false,\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function SpringSwitch({\n  className,\n  title = \"Notifications\",\n  subtitle = \"Choose what reaches your inbox.\",\n  items = DEFAULT_ITEMS,\n  onChange,\n}: SpringSwitchProps) {\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  const [state, setState] = useState<Record<string, boolean>>({});\n\n  const isOn = (item: SpringSwitchItem) =>\n    state[item.id] ?? item.defaultOn ?? false;\n\n  const onCount = items.filter(isOn).length;\n\n  const toggle = (item: SpringSwitchItem) => {\n    const next = !isOn(item);\n    setState((current) => ({ ...current, [item.id]: next }));\n    onChange?.(item.id, next);\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-start justify-between gap-3\">\n            <div className=\"min-w-0\">\n              <h2 className=\"text-[15px] font-semibold tracking-tight\">\n                {title}\n              </h2>\n              <p className=\"mt-0.5 text-[13px] text-muted-foreground\">\n                {subtitle}\n              </p>\n            </div>\n            <span className=\"shrink-0 text-[12px] tabular-nums text-muted-foreground\">\n              {onCount} of {items.length} on\n            </span>\n          </div>\n\n          {items.length > 0 ? (\n            <ul className=\"mt-4 border-t border-border\">\n              {items.map((item, index) => {\n                const on = isOn(item);\n                const labelId = `${uid}-switch-label-${index}`;\n                const descId = `${uid}-switch-desc-${index}`;\n                return (\n                  <motion.li\n                    key={item.id}\n                    initial={reduce ? false : { opacity: 0, y: 8 }}\n                    animate={\n                      show\n                        ? { opacity: 1, y: 0 }\n                        : { opacity: 0, y: reduce ? 0 : 8 }\n                    }\n                    transition={{\n                      duration: reduce ? 0 : 0.45,\n                      ease: EASE,\n                      delay: reduce ? 0 : index * 0.05,\n                    }}\n                    className=\"flex items-start justify-between gap-4 border-b border-border py-4 last:border-b-0\"\n                  >\n                    <div className=\"min-w-0\">\n                      <p\n                        id={labelId}\n                        className=\"text-[14px] font-medium leading-5 tracking-[-0.01em]\"\n                      >\n                        {item.title}\n                      </p>\n                      <p\n                        id={descId}\n                        className=\"mt-1 text-[12.5px] leading-5 text-muted-foreground\"\n                      >\n                        {item.description}\n                      </p>\n                    </div>\n\n                    <motion.button\n                      type=\"button\"\n                      role=\"switch\"\n                      aria-checked={on}\n                      aria-labelledby={labelId}\n                      aria-describedby={descId}\n                      onClick={() => toggle(item)}\n                      whileTap={reduce ? undefined : { scale: 0.95 }}\n                      transition={reduce ? { duration: 0 } : KNOB_SPRING}\n                      className={cn(\n                        \"relative mt-0.5 inline-flex h-6 w-11 shrink-0 items-center rounded-full border p-0.5 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n                        on\n                          ? \"border-foreground/20 bg-foreground\"\n                          : \"border-border bg-muted hover:bg-muted/70\"\n                      )}\n                    >\n                      <motion.span\n                        aria-hidden=\"true\"\n                        initial={false}\n                        animate={{ x: on ? 20 : 0 }}\n                        transition={reduce ? { duration: 0 } : KNOB_SPRING}\n                        className={cn(\n                          \"grid h-5 w-5 place-items-center rounded-full bg-background shadow-sm\",\n                          on ? null : \"border border-border\"\n                        )}\n                      >\n                        <motion.span\n                          initial={false}\n                          animate={{\n                            opacity: on ? 1 : 0,\n                            scale: on ? 1 : 0.4,\n                          }}\n                          transition={\n                            reduce\n                              ? { duration: 0 }\n                              : { duration: 0.18, ease: EASE }\n                          }\n                          className=\"h-1.5 w-1.5 rounded-full bg-foreground\"\n                        />\n                      </motion.span>\n                    </motion.button>\n                  </motion.li>\n                );\n              })}\n            </ul>\n          ) : (\n            <p className=\"mt-5 text-[13px] text-muted-foreground\">\n              No notification channels yet.\n            </p>\n          )}\n\n          <p className=\"mt-4 flex items-center gap-2 text-[12px] text-muted-foreground\">\n            <span\n              aria-hidden=\"true\"\n              className=\"h-1.5 w-1.5 shrink-0 rounded-full bg-success\"\n            />\n            Changes save as you toggle.\n          </p>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default SpringSwitch;\n"}]}