{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"animated-tabs-indicator","type":"registry:component","title":"Animated Tabs Indicator","description":"Tabs whose indicator stretches between tabs: the leading edge leaves first and the trailing edge follows on a softer spring. Labels flip colour exactly where the pill passes, hover shows a ghost highlight, optional panels slide by direction. No dependencies.","dependencies":[],"registryDependencies":[],"files":[{"path":"components/viberdy/animated-tabs-indicator.tsx","content":"\"use client\";\n\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport type { KeyboardEvent, PointerEvent, ReactNode } from \"react\";\n\nconst DEFAULT_TABS = [\"Overview\", \"Pricing\", \"Reviews\"];\nconst SAFE_COLOR = /^(#[0-9a-f]{3,8}|[a-z]{3,20})$/i;\n\n// Two springs per travel: the leading edge is stiff and fast, the trailing edge is soft and\n// late, so the pill stretches across the gap and then catches up. [stiffness, damping]\nconst LEAD: [number, number] = [520, 36];\nconst TRAIL: [number, number] = [160, 22];\n\nconst TRACK = {\n  light: { bg: \"#f5f5f5\", border: \"#e5e5e5\", text: \"#737373\", hover: \"#262626\", ghost: \"rgba(0,0,0,0.08)\" },\n  dark: { bg: \"rgba(255,255,255,0.05)\", border: \"rgba(255,255,255,0.1)\", text: \"rgba(255,255,255,0.55)\", hover: \"#ffffff\", ghost: \"rgba(255,255,255,0.11)\" },\n} as const;\n\ntype Edge = { x: number; v: number; t: number; k: number; c: number };\n\n/**\n * Tabs with a sliding indicator that stretches between tabs: the edge facing the destination\n * leaves first, the other edge follows on a softer spring. The label colour flips exactly\n * where the pill passes (a clipped copy of the labels rides inside the pill). Optional\n * `panels` crossfade and slide in the direction of travel. Roving tabindex, arrow keys,\n * Home and End. Hand-written springs, no dependencies.\n */\nexport function AnimatedTabs({\n  tabs = DEFAULT_TABS,\n  shape = \"pill\",\n  panels,\n  defaultIndex = 0,\n  onChange,\n  accent = \"#ef4444\",\n  accentText = \"#ffffff\",\n  tone = \"light\",\n  className = \"\",\n  ariaLabel = \"Tabs\",\n}: {\n  tabs?: string[];\n  shape?: \"pill\" | \"rounded\";\n  panels?: ReactNode[];\n  defaultIndex?: number;\n  onChange?: (index: number, tab: string) => void;\n  accent?: string;\n  accentText?: string;\n  tone?: \"light\" | \"dark\";\n  className?: string;\n  ariaLabel?: string;\n}) {\n  const uid = useId().replace(/[^a-zA-Z0-9]/g, \"\");\n  const count = tabs.length;\n  const [picked, setPicked] = useState(defaultIndex);\n  const active = Math.max(0, Math.min(picked, count - 1));\n  const tabsKey = tabs.join(\"\\u0001\");\n  const hasPanels = !!panels;\n  const accentColor = SAFE_COLOR.test(accent) ? accent : \"#ef4444\";\n  const accentInk = SAFE_COLOR.test(accentText) ? accentText : \"#ffffff\";\n  const c = TRACK[tone === \"dark\" ? \"dark\" : \"light\"];\n  const round = shape === \"pill\" ? \"rounded-full\" : \"rounded-lg\";\n  const roundFocus = shape === \"pill\" ? \"focus-visible:!rounded-full\" : \"focus-visible:!rounded-lg\";\n\n  const listRef = useRef<HTMLDivElement>(null);\n  const btnRefs = useRef<(HTMLButtonElement | null)[]>([]);\n  const indRef = useRef<HTMLDivElement>(null);\n  const cloneRef = useRef<HTMLDivElement>(null);\n  const ghostRef = useRef<HTMLDivElement>(null);\n  const engine = useRef<{ go: (i: number, snap: boolean) => void } | null>(null);\n  const activeRef = useRef(active);\n  const onChangeRef = useRef(onChange);\n  useEffect(() => {\n    activeRef.current = active;\n    onChangeRef.current = onChange;\n  });\n\n  // The spring engine: one rAF loop that sleeps at rest. Everything it touches is written to the DOM directly.\n  useEffect(() => {\n    const list = listRef.current;\n    const ind = indRef.current;\n    const clone = cloneRef.current;\n    if (!list || !ind || !clone) return;\n    const L: Edge = { x: 0, v: 0, t: 0, k: LEAD[0], c: LEAD[1] };\n    const R: Edge = { x: 0, v: 0, t: 0, k: LEAD[0], c: LEAD[1] };\n    let top = 0;\n    let height = 0;\n    let shown = false;\n    let raf = 0;\n    let last = 0;\n    const reduced = () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n\n    const write = () => {\n      const w = Math.max(0, R.x - L.x);\n      const rest = (btnRefs.current[activeRef.current]?.offsetWidth ?? w) || 1;\n      const stretch = Math.max(0, Math.min(1, (w - rest) / (rest * 1.4)));\n      ind.style.transform = \"translate3d(\" + L.x.toFixed(2) + \"px,\" + top + \"px,0) scale(1,\" + (1 - stretch * 0.1).toFixed(3) + \")\";\n      ind.style.width = w.toFixed(2) + \"px\";\n      ind.style.height = height + \"px\";\n      clone.style.transform = \"translate3d(\" + (-L.x).toFixed(2) + \"px,\" + -top + \"px,0)\";\n      if (!shown) {\n        shown = true;\n        ind.style.visibility = \"visible\";\n      }\n    };\n    const measure = (i: number) => {\n      const b = btnRefs.current[i];\n      if (!b) return false;\n      L.t = b.offsetLeft;\n      R.t = b.offsetLeft + b.offsetWidth;\n      top = b.offsetTop;\n      height = b.offsetHeight;\n      return true;\n    };\n    const step = (dt: number) => {\n      for (const e of [L, R]) {\n        e.v += (-e.k * (e.x - e.t) - e.c * e.v) * dt;\n        e.x += e.v * dt;\n      }\n    };\n    const tick = (now: number) => {\n      const dt = Math.min(0.05, (now - last) / 1000);\n      last = now;\n      const n = Math.max(1, Math.ceil(dt / 0.004));\n      for (let s = 0; s < n; s++) step(dt / n);\n      const done = [L, R].every((e) => Math.abs(e.x - e.t) < 0.02 && Math.abs(e.v) < 0.05);\n      if (done) {\n        L.x = L.t;\n        R.x = R.t;\n        L.v = R.v = 0;\n        write();\n        raf = 0;\n        return;\n      }\n      write();\n      raf = requestAnimationFrame(tick);\n    };\n    const wake = () => {\n      if (!raf) {\n        last = performance.now();\n        raf = requestAnimationFrame(tick);\n      }\n    };\n    engine.current = {\n      go(i, snap) {\n        const oldL = L.t;\n        if (!measure(i)) return;\n        if (snap || !shown || reduced()) {\n          L.x = L.t;\n          R.x = R.t;\n          L.v = R.v = 0;\n          write();\n          return;\n        }\n        const right = L.t >= oldL;\n        const lead = right ? R : L;\n        const trail = right ? L : R;\n        [lead.k, lead.c] = LEAD;\n        [trail.k, trail.c] = TRAIL;\n        wake();\n      },\n    };\n    engine.current.go(activeRef.current, true);\n    const ro = new ResizeObserver(() => engine.current?.go(activeRef.current, true));\n    ro.observe(list);\n    for (const b of btnRefs.current) if (b) ro.observe(b);\n    const fonts = document.fonts;\n    if (fonts) void fonts.ready.then(() => engine.current?.go(activeRef.current, true));\n    return () => {\n      ro.disconnect();\n      if (raf) cancelAnimationFrame(raf);\n      engine.current = null;\n    };\n  }, [tabsKey, shape]);\n\n  useEffect(() => {\n    engine.current?.go(active, false);\n  }, [active]);\n\n  const choose = (i: number) => {\n    if (i === active) return;\n    setPicked(i);\n    onChangeRef.current?.(i, tabs[i] ?? \"\");\n  };\n\n  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n    const key = e.key;\n    let next = -1;\n    if (key === \"ArrowRight\" || key === \"ArrowDown\") next = (active + 1) % count;\n    else if (key === \"ArrowLeft\" || key === \"ArrowUp\") next = (active - 1 + count) % count;\n    else if (key === \"Home\") next = 0;\n    else if (key === \"End\") next = count - 1;\n    if (next < 0) return;\n    e.preventDefault();\n    choose(next);\n    btnRefs.current[next]?.focus();\n  };\n\n  const showGhost = (i: number, e: PointerEvent<HTMLButtonElement>) => {\n    const g = ghostRef.current;\n    const b = btnRefs.current[i];\n    if (!g || !b || e.pointerType !== \"mouse\" || i === active) return;\n    const wasHidden = g.style.opacity !== \"1\";\n    if (wasHidden) g.style.transition = \"none\";\n    g.style.transform = \"translate3d(\" + b.offsetLeft + \"px,\" + b.offsetTop + \"px,0)\";\n    g.style.width = b.offsetWidth + \"px\";\n    g.style.height = b.offsetHeight + \"px\";\n    if (wasHidden) void g.offsetWidth;\n    g.style.transition = \"\";\n    g.style.opacity = \"1\";\n  };\n  const hideGhost = () => {\n    if (ghostRef.current) ghostRef.current.style.opacity = \"0\";\n  };\n\n  const labelBox =\n    \"flex shrink-0 items-center justify-center whitespace-nowrap px-[var(--atb-px,16px)] h-[var(--atb-h,40px)] text-[length:var(--atb-fs,14px)] font-semibold leading-none \" + round;\n\n  const bar = (\n    <div\n      ref={listRef}\n      role=\"tablist\"\n      aria-label={ariaLabel}\n      onKeyDown={onKeyDown}\n      onPointerLeave={hideGhost}\n      className={\n        \"relative inline-flex max-w-full select-none gap-1 border p-1 \" +\n        (shape === \"pill\" ? \"rounded-full\" : \"rounded-xl\") +\n        (hasPanels ? \" self-start\" : \" \" + className)\n      }\n      style={{ background: c.bg, borderColor: c.border, WebkitTapHighlightColor: \"transparent\" }}\n    >\n      <div\n        ref={ghostRef}\n        aria-hidden\n        className={\"pointer-events-none absolute left-0 top-0 opacity-0 transition-[opacity] duration-200 motion-reduce:transition-none \" + round}\n        style={{ background: c.ghost, willChange: \"transform, width\" }}\n      />\n      {tabs.map((tab, i) => {\n        const on = i === active;\n        return (\n          <button\n            key={tab + \"\\u0001\" + i}\n            ref={(el) => {\n              btnRefs.current[i] = el;\n            }}\n            id={\"atb\" + uid + \"t\" + i}\n            type=\"button\"\n            role=\"tab\"\n            aria-selected={on}\n            aria-controls={hasPanels ? \"atb\" + uid + \"p\" + i : undefined}\n            tabIndex={on ? 0 : -1}\n            onClick={() => choose(i)}\n            onPointerEnter={(e) => showGhost(i, e)}\n            className={\n              \"group relative cursor-pointer border-0 bg-transparent p-0 transition-colors duration-200 focus-visible:!outline-2 focus-visible:!outline-offset-2 focus-visible:!outline-sky-500 \" +\n              round +\n              \" \" +\n              roundFocus\n            }\n            style={{ color: c.text }}\n            onMouseEnter={(e) => {\n              if (!on) e.currentTarget.style.color = c.hover;\n            }}\n            onMouseLeave={(e) => {\n              e.currentTarget.style.color = c.text;\n            }}\n          >\n            <span className={labelBox + \" transition-transform duration-150 ease-out group-active:scale-[0.94] motion-reduce:transition-none\"}>{tab}</span>\n          </button>\n        );\n      })}\n      <div\n        ref={indRef}\n        aria-hidden\n        className={\"pointer-events-none absolute left-0 top-0 \" + round}\n        style={{\n          visibility: \"hidden\",\n          willChange: \"transform, width\",\n          transformOrigin: \"50% 50%\",\n          boxShadow:\n            \"0 6px 16px -6px color-mix(in srgb, \" + accentColor + \" 70%, transparent), 0 1px 2px color-mix(in srgb, \" + accentColor + \" 40%, transparent)\",\n        }}\n      >\n        <div\n          className={\"absolute inset-0 overflow-hidden \" + round}\n          style={{\n            backgroundColor: accentColor,\n            backgroundImage: \"linear-gradient(180deg, rgba(255,255,255,0.24), rgba(255,255,255,0) 60%)\",\n            boxShadow: \"inset 0 1px 0 rgba(255,255,255,0.35), inset 0 -1px 0 rgba(0,0,0,0.12)\",\n          }}\n        >\n          <div ref={cloneRef} className=\"absolute left-0 top-0 inline-flex gap-1 p-1\" style={{ color: accentInk }}>\n            {tabs.map((tab, i) => (\n              <span key={tab + \"\\u0001\" + i} className={labelBox}>\n                {tab}\n              </span>\n            ))}\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n\n  if (!hasPanels) return bar;\n\n  return (\n    <div className={\"inline-flex flex-col gap-4 \" + className}>\n      {bar}\n      <div className=\"grid\">\n        {tabs.map((tab, i) => {\n          const rel = i - active;\n          const on = rel === 0;\n          return (\n            <div\n              key={tab + \"\\u0001\" + i}\n              id={\"atb\" + uid + \"p\" + i}\n              role=\"tabpanel\"\n              aria-labelledby={\"atb\" + uid + \"t\" + i}\n              tabIndex={on ? 0 : -1}\n              className=\"[grid-area:1/1] rounded-lg focus-visible:!outline-2 focus-visible:!outline-offset-2 focus-visible:!outline-sky-500 motion-reduce:!transform-none motion-reduce:!blur-none\"\n              style={{\n                opacity: on ? 1 : 0,\n                visibility: on ? \"visible\" : \"hidden\",\n                transform: on ? \"none\" : \"translate3d(\" + (rel < 0 ? -28 : 28) + \"px,0,0)\",\n                filter: on ? \"none\" : \"blur(4px)\",\n                transition: on\n                  ? \"opacity 380ms cubic-bezier(0.22,1,0.36,1) 60ms, transform 480ms cubic-bezier(0.22,1,0.36,1) 60ms, filter 380ms ease 60ms\"\n                  : \"opacity 200ms ease, transform 300ms cubic-bezier(0.22,1,0.36,1), filter 200ms ease, visibility 0s linear 300ms\",\n              }}\n            >\n              {panels[i] ?? null}\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n","type":"registry:component","target":"components/viberdy/animated-tabs-indicator.tsx"}],"meta":{"tier":"free","tags":["tabs","indicator","spring","nav","segmented-control","gooey","keyboard"],"href":"https://viberdy.dev/library/animated-tabs-indicator","aiPrompt":"Create a single-file React + Tailwind component named AnimatedTabs (a \"use client\" file, React only, no animation library).\n\nProps (all optional): tabs string[] default [\"Overview\",\"Pricing\",\"Reviews\"]; shape \"pill\" | \"rounded\" default \"pill\"; panels ReactNode[] (one per tab; when given, tab panels render under the list); defaultIndex 0; onChange(index, tab); accent \"#ef4444\" and accentText \"#ffffff\" (validate with /^(#[0-9a-f]{3,8}|[a-z]{3,20})$/i); tone \"light\" | \"dark\" (default light); className (on the tablist, or on the outer wrapper when panels are given); ariaLabel \"Tabs\". Size comes from CSS variables --atb-fs (14px), --atb-h (40px), --atb-px (16px).\n\nStructure: a relative inline-flex tablist (1px border, p-1, gap-1, rounded-full for pill or rounded-xl for rounded; light = #f5f5f5 with #e5e5e5 border, dark = rgba(255,255,255,0.05) with 0.1 border). Inside: a ghost highlight (absolute, opacity 0, shown on mouse hover only), the tab buttons (role=tab, no padding, an inner label box of height var(--atb-h), px var(--atb-px), font-semibold, nowrap; inactive text #737373 light / 55% white dark, hover #262626 / white), and the indicator on top (absolute, pointer-events none, aria-hidden). The indicator has an accent glow shadow, and an inner overflow-hidden layer filled with the accent plus a white 24% to 0 top sheen and a 1px inset top highlight; inside that layer sits a copy of the whole label row (same padding, gap, label boxes) coloured accentText and translated by (-indicatorLeft, -indicatorTop) so the white labels show only where the pill is.\n\nMotion: one rAF loop in a mount effect that sleeps at rest, semi-implicit Euler, substeps of at most 4 ms. Left and right edges are separate springs targeting the active button's offsetLeft and offsetLeft+offsetWidth (layout pixels). On a change the edge facing the destination gets stiffness 520 / damping 36, the other gets 160 / 22, so the pill stretches then catches up; scaleY dips up to 10% with the stretch. Resize or font load snaps without animation (ResizeObserver, document.fonts.ready). Hidden until first measured. Ghost: moves instantly when first shown, then transitions opacity 200ms. Label press scales to 0.94. Panels: all stacked in one grid cell; inactive panels are opacity 0, visibility hidden, translated 28px toward their side (left for earlier tabs, right for later), blur 4px; the active one animates in over 380 to 480ms cubic-bezier(0.22,1,0.36,1) with a 60ms delay.\n\nKeyboard: roving tabindex, Arrow keys wrap and activate and focus, Home/End. Focus ring: sky-500, 2px, offset 2, written as important utilities (also restore the corner radius). ARIA: tablist/tab/tabpanel with aria-selected, aria-controls and aria-labelledby from useId.\n\nprefers-reduced-motion: the indicator snaps to the target and panels crossfade with no transform or blur. Phone: tap works, ghost is mouse only.","license":"https://viberdy.dev/license"}}