{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"tab-bar","type":"registry:component","title":"Tab bar","description":"A phone tab bar whose pill slides over and fills in the icon it lands on.","author":"Yash Bavadiya <https://xevrion.dev>","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"components/tab-bar.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useId, useRef, useState } from \"react\";\nimport { AnimatePresence, LayoutGroup, motion } from \"motion/react\";\nimport { useReducedMotion } from \"@/hooks/use-reduced-motion\";\nimport { cn } from \"@/lib/utils\";\n\nexport type TabBarItem = {\n  id: string;\n  label: string;\n  // Drawn once and reused for both states: outline when idle, filled when\n  // active, so the two can never drift apart.\n  icon: React.ReactNode;\n};\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\n// The pill and the widths share one spring so the row reflows as a single\n// motion. No bounce: a pill that overshoots its tab reads as imprecise.\nconst SLIDE = { type: \"spring\", visualDuration: 0.3, bounce: 0 } as const;\nconst INSTANT = { duration: 0 } as const;\n// Waits a beat for the tab to start widening, so the label fades into room\n// that is opening rather than being squeezed.\nconst LABEL_IN = { duration: 0.2, delay: 0.05, ease: EASE_OUT };\nconst LABEL_OUT = { duration: 0.1, ease: EASE_OUT };\n\nexport function TabBar({\n  items,\n  value,\n  onChange,\n  label,\n  idBase,\n  className,\n}: {\n  items: readonly TabBarItem[];\n  value: string;\n  onChange: (id: string) => void;\n  label: string;\n  // Lets the caller point tab panels back at their tabs.\n  idBase: string;\n  className?: string;\n}) {\n  const reduce = useReducedMotion() ?? false;\n  const refs = useRef(new Map<string, HTMLButtonElement>());\n  const index = Math.max(\n    items.findIndex((t) => t.id === value),\n    0,\n  );\n  const layout = reduce ? INSTANT : SLIDE;\n\n  return (\n    <LayoutGroup id={idBase}>\n      <div\n        role=\"tablist\"\n        aria-label={label}\n        className={cn(\"flex items-center justify-between\", className)}\n        onKeyDown={(e) => {\n          const target = {\n            ArrowRight: index + 1,\n            ArrowLeft: index - 1,\n            Home: 0,\n            End: items.length - 1,\n          }[e.key];\n          if (target === undefined) return;\n          e.preventDefault();\n          // Automatic activation, animated exactly like a tap.\n          const next = items[(target + items.length) % items.length].id;\n          onChange(next);\n          refs.current.get(next)?.focus();\n        }}\n      >\n        {items.map((item) => {\n          const active = item.id === items[index].id;\n          return (\n            <motion.button\n              key={item.id}\n              ref={(el) => {\n                if (el) refs.current.set(item.id, el);\n                else refs.current.delete(item.id);\n              }}\n              type=\"button\"\n              role=\"tab\"\n              id={`${idBase}-tab-${item.id}`}\n              aria-selected={active}\n              aria-controls={active ? `${idBase}-panel-${item.id}` : undefined}\n              aria-label={item.label}\n              tabIndex={active ? 0 : -1}\n              onClick={() => onChange(item.id)}\n              layout\n              // Only a change of tab moves anything, so unrelated renders\n              // skip the measure.\n              layoutDependency={value}\n              transition={{ layout }}\n              // Radius set in style so Motion can correct it while the\n              // button's box is scaled mid-morph.\n              style={{ borderRadius: 24 }}\n              className={cn(\n                \"group relative flex h-12 touch-manipulation items-center overflow-hidden px-3 outline-hidden select-none focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground\",\n                active ? \"text-foreground\" : \"text-muted-foreground\",\n                \"transition-[color] duration-150 ease-out\",\n              )}\n            >\n              {active && (\n                <motion.span\n                  layoutId=\"pill\"\n                  aria-hidden\n                  className=\"absolute inset-0 bg-foreground/[0.07]\"\n                  style={{ borderRadius: 24 }}\n                  transition={{ layout }}\n                />\n              )}\n              {/* Position-only, so the icon and label ride along with the\n                  morph without being stretched by it. */}\n              <motion.span\n                layout=\"position\"\n                layoutDependency={value}\n                transition={{ layout }}\n                className=\"relative flex\"\n              >\n                <span className=\"relative flex items-center gap-2 transition-[scale] duration-150 ease-out group-active:scale-[0.96] motion-reduce:transition-none\">\n                  <Icon active={active}>{item.icon}</Icon>\n                  <AnimatePresence initial={false} mode=\"popLayout\">\n                    {active && (\n                      <motion.span\n                        key=\"label\"\n                        aria-hidden\n                        initial={{\n                          opacity: 0,\n                          filter: reduce ? \"blur(0px)\" : \"blur(4px)\",\n                        }}\n                        animate={{\n                          opacity: 1,\n                          filter: \"blur(0px)\",\n                          transition: LABEL_IN,\n                        }}\n                        exit={{\n                          opacity: 0,\n                          filter: \"blur(0px)\",\n                          transition: LABEL_OUT,\n                        }}\n                        className=\"pr-1 text-sm font-medium whitespace-nowrap\"\n                      >\n                        {item.label}\n                      </motion.span>\n                    )}\n                  </AnimatePresence>\n                </span>\n              </motion.span>\n            </motion.button>\n          );\n        })}\n      </div>\n    </LayoutGroup>\n  );\n}\n\nfunction Icon({\n  active,\n  children,\n}: {\n  active: boolean;\n  children: React.ReactNode;\n}) {\n  return (\n    <span className=\"grid size-6 shrink-0\" aria-hidden>\n      <svg\n        viewBox=\"0 0 24 24\"\n        className=\"col-start-1 row-start-1 size-6\"\n        fill=\"none\"\n        stroke=\"currentColor\"\n        strokeWidth={1.75}\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      >\n        {children}\n      </svg>\n      {/* The fill floods out from the icon's middle and drains back in: a\n          clip-path transition, so a quick second tap reverses it midway. */}\n      <svg\n        viewBox=\"0 0 24 24\"\n        className={cn(\n          \"col-start-1 row-start-1 size-6 transition-[clip-path,opacity] ease-[cubic-bezier(0.23,1,0.32,1)] motion-reduce:[clip-path:none]\",\n          active\n            ? \"[clip-path:circle(75%_at_50%_55%)] opacity-100 duration-200\"\n            : \"[clip-path:circle(0%_at_50%_55%)] opacity-0 duration-150\",\n        )}\n        fill=\"currentColor\"\n        stroke=\"currentColor\"\n        strokeWidth={1.75}\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n      >\n        {children}\n      </svg>\n    </span>\n  );\n}\n\nconst ITEMS: TabBarItem[] = [\n  {\n    id: \"home\",\n    label: \"Home\",\n    icon: (\n      <path d=\"M4 10.25 12 4l8 6.25V19a1 1 0 0 1-1 1h-4.5v-5.25h-5V20H5a1 1 0 0 1-1-1Z\" />\n    ),\n  },\n  {\n    id: \"search\",\n    label: \"Search\",\n    icon: (\n      <>\n        <circle cx=\"11\" cy=\"11\" r=\"6.25\" />\n        <path d=\"m20 20-4.5-4.5\" />\n      </>\n    ),\n  },\n  {\n    id: \"activity\",\n    label: \"Activity\",\n    icon: (\n      <>\n        <path d=\"M6 16.5V11a6 6 0 0 1 12 0v5.5l1.5 2h-15Z\" />\n        <path d=\"M10 20.75a2 2 0 0 0 4 0Z\" />\n      </>\n    ),\n  },\n  {\n    id: \"profile\",\n    label: \"Profile\",\n    icon: (\n      <>\n        <circle cx=\"12\" cy=\"8.5\" r=\"3.75\" />\n        <path d=\"M5.25 20c.7-3.4 3.4-5.5 6.75-5.5s6.05 2.1 6.75 5.5Z\" />\n      </>\n    ),\n  },\n];\n\nconst panel = {\n  enter: (reduce: boolean) => ({\n    opacity: 0,\n    y: reduce ? 0 : 6,\n    filter: reduce ? \"blur(0px)\" : \"blur(4px)\",\n  }),\n  center: {\n    opacity: 1,\n    y: 0,\n    filter: \"blur(0px)\",\n    transition: { duration: 0.24, ease: EASE_OUT },\n  },\n  // Faster and without movement, so the outgoing screen never competes with\n  // the incoming one.\n  exit: {\n    opacity: 0,\n    y: 0,\n    filter: \"blur(0px)\",\n    transition: { duration: 0.12, ease: EASE_OUT },\n  },\n};\n\nfunction Row({\n  title,\n  meta,\n  lead,\n}: {\n  title: string;\n  meta: string;\n  lead?: string;\n}) {\n  return (\n    <li className=\"flex items-center gap-3 py-2.5\">\n      {lead && (\n        <span className=\"flex size-9 shrink-0 items-center justify-center rounded-full bg-muted text-[13px] font-medium text-foreground\">\n          {lead}\n        </span>\n      )}\n      <span className=\"flex min-w-0 flex-col\">\n        <span className=\"truncate text-[15px] text-foreground\">{title}</span>\n        <span className=\"truncate text-[13px] text-muted-foreground\">{meta}</span>\n      </span>\n    </li>\n  );\n}\n\nconst SCREENS: Record<string, { title: string; body: React.ReactNode }> = {\n  home: {\n    title: \"Home\",\n    body: (\n      <>\n        <p className=\"text-[15px] text-muted-foreground\">Three things on today.</p>\n        <ul className=\"mt-3 divide-y divide-border\">\n          <Row title=\"Standup notes\" meta=\"9:30, with the design team\" />\n          <Row title=\"Review tab bar\" meta=\"11:00, two comments open\" />\n          <Row title=\"Ship the release\" meta=\"Before 5, tagged v2.4\" />\n        </ul>\n      </>\n    ),\n  },\n  search: {\n    title: \"Search\",\n    body: (\n      <>\n        <input\n          type=\"search\"\n          aria-label=\"Search\"\n          placeholder=\"Search notes and people\"\n          className=\"h-11 w-full rounded-xl bg-muted px-3.5 text-[15px] text-foreground outline-hidden placeholder:text-muted-foreground focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-foreground\"\n        />\n        <p className=\"mt-5 text-[13px] font-medium text-muted-foreground\">Recent</p>\n        <ul className=\"mt-1 divide-y divide-border\">\n          <Row title=\"spring presets\" meta=\"Searched yesterday\" />\n          <Row title=\"Mira Chen\" meta=\"Person\" />\n          <Row title=\"release checklist\" meta=\"Searched Monday\" />\n        </ul>\n      </>\n    ),\n  },\n  activity: {\n    title: \"Activity\",\n    body: (\n      <ul className=\"divide-y divide-border\">\n        <Row lead=\"MC\" title=\"Mira liked your note\" meta=\"2 minutes ago\" />\n        <Row lead=\"JO\" title=\"Jon replied in Review\" meta=\"18 minutes ago\" />\n        <Row lead=\"AK\" title=\"Ana shared a folder\" meta=\"1 hour ago\" />\n      </ul>\n    ),\n  },\n  profile: {\n    title: \"Profile\",\n    body: (\n      <>\n        <div className=\"flex items-center gap-3\">\n          <span className=\"flex size-12 items-center justify-center rounded-full bg-muted text-[15px] font-medium text-foreground\">\n            YB\n          </span>\n          <span className=\"flex flex-col\">\n            <span className=\"text-[15px] font-medium text-foreground\">\n              Yash Bavadiya\n            </span>\n            <span className=\"text-[13px] text-muted-foreground\">@xevrion</span>\n          </span>\n        </div>\n        <ul className=\"mt-4 divide-y divide-border\">\n          <Row title=\"Notifications\" meta=\"Mentions and replies\" />\n          <Row title=\"Appearance\" meta=\"Follows the system\" />\n          <Row title=\"Privacy\" meta=\"Profile visible to team\" />\n        </ul>\n      </>\n    ),\n  },\n};\n\nexport default function TabBarDemo() {\n  const reduce = useReducedMotion() ?? false;\n  const base = useId();\n  const [tab, setTab] = useState(\"home\");\n  const screen = SCREENS[tab];\n\n  return (\n    // 44px outer radius = the screen's 36px plus the 8px bezel.\n    <div className=\"flex h-[560px] w-[min(320px,100%)] flex-col rounded-[44px] bg-muted p-2 shadow-raised\">\n      <div className=\"relative flex flex-1 flex-col overflow-hidden rounded-[36px] bg-background\">\n        <div\n          aria-hidden\n          className=\"flex h-11 shrink-0 items-center justify-between px-7 pt-1 text-[13px] font-semibold text-foreground tabular-nums\"\n        >\n          <span>9:41</span>\n          <svg viewBox=\"0 0 26 12\" className=\"h-3 w-[26px]\" fill=\"none\">\n            <rect\n              x=\"0.5\"\n              y=\"0.5\"\n              width=\"22\"\n              height=\"11\"\n              rx=\"3\"\n              stroke=\"currentColor\"\n              opacity=\"0.4\"\n            />\n            <rect x=\"2\" y=\"2\" width=\"15\" height=\"8\" rx=\"1.75\" fill=\"currentColor\" />\n            <path d=\"M24.5 4v4\" stroke=\"currentColor\" opacity=\"0.4\" strokeLinecap=\"round\" />\n          </svg>\n        </div>\n\n        {/* Screens share one grid cell while they cross, so the old one\n            never pushes the new one around. */}\n        <div className=\"grid min-h-0 flex-1\">\n          <AnimatePresence initial={false} custom={reduce}>\n            <motion.div\n              key={tab}\n              role=\"tabpanel\"\n              id={`${base}-panel-${tab}`}\n              aria-labelledby={`${base}-tab-${tab}`}\n              tabIndex={0}\n              custom={reduce}\n              variants={panel}\n              initial=\"enter\"\n              animate=\"center\"\n              exit=\"exit\"\n              className=\"col-start-1 row-start-1 min-h-0 overflow-hidden px-5 pt-3 outline-hidden focus-visible:outline-2 focus-visible:outline-solid focus-visible:-outline-offset-2 focus-visible:outline-foreground\"\n            >\n              <h3 className=\"text-2xl font-semibold tracking-tight text-foreground\">\n                {screen.title}\n              </h3>\n              <div className=\"mt-3\">{screen.body}</div>\n            </motion.div>\n          </AnimatePresence>\n        </div>\n\n        <div className=\"shrink-0 border-t border-border px-4 pt-2\">\n          <TabBar\n            idBase={base}\n            label=\"Sections\"\n            items={ITEMS}\n            value={tab}\n            onChange={setTab}\n          />\n          <div aria-hidden className=\"mx-auto mt-3 mb-2 h-1 w-28 rounded-full bg-foreground/25\" />\n        </div>\n      </div>\n    </div>\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":["navigation"],"docs":"From ui lab: https://lab.xevrion.dev/lab/tab-bar"}