{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"moving-radio","type":"registry:block","title":"Moving radio","description":"A radio group with a dot that travels between options on a spring, updating a live schedule summary underneath.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/moving-radio/moving-radio.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 DOT_SPRING = { type: \"spring\", stiffness: 480, damping: 38 } as const;\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type MovingRadioOption = {\n  id: string;\n  label: string;\n  description: string;\n  /** Small note aligned to the right of the label, e.g. \"Next 02:00\". */\n  meta?: string;\n  /** Line shown under the group when this option is selected. */\n  summary: string;\n};\n\nexport type MovingRadioProps = {\n  className?: string;\n  title?: string;\n  subtitle?: string;\n  options?: MovingRadioOption[];\n  /** Option selected on first render. Defaults to the first one. */\n  defaultOption?: string;\n  /** Called with the id of the newly selected option. */\n  onChange?: (id: string) => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_OPTIONS: MovingRadioOption[] = [\n  {\n    id: \"six-hours\",\n    label: \"Every 6 hours\",\n    description: \"Snapshots at 00:00, 06:00, 12:00 and 18:00.\",\n    meta: \"Next 18:00\",\n    summary: \"4 snapshots a day, kept for 7 days.\",\n  },\n  {\n    id: \"daily\",\n    label: \"Every day\",\n    description: \"One snapshot at 02:00, when traffic is at its lowest.\",\n    meta: \"Next 02:00\",\n    summary: \"7 daily snapshots, about 42 GB in total.\",\n  },\n  {\n    id: \"weekly\",\n    label: \"Every week\",\n    description: \"A Sunday snapshot for long-term restore points.\",\n    meta: \"Sun 03:00\",\n    summary: \"12 weekly snapshots, about 96 GB in total.\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function MovingRadio({\n  className,\n  title = \"Backup schedule\",\n  subtitle = \"Applied to the production database.\",\n  options = DEFAULT_OPTIONS,\n  defaultOption,\n  onChange,\n}: MovingRadioProps) {\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 groupName = `moving-radio-${uid}`;\n  const dotId = `moving-radio-dot-${uid}`;\n\n  const [selected, setSelected] = useState(() => {\n    if (defaultOption && options.some((option) => option.id === defaultOption)) {\n      return defaultOption;\n    }\n    return options[0]?.id ?? \"\";\n  });\n\n  const found = options.findIndex((option) => option.id === selected);\n  const index = found < 0 ? 0 : found;\n  const current = options[index];\n\n  const choose = (id: string) => {\n    setSelected(id);\n    onChange?.(id);\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=\"min-w-0\">\n            <h2 className=\"text-[15px] font-semibold tracking-tight\">{title}</h2>\n            <p className=\"mt-0.5 text-[13px] text-muted-foreground\">\n              {subtitle}\n            </p>\n          </div>\n\n          {options.length > 0 && current ? (\n            <>\n              <fieldset className=\"mt-4\">\n                <legend className=\"sr-only\">{title}</legend>\n                <div className=\"overflow-hidden rounded-lg border border-border\">\n                  {options.map((option, position) => {\n                    const checked = position === index;\n                    return (\n                      <label\n                        key={option.id}\n                        className={cn(\n                          \"flex cursor-pointer items-start gap-3 px-3 py-3.5 transition-colors sm:px-4\",\n                          checked ? \"bg-muted/50\" : \"hover:bg-muted/30\",\n                          position > 0 && \"border-t border-border\"\n                        )}\n                      >\n                        <input\n                          type=\"radio\"\n                          name={groupName}\n                          value={option.id}\n                          checked={checked}\n                          onChange={() => choose(option.id)}\n                          className=\"peer sr-only\"\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-full border bg-background transition-colors duration-200 peer-focus-visible:ring-2 peer-focus-visible:ring-ring peer-focus-visible:ring-offset-2 peer-focus-visible:ring-offset-background\",\n                            checked\n                              ? \"border-foreground\"\n                              : \"border-border\"\n                          )}\n                        >\n                          {checked ? (\n                            <motion.span\n                              layoutId={dotId}\n                              transition={\n                                reduce ? { duration: 0 } : DOT_SPRING\n                              }\n                              className=\"h-2 w-2 rounded-full bg-foreground\"\n                            />\n                          ) : null}\n                        </span>\n\n                        <span className=\"min-w-0 flex-1\">\n                          <span className=\"flex flex-wrap items-baseline justify-between gap-x-3 gap-y-0.5\">\n                            <span\n                              className={cn(\n                                \"text-[14px] font-medium leading-5 tracking-[-0.01em] transition-colors\",\n                                checked\n                                  ? \"text-foreground\"\n                                  : \"text-foreground/85\"\n                              )}\n                            >\n                              {option.label}\n                            </span>\n                            {option.meta ? (\n                              <span className=\"text-[11.5px] tabular-nums text-muted-foreground\">\n                                {option.meta}\n                              </span>\n                            ) : null}\n                          </span>\n                          <span className=\"mt-0.5 block text-[12.5px] leading-5 text-muted-foreground\">\n                            {option.description}\n                          </span>\n                        </span>\n                      </label>\n                    );\n                  })}\n                </div>\n              </fieldset>\n\n              <div className=\"mt-4 min-h-[44px] rounded-lg bg-muted/40 px-3 py-2.5\">\n                <AnimatePresence mode=\"wait\" initial={false}>\n                  <motion.p\n                    key={current.id}\n                    initial={reduce ? false : { opacity: 0, y: 4 }}\n                    animate={{ opacity: 1, y: 0 }}\n                    exit={reduce ? undefined : { opacity: 0, y: -4 }}\n                    transition={{\n                      duration: reduce ? 0 : 0.2,\n                      ease: EASE,\n                    }}\n                    className=\"text-[12.5px] leading-5 text-muted-foreground\"\n                  >\n                    <span className=\"font-medium text-foreground\">\n                      {current.label}.\n                    </span>{\" \"}\n                    {current.summary}\n                  </motion.p>\n                </AnimatePresence>\n              </div>\n            </>\n          ) : (\n            <p className=\"mt-5 text-[13px] text-muted-foreground\">\n              No schedule options yet.\n            </p>\n          )}\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default MovingRadio;\n"}]}