{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"bead-tray","type":"registry:ui","title":"Bead Tray","description":"A segmented tray where the active item pulls out on a liquid metaball thread and drops a bead when it snaps.","dependencies":["motion","figma-squircle","react-use-measure"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/bead-tray.tsx","type":"registry:ui","content":"\"use client\";\n\n// Adapted from Calamansi UI by fujiDevv (MIT License).\n// https://github.com/fujiDevv/calamansi-ui\n\nimport { useEffect, useId, useMemo, useState, type ComponentProps, type ReactNode } from \"react\";\nimport { motion, useReducedMotion, useSpring, useTransform } from \"motion/react\";\nimport { getSvgPath } from \"figma-squircle\";\nimport useMeasure from \"react-use-measure\";\nimport { cn } from \"@/lib/utils\";\n\n/* ─── Liquid bar primitives (inlined from Calamansi's lib/liquid) ────────────\n   Surfaces are painted on layers inset by springs, so seams open without the\n   layout moving. The stage is blurred, pushed through a steep alpha ramp so any\n   bleed between neighbours becomes solid matter (a metaball thread), and the\n   crisp stage is drawn back on top so labels stay sharp. */\n\nconst LIQUID_SPRING = { type: \"spring\", stiffness: 200, damping: 28, mass: 1 } as const;\n/** Where a blurred thread gives up, as a multiple of the blur radius. */\nconst LIQUID_SEVER = 1.234;\n/** Default blur as a share of the gap. */\nconst LIQUID_VISCOSITY = 0.55;\n/** Sealed extension, in corner radii: enough to hide a neighbour's corner. */\nconst LIQUID_SEAL = 2;\nconst BEAD_SIZE = 0.18;\nconst BEAD_FADE = 0.22;\nconst BEAD_PEAK_MAX = 0.92;\nconst SWALLOW_REACH = 0.25;\n\nfunction beadPercent(gap: number, sever: number, open: number) {\n  if (!Number.isFinite(gap) || open <= 0) return 0;\n  const travel = gap / open;\n  if (travel <= 0) return 0;\n  const peak = Math.min(sever / open, BEAD_PEAK_MAX);\n  if (travel < peak) return BEAD_SIZE * 100 * (travel / peak) ** 1.6;\n  const fade = Math.min(BEAD_FADE, 1 - peak);\n  const past = (travel - peak) / fade;\n  return past >= 1 ? 0 : BEAD_SIZE * 100 * (1 - past);\n}\n\nfunction swallowShare(retracted: number, pull: number) {\n  if (pull <= 0) return 1;\n  const reach = SWALLOW_REACH * pull;\n  const s = Math.min(1, Math.max(0, (reach - retracted) / reach));\n  return s * s * (3 - 2 * s);\n}\n\nfunction FuseFilter({ id, blur, threshold }: { id: string; blur: number; threshold: number }) {\n  return (\n    <svg aria-hidden=\"true\" className=\"pointer-events-none absolute size-0\">\n      <defs>\n        <filter id={id} x=\"-8%\" y=\"-60%\" width=\"116%\" height=\"220%\" colorInterpolationFilters=\"sRGB\">\n          <feGaussianBlur in=\"SourceGraphic\" stdDeviation={blur} result=\"blur\" />\n          <feColorMatrix\n            in=\"blur\"\n            type=\"matrix\"\n            values={`1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 ${threshold} ${-threshold / 2}`}\n            result=\"goo\"\n          />\n          <feComposite in=\"SourceGraphic\" in2=\"goo\" operator=\"over\" />\n        </filter>\n      </defs>\n    </svg>\n  );\n}\n\nfunction Squircle({ className, radius }: { className?: string; radius: number }) {\n  const [ref, bounds] = useMeasure({ offsetSize: true });\n  const path = useMemo(\n    () =>\n      bounds.width > 0 && bounds.height > 0\n        ? getSvgPath({ width: bounds.width, height: bounds.height, cornerRadius: radius, cornerSmoothing: 1 })\n        : null,\n    [bounds.width, bounds.height, radius],\n  );\n  return (\n    <span aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block\">\n      <span\n        ref={ref}\n        style={path ? { clipPath: `path('${path}')` } : { borderRadius: radius }}\n        className={cn(\"relative block size-full overflow-hidden\", className)}\n      />\n    </span>\n  );\n}\n\ntype LiquidSegmentProps = {\n  seamLeft: boolean;\n  seamRight: boolean;\n  atStart: boolean;\n  atEnd: boolean;\n  radius: number;\n  pull: number;\n  seal: number;\n  sever: number;\n  bead: boolean;\n  beadFill: string;\n  paint: string;\n  reduced: boolean;\n  children: ReactNode;\n};\n\nfunction LiquidSegment({\n  seamLeft,\n  seamRight,\n  atStart,\n  atEnd,\n  radius,\n  pull,\n  seal,\n  sever,\n  bead,\n  beadFill,\n  paint,\n  reduced,\n  children,\n}: LiquidSegmentProps) {\n  const retractLeft = useSpring(!atStart && seamLeft ? pull : 0, LIQUID_SPRING);\n  const retractRight = useSpring(!atEnd && seamRight ? pull : 0, LIQUID_SPRING);\n\n  useEffect(() => {\n    const left = !atStart && seamLeft ? pull : 0;\n    const right = !atEnd && seamRight ? pull : 0;\n    if (reduced) {\n      retractLeft.jump(left);\n      retractRight.jump(right);\n    } else {\n      retractLeft.set(left);\n      retractRight.set(right);\n    }\n  }, [atStart, atEnd, seamLeft, seamRight, pull, reduced, retractLeft, retractRight]);\n\n  // the swallow under the neighbour waits for the retraction to come home, then eases in\n  const left = useTransform(() => {\n    const r = retractLeft.get();\n    return r - (atStart ? 0 : seal * swallowShare(r, pull));\n  });\n  const right = useTransform(() => {\n    const r = retractRight.get();\n    return r - (atEnd ? 0 : seal * swallowShare(r, pull));\n  });\n  // content rides with the visible edge only\n  const shift = useTransform(() => (Math.max(0, left.get()) - Math.max(0, right.get())) / 2);\n  const beadHeight = useTransform(() => `${beadPercent(left.get() * 2, sever, pull * 2)}%`);\n\n  return (\n    <li className=\"relative flex\">\n      <motion.span aria-hidden=\"true\" className=\"pointer-events-none absolute inset-y-0 block\" style={{ left, right }}>\n        <Squircle radius={radius} className={cn(\"transition-colors duration-300 ease-out\", paint)} />\n      </motion.span>\n      {bead && !atStart && (\n        <motion.span\n          aria-hidden=\"true\"\n          className={cn(\"pointer-events-none absolute top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-full bg-current\", beadFill)}\n          style={{ left: 0, height: beadHeight, aspectRatio: 1 }}\n        />\n      )}\n      <motion.div className=\"relative z-10 flex min-w-0\" style={{ x: shift }}>\n        {children}\n      </motion.div>\n    </li>\n  );\n}\n\n/* ─── Gooey nav ──────────────────────────────────────────────────────────── */\n\nexport type BeadTrayVariant = \"white\" | \"calamansi\" | \"slate\" | \"citrus\";\nexport type BeadTraySize = \"xs\" | \"sm\" | \"md\" | \"lg\";\n\nconst VARIANTS: Record<BeadTrayVariant, { paint: string; juice: string; ink: string }> = {\n  white: { paint: \"bg-white dark:bg-[#1c1c1f]\", juice: \"text-white dark:text-[#1c1c1f]\", ink: \"text-foreground\" },\n  calamansi: { paint: \"bg-[#5c7a67] dark:bg-[#16221a]\", juice: \"text-[#5c7a67] dark:text-[#16221a]\", ink: \"text-white\" },\n  slate: { paint: \"bg-[#687396] dark:bg-[#1e293b]\", juice: \"text-[#687396] dark:text-[#1e293b]\", ink: \"text-white\" },\n  citrus: { paint: \"bg-[#b87152] dark:bg-[#22120b]\", juice: \"text-[#b87152] dark:text-[#22120b]\", ink: \"text-white\" },\n};\n\n/** The tray. Must be opaque: the fuse's alpha ramp erases translucent paint. */\nconst TRAY = \"bg-muted\";\nconst TRAY_INK = \"text-muted\";\n\n/** Radii hold the brand's ~0.42 share of the bar height; `separation` is the gap the pill opens. */\nconst SIZES = {\n  xs: { label: \"gap-1 px-3.5 py-1.5 text-[11px] leading-4 [&_svg]:size-[11px]\", radius: 12, separation: 14 },\n  sm: { label: \"gap-1.5 px-4 py-2 text-xs leading-4 [&_svg]:size-3\", radius: 14, separation: 16 },\n  md: { label: \"gap-2 px-5 py-2.5 text-sm leading-5 [&_svg]:size-3.5\", radius: 16, separation: 20 },\n  lg: { label: \"gap-2.5 px-6 py-3 text-base leading-6 [&_svg]:size-4\", radius: 20, separation: 24 },\n} as const;\n\nexport type BeadTrayItem = string | { label: string; href?: string; icon?: ReactNode };\ntype NavItem = { label: string; href?: string; icon?: ReactNode };\nconst toItem = (item: BeadTrayItem): NavItem => (typeof item === \"string\" ? { label: item } : item);\n\nexport type BeadTrayProps = Omit<ComponentProps<\"nav\">, \"onChange\"> & {\n  /** Labels, or `{ label, href, icon }` objects. */\n  items: BeadTrayItem[];\n  /** Active index, for controlled use. */\n  value?: number;\n  /** Active index on mount when uncontrolled. */\n  defaultValue?: number;\n  /** Fired with the new index on selection. */\n  onChange?: (index: number) => void;\n  /** Palette the active pill wears. */\n  variant?: BeadTrayVariant;\n  /** Label size, and with it the radius and travel. */\n  size?: BeadTraySize;\n  /** Override the gap the pill opens on each side, in px. */\n  separation?: number;\n  /** Override the corner radius, in px. */\n  radius?: number;\n  /** SVG blur behind the fuse, in px (how far the thread reaches). Defaults to 0.55 of the gap. */\n  viscosity?: number;\n  /** Alpha ramp slope: how hard the fused edge is. */\n  threshold?: number;\n  /** Run the fuse at all. Off, the surfaces simply pull apart. */\n  gooey?: boolean;\n  /** Leave a bead of juice behind when the thread severs. */\n  bead?: boolean;\n};\n\nexport function BeadTray({\n  items,\n  value,\n  defaultValue = 0,\n  onChange,\n  variant = \"calamansi\",\n  size = \"md\",\n  separation,\n  radius,\n  viscosity,\n  threshold = 19,\n  gooey = true,\n  bead = true,\n  className,\n  ...props\n}: BeadTrayProps) {\n  const reduced = useReducedMotion() ?? false;\n  const filterId = `gooey-nav-${useId().replace(/:/g, \"\")}`;\n  const [uncontrolled, setUncontrolled] = useState(defaultValue);\n\n  const active = value ?? uncontrolled;\n  const preset = SIZES[size] ?? SIZES.md;\n  const gap = separation ?? preset.separation;\n  const corner = radius ?? preset.radius;\n  const blur = viscosity ?? gap * LIQUID_VISCOSITY;\n  const sever = LIQUID_SEVER * blur;\n  const seal = LIQUID_SEAL * corner;\n  const pull = gap / 2;\n  const palette = VARIANTS[variant] ?? VARIANTS.calamansi;\n  const fused = gooey && !reduced;\n\n  // a seam opens either side of the active pill, and nowhere else\n  const open = (seam: number) => seam - 1 === active || seam === active;\n\n  return (\n    <nav data-variant={variant} className={cn(\"inline-block\", className)} {...props}>\n      <FuseFilter id={filterId} blur={blur} threshold={threshold} />\n      <ul className=\"relative flex items-center\" style={fused ? { filter: `url(#${filterId})` } : undefined}>\n        {items.map((item, index) => {\n          const { label, href, icon } = toItem(item);\n          const isActive = index === active;\n          const shared = {\n            \"aria-current\": isActive ? (href ? (\"page\" as const) : true) : undefined,\n            className: cn(\n              \"flex w-full cursor-pointer items-center justify-center whitespace-nowrap rounded-full font-semibold outline-none focus-visible:ring-2 focus-visible:ring-ring [&_svg]:shrink-0\",\n              // the label arrives with the pill and leaves the moment it does\n              isActive ? \"transition-colors duration-[400ms]\" : \"transition-colors duration-0\",\n              preset.label,\n              isActive ? palette.ink : \"text-muted-foreground hover:text-foreground\",\n            ),\n            onClick: () => {\n              if (value === undefined) setUncontrolled(index);\n              onChange?.(index);\n            },\n          };\n          return (\n            <LiquidSegment\n              key={`${index}-${label}`}\n              seamLeft={open(index)}\n              seamRight={open(index + 1)}\n              atStart={index === 0}\n              atEnd={index === items.length - 1}\n              radius={corner}\n              pull={pull}\n              seal={seal}\n              sever={sever}\n              bead={bead}\n              beadFill={open(index) ? palette.juice : TRAY_INK}\n              paint={cn(TRAY, isActive && palette.paint)}\n              reduced={reduced}\n            >\n              {href ? (\n                <a href={href} {...shared}>\n                  {icon}\n                  {label}\n                </a>\n              ) : (\n                <button type=\"button\" {...shared}>\n                  {icon}\n                  {label}\n                </button>\n              )}\n            </LiquidSegment>\n          );\n        })}\n      </ul>\n    </nav>\n  );\n}\n"}]}