{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"arc-sidebar","type":"registry:ui","title":"Arc Sidebar","description":"A sectioned nav whose active marker arcs between rows, shoves the label aside on landing and crossfades into each section's ink. Works as a static rail or a drawer.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/arc-sidebar.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 { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { X } from \"lucide-react\";\nimport {\n  useEffect,\n  useId,\n  useState,\n  type ComponentProps,\n  type CSSProperties,\n  type ReactNode,\n} from \"react\";\nimport { cn } from \"@/lib/utils\";\n\n/**\n * ─────────────────────────────────────────────────────────────────────────────\n * THE SIDEBAR\n *\n * A nav of\n * sections with a single marker, and the same panel is both the rail beside the\n * content and the drawer over it on a phone — `variant` picks which, so nothing\n * about the list has to be written twice.\n *\n * The marker is one element shared between rows through a motion `layoutId`, so\n * picking a row reads as it travelling rather than a second row lighting up, and\n * its colour crossfades from the section it is leaving into the section it lands\n * on. The path is not a straight line: the layout projection carries it between\n * rows while a synced x keyframe bows it out to the left, and the bow scales with\n * distance — adjacent hops barely bend, a long jump swings wide. When the arc\n * closes the label is shoved aside, so the label reads as pushed by the marker\n * rather than moving on its own.\n * ─────────────────────────────────────────────────────────────────────────────\n */\n\nexport type SidebarItem = {\n  label: string;\n  href?: string;\n  icon?: ReactNode;\n  badge?: string | number;\n  disabled?: boolean;\n};\n\nexport type SidebarSection = {\n  /** Eyebrow above the section's rows. */\n  label?: string;\n  /**\n   * The section's ink. The marker takes it while a row here is active and\n   * crossfades into the next section's, so a jump across the nav changes colour\n   * on the way. Defaults to `markerColor`.\n   */\n  color?: string;\n  items: SidebarItem[];\n};\n\n/** How the active row is marked. */\nexport type SidebarMarkerStyle = \"dot\" | \"pip\" | \"bar\" | \"glow\";\n\nexport type ArcSidebarProps = Omit<ComponentProps<\"nav\">, \"onChange\"> & {\n  /** The nav itself: sections of rows, in the order they are drawn. */\n  sections: SidebarSection[];\n  /**\n   * The row to mark, matched against an item's href — the route-driven case, and\n   * the one this site uses.\n   */\n  activeHref?: string;\n  /** Active row as a flat index, for controlled use. */\n  value?: number;\n  /** Active row as a flat index on mount when uncontrolled. Default: 0 */\n  defaultValue?: number;\n  /** Fired with the flat index and the row that was picked. */\n  onChange?: (index: number, item: SidebarItem) => void;\n  /** Fired on every selection, whatever drives the active row. */\n  onNavigate?: (item: SidebarItem) => void;\n  /** Sits above the list and does not scroll — a wordmark, a switcher. */\n  header?: ReactNode;\n  /** Pinned below the list and does not scroll — an account row, a version. */\n  footer?: ReactNode;\n  /** The static panel, or the sliding overlay. Default: \"rail\" */\n  variant?: \"rail\" | \"drawer\";\n  /**\n   * Drawers only: `fixed` covers the viewport and locks page scroll; `absolute`\n   * covers the nearest positioned ancestor instead (a preview, a device frame).\n   * Default: \"fixed\"\n   */\n  position?: \"fixed\" | \"absolute\";\n  /** Whether the drawer is showing. Drawers only. */\n  open?: boolean;\n  /** Fired when the drawer asks to close — Escape, the scrim, the close button. */\n  onOpenChange?: (open: boolean) => void;\n  /** Announced as the nav's label, and the drawer's dialog name. Default: \"Sidebar\" */\n  navLabel?: string;\n  /** Marker shape. Default: \"dot\" */\n  marker?: SidebarMarkerStyle;\n  /** Marker colour, and the fallback for any section without one. Default: var(--primary, #b4e84c) */\n  markerColor?: string;\n  /**\n   * Fade the list at both edges once it scrolls, with the 3rem of padding the\n   * stops are matched to. Off, the rows hug the scroller's edges instead. The\n   * padding comes and goes with the fade, so it is never dead space.\n   * Default: true\n   */\n  fade?: boolean;\n  /** The panel's own box — width, position, borders, and whether it sticks. */\n  className?: string;\n};\n\n/** Flight time (ms) of the marker's arc, and the moment in it (0–1) it lands. */\nconst FLIGHT_MS = 350;\nconst IMPACT = 0.8;\nconst IMPACT_MS = FLIGHT_MS * IMPACT;\n/** Near-critically damped: a bouncier spring sails past the target on long jumps. */\nconst SPRING = { type: \"spring\", stiffness: 800, damping: 52 } as const;\n\n/**\n * How far (px) the marker bows out to the left while travelling, scaled with the\n * rows crossed. The ceiling keeps the widest arc inside the panel's gutter.\n */\nconst ARC_MIN = 8;\nconst ARC_MAX = 24;\nconst ARC_PER_ROW = 2;\n/** Where the marker sits at the landing, with the label still at x=0. */\nconst TOUCH = -5;\n\nfunction arcOffset(rows: number) {\n  if (rows === 0) return 0;\n  return -Math.min(ARC_MAX, ARC_MIN + (rows - 1) * ARC_PER_ROW);\n}\n\n/** The active label sits shoved to the right of the marker. */\nconst RECOIL = { type: \"spring\", stiffness: 1000, damping: 60 } as const;\nconst RETURN = { type: \"spring\", stiffness: 900, damping: 45 } as const;\n\nfunction labelTransition(active: boolean, travelling: boolean) {\n  if (!active) return RETURN;\n  // wait for the marker to arrive, so the label reads as pushed rather than self-moving\n  return { ...RECOIL, delay: travelling ? IMPACT_MS / 1000 : 0 };\n}\n\nconst EYEBROW =\n  \"text-[11px] font-semibold tracking-[0.16em] text-muted-foreground uppercase\";\n\nconst ROW =\n  \"relative block w-full cursor-pointer rounded-md py-1 text-left text-sm outline-none transition-colors duration-200 focus-visible:ring-[1.5px] focus-visible:ring-ring/60 focus-visible:ring-inset\";\n\n/** The scroller: no bar, and the list fades at both ends when it overflows. */\nconst SCROLLER =\n  // the negative margin + padding is a gutter, so the marker's arc is not clipped by the scroller\n  \"-ml-7 min-h-0 flex-1 overflow-y-auto pl-7 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden\";\n/** Room to breathe under the fade, added with it so it is never dead space. */\nconst FADE_PADDING = \"py-12\";\n\nconst FADE_MASK =\n  \"linear-gradient(to bottom, transparent 0, #000 3rem, #000 calc(100% - 3rem), transparent 100%)\";\n\ntype Row = { item: SidebarItem; color?: string };\n\nfunction Marker({\n  layoutId,\n  color,\n  from,\n  offset,\n  marker,\n  reduced,\n}: {\n  layoutId: string;\n  /** The colour the section the marker is landing on is painted in. */\n  color: string;\n  /** The ink of the section it is leaving, to crossfade out of. */\n  from: string;\n  /** Arc bow in px; 0 means no travel, so no arc and no crossfade. */\n  offset: number;\n  marker: SidebarMarkerStyle;\n  reduced: boolean;\n}) {\n  const travelling = offset !== 0;\n\n  return (\n    <motion.span\n      layoutId={layoutId}\n      // The shared-layout projection moves the marker in a straight line between\n      // rows; a synced x keyframe on top of it bends that path into an arc.\n      animate={{ x: travelling ? [0, offset, TOUCH, 0] : 0 }}\n      transition={{\n        layout: SPRING,\n        // peak early, close on the label's edge by impact, then ride alongside it\n        x: {\n          duration: FLIGHT_MS / 1000,\n          times: [0, 0.3, IMPACT, 1],\n          ease: [\"easeOut\", \"easeInOut\", \"easeOut\"],\n        },\n      }}\n      className={cn(\n        \"absolute left-0\",\n        marker === \"bar\"\n          ? \"top-1/2 h-4 w-[3px] -translate-y-1/2\"\n          : \"top-[calc(50%-3px)] size-1.5\",\n      )}\n    >\n      {/* Colour lives on an inner span, clear of the element owning the projection. */}\n      <motion.span\n        // The marker mounts fresh on its new row, so the colour it is leaving has\n        // to be handed to it: it starts on the old section's ink and fades across\n        // to the new one over the same window the arc flies. No travel (first\n        // paint, reduced motion) means no journey, so it just appears.\n        initial={travelling ? { backgroundColor: from } : false}\n        animate={{ backgroundColor: color }}\n        transition={{\n          duration: reduced ? 0 : FLIGHT_MS / 1000,\n          ease: \"easeOut\",\n        }}\n        className={cn(\n          \"block size-full rounded-full\",\n          marker === \"pip\" &&\n            \"rotate-45 rounded-tl-full rounded-tr-full rounded-br-full rounded-bl-xs\",\n        )}\n        style={\n          marker === \"glow\" ? { boxShadow: `0 0 10px ${color}` } : undefined\n        }\n      />\n    </motion.span>\n  );\n}\n\nexport function ArcSidebar({\n  sections,\n  activeHref,\n  value,\n  defaultValue = 0,\n  onChange,\n  onNavigate,\n  header,\n  footer,\n  variant = \"rail\",\n  position = \"fixed\",\n  open = false,\n  onOpenChange,\n  navLabel = \"Sidebar\",\n  marker = \"dot\",\n  markerColor = \"var(--primary, #b4e84c)\",\n  fade = true,\n  className,\n  ...props\n}: ArcSidebarProps) {\n  const reduced = useReducedMotion() === true;\n  // A rail and a drawer can be mounted at once: keep each marker's shared layout\n  // scoped to its own instance.\n  const markerId = useId();\n  const [internalValue, setInternalValue] = useState(defaultValue);\n\n  // Every row in nav order, so the marker knows how far it travels and what it\n  // leaves behind.\n  const rows: Row[] = sections.flatMap((section) =>\n    section.items.map((item) => ({ item, color: section.color })),\n  );\n  const indexOf = new Map(rows.map((row, index) => [row.item, index]));\n\n  const derived =\n    activeHref !== undefined\n      ? rows.findIndex((row) => row.item.href === activeHref)\n      : undefined;\n  const activeIndex = value ?? derived ?? internalValue;\n\n  // Remember where the marker came from, updated during render (the derived-state\n  // pattern) so it mounts already knowing its journey. First paint: from === to.\n  const [travel, setTravel] = useState({ from: activeIndex, to: activeIndex });\n  if (travel.to !== activeIndex) {\n    setTravel({ from: travel.to, to: activeIndex });\n  }\n\n  // A previous index of -1 means the marker was not on screen at all (a route\n  // outside the nav, say): it should appear in place rather than fly in.\n  const travelled =\n    travel.from >= 0 && activeIndex >= 0\n      ? Math.abs(activeIndex - travel.from)\n      : 0;\n  const offset = reduced ? 0 : arcOffset(travelled);\n  const travelling = offset !== 0;\n\n  const activeColor = rows[activeIndex]?.color ?? markerColor;\n  const leavingColor =\n    travel.from >= 0 ? (rows[travel.from]?.color ?? markerColor) : activeColor;\n\n  const select = (index: number, item: SidebarItem) => {\n    // Route-driven and controlled panels already know their row.\n    if (value === undefined && activeHref === undefined) {\n      setInternalValue(index);\n    }\n    onChange?.(index, item);\n    onNavigate?.(item);\n  };\n\n  const panel = (\n    <>\n      {header && <div className=\"pb-2\">{header}</div>}\n\n      <div\n        className={cn(SCROLLER, fade && FADE_PADDING)}\n        style={fade ? fadeStyle() : undefined}\n      >\n        {sections.map((section, sectionIndex) => (\n          <div key={section.label ?? sectionIndex} className=\"mb-5 last:mb-0\">\n            {section.label && (\n              <p className={cn(EYEBROW, \"mb-2 px-1\")}>{section.label}</p>\n            )}\n\n            <ul className=\"flex flex-col gap-0.5\">\n              {section.items.map((item) => {\n                const index = indexOf.get(item) ?? -1;\n                const active = index === activeIndex;\n\n                const content = (\n                  <motion.span\n                    className=\"inline-flex min-w-0 items-center gap-2\"\n                    initial={false}\n                    animate={{ x: reduced ? 0 : active ? 12 : 0 }}\n                    transition={labelTransition(active, travelling)}\n                  >\n                    {item.icon && (\n                      <span className=\"size-4 shrink-0 [&_svg]:size-4\">\n                        {item.icon}\n                      </span>\n                    )}\n                    <span className=\"truncate\">{item.label}</span>\n                    {item.badge !== undefined && (\n                      <span\n                        className={cn(\n                          \"ml-1 rounded-full px-1.5 py-0.5 text-[10px] font-medium leading-tight transition-colors\",\n                          active\n                            ? \"bg-primary/20 font-semibold text-foreground\"\n                            : \"bg-muted text-muted-foreground\",\n                        )}\n                      >\n                        {item.badge}\n                      </span>\n                    )}\n                  </motion.span>\n                );\n\n                const rowClass = cn(\n                  ROW,\n                  active\n                    ? \"font-medium text-foreground\"\n                    : \"text-muted-foreground hover:text-foreground\",\n                  item.disabled && \"pointer-events-none opacity-40\",\n                );\n\n                return (\n                  <li\n                    key={`${sectionIndex}-${item.label}`}\n                    className=\"relative\"\n                  >\n                    {active && (\n                      <Marker\n                        layoutId={markerId}\n                        color={activeColor}\n                        from={leavingColor}\n                        offset={offset}\n                        marker={marker}\n                        reduced={reduced}\n                      />\n                    )}\n\n                    {item.disabled ? (\n                      <span\n                        aria-disabled=\"true\"\n                        className={cn(rowClass, \"cursor-not-allowed\")}\n                      >\n                        {content}\n                      </span>\n                    ) : item.href ? (\n                      <a\n                        href={item.href}\n                        aria-current={active ? \"page\" : undefined}\n                        onClick={() => select(index, item)}\n                        className={rowClass}\n                      >\n                        {content}\n                      </a>\n                    ) : (\n                      <button\n                        type=\"button\"\n                        // A row with no href still has to expose which one is\n                        // marked — \"page\" is for links, \"true\" is for the rest.\n                        aria-current={active ? \"true\" : undefined}\n                        onClick={() => select(index, item)}\n                        className={rowClass}\n                      >\n                        {content}\n                      </button>\n                    )}\n                  </li>\n                );\n              })}\n            </ul>\n          </div>\n        ))}\n      </div>\n\n      {footer && <div className=\"pt-2\">{footer}</div>}\n    </>\n  );\n\n  if (variant === \"drawer\") {\n    return (\n      <Drawer\n        open={open}\n        position={position}\n        onOpenChange={onOpenChange}\n        navLabel={navLabel}\n        reduced={reduced}\n        className={className}\n      >\n        {panel}\n      </Drawer>\n    );\n  }\n\n  return (\n    <nav\n      aria-label={navLabel}\n      className={cn(\"flex min-h-0 flex-col\", className)}\n      {...props}\n    >\n      {panel}\n    </nav>\n  );\n}\n\n/** The mask that fades a scrolling list at both ends. */\nfunction fadeStyle(): CSSProperties {\n  return { maskImage: FADE_MASK, WebkitMaskImage: FADE_MASK };\n}\n\n/**\n * The overlay the same panel is drawn in on a phone: the page is locked behind\n * it, Escape and the scrim both dismiss it, and it slides in from the edge the\n * rail would have been on.\n */\nfunction Drawer({\n  open,\n  position,\n  onOpenChange,\n  navLabel,\n  reduced,\n  className,\n  children,\n}: {\n  open: boolean;\n  position: \"fixed\" | \"absolute\";\n  onOpenChange?: (open: boolean) => void;\n  navLabel: string;\n  reduced: boolean;\n  className?: string;\n  children: ReactNode;\n}) {\n  useEffect(() => {\n    if (!open) return;\n\n    // only a viewport drawer locks the page behind it\n    const lock = position === \"fixed\";\n    const previousOverflow = document.body.style.overflow;\n    if (lock) document.body.style.overflow = \"hidden\";\n\n    const onKeyDown = (event: KeyboardEvent) => {\n      if (event.key === \"Escape\") onOpenChange?.(false);\n    };\n    document.addEventListener(\"keydown\", onKeyDown);\n\n    return () => {\n      if (lock) document.body.style.overflow = previousOverflow;\n      document.removeEventListener(\"keydown\", onKeyDown);\n    };\n  }, [open, onOpenChange, position]);\n\n  return (\n    <AnimatePresence>\n      {open && (\n        <div className={cn(position === \"absolute\" ? \"absolute\" : \"fixed\", \"inset-0 z-50\")}>\n          <motion.div\n            aria-hidden=\"true\"\n            onClick={() => onOpenChange?.(false)}\n            initial={{ opacity: 0 }}\n            animate={{ opacity: 1 }}\n            exit={{ opacity: 0 }}\n            className=\"absolute inset-0 bg-background/80 backdrop-blur-sm\"\n          />\n\n          <motion.div\n            role=\"dialog\"\n            aria-modal=\"true\"\n            aria-label={navLabel}\n            initial={{ x: reduced ? 0 : \"-100%\" }}\n            animate={{ x: 0 }}\n            exit={{ x: reduced ? 0 : \"-100%\" }}\n            transition={\n              reduced\n                ? { duration: 0 }\n                : { type: \"spring\", stiffness: 420, damping: 40 }\n            }\n            className={cn(\n              \"absolute inset-y-0 left-0 flex w-72 max-w-[85%] flex-col border-r border-border bg-background pb-6 pl-7 pr-4 pt-4\",\n              className,\n            )}\n          >\n            <div className=\"mb-2 flex items-center justify-between gap-4 pb-2\">\n              <span className={EYEBROW}>{navLabel}</span>\n              <button\n                type=\"button\"\n                onClick={() => onOpenChange?.(false)}\n                aria-label=\"Close navigation\"\n                className=\"flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:text-foreground focus-visible:ring-[1.5px] focus-visible:ring-ring/60 focus-visible:ring-inset focus-visible:outline-none\"\n              >\n                <X className=\"size-4\" />\n              </button>\n            </div>\n\n            <nav aria-label={navLabel} className=\"flex min-h-0 flex-1 flex-col\">\n              {children}\n            </nav>\n          </motion.div>\n        </div>\n      )}\n    </AnimatePresence>\n  );\n}\n\n"}]}