{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-legend",
  "title": "Chart legend",
  "description": "Shared interactive legend: aria-pressed toggle buttons with popping square, dot, line or dashed swatches, hidden-state strike-through, optional values and a hover channel that highlights a series in its chart.",
  "dependencies": [],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/charts/chart-legend.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens } from \"@/lib/goodui-motion\"\n\n/*\n * The legend the goodui charts share. Items are real toggle buttons when the legend is interactive, so\n * hiding a series works with the keyboard and screen readers (aria-pressed), and hovering an item can\n * highlight its series in the chart through onActiveChange.\n */\n\nexport type ChartLegendShape = \"square\" | \"dot\" | \"line\" | \"dashed\"\n\nexport type ChartLegendItem = {\n  key: string\n  label: React.ReactNode\n  /** Any CSS color, e.g. \"var(--chart-2)\". */\n  color: string\n  /** Muted value after the label, e.g. \"42%\" or \"$12.4K\". */\n  value?: React.ReactNode\n  /** Swatch shape for this item; the legend's shape otherwise. */\n  shape?: ChartLegendShape\n}\n\nexport type ChartLegendLabels = {\n  /** Tooltip (title) of a toggle button; its accessible name stays the visible label. */\n  toggle: (label: string, hidden: boolean) => string\n  /** Accessible name of the list. */\n  legend: string\n}\nconst DEFAULT_LABELS: ChartLegendLabels = {\n  toggle: (label, hidden) => (hidden ? `Show ${label}` : `Hide ${label}`),\n  legend: \"Legend\",\n}\n\nexport type ChartLegendProps = Omit<React.ComponentProps<\"div\">, \"onToggle\" | \"hidden\"> & {\n  items: ChartLegendItem[]\n  /** Keys of hidden items (controlled). */\n  hidden?: string[]\n  /** Hidden keys to start with when uncontrolled. */\n  defaultHidden?: string[]\n  /** Called with the new hidden keys after a toggle. */\n  onHiddenChange?: (hidden: string[]) => void\n  /** Items toggle on click, Enter and Space. On by default when hidden, defaultHidden or onHiddenChange is set. */\n  interactive?: boolean\n  /** Key of the highlighted item (controlled hover sync). */\n  active?: string | null\n  /** Called when the pointer or focus enters an item (key) or leaves (null). */\n  onActiveChange?: (key: string | null) => void\n  /** Swatch shape: square for bars and areas, line or dashed for lines, dot for points. */\n  shape?: ChartLegendShape\n  layout?: \"row\" | \"column\"\n  align?: \"start\" | \"center\" | \"end\"\n  size?: \"sm\" | \"md\"\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<ChartLegendLabels>\n}\n\nconst CSS = `\n.pc-chart-legend-item{color:var(--foreground);transition:color .2s,opacity .2s}\n.pc-chart-legend-item[data-hidden=true]{color:var(--muted-foreground)}\n.pc-chart-legend-item[data-dim=true]{opacity:.45}\n.pc-chart-legend-swatch{--pc-sw:var(--chart-1);background:var(--pc-sw);transition:background-color .2s,box-shadow .2s,transform .3s var(--pc-ease-spring)}\n.pc-chart-legend-item[data-hidden=true] .pc-chart-legend-swatch{transform:scale(.85)}\nbutton.pc-chart-legend-item:active .pc-chart-legend-swatch{transform:scale(.8);transition-duration:.08s}\n.pc-chart-legend-swatch[data-shape=square]{width:10px;height:10px;border-radius:3px}\n.pc-chart-legend-swatch[data-shape=dot]{width:9px;height:9px;border-radius:999px}\n.pc-chart-legend-swatch[data-shape=line]{width:14px;height:2.5px;border-radius:999px}\n.pc-chart-legend-swatch[data-shape=dashed]{width:14px;height:0;background:none;border-top:2.5px dashed var(--pc-sw)}\n.pc-chart-legend-item[data-hidden=true] .pc-chart-legend-swatch{background:transparent;box-shadow:inset 0 0 0 1.5px var(--pc-sw)}\n.pc-chart-legend-item[data-hidden=true] .pc-chart-legend-swatch[data-shape=dashed]{border-color:color-mix(in oklab,var(--pc-sw) 45%,transparent)}\n.pc-chart-legend-item[data-hidden=true] .pc-chart-legend-text{text-decoration:line-through;text-decoration-color:color-mix(in oklab,var(--muted-foreground) 60%,transparent)}\n@media (prefers-reduced-motion:reduce){.pc-chart-legend-item,.pc-chart-legend-swatch{transition:none}}\n`\n\n/** Interactive legend: toggle buttons with aria-pressed, a hover highlight channel and square, dot, line or dashed swatches. */\nexport function ChartLegend({\n  items,\n  hidden: hiddenProp,\n  defaultHidden,\n  onHiddenChange,\n  interactive,\n  active,\n  onActiveChange,\n  shape = \"square\",\n  layout = \"row\",\n  align = \"start\",\n  size = \"md\",\n  labels,\n  className,\n  ...props\n}: ChartLegendProps) {\n  const l = useLabels(\"chart-legend\", DEFAULT_LABELS, labels)\n  const [inner, setInner] = React.useState<string[]>(defaultHidden ?? [])\n  const hidden = hiddenProp ?? inner\n  const canToggle = interactive ?? (hiddenProp !== undefined || defaultHidden !== undefined || onHiddenChange !== undefined)\n\n  const toggle = (key: string) => {\n    const next = hidden.includes(key) ? hidden.filter((k) => k !== key) : [...hidden, key]\n    if (hiddenProp === undefined) setInner(next)\n    onHiddenChange?.(next)\n  }\n\n  const text = size === \"sm\" ? \"text-xs\" : \"text-sm\"\n  const pad = size === \"sm\" ? \"px-1.5 py-0.5\" : \"px-2 py-1\"\n\n  return (\n    <div\n      role={canToggle ? \"group\" : \"list\"}\n      aria-label={l.legend}\n      className={cn(\n        \"pc-chart-legend flex min-w-0\",\n        layout === \"row\" ? \"flex-row flex-wrap items-center\" : \"flex-col items-stretch\",\n        layout === \"row\" && (align === \"center\" ? \"justify-center\" : align === \"end\" ? \"justify-end\" : \"justify-start\"),\n        size === \"sm\" ? \"gap-x-1 gap-y-0.5\" : \"gap-x-2 gap-y-1\",\n        className,\n      )}\n      {...props}\n    >\n      <GooduiTokens />\n      <style href=\"pc-chart-legend\" precedence=\"default\">\n        {CSS}\n      </style>\n      {items.map((item) => {\n        const isHidden = hidden.includes(item.key)\n        const dim = active != null && active !== item.key && !isHidden\n        const label = typeof item.label === \"string\" ? item.label : item.key\n        const content = (\n          <>\n            <span aria-hidden=\"true\" data-shape={item.shape ?? shape} className=\"pc-chart-legend-swatch inline-block shrink-0\" style={{ [\"--pc-sw\" as string]: item.color }} />\n            <span className=\"pc-chart-legend-text truncate\">{item.label}</span>\n            {item.value !== undefined && <span className=\"shrink-0 text-muted-foreground tabular-nums\">{item.value}</span>}\n          </>\n        )\n        const common = {\n          \"data-hidden\": isHidden,\n          \"data-dim\": dim,\n          onPointerEnter: () => onActiveChange?.(item.key),\n          onPointerLeave: () => onActiveChange?.(null),\n          className: cn(\"pc-chart-legend-item inline-flex min-w-0 items-center gap-2 rounded-md leading-none\", text, pad, layout === \"column\" && \"justify-start\"),\n        }\n        return canToggle ? (\n          <button\n            key={item.key}\n            type=\"button\"\n            aria-pressed={!isHidden}\n            title={l.toggle(label, isHidden)}\n            onClick={() => toggle(item.key)}\n            onFocus={() => onActiveChange?.(item.key)}\n            onBlur={() => onActiveChange?.(null)}\n            {...common}\n            className={cn(\"pc-press\", common.className, \"cursor-pointer outline-none select-none hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50\")}\n          >\n            {content}\n          </button>\n        ) : (\n          <div key={item.key} role=\"listitem\" {...common}>\n            {content}\n          </div>\n        )\n      })}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/chart-legend.tsx"
    }
  ],
  "categories": [
    "components",
    "charts"
  ],
  "type": "registry:component"
}