{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "view-switcher",
  "type": "registry:ui",
  "title": "View Switcher",
  "description": "The bar of a table's open views, with an Add view menu.",
  "categories": [
    "navigation"
  ],
  "dependencies": [
    "@base-ui/react@1.9.0",
    "@hugeicons/core-free-icons@4.4.0",
    "@hugeicons/react@1.1.10"
  ],
  "registryDependencies": [
    "https://sprout.tulipfarm.ai/r/sprout-base.json",
    "https://sprout.tulipfarm.ai/r/button.json",
    "https://sprout.tulipfarm.ai/r/dropdown-menu.json",
    "https://sprout.tulipfarm.ai/r/tabs.json"
  ],
  "files": [
    {
      "path": "registry/sprout/components/ui/view-switcher.tsx",
      "type": "registry:ui",
      "target": "components/ui/view-switcher.tsx",
      "content": "'use client';\n\nimport { Tabs as BaseTabs } from '@base-ui/react/tabs';\nimport {\n  useEffect,\n  useId,\n  useLayoutEffect,\n  useRef,\n  type ComponentType,\n  type KeyboardEvent,\n  type ReactNode,\n} from 'react';\nimport { AddIcon } from '@/lib/sprout/icons/add-icon';\nimport { CheckIcon } from '@/lib/sprout/icons/check-icon';\nimport { CloseIcon } from '@/lib/sprout/icons/close-icon';\nimport { cn } from '@/lib/sprout/classes';\nimport { Button } from '@/components/ui/button';\nimport { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';\nimport { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs';\n\nexport interface ViewSwitcherView {\n  id: string;\n  label: string;\n  icon: ComponentType<{ size?: number }>;\n  closable?: boolean | undefined;\n}\n\nexport interface ViewSwitcherOption extends ViewSwitcherView {\n  description: string;\n  unavailable?: string | undefined;\n}\n\nexport interface ViewSwitcherProps {\n  views: ViewSwitcherView[];\n  value: string;\n  onValueChange: (id: string) => void;\n  options?: ViewSwitcherOption[] | undefined;\n  onAdd?: ((id: string) => void) | undefined;\n  onClose?: ((id: string) => void) | undefined;\n  label?: string | undefined;\n  addLabel?: string | undefined;\n  actions?: ReactNode;\n  panelId?: string | undefined;\n  className?: string | undefined;\n}\n\nexport function viewSwitcherTabId(panelId: string, viewId: string) {\n  return `${panelId}-tab-${viewId}`;\n}\n\nconst fade = 24;\n\nexport function ViewSwitcher({\n  views,\n  value,\n  onValueChange,\n  options,\n  onAdd,\n  onClose,\n  label = 'Views',\n  addLabel = 'Add view',\n  actions,\n  panelId,\n  className,\n}: ViewSwitcherProps) {\n  const baseId = useId();\n  const listRef = useRef<HTMLDivElement>(null);\n  const tabs = useRef(new Map<string, HTMLElement>());\n  const pendingFocus = useRef<{ closed: string; neighbour: string | undefined } | null>(null);\n  const open = new Set(views.map(view => view.id));\n\n  const tabId = (id: string) => (panelId ? viewSwitcherTabId(panelId, id) : `${baseId}-tab-${id}`);\n\n  function close(index: number) {\n    const view = views[index];\n    if (!view || !view.closable || !onClose) return;\n    const tab = tabs.current.get(view.id);\n    if (tab && tab === document.activeElement) {\n      pendingFocus.current = { closed: view.id, neighbour: (views[index + 1] ?? views[index - 1])?.id };\n    }\n    onClose(view.id);\n  }\n\n  function onTabKeyDown(event: KeyboardEvent<HTMLElement>, index: number) {\n    if (event.key !== 'Delete' && event.key !== 'Backspace') return;\n    event.preventDefault();\n    close(index);\n  }\n\n  useLayoutEffect(() => {\n    const pending = pendingFocus.current;\n    if (!pending || open.has(pending.closed)) return;\n    pendingFocus.current = null;\n    const target = value !== pending.closed && open.has(value) ? value : pending.neighbour;\n    if (target) tabs.current.get(target)?.focus();\n  }, [views, value]);\n\n  useLayoutEffect(() => {\n    const list = listRef.current;\n    const tab = tabs.current.get(value);\n    if (!list || !tab || list.scrollWidth <= list.clientWidth) return;\n    const bounds = list.getBoundingClientRect();\n    const rect = tab.getBoundingClientRect();\n    if (rect.left < bounds.left + fade) list.scrollLeft -= bounds.left + fade - rect.left;\n    else if (rect.right > bounds.right - fade) list.scrollLeft += rect.right - (bounds.right - fade);\n  }, [value, views]);\n\n  useEffect(() => {\n    const list = listRef.current;\n    if (!list) return;\n    const update = () => {\n      const scrolled = Math.abs(list.scrollLeft);\n      const max = list.scrollWidth - list.clientWidth;\n      list.toggleAttribute('data-overflow-start', scrolled > 1);\n      list.toggleAttribute('data-overflow-end', max - scrolled > 1);\n    };\n    update();\n    list.addEventListener('scroll', update, { passive: true });\n    const observer = new ResizeObserver(update);\n    observer.observe(list);\n    for (const child of list.children) observer.observe(child);\n    return () => {\n      list.removeEventListener('scroll', update);\n      observer.disconnect();\n    };\n  }, [views]);\n\n  return (\n    <div data-slot=\"view-switcher\" className={cn('sprout-view-switcher', className)}>\n      <Tabs\n        className=\"sprout-view-switcher-tabs\"\n        value={open.has(value) ? value : null}\n        onValueChange={next => {\n          if (typeof next === 'string') onValueChange(next);\n        }}\n      >\n        <TabsList\n          ref={listRef}\n          variant=\"line\"\n          activateOnFocus\n          loopFocus={false}\n          aria-label={label}\n          className=\"sprout-view-switcher-list\"\n        >\n          {views.map((view, index) => {\n            const Icon = view.icon;\n            const closable = Boolean(view.closable && onClose);\n            return (\n              <TabsTrigger\n                key={view.id}\n                value={view.id}\n                id={tabId(view.id)}\n                ref={(node: HTMLElement | null) => {\n                  if (node) tabs.current.set(view.id, node);\n                  else tabs.current.delete(view.id);\n                }}\n                className=\"sprout-view-switcher-tab\"\n                data-closable={closable || undefined}\n                aria-keyshortcuts={closable ? 'Delete' : undefined}\n                {...(panelId ? { 'aria-controls': panelId } : {})}\n                onKeyDown={event => onTabKeyDown(event, index)}\n              >\n                <span className=\"sprout-view-switcher-icon\" aria-hidden=\"true\">\n                  <Icon size={14} />\n                </span>\n                <span className=\"sprout-view-switcher-label\">{view.label}</span>\n                {closable && (\n                  <span\n                    aria-hidden=\"true\"\n                    title={`Close ${view.label}`}\n                    data-slot=\"view-switcher-close\"\n                    className=\"sprout-view-switcher-close\"\n                    onPointerDown={event => {\n                      event.preventDefault();\n                      event.stopPropagation();\n                    }}\n                    onMouseDown={event => event.preventDefault()}\n                    onClick={event => {\n                      event.preventDefault();\n                      event.stopPropagation();\n                      close(index);\n                    }}\n                  >\n                    <CloseIcon size=\"xs\" />\n                  </span>\n                )}\n              </TabsTrigger>\n            );\n          })}\n          <BaseTabs.Indicator className=\"sprout-view-switcher-indicator\" />\n        </TabsList>\n      </Tabs>\n      {options && (\n        <DropdownMenu>\n          <DropdownMenuTrigger\n            render={<Button variant=\"ghost\" size=\"sm\" className=\"sprout-view-switcher-add\" />}\n            data-slot=\"view-switcher-add\"\n          >\n            <AddIcon size=\"sm\" aria-hidden=\"true\" data-icon=\"inline-start\" />\n            <span className=\"sprout-view-switcher-add-label\">{addLabel}</span>\n          </DropdownMenuTrigger>\n          <DropdownMenuContent align=\"start\" size=\"menu\" className=\"sprout-view-switcher-menu\">\n            <div className=\"sprout-view-switcher-options\">\n              {options.map(option => (\n                <ViewOption\n                  key={option.id}\n                  option={option}\n                  baseId={baseId}\n                  isOpen={open.has(option.id)}\n                  onSelect={() => onAdd?.(option.id)}\n                />\n              ))}\n            </div>\n          </DropdownMenuContent>\n        </DropdownMenu>\n      )}\n      {actions != null && actions !== false && (\n        <div data-slot=\"view-switcher-actions\" className=\"sprout-view-switcher-actions\">\n          {actions}\n        </div>\n      )}\n    </div>\n  );\n}\n\nfunction ViewOption({\n  option,\n  baseId,\n  isOpen,\n  onSelect,\n}: {\n  option: ViewSwitcherOption;\n  baseId: string;\n  isOpen: boolean;\n  onSelect: () => void;\n}) {\n  const Icon = option.icon;\n  const unavailable = Boolean(option.unavailable);\n  const titleId = `${baseId}-option-${option.id}-title`;\n  const noteId = `${baseId}-option-${option.id}-note`;\n  const openId = `${baseId}-option-${option.id}-open`;\n  return (\n    <DropdownMenuItem\n      data-slot=\"view-switcher-option\"\n      className=\"sprout-view-switcher-option\"\n      disabled={unavailable}\n      data-open={isOpen || undefined}\n      aria-labelledby={titleId}\n      aria-describedby={isOpen ? `${openId} ${noteId}` : noteId}\n      onClick={unavailable ? undefined : onSelect}\n    >\n      <span className=\"sprout-view-switcher-tile\" aria-hidden=\"true\">\n        <Icon size={16} />\n      </span>\n      <span className=\"sprout-view-switcher-option-text\">\n        <span id={titleId} className=\"sprout-view-switcher-option-title\">\n          {option.label}\n        </span>\n        <span\n          id={noteId}\n          className=\"sprout-view-switcher-option-note\"\n          data-kind={unavailable ? 'unavailable' : 'description'}\n        >\n          {option.unavailable || option.description}\n        </span>\n      </span>\n      {isOpen && (\n        <span className=\"sprout-view-switcher-check\">\n          <CheckIcon size=\"md\" aria-hidden=\"true\" />\n          <span id={openId} className=\"sprout-view-switcher-sr-only\">\n            Already open.\n          </span>\n        </span>\n      )}\n    </DropdownMenuItem>\n  );\n}\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/add-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/add-icon.tsx",
      "content": "import { Add01Icon as glyph } from '@hugeicons/core-free-icons';\nimport { createIcon } from '@/lib/sprout/icons/create-icon';\n\nexport const AddIcon = /* @__PURE__ */ createIcon(glyph, 'AddIcon');\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/check-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/check-icon.tsx",
      "content": "import { Tick02Icon as glyph } from '@hugeicons/core-free-icons';\nimport { createIcon } from '@/lib/sprout/icons/create-icon';\n\nexport const CheckIcon = /* @__PURE__ */ createIcon(glyph, 'CheckIcon');\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/close-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/close-icon.tsx",
      "content": "import { Cancel01Icon as glyph } from '@hugeicons/core-free-icons';\nimport { createIcon } from '@/lib/sprout/icons/create-icon';\n\nexport const CloseIcon = /* @__PURE__ */ createIcon(glyph, 'CloseIcon');\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/create-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/create-icon.tsx",
      "content": "import { HugeiconsIcon, type IconSvgElement } from '@hugeicons/react';\nimport type { ReactElement, Ref, SVGProps } from 'react';\n\nexport const iconSizes = {\n  xs: 12,\n  sm: 14,\n  md: 16,\n  lg: 18,\n  xl: 20,\n  '2xl': 24,\n} as const;\n\nexport type IconSize = keyof typeof iconSizes;\n\nexport const iconStrokeWidth = 1.5;\n\nexport type IconProps = Omit<SVGProps<SVGSVGElement>, 'ref'> & {\n  size?: IconSize | number;\n  ref?: Ref<SVGSVGElement>;\n};\n\nexport type IconComponent = (props: IconProps) => ReactElement;\n\nexport function createIcon(glyph: IconSvgElement, name: string): IconComponent {\n  const Icon = ({ size = '2xl', strokeWidth = iconStrokeWidth, ...props }: IconProps) => (\n    <HugeiconsIcon\n      icon={glyph}\n      size={typeof size === 'number' ? size : iconSizes[size]}\n      strokeWidth={Number(strokeWidth)}\n      {...props}\n    />\n  );\n  Icon.displayName = name;\n  return Icon;\n}\n"
    }
  ],
  "css": {
    "@layer components": {
      ".sprout-view-switcher": {
        "border-bottom": "1px solid var(--sprout-line)",
        "min-width": "0",
        "font-family": "var(--sprout-font)",
        "align-items": "center",
        "gap": "4px",
        "display": "flex",
        "container-type": "inline-size"
      },
      ".sprout-view-switcher-tabs.sprout-tabs": {
        "flex": "0 auto",
        "min-width": "0",
        "display": "flex"
      },
      ".sprout-view-switcher-list.sprout-tabs-list": {
        "--sprout-view-switcher-fade-start": "0px",
        "--sprout-view-switcher-fade-end": "0px",
        "width": "auto",
        "max-width": "100%",
        "height": "calc(var(--sprout-control-height) + 8px)",
        "overscroll-behavior-x": "contain",
        "scrollbar-width": "none",
        "mask-image": "linear-gradient(to right,\n      transparent 0,\n      oklch(0% 0 0) var(--sprout-view-switcher-fade-start),\n      oklch(0% 0 0) calc(100% - var(--sprout-view-switcher-fade-end)),\n      transparent 100%)",
        "justify-content": "flex-start",
        "gap": "2px",
        "padding": "0 2px",
        "overflow": "auto hidden"
      },
      ".sprout-view-switcher-list::-webkit-scrollbar": {
        "display": "none"
      },
      ".sprout-view-switcher-list[data-overflow-start]": {
        "--sprout-view-switcher-fade-start": "24px"
      },
      ".sprout-view-switcher-list[data-overflow-end]": {
        "--sprout-view-switcher-fade-end": "24px"
      },
      ".sprout-view-switcher-list:dir(rtl)": {
        "mask-image": "linear-gradient(to left,\n      transparent 0,\n      oklch(0% 0 0) var(--sprout-view-switcher-fade-start),\n      oklch(0% 0 0) calc(100% - var(--sprout-view-switcher-fade-end)),\n      transparent 100%)"
      },
      ".sprout-view-switcher-tab.sprout-tabs-trigger": {
        "height": "calc(var(--sprout-control-height) - 4px)",
        "color": "var(--sprout-muted)",
        "border": "0",
        "flex": "none",
        "gap": "6px",
        "padding": "0 8px"
      },
      ".sprout-view-switcher-tab.sprout-tabs-trigger:after": {
        "display": "none"
      },
      ".sprout-view-switcher-tab.sprout-tabs-trigger:focus-visible": {
        "outline-offset": "-2px"
      },
      ".sprout-view-switcher-tab.sprout-tabs-trigger[data-active]": {
        "color": "var(--sprout-text)"
      },
      ".sprout-view-switcher-tab[data-closable]": {
        "padding-inline-end": "4px"
      },
      "@media (hover: hover)": {
        ".sprout-view-switcher-tab.sprout-tabs-trigger:hover": {
          "background": "var(--sprout-hover)",
          "color": "var(--sprout-text)"
        },
        ".sprout-view-switcher-close:hover": {
          "background": "var(--sprout-active)",
          "color": "var(--sprout-text)"
        },
        ".sprout-view-switcher-tab[data-closable]": {
          "padding-inline-end": "8px"
        },
        ".sprout-view-switcher-close": {
          "inset-inline-end": "4px",
          "position": "absolute",
          "top": "50%",
          "translate": "0 -50%"
        },
        ".sprout-view-switcher-tab[data-closable]:is(:hover, :focus-visible) .sprout-view-switcher-label": {
          "mask-image": "linear-gradient(to left, #0000 14px, oklch(0% 0 0) 24px)"
        },
        ".sprout-view-switcher-tab[data-closable]:is(:hover, :focus-visible) .sprout-view-switcher-label:dir(rtl)": {
          "mask-image": "linear-gradient(to right, #0000 14px, oklch(0% 0 0) 24px)"
        },
        ".sprout-view-switcher-add.sprout-button:hover": {
          "color": "var(--sprout-text)"
        }
      },
      ".sprout-view-switcher-icon": {
        "flex-shrink": "0",
        "display": "inline-flex"
      },
      ".sprout-view-switcher-tab .sprout-view-switcher-icon svg": {
        "width": "14px",
        "height": "14px"
      },
      ".sprout-view-switcher-close": {
        "border-radius": "calc(var(--sprout-radius-control) - 2px)",
        "width": "16px",
        "height": "16px",
        "color": "var(--sprout-muted)",
        "opacity": "0",
        "cursor": "pointer",
        "flex-shrink": "0",
        "justify-content": "center",
        "align-items": "center",
        "display": "inline-flex"
      },
      ".sprout-view-switcher-tab .sprout-view-switcher-close svg": {
        "pointer-events": "none",
        "width": "12px",
        "height": "12px"
      },
      ".sprout-view-switcher-tab:focus-visible .sprout-view-switcher-close, .sprout-view-switcher-tab:hover .sprout-view-switcher-close": {
        "opacity": "1"
      },
      "@media (hover: none)": {
        ".sprout-view-switcher-close": {
          "opacity": "1"
        }
      },
      "@media (pointer: coarse)": {
        ".sprout-view-switcher-close": {
          "position": "relative"
        },
        ".sprout-view-switcher-close:after": {
          "content": "\"\"",
          "position": "absolute",
          "inset": "-4px"
        }
      },
      ".sprout-view-switcher-indicator": {
        "bottom": "0",
        "left": "var(--active-tab-left)",
        "width": "var(--active-tab-width)",
        "background": "var(--sprout-text)",
        "pointer-events": "none",
        "border-radius": "2px 2px 0 0",
        "height": "2px",
        "position": "absolute"
      },
      ".sprout-view-switcher-add.sprout-button": {
        "color": "var(--sprout-muted)",
        "flex": "none"
      },
      ".sprout-view-switcher-add.sprout-button[data-popup-open]": {
        "color": "var(--sprout-text)"
      },
      ".sprout-view-switcher-actions": {
        "flex": "none",
        "align-items": "center",
        "gap": "4px",
        "margin-inline-start": "auto",
        "padding-inline-start": "8px",
        "display": "flex"
      },
      "@container (width <= 479px)": {
        ".sprout-view-switcher-add-label": {
          "clip-path": "inset(50%)",
          "white-space": "nowrap",
          "width": "1px",
          "height": "1px",
          "position": "absolute",
          "overflow": "hidden"
        },
        ".sprout-view-switcher-add.sprout-button": {
          "padding-inline": "6px"
        },
        ".sprout-view-switcher-options": {
          "grid-template-columns": "minmax(0, 1fr)"
        }
      },
      ".sprout-view-switcher-menu.sprout-dropdown-menu-content": {
        "width": "min(36rem, var(--available-width))",
        "padding": "6px",
        "container-type": "inline-size"
      },
      ".sprout-view-switcher-options": {
        "grid-template-columns": "repeat(2, minmax(0, 1fr))",
        "gap": "2px",
        "display": "grid"
      },
      ".sprout-view-switcher-option.sprout-dropdown-menu-item": {
        "align-items": "flex-start",
        "gap": "10px",
        "padding": "8px"
      },
      ".sprout-view-switcher-tile": {
        "border-radius": "var(--sprout-radius-control)",
        "background": "var(--sprout-hover)",
        "width": "32px",
        "height": "32px",
        "color": "var(--sprout-text)",
        "flex-shrink": "0",
        "justify-content": "center",
        "align-items": "center",
        "display": "inline-flex"
      },
      ".sprout-view-switcher-option[data-highlighted] .sprout-view-switcher-tile": {
        "background": "var(--sprout-surface)",
        "box-shadow": "inset 0 0 0 1px var(--sprout-line)"
      },
      ".sprout-view-switcher-option-text": {
        "flex": "auto",
        "gap": "2px",
        "min-width": "0",
        "padding-top": "1px",
        "display": "grid"
      },
      ".sprout-view-switcher-option-title": {
        "font-weight": "500",
        "line-height": "1.25rem"
      },
      ".sprout-view-switcher-option-note": {
        "color": "var(--sprout-muted)",
        "font-size": "var(--sprout-text-xs)",
        "overflow-wrap": "anywhere",
        "line-height": "1rem"
      },
      ".sprout-view-switcher-check": {
        "color": "var(--sprout-text)",
        "flex-shrink": "0",
        "align-self": "center",
        "display": "inline-flex"
      },
      ".sprout-view-switcher-option.sprout-dropdown-menu-item[data-disabled]": {
        "opacity": "1",
        "cursor": "not-allowed"
      },
      ".sprout-view-switcher-option[data-disabled] .sprout-view-switcher-tile": {
        "box-shadow": "inset 0 0 0 1px var(--sprout-line)",
        "color": "var(--sprout-muted)",
        "background": "none"
      },
      ".sprout-view-switcher-option[data-disabled] .sprout-view-switcher-option-title": {
        "color": "var(--sprout-muted)"
      },
      ".sprout-view-switcher-option-note[data-kind=\"unavailable\"]": {
        "white-space": "normal"
      },
      ".sprout-view-switcher-sr-only": {
        "clip-path": "inset(50%)",
        "white-space": "nowrap",
        "width": "1px",
        "height": "1px",
        "position": "absolute",
        "overflow": "hidden"
      },
      "@media (forced-colors: active)": {
        ".sprout-view-switcher-indicator": {
          "background": "highlight"
        },
        ".sprout-view-switcher-tile": {
          "border": "1px solid canvastext"
        }
      },
      "@media (prefers-reduced-motion: no-preference)": {
        ".sprout-view-switcher-indicator": {
          "transition": "left var(--sprout-motion-normal) var(--sprout-motion-ease),\n        width var(--sprout-motion-normal) var(--sprout-motion-ease)"
        },
        ".sprout-view-switcher-close": {
          "transition": "opacity var(--sprout-motion-fast) var(--sprout-motion-ease),\n        background-color var(--sprout-motion-fast) var(--sprout-motion-ease)"
        },
        ".sprout-view-switcher-tile": {
          "transition": "background-color var(--sprout-motion-fast) var(--sprout-motion-ease),\n        box-shadow var(--sprout-motion-fast) var(--sprout-motion-ease)"
        }
      }
    }
  },
  "docs": "Docs and examples: https://sprout.tulipfarm.ai/components/view-switcher/",
  "meta": {
    "source": "packages/sprout/src/components/view-switcher.tsx"
  }
}
