{
  "$schema": "https://uipkge.dev/schema/registry-item.json",
  "name": "theme-river",
  "title": "Theme River",
  "type": "registry:ui",
  "files": [
    {
      "path": "packages/registry-svelte/components/theme-river/ThemeRiver.svelte",
      "content": "<script lang=\"ts\" module>\n  import type { HTMLAttributes } from 'svelte/elements'\n\n  export interface ThemeRiverProps extends HTMLAttributes<HTMLDivElement> {\n    /** `[time, value, series]` tuples. Time can be a date string or number. */\n    data: [string | number, number, string][]\n    height?: number | string\n    option?: any\n    /** Accessible name announced for the chart image. Defaults to \"Chart\". */\n    ariaLabel?: string\n    ref?: HTMLDivElement | null\n  }\n</script>\n\n<script lang=\"ts\">\n  import { init, use, type ECharts } from 'echarts/core'\n  import { CanvasRenderer } from 'echarts/renderers'\n  import { ThemeRiverChart } from 'echarts/charts'\n  import { LegendComponent, SingleAxisComponent, TooltipComponent } from 'echarts/components'\n  import { untrack } from 'svelte'\n  import { cn } from '$lib/utils'\n  import { ChartTheme, mergeOptionBlock } from './useChartTheme.svelte'\n\n  use([CanvasRenderer, ThemeRiverChart, SingleAxisComponent, TooltipComponent, LegendComponent])\n\n  let {\n    data,\n    height = 320,\n    option,\n    class: className,\n    ariaLabel,\n    ref = $bindable(null),\n    ...restProps\n  }: ThemeRiverProps = $props()\n\n  const theme = new ChartTheme()\n\n  const mergedOption = $derived.by(() => {\n    const series = [\n      {\n        type: 'themeRiver',\n        data,\n        label: { show: false },\n        emphasis: { itemStyle: { shadowBlur: 12, shadowColor: 'rgba(0,0,0,0.2)' } },\n      },\n    ]\n\n    const userOption: any = option ?? {}\n    const {\n      series: userSeries,\n      singleAxis: userSingleAxis,\n      tooltip: userTooltip,\n      legend: userLegend,\n      ...userRest\n    } = userOption\n    const mergedSeries = Array.isArray(userSeries)\n      ? series.map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) }))\n      : series\n\n    return {\n      color: theme.colors,\n      tooltip: mergeOptionBlock(\n        {\n          trigger: 'axis',\n          axisPointer: { type: 'line', lineStyle: { color: theme.axisColor, opacity: 0.8 } },\n          backgroundColor: theme.tooltipBg,\n          borderColor: theme.tooltipBorder,\n          textStyle: { color: theme.tooltipText, fontSize: 12 },\n        },\n        userTooltip,\n      ),\n      legend:\n        userLegend?.show === false\n          ? undefined\n          : mergeOptionBlock(\n              {\n                bottom: 0,\n                icon: 'circle',\n                itemWidth: 8,\n                itemHeight: 8,\n                textStyle: { fontSize: 11, color: theme.textColor },\n              },\n              userLegend,\n            ),\n      singleAxis: mergeOptionBlock(\n        {\n          top: 12,\n          bottom: 40,\n          type: 'time',\n          axisTick: { show: false },\n          axisLabel: { color: theme.textColor, fontSize: 11 },\n          axisLine: { lineStyle: { color: theme.axisColor } },\n        },\n        userSingleAxis,\n      ),\n      series: mergedSeries,\n      ...userRest,\n    }\n  })\n\n  const heightStyle = $derived(/^\\d+$/.test(String(height)) ? `${height}px` : String(height))\n\n  let chartEl: HTMLDivElement | null = $state(null)\n  let chart: ECharts | null = null\n\n  $effect(() => {\n    if (!chartEl) return\n    chart = init(chartEl)\n    const instance = chart\n    const ro = new ResizeObserver(() => instance.resize())\n    ro.observe(chartEl)\n    // Apply the current option without subscribing — the update effect below owns that.\n    untrack(() => {\n      instance.setOption(mergedOption)\n    })\n    return () => {\n      ro.disconnect()\n      instance.dispose()\n      if (chart === instance) chart = null\n    }\n  })\n\n  $effect(() => {\n    const opt = mergedOption\n    untrack(() => {\n      chart?.setOption(opt)\n    })\n  })\n</script>\n\n<div\n  bind:this={ref}\n  role=\"img\"\n  aria-label={ariaLabel || 'Chart'}\n  data-uipkge=\"\"\n  data-slot=\"theme-river\"\n  style:height={heightStyle}\n  class={cn('w-full', className)}\n  {...restProps}\n>\n  <div bind:this={chartEl} class=\"size-full\"></div>\n</div>\n",
      "type": "registry:ui",
      "target": "~/src/lib/components/ui/theme-river/ThemeRiver.svelte"
    },
    {
      "path": "packages/registry-svelte/components/theme-river/index.ts",
      "content": "export { default as ThemeRiver, type ThemeRiverProps } from './ThemeRiver.svelte'\n",
      "type": "registry:ui",
      "target": "~/src/lib/components/ui/theme-river/index.ts"
    },
    {
      "path": "packages/registry-svelte/components/theme-river/useChartTheme.svelte.ts",
      "content": "// Local port of the Vue `useChartTheme` helpers this chart needs. Chart\n// palette is driven by Tailwind v4 CSS variables so dark/light flips happen\n// automatically when the consumer toggles their theme class. Values resolve\n// at runtime via `getComputedStyle`, so they pick up whatever the consumer\n// set in their own `tailwind.css` -- no fork required.\n\nexport function toCanvasColor(cssColor: string): string {\n  const value = cssColor.trim()\n  const match = value.match(\n    /^oklch\\(\\s*([+-]?(?:\\d+\\.?\\d*|\\.\\d+))(%?)\\s+([+-]?(?:\\d+\\.?\\d*|\\.\\d+))\\s+([+-]?(?:\\d+\\.?\\d*|\\.\\d+))(?:deg)?(?:\\s*\\/\\s*([+-]?(?:\\d+\\.?\\d*|\\.\\d+))(%?))?\\s*\\)$/i,\n  )\n\n  if (match) {\n    const lightness = Number(match[1]) / (match[2] === '%' ? 100 : 1)\n    const chroma = Number(match[3])\n    const hue = (Number(match[4]) * Math.PI) / 180\n    const alpha = match[5] == null ? 1 : Number(match[5]) / (match[6] === '%' ? 100 : 1)\n    const a = chroma * Math.cos(hue)\n    const b = chroma * Math.sin(hue)\n\n    const l = Math.pow(lightness + 0.3963377774 * a + 0.2158037573 * b, 3)\n    const m = Math.pow(lightness - 0.1055613458 * a - 0.0638541728 * b, 3)\n    const s = Math.pow(lightness - 0.0894841775 * a - 1.291485548 * b, 3)\n    const linear = [\n      4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,\n      -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,\n      -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,\n    ]\n    const rgb = linear.map((channel) => {\n      const encoded = channel <= 0.0031308 ? 12.92 * channel : 1.055 * Math.pow(channel, 1 / 2.4) - 0.055\n      return Math.round(Math.min(1, Math.max(0, encoded)) * 255)\n    })\n\n    return alpha < 1 ? `rgba(${rgb.join(', ')}, ${alpha})` : `rgb(${rgb.join(', ')})`\n  }\n\n  if (typeof document === 'undefined') return cssColor\n  if (!_hexCanvas) {\n    _hexCanvas = document.createElement('canvas').getContext('2d')\n  }\n  if (!_hexCanvas) return cssColor\n  _hexCanvas.fillStyle = '#010203'\n  _hexCanvas.fillStyle = value\n  const normalized = _hexCanvas.fillStyle as string\n  return normalized === '#010203' && value.toLowerCase() !== '#010203' ? value : normalized\n}\n\nlet _hexCanvas: CanvasRenderingContext2D | null = null\n\n// Convert any CSS color (hex, rgb, oklch, color()) + alpha 0..1 to a\n// canvas-safe rgba(r,g,b,a). `colorString + '40'` (8-digit hex alpha)\n// only works when `colorString` is `#rrggbb`; once tokens resolve to\n// oklch() post-hydration the gradient stops break and the canvas paint\n// throws every frame. Stay defensive and always return rgba.\nexport function toRgba(cssColor: string, alpha: number): string {\n  const normalized = toCanvasColor(cssColor)\n  if (normalized.startsWith('#') && normalized.length === 7) {\n    const r = parseInt(normalized.slice(1, 3), 16)\n    const g = parseInt(normalized.slice(3, 5), 16)\n    const b = parseInt(normalized.slice(5, 7), 16)\n    return `rgba(${r},${g},${b},${alpha})`\n  }\n  if (normalized.startsWith('rgba(')) {\n    return normalized.replace(/,\\s*[\\d.]+\\s*\\)$/, `,${alpha})`)\n  }\n  if (normalized.startsWith('rgb(')) {\n    return normalized.replace(/^rgb\\(/, 'rgba(').replace(/\\)$/, `,${alpha})`)\n  }\n  // Canvas refused to parse this color -- ship the original string and\n  // let ECharts complain (better than crashing the paint loop).\n  return cssColor\n}\n\nfunction resolveVar(name: string, fallback: string): string {\n  if (typeof window === 'undefined') return fallback\n  const v = getComputedStyle(document.documentElement).getPropertyValue(name).trim()\n  if (!v) return fallback\n  return toCanvasColor(v)\n}\n\n// SSR / pre-hydration fallback palette. Hex values picked to roughly\n// match the shadcn Neutral defaults in `tailwind.css` so the first paint\n// doesn't flicker.\nconst CHART_FALLBACK = ['#f59e0b', '#14b8a6', '#3b82f6', '#f97316', '#eab308']\n\n// Reactive theme-token source. `version` bumps whenever `<html>`\n// class/style changes (the typical shadcn dark-mode pivot) so every getter\n// re-resolves and downstream ECharts options re-paint.\nexport class ChartTheme {\n  version = $state(0)\n\n  constructor() {\n    if (typeof window === 'undefined') return\n    // Bump once on the first paint so post-hydration getComputedStyle reads\n    // the *resolved* CSS values.\n    requestAnimationFrame(() => this.version++)\n    new MutationObserver(() => this.version++).observe(document.documentElement, {\n      attributes: true,\n      attributeFilter: ['class', 'style', 'data-theme'],\n    })\n  }\n\n  get colors(): string[] {\n    this.version\n    return Array.from({ length: 5 }, (_, i) => resolveVar(`--chart-${i + 1}`, CHART_FALLBACK[i]!))\n  }\n\n  get textColor(): string {\n    this.version\n    return resolveVar('--muted-foreground', '#888888')\n  }\n\n  get axisColor(): string {\n    this.version\n    return resolveVar('--border', '#e5e5e5')\n  }\n\n  get tooltipBg(): string {\n    this.version\n    return resolveVar('--popover', 'rgba(255,255,255,0.96)')\n  }\n\n  get tooltipBorder(): string {\n    this.version\n    return resolveVar('--border', '#e5e5e5')\n  }\n\n  get tooltipText(): string {\n    this.version\n    return resolveVar('--popover-foreground', '#333333')\n  }\n}\n\n// Two-level deep merge for ECharts option blocks (xAxis, yAxis, grid,\n// tooltip, legend, singleAxis, parallel, etc.). The top-level keys merge\n// shallowly, but one nested level (axisLabel, axisLine, splitLine, etc.)\n// merges shallowly too so a consumer passing `xAxis: { axisLabel: { fontSize: 9 } }`\n// doesn't blow away the wrapper's `color` + base font defaults on the same\n// axisLabel block. Arrays + primitives replace outright.\nexport function mergeOptionBlock<T extends Record<string, any>>(base: T, user: Partial<T> | undefined): T {\n  if (!user) return base\n  const out: any = { ...base }\n  for (const k of Object.keys(user)) {\n    const bv = (base as any)[k]\n    const uv = (user as any)[k]\n    if (bv != null && uv != null && typeof bv === 'object' && typeof uv === 'object' && !Array.isArray(bv) && !Array.isArray(uv)) {\n      out[k] = { ...bv, ...uv }\n    } else {\n      out[k] = uv\n    }\n  }\n  return out\n}\n",
      "type": "registry:ui",
      "target": "~/src/lib/components/ui/theme-river/useChartTheme.svelte.ts"
    }
  ],
  "dependencies": [
    "echarts"
  ],
  "devDependencies": [],
  "registryDependencies": [],
  "description": "Theme-river (streamgraph) wrapper around Apache ECharts. Stacked areas centred on a baseline along a time axis — good for topic-volume drift over time.",
  "categories": [
    "chart"
  ]
}