{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "theme",
  "type": "registry:lib",
  "title": "Klar theme",
  "description": "Design tokens, light/dark scope and helpers every Klar chart needs. Installed automatically with any chart.",
  "dependencies": [
    "clsx",
    "klar-charts",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/klar/components/ThemeScope.tsx",
      "content": "import { useMemo, type CSSProperties, type ReactNode } from 'react'\nimport { cn } from '@/components/klar/lib/cn'\nimport { ThemeContext, type Theme } from '@/components/klar/lib/theme'\n\n/**\n * Applies the Klar tokens (colours, surfaces, type) to a subtree. Every chart must sit inside one.\n *\n *   <ThemeScope theme=\"dark\" accent=\"#6e56cf\">…charts…</ThemeScope>\n *\n * `theme` defaults to light; `accent` (any CSS colour) defaults to the kit blue. `surface` paints the\n * scope with the theme's panel colour (useful for dark charts on a light page).\n */\nexport function ThemeScope({\n  theme = 'light',\n  accent,\n  surface = false,\n  onToggle,\n  children,\n  className,\n  style,\n}: {\n  theme?: Theme\n  accent?: string\n  surface?: boolean\n  onToggle?: () => void\n  children: ReactNode\n  className?: string\n  style?: CSSProperties\n}) {\n  const ctx = useMemo(() => ({ theme, toggle: onToggle ?? (() => {}) }), [theme, onToggle])\n  return (\n    <ThemeContext.Provider value={ctx}>\n      <div\n        className={cn('klar', theme === 'dark' && 'dark', surface && 'bg-panel', className)}\n        style={(accent ? { '--accent': accent, ...style } : style) as CSSProperties}\n      >\n        {children}\n      </div>\n    </ThemeContext.Provider>\n  )\n}\n",
      "type": "registry:component",
      "target": "@components/klar/ThemeScope.tsx"
    },
    {
      "path": "registry/klar/lib/cn.ts",
      "content": "import { clsx, type ClassValue } from 'clsx'\nimport { twMerge } from 'tailwind-merge'\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs))\n}\n",
      "type": "registry:component",
      "target": "@components/klar/lib/cn.ts"
    },
    {
      "path": "registry/klar/lib/theme.tsx",
      "content": "import { createContext, useContext } from 'react'\n\nexport type Theme = 'light' | 'dark'\n\nexport const ThemeContext = createContext<{ theme: Theme; toggle: () => void }>({\n  theme: 'light',\n  toggle: () => {},\n})\n\nexport const useTheme = () => useContext(ThemeContext)\n",
      "type": "registry:component",
      "target": "@components/klar/lib/theme.tsx"
    },
    {
      "path": "registry/klar/lib/use-measure.ts",
      "content": "import { useLayoutEffect, useRef, useState } from 'react'\n\n/** Tracks an element's content box so SVG charts can draw in real pixels. */\nexport function useMeasure<T extends HTMLElement = HTMLDivElement>() {\n  const ref = useRef<T>(null)\n  const [size, setSize] = useState({ width: 0, height: 0 })\n\n  useLayoutEffect(() => {\n    const el = ref.current\n    if (!el) return\n    const update = () => {\n      const r = el.getBoundingClientRect()\n      // offsetWidth ignores ancestor transforms (scaled previews), rect does not.\n      const width = el.offsetWidth || r.width\n      const height = el.offsetHeight || r.height\n      setSize((s) => (s.width === width && s.height === height ? s : { width, height }))\n    }\n    update()\n    const ro = new ResizeObserver(update)\n    ro.observe(el)\n    return () => ro.disconnect()\n  }, [])\n\n  return [ref, size] as const\n}\n",
      "type": "registry:component",
      "target": "@components/klar/lib/use-measure.ts"
    },
    {
      "path": "registry/klar/lib/format.ts",
      "content": "export const fmt = {\n  /** 12,480 */\n  int: (n: number) => Math.round(n).toLocaleString('en-US'),\n  /** $24,580 or $24,580.50 */\n  usd: (n: number, digits = 0) =>\n    n.toLocaleString('en-US', {\n      style: 'currency',\n      currency: 'USD',\n      minimumFractionDigits: digits,\n      maximumFractionDigits: digits,\n    }),\n  /** $96K, $1.2M */\n  usdShort: (n: number) => '$' + fmt.short(n),\n  /** 96K, 1.2M */\n  short: (n: number) => {\n    const a = Math.abs(n)\n    if (a >= 1e9) return trim(n / 1e9) + 'B'\n    if (a >= 1e6) return trim(n / 1e6) + 'M'\n    if (a >= 1e3) return trim(n / 1e3) + 'K'\n    return String(Math.round(n))\n  },\n  pct: (n: number, digits = 0) => n.toFixed(digits) + '%',\n}\n\nfunction trim(n: number) {\n  return n >= 100 ? Math.round(n).toString() : n.toFixed(1).replace(/\\.0$/, '')\n}\n\nexport type Fmt = typeof fmt\n\nconst fmtCache = new Map<string, Fmt>()\n\n/**\n * `fmt` bound to a locale and currency, with the same five methods.\n * `fmtFor()` / `fmtFor('en-US', 'USD')` returns `fmt` itself, so output is unchanged;\n * other locales use Intl throughout, e.g. `fmtFor('de-DE', 'EUR').usd(24580.5, 2)` → \"24.580,50 €\",\n * `.short(1_200_000)` → \"1,2 Mio.\", `.pct(12.5, 1)` → \"12,5 %\".\n * (`usd` / `usdShort` keep their names but format in `currency`.)\n */\nexport function fmtFor(locale = 'en-US', currency = 'USD'): Fmt {\n  if (locale === 'en-US' && currency === 'USD') return fmt\n  const key = `${locale}|${currency}`\n  const hit = fmtCache.get(key)\n  if (hit) return hit\n  const compact = new Intl.NumberFormat(locale, { notation: 'compact', maximumFractionDigits: 1 })\n  const curCompact = new Intl.NumberFormat(locale, { style: 'currency', currency, notation: 'compact', maximumFractionDigits: 1 })\n  const f: Fmt = {\n    int: (n) => Math.round(n).toLocaleString(locale),\n    usd: (n, digits = 0) =>\n      n.toLocaleString(locale, { style: 'currency', currency, minimumFractionDigits: digits, maximumFractionDigits: digits }),\n    usdShort: (n) => curCompact.format(n),\n    short: (n) => (Math.abs(n) >= 1e3 ? compact.format(n) : Math.round(n).toLocaleString(locale)),\n    pct: (n, digits = 0) =>\n      (n / 100).toLocaleString(locale, { style: 'percent', minimumFractionDigits: digits, maximumFractionDigits: digits }),\n  }\n  fmtCache.set(key, f)\n  return f\n}\n\n/** Deterministic pseudo-random series so previews never jitter between renders. */\nexport function seeded(seed: number) {\n  let s = seed % 2147483647\n  if (s <= 0) s += 2147483646\n  return () => (s = (s * 16807) % 2147483647) / 2147483647\n}\n\nexport function series(\n  length: number,\n  { seed = 1, base = 50, spread = 20, trend = 0, smooth = 0.6 } = {},\n) {\n  const rnd = seeded(seed)\n  const out: number[] = []\n  let prev = base\n  for (let i = 0; i < length; i++) {\n    const target = base + trend * i + (rnd() - 0.5) * 2 * spread\n    prev = prev * smooth + target * (1 - smooth)\n    out.push(Math.max(0, prev))\n  }\n  return out\n}\n",
      "type": "registry:component",
      "target": "@components/klar/lib/format.ts"
    }
  ],
  "css": {
    "@import \"klar-charts/theme.css\"": {}
  },
  "docs": "Wrap charts in <ThemeScope theme=\"light\" | \"dark\" accent=\"#3366ff\">. Tokens are scoped to that element, so they never leak into the rest of your app.",
  "categories": [
    "charts",
    "theme"
  ]
}
