{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "expandable-tabs",
  "title": "Expandable Tabs",
  "description": "A pill-shaped tab bar that expands into a floating panel, with Motion driving the shell, label, and content transitions.",
  "dependencies": ["motion", "lucide-react", "react-use-measure"],
  "registryDependencies": ["utils"],
  "files": [
    {
      "path": "registry/primitives/disclosure/expandable-tabs/index.tsx",
      "content": "\"use client\";\n\nimport { cn } from \"@/lib/utils\";\nimport {\n  AnimatePresence,\n  LayoutGroup,\n  motion,\n  useReducedMotion,\n} from \"motion/react\";\nimport type { Transition } from \"motion/react\";\nimport type { ComponentType, ReactNode } from \"react\";\nimport {\n  useCallback,\n  useEffect,\n  useId,\n  useMemo,\n  useRef,\n  useState,\n} from \"react\";\nimport useMeasure from \"react-use-measure\";\n\nexport interface ExpandableTab {\n  /** Panel content rendered when this tab is selected. */\n  content: ReactNode;\n  icon: ComponentType<{ className?: string }>;\n  iconClassName?: string;\n  id: string;\n  label: string;\n}\n\nexport interface ExpandableTabsClassNames {\n  /** Merged onto the content wrapper shown while a tab is open. */\n  content?: string;\n  /** Merged onto the revealed label span inside the active tab button. */\n  label?: string;\n  /** Merged onto the outermost element. */\n  root?: string;\n  /** Merged onto the pill shell (background, rounding, border). */\n  toolbar?: string;\n  /** Merged onto every tab button. */\n  trigger?: string;\n  /** Merged onto the currently active tab button, in addition to `trigger`. */\n  triggerActive?: string;\n}\n\nexport interface ExpandableTabsProps {\n  className?: string;\n  classNames?: ExpandableTabsClassNames;\n  /** Uncontrolled initial selected tab id. @default null */\n  defaultSelectedId?: string | null;\n  /** Delay in seconds before an active tab's label starts revealing. @default 0.08 */\n  labelDelay?: number;\n  /** Maximum width of the open content panel (CSS value). @default \"24rem\" */\n  maxContentWidth?: string;\n  /** Called whenever the selected tab id changes (controlled or uncontrolled). */\n  onSelectedIdChange?: (id: string | null) => void;\n  /** Controlled selected tab id. `null` means no panel is open. */\n  selectedId?: string | null;\n  /** Bounce of the shared spring. 0 is a smooth ease, higher values overshoot more. @default 0 */\n  springBounce?: number;\n  /** Duration of the shared spring driving the shell, pill, tab, and content transitions. @default 0.45 */\n  springDuration?: number;\n  tabs: ExpandableTab[];\n  /** Vertical scale applied to a tab while pressed, for tactile press feedback. @default 0.8 */\n  tapScale?: number;\n}\n\nconst contentVariants = {\n  initial: (direction: number) => ({ x: `${60 * direction}%`, opacity: 0 }),\n  active: { x: \"0%\", opacity: 1 },\n  exit: (direction: number) => ({ x: `${-60 * direction}%`, opacity: 0 }),\n};\n\n/** Shared padding/gap so the ghost row and real row can't drift apart. */\nfunction triggerBoxStyle(isActive: boolean) {\n  return {\n    gap: isActive ? \".5rem\" : 0,\n    paddingLeft: isActive ? \"1rem\" : \".5rem\",\n    paddingRight: isActive ? \"1rem\" : \".5rem\",\n  };\n}\n\nexport function ExpandableTabs({\n  tabs,\n  selectedId: controlledSelectedId,\n  defaultSelectedId = null,\n  onSelectedIdChange,\n  springDuration = 0.45,\n  springBounce = 0,\n  labelDelay = 0.08,\n  maxContentWidth = \"24rem\",\n  tapScale = 0.8,\n  className,\n  classNames,\n}: ExpandableTabsProps) {\n  const idPrefix = useId();\n  const [internalSelectedId, setInternalSelectedId] = useState<string | null>(\n    defaultSelectedId\n  );\n  const [direction, setDirection] = useState(1);\n  const [lastContentHeight, setLastContentHeight] = useState(0);\n  const prefersReducedMotion = useReducedMotion();\n\n  const isControlled = controlledSelectedId !== undefined;\n  const selected = isControlled ? controlledSelectedId : internalSelectedId;\n\n  const setSelected = useCallback(\n    (id: string | null) => {\n      if (!isControlled) {\n        setInternalSelectedId(id);\n      }\n      onSelectedIdChange?.(id);\n    },\n    [isControlled, onSelectedIdChange]\n  );\n\n  const containerRef = useRef<HTMLDivElement>(null);\n  const triggerRefs = useRef<(HTMLButtonElement | null)[]>([]);\n\n  const [rowRef, rowBounds] = useMeasure();\n  const [measureRef, bounds] = useMeasure();\n\n  const activeIndex = tabs.findIndex((tab) => tab.id === selected);\n  const activeTab = activeIndex === -1 ? null : tabs[activeIndex];\n  const barHeight = rowBounds.height || 52;\n\n  const spring = useMemo<Transition>(\n    () =>\n      prefersReducedMotion\n        ? { duration: 0 }\n        : { duration: springDuration, type: \"spring\", bounce: springBounce },\n    [prefersReducedMotion, springDuration, springBounce]\n  );\n\n  // Opacity fades faster than the shell shrinks, so text isn't clipped mid-fade.\n  const contentTransition = useMemo<Transition>(\n    () => ({\n      ...spring,\n      opacity: { duration: prefersReducedMotion ? 0 : 0.15 },\n    }),\n    [spring, prefersReducedMotion]\n  );\n\n  // Width tracks the button in lockstep; `labelDelay` only staggers the fade.\n  const labelTransition = useMemo<Transition>(\n    () => ({\n      ...spring,\n      opacity: { ...spring, delay: labelDelay },\n    }),\n    [spring, labelDelay]\n  );\n\n  useEffect(() => {\n    function handleMouseDown(event: MouseEvent) {\n      if (\n        selected !== null &&\n        containerRef.current &&\n        !containerRef.current.contains(event.target as Node)\n      ) {\n        setSelected(null);\n      }\n    }\n    document.addEventListener(\"mousedown\", handleMouseDown);\n    return () => document.removeEventListener(\"mousedown\", handleMouseDown);\n  }, [selected, setSelected]);\n\n  useEffect(() => {\n    if (selected === null) {\n      return;\n    }\n\n    const openIndex = activeIndex;\n\n    function handleKeyDown(event: KeyboardEvent) {\n      if (event.key === \"Escape\") {\n        setSelected(null);\n        triggerRefs.current[openIndex]?.focus();\n      }\n    }\n\n    document.addEventListener(\"keydown\", handleKeyDown);\n    return () => document.removeEventListener(\"keydown\", handleKeyDown);\n  }, [selected, activeIndex, setSelected]);\n\n  useEffect(() => {\n    if (bounds.height > 0) {\n      setLastContentHeight(bounds.height);\n    }\n  }, [bounds.height]);\n  const contentHeight = bounds.height > 0 ? bounds.height : lastContentHeight;\n\n  function handleTabClick(tab: ExpandableTab, index: number) {\n    if (selected === null) {\n      // Opening from closed: fade in place, no lateral tab to slide from.\n      setDirection(0);\n      setSelected(tab.id);\n    } else if (selected === tab.id) {\n      // Closing: fade out in place, don't slide toward a stale direction.\n      setDirection(0);\n      setSelected(null);\n    } else {\n      setDirection(index > activeIndex ? 1 : -1);\n      setSelected(tab.id);\n    }\n  }\n\n  return (\n    <div\n      className={cn(classNames?.root, className)}\n      ref={containerRef}\n      // Opt out of CSS scroll anchoring so our animated resizes don't shift window.scrollY.\n      style={{ overflowAnchor: \"none\" }}\n    >\n      <LayoutGroup>\n        <motion.div\n          animate={{\n            height: selected === null ? barHeight : contentHeight + barHeight,\n            width:\n              selected === null\n                ? rowBounds.width\n                : Math.max(rowBounds.width, bounds.width),\n          }}\n          className={cn(\n            \"bg-background relative overflow-hidden rounded-3xl\",\n            classNames?.toolbar\n          )}\n          initial={false}\n          transition={spring}\n        >\n          {/* No `mode=\"popLayout\"`: it'd snapshot exit position in absolute\n              pixels, fighting the live centering below as the pill shrinks. */}\n          <AnimatePresence custom={direction} initial={false}>\n            {selected !== null && activeTab && (\n              <motion.div\n                animate=\"active\"\n                aria-labelledby={`${idPrefix}-trigger-${activeTab.id}`}\n                className={cn(\n                  \"bg-background/80 absolute top-0 left-1/2 -translate-x-1/2 p-4 pb-4\",\n                  classNames?.content\n                )}\n                custom={direction}\n                exit=\"exit\"\n                id={`${idPrefix}-panel-${activeTab.id}`}\n                initial=\"initial\"\n                key={activeTab.id}\n                ref={measureRef}\n                role=\"region\"\n                style={{ maxWidth: maxContentWidth }}\n                transition={contentTransition}\n                variants={contentVariants}\n              >\n                {activeTab.content}\n              </motion.div>\n            )}\n          </AnimatePresence>\n\n          {/* Ghost row: measures the settled target size so useMeasure doesn't\n              chase the label's own reveal animation frame-by-frame. */}\n          <div\n            aria-hidden=\"true\"\n            className=\"bg-background invisible absolute bottom-0 left-1/2 -translate-x-1/2 p-2\"\n            ref={rowRef}\n          >\n            <div className=\"flex h-9 items-center gap-1\">\n              {tabs.map((tab) => {\n                const isActive = selected === tab.id;\n                return (\n                  <div\n                    className=\"flex h-full items-center rounded-2xl text-sm font-medium\"\n                    key={tab.id}\n                    style={triggerBoxStyle(isActive)}\n                  >\n                    <tab.icon className={cn(\"size-4\", tab.iconClassName)} />\n                    {isActive && (\n                      <span className=\"font-medium tracking-tight whitespace-nowrap\">\n                        {tab.label}\n                      </span>\n                    )}\n                  </div>\n                );\n              })}\n            </div>\n          </div>\n\n          {/* left-0 w-full + justify-center (not translate-x centering) so a\n              focused button's position doesn't shift mid-animation and\n              trigger browser auto-scroll. Fine here since it isn't measured. */}\n          <div className=\"bg-background absolute bottom-0 left-0 w-full p-2\">\n            <div className=\"flex h-9 items-center justify-center gap-1\">\n              {tabs.map((tab, index) => {\n                const Icon = tab.icon;\n                const isActive = selected === tab.id;\n                const triggerId = `${idPrefix}-trigger-${tab.id}`;\n                const panelId = `${idPrefix}-panel-${tab.id}`;\n\n                return (\n                  <motion.button\n                    animate={triggerBoxStyle(isActive)}\n                    aria-controls={panelId}\n                    aria-expanded={isActive}\n                    className={cn(\n                      \"flex h-full cursor-pointer items-center rounded-2xl text-sm font-medium transition-colors duration-300\",\n                      isActive\n                        ? cn(\"bg-foreground/4\", classNames?.triggerActive)\n                        : \"text-muted-foreground hover:bg-muted hover:text-foreground\",\n                      classNames?.trigger\n                    )}\n                    id={triggerId}\n                    initial={false}\n                    key={tab.id}\n                    onClick={() => handleTabClick(tab, index)}\n                    ref={(node) => {\n                      triggerRefs.current[index] = node;\n                    }}\n                    transition={spring}\n                    type=\"button\"\n                    whileTap={{\n                      scaleY: prefersReducedMotion ? 1 : tapScale,\n                      transition: { duration: 0.2 },\n                    }}\n                  >\n                    <Icon className={cn(\"size-4\", tab.iconClassName)} />\n                    <AnimatePresence initial={false}>\n                      {isActive && (\n                        <motion.span\n                          animate={{ width: \"auto\", opacity: 1 }}\n                          className={cn(\n                            \"overflow-hidden font-medium tracking-tight whitespace-nowrap\",\n                            classNames?.label\n                          )}\n                          exit={{\n                            width: 0,\n                            opacity: 0,\n                            transition: labelTransition,\n                          }}\n                          initial={{ width: 0, opacity: 0 }}\n                          transition={labelTransition}\n                        >\n                          {tab.label}\n                        </motion.span>\n                      )}\n                    </AnimatePresence>\n                  </motion.button>\n                );\n              })}\n            </div>\n          </div>\n        </motion.div>\n      </LayoutGroup>\n    </div>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/sora-ui/disclosure/expandable-tabs.tsx"
    }
  ],
  "meta": {
    "inspiration": {
      "type": "reimplemented",
      "label": "beui.dev's Expandable Tabs",
      "url": "https://beui.dev",
      "stack": "Motion and React"
    }
  },
  "type": "registry:ui"
}
