{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sidebar-rail",
  "type": "registry:component",
  "title": "Sidebar Rail",
  "description": "A 56px icon rail. Hovering or focusing an item opens a flyout with its submenu, and items without a submenu show a tooltip. Arrow keys move through the rail.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "sidebars",
    "sidebar",
    "rail",
    "icons",
    "flyout",
    "tooltip",
    "navigation"
  ],
  "docs": "https://hyperblur.dev/effects/sidebar-rail",
  "files": [
    {
      "path": "registry/hyperblur/sidebar-rail.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/sidebar-rail.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nconst ICONS = {\n  home: \"M3.5 9.2L10 3.8l6.5 5.4V16a.8.8 0 0 1-.8.8H12v-4.5H8v4.5H4.3a.8.8 0 0 1-.8-.8z\",\n  layers: \"M10 3.5l6.5 3.3L10 10 3.5 6.8zM3.5 10.2L10 13.5l6.5-3.3M3.5 13.4L10 16.7l6.5-3.3\",\n  chart: \"M4 16V9M10 16V4M16 16v-5\",\n  users:\n    \"M7.5 9a2.7 2.7 0 1 0 0-5.4 2.7 2.7 0 0 0 0 5.4zM2.5 16.2c.4-2.7 2.5-4.2 5-4.2s4.6 1.5 5 4.2M13 4.2a2.7 2.7 0 0 1 0 5M14.5 12.2c1.6.5 2.7 1.8 3 4\",\n  folder:\n    \"M3 6.2a1.5 1.5 0 0 1 1.5-1.5h3.1l1.6 1.8h5.8A1.5 1.5 0 0 1 16.5 8v6.3a1.5 1.5 0 0 1-1.5 1.5H4.5A1.5 1.5 0 0 1 3 14.3z\",\n  bolt: \"M11 3L4.5 11h4.5l-1 6 6.5-8H10z\",\n  sliders: \"M4 6h7M14 6h2M4 14h2M9 14h7M12.5 4v4M7.5 12v4\",\n  help: \"M10 17a7 7 0 1 0 0-14 7 7 0 0 0 0 14zM8 8a2 2 0 1 1 2.8 1.8c-.5.3-.8.7-.8 1.2M10 13.6v.01\",\n} as const;\n\nexport type SidebarRailIconName = keyof typeof ICONS;\n\nexport function SidebarRailIcon({ name }: { name: SidebarRailIconName }) {\n  return (\n    <svg viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d={ICONS[name]} />\n    </svg>\n  );\n}\n\nexport interface SidebarRailLink {\n  id: string;\n  label: string;\n  badge?: string | number;\n  href?: string;\n}\n\nexport interface SidebarRailItem {\n  id: string;\n  label: string;\n  icon?: React.ReactNode;\n  links?: SidebarRailLink[];\n  href?: string;\n}\n\nexport interface SidebarRailProps\n  extends Omit<React.HTMLAttributes<HTMLElement>, \"onChange\" | \"children\" | \"defaultValue\"> {\n  items?: SidebarRailItem[];\n  active?: string;\n  defaultActive?: string;\n  onChange?: (id: string) => void;\n  openId?: string | null;\n  defaultOpenId?: string | null;\n  onOpenChange?: (id: string | null) => void;\n  brand?: React.ReactNode;\n  footer?: React.ReactNode;\n  flyoutWidth?: number;\n  itemSize?: number;\n  color?: string;\n}\n\nconst defaultItems: SidebarRailItem[] = [\n  { id: \"home\", label: \"Home\", icon: <SidebarRailIcon name=\"home\" /> },\n  {\n    id: \"projects\",\n    label: \"Projects\",\n    icon: <SidebarRailIcon name=\"layers\" />,\n    links: [\n      { id: \"projects-all\", label: \"All projects\" },\n      { id: \"projects-active\", label: \"Active\", badge: 6 },\n      { id: \"projects-archived\", label: \"Archived\" },\n      { id: \"projects-templates\", label: \"Templates\" },\n    ],\n  },\n  {\n    id: \"analytics\",\n    label: \"Analytics\",\n    icon: <SidebarRailIcon name=\"chart\" />,\n    links: [\n      { id: \"analytics-traffic\", label: \"Traffic\" },\n      { id: \"analytics-funnels\", label: \"Funnels\" },\n      { id: \"analytics-retention\", label: \"Retention\" },\n    ],\n  },\n  {\n    id: \"team\",\n    label: \"Team\",\n    icon: <SidebarRailIcon name=\"users\" />,\n    links: [\n      { id: \"team-members\", label: \"Members\", badge: 12 },\n      { id: \"team-roles\", label: \"Roles\" },\n      { id: \"team-invites\", label: \"Invites\", badge: 2 },\n    ],\n  },\n  { id: \"settings\", label: \"Settings\", icon: <SidebarRailIcon name=\"sliders\" /> },\n];\n\nconst css = `\n.hb-rail {\n  position: relative;\n  display: flex;\n  flex-direction: column;\n  align-items: center;\n  box-sizing: border-box;\n  width: 56px;\n  height: 100%;\n  min-height: 0;\n  padding: 10px 0;\n  border-right: 1px solid #262626;\n  background: #0f0f0f;\n  color: #ededed;\n  font-family: var(--font-sans, ui-sans-serif, system-ui, sans-serif);\n  font-size: 13px;\n  line-height: 18px;\n  --hb-rail-accent: #2f7bff;\n}\n.hb-rail *, .hb-rail *::before, .hb-rail *::after { box-sizing: border-box; }\n.hb-rail-brand { display: grid; flex: none; place-items: center; width: 32px; height: 32px; margin-bottom: 10px; }\n.hb-rail-list { display: flex; flex: 1; flex-direction: column; align-items: center; gap: 4px; min-height: 0; margin: 0; padding: 0; list-style: none; }\n.hb-rail-li { position: relative; display: flex; justify-content: center; width: 100%; }\n.hb-rail-btn {\n  position: relative;\n  display: grid;\n  place-items: center;\n  width: var(--hb-rail-btn, 40px);\n  height: var(--hb-rail-btn, 40px);\n  margin: 0;\n  padding: 0;\n  border: 0;\n  border-radius: 12px;\n  background: transparent;\n  color: #8f8f8f;\n  text-decoration: none;\n  cursor: pointer;\n  transition: background-color .18s ease, color .18s ease, transform .18s ease;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-rail-btn svg { width: calc(var(--hb-rail-btn, 40px) * .5); height: calc(var(--hb-rail-btn, 40px) * .5); }\n.hb-rail-btn:hover, .hb-rail-li[data-open=\"true\"] > .hb-rail-btn { background: #1c1c1c; color: #ededed; }\n.hb-rail-btn:active { transform: scale(.94); }\n.hb-rail-btn[aria-current=\"true\"] { background: #1c1c1c; color: #ffffff; }\n.hb-rail-btn:focus-visible { outline: 2px solid var(--hb-rail-accent); outline-offset: 1px; }\n.hb-rail-bar {\n  position: absolute;\n  top: 24%;\n  bottom: 24%;\n  left: -8px;\n  width: 3px;\n  border-radius: 0 3px 3px 0;\n  background: var(--hb-rail-accent);\n  transform: scaleY(0);\n  transform-origin: center;\n  transition: transform .28s cubic-bezier(.2, .9, .2, 1);\n}\n.hb-rail-btn[aria-current=\"true\"] .hb-rail-bar { transform: scaleY(1); }\n.hb-rail-tip {\n  position: absolute;\n  top: 50%;\n  left: calc(100% + 10px);\n  z-index: 30;\n  padding: 5px 9px;\n  border: 1px solid #2a2a2a;\n  border-radius: 8px;\n  background: #171717;\n  box-shadow: 0 8px 24px rgba(0, 0, 0, .5);\n  color: #ededed;\n  font-size: 12px;\n  font-weight: 500;\n  line-height: 16px;\n  white-space: nowrap;\n  opacity: 0;\n  pointer-events: none;\n  transform: translate3d(-4px, -50%, 0);\n  transition: opacity .15s ease, transform .15s ease;\n  visibility: hidden;\n}\n.hb-rail-btn:hover .hb-rail-tip,\n.hb-rail-btn:focus-visible .hb-rail-tip {\n  opacity: 1;\n  transform: translate3d(0, -50%, 0);\n  visibility: visible;\n  transition-delay: .25s;\n}\n.hb-rail-foot { display: grid; flex: none; place-items: center; margin-top: 8px; }\n.hb-rail-fly {\n  position: absolute;\n  top: var(--hb-rail-fy, -6px);\n  left: 100%;\n  z-index: 40;\n  padding-left: 12px;\n  animation: hb-rail-in .2s cubic-bezier(.2, .9, .2, 1) both;\n}\n.hb-rail-panel {\n  width: var(--hb-rail-fw, 176px);\n  padding: 6px;\n  border: 1px solid #2a2a2a;\n  border-radius: 14px;\n  background: #151515;\n  box-shadow: 0 18px 40px rgba(0, 0, 0, .55), 0 2px 8px rgba(0, 0, 0, .3);\n}\n.hb-rail-title { padding: 6px 8px 8px; color: #8f8f8f; font-size: 11px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; }\n.hb-rail-links { display: flex; flex-direction: column; gap: 1px; margin: 0; padding: 0; list-style: none; }\n.hb-rail-link {\n  display: flex;\n  align-items: center;\n  gap: 8px;\n  width: 100%;\n  height: 30px;\n  margin: 0;\n  padding: 0 8px;\n  border: 0;\n  border-radius: 8px;\n  background: transparent;\n  color: #b4b4b4;\n  font: inherit;\n  font-size: 13px;\n  text-align: left;\n  text-decoration: none;\n  cursor: pointer;\n  transition: background-color .15s ease, color .15s ease;\n}\n.hb-rail-link:hover { background: #202020; color: #ededed; }\n.hb-rail-link[aria-current=\"true\"] { background: #1f1f1f; color: #ffffff; }\n.hb-rail-link[aria-current=\"true\"]::before {\n  content: \"\";\n  flex: none;\n  width: 5px;\n  height: 5px;\n  margin-right: -2px;\n  border-radius: 999px;\n  background: var(--hb-rail-accent);\n}\n.hb-rail-link:focus-visible { outline: 2px solid var(--hb-rail-accent); outline-offset: -2px; }\n.hb-rail-lt { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }\n.hb-rail-count { flex: none; min-width: 18px; padding: 0 6px; border-radius: 999px; background: #262626; color: #ededed; font-size: 11px; line-height: 18px; text-align: center; }\n@keyframes hb-rail-in {\n  from { opacity: 0; transform: translate3d(-6px, 0, 0); }\n  to { opacity: 1; transform: translate3d(0, 0, 0); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-rail-btn, .hb-rail-bar, .hb-rail-tip, .hb-rail-link { transition: none; }\n  .hb-rail-fly { animation: none; }\n}\n`;\n\nexport function SidebarRail({\n  items = defaultItems,\n  active,\n  defaultActive,\n  onChange,\n  openId,\n  defaultOpenId = null,\n  onOpenChange,\n  brand,\n  footer,\n  flyoutWidth = 176,\n  itemSize = 40,\n  color = \"#2f7bff\",\n  className = \"\",\n  style,\n  onKeyDown,\n  onBlur,\n  onMouseLeave,\n  onMouseEnter,\n  ...rest\n}: SidebarRailProps) {\n  const uid = React.useId();\n  const rootRef = React.useRef<HTMLElement>(null);\n  const flyRef = React.useRef<HTMLDivElement>(null);\n  const btnRefs = React.useRef<Map<string, HTMLElement>>(new Map());\n  const timer = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  const [innerActive, setInnerActive] = React.useState(defaultActive ?? \"\");\n  const [innerOpen, setInnerOpen] = React.useState<string | null>(defaultOpenId);\n  const [focusId, setFocusId] = React.useState(\"\");\n  const current = active ?? innerActive;\n  const open = openId !== undefined ? openId : innerOpen;\n  const openItem = open ? items.find((i) => i.id === open && i.links && i.links.length > 0) : undefined;\n  const openKey = openItem ? openItem.id : \"\";\n\n  const setOpen = React.useCallback(\n    (id: string | null) => {\n      if (openId === undefined) setInnerOpen(id);\n      onOpenChange?.(id);\n    },\n    [openId, onOpenChange],\n  );\n\n  const clearTimer = React.useCallback(() => {\n    if (timer.current) clearTimeout(timer.current);\n    timer.current = null;\n  }, []);\n\n  React.useEffect(() => clearTimer, [clearTimer]);\n\n  React.useLayoutEffect(() => {\n    const fly = flyRef.current;\n    const root = rootRef.current;\n    const li = fly?.parentElement;\n    if (!fly || !root || !li) return;\n    const rootH = root.clientHeight;\n    const above = li.getBoundingClientRect().top - root.getBoundingClientRect().top;\n    const h = fly.offsetHeight;\n    const top = Math.max(-above, Math.min(-6, rootH - h - above - 6));\n    fly.style.setProperty(\"--hb-rail-fy\", `${top.toFixed(1)}px`);\n  }, [openKey]);\n\n  React.useEffect(() => {\n    if (!openKey) return;\n    const onDown = (e: PointerEvent) => {\n      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(null);\n    };\n    document.addEventListener(\"pointerdown\", onDown);\n    return () => document.removeEventListener(\"pointerdown\", onDown);\n  }, [openKey, setOpen]);\n\n  function select(id: string) {\n    if (active === undefined) setInnerActive(id);\n    onChange?.(id);\n  }\n\n  function isActive(item: SidebarRailItem) {\n    return item.id === current || (item.links?.some((l) => l.id === current) ?? false);\n  }\n\n  function focusButton(id: string) {\n    btnRefs.current.get(id)?.focus();\n  }\n\n  function flyoutLinks() {\n    return Array.from(flyRef.current?.querySelectorAll<HTMLElement>(\"[data-rail-link]\") ?? []);\n  }\n\n  function handleKeyDown(e: React.KeyboardEvent<HTMLElement>) {\n    onKeyDown?.(e);\n    if (e.defaultPrevented) return;\n    const target = e.target as HTMLElement;\n    const inFly = !!flyRef.current && flyRef.current.contains(target);\n    if (e.key === \"Escape\") {\n      if (openKey) {\n        e.preventDefault();\n        setOpen(null);\n        if (inFly) focusButton(openKey);\n      }\n      return;\n    }\n    if (inFly) {\n      const links = flyoutLinks();\n      const at = links.indexOf(target);\n      let next = -1;\n      if (e.key === \"ArrowDown\") next = Math.min(links.length - 1, at + 1);\n      else if (e.key === \"ArrowUp\") next = Math.max(0, at - 1);\n      else if (e.key === \"Home\") next = 0;\n      else if (e.key === \"End\") next = links.length - 1;\n      else if (e.key === \"ArrowLeft\") {\n        e.preventDefault();\n        setOpen(null);\n        focusButton(openKey);\n        return;\n      }\n      if (next >= 0) {\n        e.preventDefault();\n        links[next]?.focus();\n      }\n      return;\n    }\n    const id = target.dataset.railItem;\n    if (!id) return;\n    const ids = items.map((i) => i.id);\n    const here = ids.indexOf(id);\n    const item = items[here];\n    let next = -1;\n    if (e.key === \"ArrowDown\") next = Math.min(ids.length - 1, here + 1);\n    else if (e.key === \"ArrowUp\") next = Math.max(0, here - 1);\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = ids.length - 1;\n    if (next >= 0) {\n      e.preventDefault();\n      focusButton(ids[next]);\n      return;\n    }\n    if (item?.links && item.links.length > 0 && (e.key === \"Enter\" || e.key === \" \" || e.key === \"ArrowRight\")) {\n      e.preventDefault();\n      if (open !== item.id) setOpen(item.id);\n      requestAnimationFrame(() => flyoutLinks()[0]?.focus());\n    }\n  }\n\n  function handleBlur(e: React.FocusEvent<HTMLElement>) {\n    onBlur?.(e);\n    const next = e.relatedTarget as Node | null;\n    if (next && rootRef.current && !rootRef.current.contains(next)) setOpen(null);\n  }\n\n  const tabStop = items.some((i) => i.id === focusId)\n    ? focusId\n    : (items.find((i) => isActive(i))?.id ?? items[0]?.id);\n\n  const vars = { \"--hb-rail-accent\": color, \"--hb-rail-fw\": `${flyoutWidth}px`, \"--hb-rail-btn\": `${itemSize}px`, ...style } as React.CSSProperties;\n\n  return (\n    <nav\n      ref={rootRef}\n      className={`hb-rail ${className}`}\n      aria-label=\"Primary\"\n      style={vars}\n      onKeyDown={handleKeyDown}\n      onBlur={handleBlur}\n      onMouseEnter={(e) => {\n        onMouseEnter?.(e);\n        clearTimer();\n      }}\n      onMouseLeave={(e) => {\n        onMouseLeave?.(e);\n        clearTimer();\n        if (openKey) timer.current = setTimeout(() => setOpen(null), 160);\n      }}\n      {...rest}\n    >\n      <style href=\"hb-sidebar-rail\" precedence=\"default\">{css}</style>\n      {brand ? <div className=\"hb-rail-brand\">{brand}</div> : null}\n      <ul className=\"hb-rail-list\" role=\"list\">\n        {items.map((item) => {\n          const hasLinks = !!item.links && item.links.length > 0;\n          const isOpen = openKey === item.id;\n          const selected = isActive(item);\n          const setRef = (el: HTMLElement | null) => {\n            if (el) btnRefs.current.set(item.id, el);\n            else btnRefs.current.delete(item.id);\n          };\n          const flyId = `${uid}-${item.id}`;\n          const common = {\n            className: \"hb-rail-btn\",\n            \"data-rail-item\": item.id,\n            \"aria-current\": selected ? (true as const) : undefined,\n            \"aria-label\": item.label,\n            tabIndex: item.id === tabStop ? 0 : -1,\n            onFocus: () => {\n              setFocusId(item.id);\n              clearTimer();\n              if (hasLinks) {\n                if (open !== item.id) setOpen(item.id);\n              } else if (open) setOpen(null);\n            },\n            onMouseEnter: () => {\n              clearTimer();\n              if (hasLinks) {\n                if (open !== item.id) setOpen(item.id);\n              } else if (open) setOpen(null);\n            },\n          };\n          const inner = (\n            <>\n              <span className=\"hb-rail-bar\" aria-hidden=\"true\" />\n              {item.icon ?? <SidebarRailIcon name=\"layers\" />}\n              {hasLinks ? null : (\n                <span className=\"hb-rail-tip\" aria-hidden=\"true\">\n                  {item.label}\n                </span>\n              )}\n            </>\n          );\n          return (\n            <li key={item.id} className=\"hb-rail-li\" data-open={isOpen}>\n              {item.href && !hasLinks ? (\n                <a ref={setRef} href={item.href} {...common} onClick={() => select(item.id)}>\n                  {inner}\n                </a>\n              ) : (\n                <button\n                  ref={setRef}\n                  type=\"button\"\n                  {...common}\n                  aria-haspopup={hasLinks ? \"true\" : undefined}\n                  aria-expanded={hasLinks ? isOpen : undefined}\n                  aria-controls={hasLinks && isOpen ? flyId : undefined}\n                  onClick={() => {\n                    if (hasLinks) {\n                      if (!isOpen) setOpen(item.id);\n                    } else select(item.id);\n                  }}\n                >\n                  {inner}\n                </button>\n              )}\n              {isOpen && item.links ? (\n                <div ref={flyRef} id={flyId} className=\"hb-rail-fly\" role=\"group\" aria-label={item.label}>\n                  <div className=\"hb-rail-panel\">\n                    <div className=\"hb-rail-title\">{item.label}</div>\n                    <ul className=\"hb-rail-links\" role=\"list\">\n                      {item.links.map((link) => {\n                        const linkCommon = {\n                          className: \"hb-rail-link\",\n                          \"data-rail-link\": link.id,\n                          \"aria-current\": link.id === current ? (true as const) : undefined,\n                          onClick: () => {\n                            select(link.id);\n                            setOpen(null);\n                          },\n                        };\n                        const body = (\n                          <>\n                            <span className=\"hb-rail-lt\">{link.label}</span>\n                            {link.badge !== undefined && link.badge !== \"\" ? <span className=\"hb-rail-count\">{link.badge}</span> : null}\n                          </>\n                        );\n                        return (\n                          <li key={link.id}>\n                            {link.href ? (\n                              <a href={link.href} {...linkCommon}>\n                                {body}\n                              </a>\n                            ) : (\n                              <button type=\"button\" {...linkCommon}>\n                                {body}\n                              </button>\n                            )}\n                          </li>\n                        );\n                      })}\n                    </ul>\n                  </div>\n                </div>\n              ) : null}\n            </li>\n          );\n        })}\n      </ul>\n      {footer ? <div className=\"hb-rail-foot\">{footer}</div> : null}\n    </nav>\n  );\n}\n"
    }
  ]
}