{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"animated-tabs","type":"registry:block","title":"Animated tabs","description":"A segmented view switcher whose pill glides between tabs on a spring while the panel slides and the card height follows.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/animated-tabs/animated-tabs.tsx","type":"registry:component","content":"\"use client\";\n\nimport {\n  useEffect,\n  useId,\n  useRef,\n  useState,\n  type KeyboardEvent,\n} 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];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type AnimatedTabRow = {\n  label: string;\n  value: string;\n};\n\nexport type AnimatedTab = {\n  id: string;\n  label: string;\n  /** Headline value shown inside the panel. */\n  value: string;\n  /** One line under the value, e.g. \"+12.6% vs last week\". */\n  note: string;\n  /** Label/value rows under the headline. */\n  rows: AnimatedTabRow[];\n};\n\nexport type AnimatedTabsProps = {\n  className?: string;\n  title?: string;\n  subtitle?: string;\n  tabs?: AnimatedTab[];\n  /** Tab selected on first render. Defaults to the first tab. */\n  defaultTab?: string;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_TABS: AnimatedTab[] = [\n  {\n    id: \"requests\",\n    label: \"Requests\",\n    value: \"2.41M\",\n    note: \"+12.6% vs last week\",\n    rows: [\n      { label: \"Average latency\", value: \"184 ms\" },\n      { label: \"Error rate\", value: \"0.07%\" },\n      { label: \"Busiest hour\", value: \"14:00\" },\n    ],\n  },\n  {\n    id: \"storage\",\n    label: \"Storage\",\n    value: \"486 GB\",\n    note: \"62% of the 780 GB plan\",\n    rows: [\n      { label: \"Objects\", value: \"1.28M\" },\n      { label: \"Egress this month\", value: \"92 GB\" },\n      { label: \"Backups\", value: \"14\" },\n    ],\n  },\n  {\n    id: \"seats\",\n    label: \"Seats\",\n    value: \"27\",\n    note: \"3 invites waiting\",\n    rows: [\n      { label: \"Active today\", value: \"19\" },\n      { label: \"Administrators\", value: \"4\" },\n      { label: \"Last joined\", value: \"2 days ago\" },\n    ],\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* panel                                                               */\n/* ------------------------------------------------------------------ */\n\nfunction TabPanel({ tab }: { tab: AnimatedTab }) {\n  return (\n    <div className=\"rounded-lg bg-muted/40 p-4 sm:p-5\">\n      <p className=\"text-[12px] font-medium text-muted-foreground\">\n        {tab.label}\n      </p>\n      <p className=\"mt-1 text-[30px] font-semibold leading-none tracking-tight tabular-nums\">\n        {tab.value}\n      </p>\n      <p className=\"mt-1.5 text-[12.5px] text-muted-foreground\">{tab.note}</p>\n\n      <dl className=\"mt-4 border-t border-border\">\n        {tab.rows.map((row) => (\n          <div\n            key={row.label}\n            className=\"flex items-baseline justify-between gap-4 border-b border-border/70 py-2 last:border-b-0\"\n          >\n            <dt className=\"min-w-0 text-[12.5px] text-muted-foreground\">\n              {row.label}\n            </dt>\n            <dd className=\"shrink-0 text-[12.5px] font-medium tabular-nums text-foreground\">\n              {row.value}\n            </dd>\n          </div>\n        ))}\n      </dl>\n    </div>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function AnimatedTabs({\n  className,\n  title = \"Project health\",\n  subtitle = \"Northwind Labs · production\",\n  tabs = DEFAULT_TABS,\n  defaultTab,\n}: AnimatedTabsProps) {\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 pillId = `animated-tabs-pill-${uid}`;\n  const idBase = `animated-tabs-${uid}`;\n\n  const [active, setActive] = useState(() => {\n    if (defaultTab && tabs.some((tab) => tab.id === defaultTab)) {\n      return defaultTab;\n    }\n    return tabs[0]?.id ?? \"\";\n  });\n\n  const found = tabs.findIndex((tab) => tab.id === active);\n  const index = found < 0 ? 0 : found;\n  const current = tabs[index];\n\n  const tabRefs = useRef<Array<HTMLButtonElement | null>>([]);\n  const panelRefs = useRef<Array<HTMLDivElement | null>>([]);\n  const [height, setHeight] = useState<number | null>(null);\n\n  /* The track keeps every panel mounted, so measure the active one to let the\n     container follow its height on the same spring as the slide. */\n  useEffect(() => {\n    const el = panelRefs.current[index];\n    if (!el) return;\n    const update = () => setHeight(el.offsetHeight);\n    update();\n    const observer = new ResizeObserver(update);\n    observer.observe(el);\n    return () => observer.disconnect();\n  }, [index, tabs]);\n\n  const select = (id: string) => {\n    setActive(id);\n  };\n\n  const onKeyDown = (\n    event: KeyboardEvent<HTMLButtonElement>,\n    position: number\n  ) => {\n    if (tabs.length === 0) return;\n    let next = position;\n    if (event.key === \"ArrowRight\") {\n      next = (position + 1) % tabs.length;\n    } else if (event.key === \"ArrowLeft\") {\n      next = (position - 1 + tabs.length) % tabs.length;\n    } else if (event.key === \"Home\") {\n      next = 0;\n    } else if (event.key === \"End\") {\n      next = tabs.length - 1;\n    } else {\n      return;\n    }\n    event.preventDefault();\n    setActive(tabs[next].id);\n    tabRefs.current[next]?.focus();\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-4\">\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\n            {tabs.length > 0 ? (\n              <div\n                role=\"tablist\"\n                aria-label={title}\n                aria-orientation=\"horizontal\"\n                className=\"inline-flex max-w-full items-center gap-1 overflow-x-auto rounded-lg bg-muted p-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\"\n              >\n                {tabs.map((tab, position) => {\n                  const selected = position === index;\n                  return (\n                    <button\n                      key={tab.id}\n                      ref={(el) => {\n                        tabRefs.current[position] = el;\n                      }}\n                      type=\"button\"\n                      role=\"tab\"\n                      id={`${idBase}-tab-${position}`}\n                      aria-selected={selected}\n                      aria-controls={`${idBase}-panel-${position}`}\n                      tabIndex={selected ? 0 : -1}\n                      onClick={() => select(tab.id)}\n                      onKeyDown={(event) => onKeyDown(event, position)}\n                      className={cn(\n                        \"relative flex-none rounded-md px-3 py-1.5 text-[13px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                        selected\n                          ? \"text-foreground\"\n                          : \"text-muted-foreground hover:text-foreground\"\n                      )}\n                    >\n                      {selected ? (\n                        <motion.span\n                          layoutId={pillId}\n                          aria-hidden=\"true\"\n                          transition={\n                            reduce\n                              ? { duration: 0 }\n                              : {\n                                  type: \"spring\",\n                                  stiffness: 420,\n                                  damping: 36,\n                                }\n                          }\n                          className=\"absolute inset-0 rounded-md bg-card shadow-sm ring-1 ring-border\"\n                        />\n                      ) : null}\n                      <span className=\"relative z-10 whitespace-nowrap\">\n                        {tab.label}\n                      </span>\n                    </button>\n                  );\n                })}\n              </div>\n            ) : null}\n          </div>\n\n          {current ? (\n            <div className=\"mt-5\">\n              {reduce ? (\n                <div>\n                  {tabs.map((tab, position) => (\n                    <div\n                      key={tab.id}\n                      id={`${idBase}-panel-${position}`}\n                      role=\"tabpanel\"\n                      aria-labelledby={`${idBase}-tab-${position}`}\n                      hidden={position !== index}\n                      tabIndex={position === index ? 0 : -1}\n                      className=\"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\"\n                    >\n                      <TabPanel tab={tab} />\n                    </div>\n                  ))}\n                </div>\n              ) : (\n                <motion.div\n                  initial={false}\n                  animate={height === null ? undefined : { height }}\n                  transition={{\n                    type: \"spring\",\n                    stiffness: 280,\n                    damping: 33,\n                  }}\n                  className=\"overflow-hidden\"\n                >\n                  <motion.div\n                    initial={false}\n                    animate={{ x: `-${index * 100}%` }}\n                    transition={{\n                      type: \"spring\",\n                      stiffness: 280,\n                      damping: 34,\n                    }}\n                    className=\"flex w-full\"\n                  >\n                    {tabs.map((tab, position) => (\n                      <div\n                        key={tab.id}\n                        ref={(el) => {\n                          panelRefs.current[position] = el;\n                        }}\n                        id={`${idBase}-panel-${position}`}\n                        role=\"tabpanel\"\n                        aria-labelledby={`${idBase}-tab-${position}`}\n                        aria-hidden={position !== index}\n                        inert={position !== index}\n                        tabIndex={position === index ? 0 : -1}\n                        className=\"w-full shrink-0 focus-visible:outline-none\"\n                      >\n                        <TabPanel tab={tab} />\n                      </div>\n                    ))}\n                  </motion.div>\n                </motion.div>\n              )}\n            </div>\n          ) : (\n            <p className=\"mt-5 text-[13px] text-muted-foreground\">\n              No views to show yet.\n            </p>\n          )}\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default AnimatedTabs;\n"}]}