{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "live-ticker-table",
  "title": "Live Ticker Table",
  "description": "A live market watchlist: prices tick every second and flash green or red when they move, change and percent update, intraday sparklines grow, and rows glide to their new places when the sort order changes.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/live-ticker-table/live-ticker-table.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type TableTheme = \"dark\" | \"light\";\n\nexport interface TickerInstrument {\n  symbol: string;\n  name: string;\n  /** Price at the open; change is measured from it. */\n  open: number;\n  /** Starting price; defaults to the open. */\n  price?: number;\n  volume?: number;\n}\n\nexport interface TickerUpdate {\n  symbol: string;\n  price: number;\n  /** Volume traded with this update. */\n  volume?: number;\n}\n\nexport interface LiveTickerTableProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: TableTheme;\n  title?: string;\n  instruments?: TickerInstrument[];\n  /** Milliseconds between updates. */\n  interval?: number;\n  /** Supplies price updates; defaults to a simulated random walk. Return only the symbols that changed. */\n  next?: (prices: Record<string, number>) => TickerUpdate[];\n  currency?: string;\n}\n\ntype SortKey = \"symbol\" | \"price\" | \"change\" | \"volume\";\n\nconst PALETTES = {\n  dark: {\n    card: \"#0E0E0E\",\n    border: \"rgba(255,255,255,0.08)\",\n    text: \"#F4F5F7\",\n    sub: \"rgba(255,255,255,0.55)\",\n    faint: \"rgba(255,255,255,0.38)\",\n    head: \"rgba(255,255,255,0.03)\",\n    chip: \"rgba(255,255,255,0.06)\",\n    hover: \"rgba(255,255,255,0.025)\",\n    up: \"#2EE6A8\",\n    down: \"#FF6B78\",\n    upFlash: \"rgba(46,230,168,0.22)\",\n    downFlash: \"rgba(255,107,120,0.22)\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(0,0,0,0.08)\",\n    text: \"#111317\",\n    sub: \"rgba(17,19,23,0.55)\",\n    faint: \"rgba(17,19,23,0.4)\",\n    head: \"rgba(17,19,23,0.025)\",\n    chip: \"rgba(17,19,23,0.05)\",\n    hover: \"rgba(17,19,23,0.02)\",\n    up: \"#0E9F6E\",\n    down: \"#E5484D\",\n    upFlash: \"rgba(14,159,110,0.16)\",\n    downFlash: \"rgba(229,72,77,0.14)\",\n  },\n};\n\nconst DEFAULT_INSTRUMENTS: TickerInstrument[] = [\n  {\n    symbol: \"NRTH\",\n    name: \"Northwind Labs\",\n    open: 164.2,\n    price: 169.47,\n    volume: 3_420_000,\n  },\n  {\n    symbol: \"BRGT\",\n    name: \"Brightline Health\",\n    open: 58.9,\n    price: 57.31,\n    volume: 1_860_000,\n  },\n  {\n    symbol: \"KNAT\",\n    name: \"Kanat Air\",\n    open: 23.45,\n    price: 24.12,\n    volume: 5_210_000,\n  },\n  {\n    symbol: \"GRPH\",\n    name: \"Graphline\",\n    open: 312.6,\n    price: 318.04,\n    volume: 940_000,\n  },\n  {\n    symbol: \"SORA\",\n    name: \"Sora Robotics\",\n    open: 87.1,\n    price: 84.66,\n    volume: 2_730_000,\n  },\n  {\n    symbol: \"LUMN\",\n    name: \"Lumen Grid\",\n    open: 41.8,\n    price: 42.95,\n    volume: 1_310_000,\n  },\n  {\n    symbol: \"HLCN\",\n    name: \"Halcyon Bank\",\n    open: 129.4,\n    price: 128.02,\n    volume: 2_050_000,\n  },\n  {\n    symbol: \"MESA\",\n    name: \"Mesa Mobility\",\n    open: 12.6,\n    price: 13.18,\n    volume: 7_890_000,\n  },\n];\n\nconst compact = new Intl.NumberFormat(\"en-US\", {\n  notation: \"compact\",\n  maximumFractionDigits: 1,\n});\n\n/** A believable intraday path from the open to the current price, seeded by the symbol so server and client agree. */\nfunction seedPath(symbol: string, open: number, price: number, n = 28) {\n  let s =\n    [...symbol].reduce((a, c) => a * 31 + c.charCodeAt(0), 7) % 2147483647 || 1;\n  const rnd = () => (s = (s * 16807) % 2147483647) / 2147483647;\n  return Array.from({ length: n }, (_, i) => {\n    const t = i / (n - 1);\n    const wobble =\n      i === 0 || i === n - 1\n        ? 0\n        : (rnd() - 0.5) * Math.abs(price - open) * 0.9 +\n          Math.sin(t * 6 + s) * open * 0.002;\n    return +(open + (price - open) * t + wobble).toFixed(2);\n  });\n}\n\nfunction simulate(prices: Record<string, number>): TickerUpdate[] {\n  const symbols = Object.keys(prices);\n  const n = 1 + Math.floor(Math.random() * 3);\n  const out: TickerUpdate[] = [];\n  for (let i = 0; i < n; i++) {\n    const s = symbols[Math.floor(Math.random() * symbols.length)];\n    const p = prices[s];\n    out.push({\n      symbol: s,\n      price: Math.max(\n        0.5,\n        +(p * (1 + (Math.random() - 0.495) * 0.006)).toFixed(2),\n      ),\n      volume: Math.round(500 + Math.random() * 20000),\n    });\n  }\n  return out;\n}\n\nfunction Spark({ values, color }: { values: number[]; color: string }) {\n  const w = 84;\n  const h = 26;\n  if (values.length < 2) return <svg width={w} height={h} aria-hidden=\"true\" />;\n  const lo = Math.min(...values);\n  const hi = Math.max(...values);\n  const d = values\n    .map(\n      (v, i) =>\n        `${i ? \"L\" : \"M\"}${((i / (values.length - 1)) * (w - 2) + 1).toFixed(1)},${(2 + (1 - (v - lo) / Math.max(1e-9, hi - lo)) * (h - 4)).toFixed(1)}`,\n    )\n    .join(\"\");\n  return (\n    <svg width={w} height={h} aria-hidden=\"true\" className=\"block\">\n      <path\n        d={d}\n        fill=\"none\"\n        stroke={color}\n        strokeWidth={1.4}\n        strokeLinejoin=\"round\"\n        strokeLinecap=\"round\"\n      />\n    </svg>\n  );\n}\n\nexport function LiveTickerTable({\n  theme = \"dark\",\n  title = \"Watchlist\",\n  instruments = DEFAULT_INSTRUMENTS,\n  interval = 900,\n  next,\n  currency = \"$\",\n  className,\n  style,\n  ...props\n}: LiveTickerTableProps) {\n  const pal = PALETTES[theme];\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const [prices, setPrices] = React.useState<Record<string, number>>(() =>\n    Object.fromEntries(instruments.map((i) => [i.symbol, i.price ?? i.open])),\n  );\n  const [volumes, setVolumes] = React.useState<Record<string, number>>(() =>\n    Object.fromEntries(instruments.map((i) => [i.symbol, i.volume ?? 0])),\n  );\n  // Recent prices per symbol for the mini charts, seeded flat so the first render matches the server.\n  const [history, setHistory] = React.useState<Record<string, number[]>>(() =>\n    Object.fromEntries(\n      instruments.map((i) => [\n        i.symbol,\n        seedPath(i.symbol, i.open, i.price ?? i.open),\n      ]),\n    ),\n  );\n  const [flash, setFlash] = React.useState<\n    Record<string, { dir: 1 | -1; at: number }>\n  >({});\n  const [paused, setPaused] = React.useState(false);\n  const [visible, setVisible] = React.useState(true);\n  const [sort, setSort] = React.useState<{ key: SortKey; dir: 1 | -1 }>({\n    key: \"change\",\n    dir: -1,\n  });\n  const [lastTick, setLastTick] = React.useState<number | null>(null);\n\n  React.useEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const io = new IntersectionObserver(([e]) => setVisible(e.isIntersecting));\n    io.observe(el);\n    const onVis = () => setVisible(!document.hidden);\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const running = !paused && visible;\n  const pricesRef = React.useRef(prices);\n  React.useEffect(() => {\n    pricesRef.current = prices;\n  }, [prices]);\n  React.useEffect(() => {\n    if (!running) return;\n    const t = window.setInterval(() => {\n      const updates = (next ?? simulate)(pricesRef.current);\n      if (!updates.length) return;\n      const now = Date.now();\n      setPrices((p) => {\n        const out = { ...p };\n        updates.forEach((u) => (out[u.symbol] = u.price));\n        return out;\n      });\n      setVolumes((v) => {\n        const out = { ...v };\n        updates.forEach(\n          (u) => (out[u.symbol] = (out[u.symbol] ?? 0) + (u.volume ?? 0)),\n        );\n        return out;\n      });\n      setHistory((h) => {\n        const out = { ...h };\n        updates.forEach(\n          (u) =>\n            (out[u.symbol] = [...(out[u.symbol] ?? []), u.price].slice(-40)),\n        );\n        return out;\n      });\n      setFlash((f) => {\n        const out = { ...f };\n        updates.forEach((u) => {\n          const prev = pricesRef.current[u.symbol];\n          if (u.price !== prev)\n            out[u.symbol] = { dir: u.price > prev ? 1 : -1, at: now };\n        });\n        return out;\n      });\n      setLastTick(now);\n    }, interval);\n    return () => window.clearInterval(t);\n  }, [running, interval, next]);\n\n  const rows = instruments.map((i) => {\n    const price = prices[i.symbol];\n    const change = price - i.open;\n    return {\n      ...i,\n      price,\n      change,\n      pct: change / i.open,\n      vol: volumes[i.symbol] ?? 0,\n    };\n  });\n  const sorted = [...rows].sort((a, b) => {\n    const v = (r: (typeof rows)[number]) =>\n      sort.key === \"symbol\"\n        ? r.symbol\n        : sort.key === \"price\"\n          ? r.price\n          : sort.key === \"change\"\n            ? r.pct\n            : r.vol;\n    const va = v(a);\n    const vb = v(b);\n    return (\n      (typeof va === \"string\"\n        ? va.localeCompare(String(vb))\n        : (va as number) - (vb as number)) * sort.dir\n    );\n  });\n\n  // FLIP: when the order changes, slide each row from where it was to where it is now.\n  const rowRefs = React.useRef(new Map<string, HTMLDivElement>());\n  const tops = React.useRef(new Map<string, number>());\n  const order = sorted.map((r) => r.symbol).join(\",\");\n  React.useLayoutEffect(() => {\n    const reduce = window.matchMedia(\n      \"(prefers-reduced-motion: reduce)\",\n    ).matches;\n    rowRefs.current.forEach((el, sym) => {\n      const top = el.offsetTop;\n      const was = tops.current.get(sym);\n      tops.current.set(sym, top);\n      if (was === undefined || was === top || reduce) return;\n      el.style.transition = \"none\";\n      el.style.transform = `translateY(${was - top}px)`;\n      requestAnimationFrame(() => {\n        el.style.transition = \"transform 420ms cubic-bezier(0.22, 1, 0.36, 1)\";\n        el.style.transform = \"\";\n      });\n    });\n  }, [order]);\n\n  const gainers = rows.filter((r) => r.change > 0).length;\n  const header = (\n    key: SortKey,\n    label: string,\n    align: \"left\" | \"right\" = \"right\",\n  ) => (\n    <div\n      role=\"columnheader\"\n      aria-sort={\n        sort.key === key\n          ? sort.dir === 1\n            ? \"ascending\"\n            : \"descending\"\n          : \"none\"\n      }\n      className={align === \"right\" ? \"text-right\" : \"text-left\"}\n    >\n      <button\n        type=\"button\"\n        onClick={() =>\n          setSort((s) =>\n            s.key === key\n              ? { key, dir: s.dir === 1 ? -1 : 1 }\n              : { key, dir: key === \"symbol\" ? 1 : -1 },\n          )\n        }\n        className=\"cursor-pointer text-[11px] font-medium tracking-[0.04em] uppercase\"\n        style={{ color: sort.key === key ? pal.text : pal.faint }}\n      >\n        {label} {sort.key === key ? (sort.dir === 1 ? \"↑\" : \"↓\") : \"\"}\n      </button>\n    </div>\n  );\n  const cols =\n    \"grid-cols-[minmax(0,1.5fr)_minmax(0,1fr)_minmax(0,1fr)] sm:grid-cols-[minmax(0,1.6fr)_1fr_1fr_96px_90px]\";\n\n  return (\n    <div\n      ref={rootRef}\n      className={cn(\"w-full overflow-hidden rounded-[20px]\", className)}\n      style={{\n        background: pal.card,\n        border: `1px solid ${pal.border}`,\n        color: pal.text,\n        fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\",\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-center justify-between gap-3 px-5 pt-5 pb-4 sm:px-6\">\n        <div className=\"flex items-center gap-2.5\">\n          <p className=\"text-[15px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          <span\n            className=\"flex items-center gap-1.5 rounded-full px-2 py-0.5 text-[10.5px] font-semibold tracking-[0.06em] uppercase\"\n            style={{ background: pal.chip, color: paused ? pal.sub : pal.up }}\n          >\n            <span className=\"relative flex size-1.5\">\n              {!paused && (\n                <span\n                  className=\"absolute inset-0 animate-ping rounded-full opacity-70 motion-reduce:hidden\"\n                  style={{ background: pal.up }}\n                />\n              )}\n              <span\n                className=\"relative size-1.5 rounded-full\"\n                style={{ background: paused ? pal.sub : pal.up }}\n              />\n            </span>\n            {paused ? \"Paused\" : \"Live\"}\n          </span>\n        </div>\n        <div className=\"flex items-center gap-3\">\n          <span\n            className=\"text-[11.5px] tabular-nums\"\n            style={{ color: pal.sub }}\n          >\n            {gainers} up · {rows.length - gainers} down\n          </span>\n          <button\n            type=\"button\"\n            onClick={() => setPaused((p) => !p)}\n            aria-pressed={paused}\n            className=\"cursor-pointer rounded-full px-3 py-1.5 text-[12px] font-medium\"\n            style={{ background: pal.chip }}\n          >\n            {paused ? \"Resume\" : \"Pause\"}\n          </button>\n        </div>\n      </div>\n\n      <div role=\"table\" aria-label={title} aria-describedby={undefined}>\n        <div\n          role=\"row\"\n          className={cn(\n            \"grid items-center gap-3 border-y px-5 py-2.5 sm:px-6\",\n            cols,\n          )}\n          style={{ borderColor: pal.border, background: pal.head }}\n        >\n          {header(\"symbol\", \"Symbol\", \"left\")}\n          {header(\"price\", \"Price\")}\n          {header(\"change\", \"Change\")}\n          <div\n            role=\"columnheader\"\n            className=\"hidden text-left text-[11px] font-medium tracking-[0.04em] uppercase sm:block\"\n            style={{ color: pal.faint }}\n          >\n            Intraday\n          </div>\n          <div className=\"hidden sm:block\">{header(\"volume\", \"Volume\")}</div>\n        </div>\n        <div className=\"relative\">\n          {sorted.map((r) => {\n            const f = flash[r.symbol];\n            const fresh = f && lastTick !== null && f.at === lastTick;\n            const color = r.change >= 0 ? pal.up : pal.down;\n            return (\n              <div\n                key={r.symbol}\n                ref={(el) => {\n                  if (el) rowRefs.current.set(r.symbol, el);\n                  else rowRefs.current.delete(r.symbol);\n                }}\n                role=\"row\"\n                className={cn(\n                  \"relative grid items-center gap-3 border-b px-5 py-2.5 text-[13px] last:border-b-0 sm:px-6\",\n                  cols,\n                )}\n                style={{ borderColor: pal.border, background: pal.card }}\n              >\n                <div role=\"cell\" className=\"flex min-w-0 items-center gap-2.5\">\n                  <span\n                    className=\"grid h-7 w-11 shrink-0 place-items-center rounded-md font-mono text-[10.5px] font-semibold\"\n                    style={{ background: pal.chip }}\n                  >\n                    {r.symbol}\n                  </span>\n                  <span className=\"truncate\" style={{ color: pal.sub }}>\n                    {r.name}\n                  </span>\n                </div>\n                <div role=\"cell\" className=\"text-right\">\n                  {/* The price cell flashes green or red for a moment when it moves. */}\n                  <span\n                    key={fresh ? f.at : \"still\"}\n                    className=\"inline-block rounded-md px-1.5 py-0.5 font-semibold tabular-nums\"\n                    style={{\n                      animation: fresh\n                        ? `${f.dir > 0 ? \"lt-up\" : \"lt-down\"} 900ms ease-out`\n                        : undefined,\n                    }}\n                  >\n                    {currency}\n                    {r.price.toFixed(2)}\n                  </span>\n                </div>\n                <div\n                  role=\"cell\"\n                  className=\"text-right tabular-nums\"\n                  style={{ color }}\n                >\n                  <span className=\"block text-[12.5px] font-medium\">\n                    {r.change >= 0 ? \"+\" : \"−\"}\n                    {Math.abs(r.change).toFixed(2)}\n                  </span>\n                  <span className=\"block text-[11px]\">\n                    {r.pct >= 0 ? \"▲\" : \"▼\"} {Math.abs(r.pct * 100).toFixed(2)}%\n                  </span>\n                </div>\n                <div role=\"cell\" className=\"hidden sm:block\">\n                  <Spark values={history[r.symbol] ?? []} color={color} />\n                </div>\n                <div\n                  role=\"cell\"\n                  className=\"hidden text-right tabular-nums sm:block\"\n                  style={{ color: pal.sub }}\n                >\n                  {compact.format(r.vol)}\n                </div>\n              </div>\n            );\n          })}\n        </div>\n      </div>\n      <style>{`@keyframes lt-up { from { background: ${pal.upFlash}; color: ${pal.up}; } to { background: transparent; } } @keyframes lt-down { from { background: ${pal.downFlash}; color: ${pal.down}; } to { background: transparent; } } @media (prefers-reduced-motion: reduce) { [style*=\"lt-up\"], [style*=\"lt-down\"] { animation: none !important; } }`}</style>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}