{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "map-route-stats",
  "type": "registry:component",
  "title": "Map Route Stats",
  "description": "An activity card with a route outline, four stats and an elevation and pace profile. Hovering or scrubbing the profile moves a dot along the route in sync.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "maps",
    "map",
    "route",
    "activity",
    "elevation",
    "pace",
    "stats"
  ],
  "docs": "https://hyperblur.dev/effects/map-route-stats",
  "files": [
    {
      "path": "registry/hyperblur/map-route-stats.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/map-route-stats.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MapRouteStatsProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  title?: string;\n  date?: string;\n  distanceKm?: number;\n  movingTimeSec?: number;\n  elevationGain?: number;\n  seed?: number;\n  accent?: string;\n  paceColor?: string;\n  onScrub?: (fraction: number, km: number) => void;\n}\n\nconst N = 160;\nconst RM_QUERY = \"(prefers-reduced-motion: reduce)\";\n\nfunction mix32(n: number) {\n  let x = n | 0;\n  x = Math.imul(x ^ (x >>> 16), 0x7feb352d);\n  x = Math.imul(x ^ (x >>> 15), 0x846ca68b);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction rnd(seed: number, a: number) {\n  return mix32(seed * 374761393 + a * 668265263 + 1013904223);\n}\n\nfunction clock(sec: number) {\n  const s = Math.max(0, Math.round(sec));\n  const h = Math.floor(s / 3600);\n  const m = Math.floor((s % 3600) / 60);\n  const r = String(s % 60).padStart(2, \"0\");\n  return h ? `${h}:${String(m).padStart(2, \"0\")}:${r}` : `${m}:${r}`;\n}\n\nfunction buildData(seed: number, gain: number, avgPace: number) {\n  const p1 = rnd(seed, 1) * 6;\n  const p2 = rnd(seed, 2) * 6;\n  const a1 = 0.18 + rnd(seed, 3) * 0.12;\n  const a2 = 0.06 + rnd(seed, 4) * 0.08;\n  const raw: [number, number][] = [];\n  for (let i = 0; i < N; i++) {\n    const t = (i / (N - 1)) * Math.PI * 2;\n    const r = 1 + a1 * Math.sin(3 * t + p1) + a2 * Math.sin(5 * t + p2);\n    raw.push([Math.cos(t) * r * 1.25, Math.sin(t) * r]);\n  }\n  let minX = Infinity;\n  let maxX = -Infinity;\n  let minY = Infinity;\n  let maxY = -Infinity;\n  for (const [x, y] of raw) {\n    minX = Math.min(minX, x);\n    maxX = Math.max(maxX, x);\n    minY = Math.min(minY, y);\n    maxY = Math.max(maxY, y);\n  }\n  const sc = 80 / Math.max(maxX - minX, maxY - minY);\n  const route = raw.map(([x, y]) => [50 + (x - (minX + maxX) / 2) * sc, 50 + (y - (minY + maxY) / 2) * sc] as [number, number]);\n  const q1 = rnd(seed, 5) * 6;\n  const q2 = rnd(seed, 6) * 6;\n  const elev: number[] = [];\n  for (let i = 0; i < N; i++) {\n    const f = i / (N - 1);\n    elev.push(Math.sin(f * Math.PI * 2 + q1) * 0.55 + Math.sin(f * Math.PI * 5 + q2) * 0.3 + Math.sin(f * Math.PI * 11 + q1) * 0.12);\n  }\n  const lo = Math.min(...elev);\n  const hi = Math.max(...elev);\n  const span = Math.max(20, gain * 0.42);\n  const base = 12 + Math.round(rnd(seed, 7) * 20);\n  const meters = elev.map((e) => base + ((e - lo) / (hi - lo || 1)) * span);\n  const pace = meters.map((m, i) => {\n    const prev = meters[Math.max(0, i - 3)];\n    const grade = (m - prev) / 3;\n    return avgPace + grade * 9 + Math.sin(i * 0.37 + q2) * 6;\n  });\n  return { route, meters, pace, lo: base, hi: base + span };\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(RM_QUERY);\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\nconst css = `\n.hb-ub6 {\n  --hb-ub6-a: #ff4d4d;\n  --hb-ub6-pc: #7fb3ff;\n  box-sizing: border-box;\n  display: grid;\n  gap: 8px;\n  width: 100%;\n  padding: 10px;\n  border-radius: 14px;\n  border: 1px solid #262626;\n  background: #111111;\n  color: #ededed;\n  font-family: inherit;\n  container-type: inline-size;\n}\n.hb-ub6 *, .hb-ub6 *::before, .hb-ub6 *::after { box-sizing: border-box; }\n.hb-ub6-hd { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; min-width: 0; padding: 0 2px; }\n.hb-ub6-tt { font-size: 13px; font-weight: 600; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }\n.hb-ub6-mono { font: 600 8.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: 0.14em; text-transform: uppercase; color: #8f8f8f; white-space: nowrap; }\n.hb-ub6-mid { display: grid; grid-template-columns: minmax(0, 34%) minmax(0, 1fr); gap: 8px; min-width: 0; }\n.hb-ub6-map {\n  position: relative;\n  min-height: 76px;\n  overflow: hidden;\n  border-radius: 10px;\n  border: 1px solid #222;\n  background: #0b0d12;\n}\n.hb-ub6-map svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }\n.hb-ub6-path { fill: none; stroke: var(--hb-ub6-a); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 1; stroke-dashoffset: 1; animation: hb-ub6-draw 1.4s cubic-bezier(0.4, 0, 0.2, 1) 0.1s forwards; }\n.hb-ub6-glow { fill: none; stroke: var(--hb-ub6-a); stroke-width: 6; stroke-opacity: 0.16; stroke-linecap: round; }\n.hb-ub6-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; min-width: 0; }\n.hb-ub6-st {\n  display: flex;\n  flex-direction: column;\n  justify-content: center;\n  gap: 4px;\n  min-width: 0;\n  padding: 6px 8px;\n  border-radius: 9px;\n  background: #161616;\n  border: 1px solid #1f1f1f;\n  opacity: 0;\n  animation: hb-ub6-up 0.5s cubic-bezier(0.2, 0.8, 0.2, 1) forwards;\n}\n.hb-ub6-st b { font-size: 14px; font-weight: 600; line-height: 1.1; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }\n.hb-ub6-st b small { margin-left: 2px; font-size: 10px; font-weight: 500; color: #8f8f8f; }\n.hb-ub6-ch {\n  position: relative;\n  height: 50px;\n  border-radius: 9px;\n  background: linear-gradient(#141414, #111111);\n  border: 1px solid #1f1f1f;\n  overflow: hidden;\n  cursor: ew-resize;\n  touch-action: pan-y;\n  outline: none;\n}\n.hb-ub6-ch:focus-visible { box-shadow: 0 0 0 2px var(--hb-ub6-a); }\n.hb-ub6-ch svg { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ub6-area { transform-box: fill-box; transform-origin: 50% 100%; animation: hb-ub6-grow 0.9s cubic-bezier(0.2, 0.8, 0.2, 1) 0.2s both; }\n.hb-ub6-cur { position: absolute; top: 0; bottom: 0; left: 0; width: 1px; background: rgba(255, 255, 255, 0.5); pointer-events: none; }\n.hb-ub6-dot {\n  position: absolute;\n  left: 0;\n  top: 0;\n  width: 9px;\n  height: 9px;\n  margin: -4.5px 0 0 -4.5px;\n  border-radius: 50%;\n  background: #fff;\n  border: 2px solid var(--hb-ub6-a);\n  box-shadow: 0 0 10px var(--hb-ub6-a);\n  pointer-events: none;\n}\n.hb-ub6-rd {\n  position: absolute;\n  top: 4px;\n  left: 5px;\n  display: flex;\n  gap: 8px;\n  padding: 3px 6px;\n  border-radius: 6px;\n  background: rgba(10, 10, 12, 0.78);\n  pointer-events: none;\n  color: #a1a1a1;\n}\n.hb-ub6-rd em { font-style: normal; color: #ededed; }\n.hb-ub6-rd .hb-ub6-rp, .hb-ub6-rd .hb-ub6-rp em { font-style: normal; color: var(--hb-ub6-pc); }\n@container (min-width: 520px) {\n  .hb-ub6 { padding: 14px; gap: 12px; }\n  .hb-ub6-tt { font-size: 15px; }\n  .hb-ub6-mid { grid-template-columns: minmax(0, 40%) minmax(0, 1fr); gap: 12px; }\n  .hb-ub6-map { min-height: 150px; }\n  .hb-ub6-stats { gap: 10px; }\n  .hb-ub6-st { padding: 12px 14px; gap: 6px; }\n  .hb-ub6-st b { font-size: 22px; }\n  .hb-ub6-ch { height: 96px; }\n}\n@container (max-width: 290px) {\n  .hb-ub6-mid { grid-template-columns: minmax(0, 1fr); }\n  .hb-ub6-map { min-height: 96px; }\n}\n@keyframes hb-ub6-draw { to { stroke-dashoffset: 0; } }\n@keyframes hb-ub6-up { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }\n@keyframes hb-ub6-grow { from { opacity: 0; transform: scaleY(0.2); } to { opacity: 1; transform: none; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-ub6-path { animation: none; stroke-dashoffset: 0; }\n  .hb-ub6-st { animation: none; opacity: 1; }\n  .hb-ub6-area { animation: none; }\n}\n`;\n\nexport function MapRouteStats({\n  title = \"Riverside loop\",\n  date = \"Sat, Nov 1 · 07:12\",\n  distanceKm = 10.42,\n  movingTimeSec = 3138,\n  elevationGain = 186,\n  seed = 5,\n  accent = \"#ff4d4d\",\n  paceColor = \"#7fb3ff\",\n  onScrub,\n  className = \"\",\n  style,\n  ...rest\n}: MapRouteStatsProps) {\n  const avgPace = movingTimeSec / Math.max(0.1, distanceKm);\n  const data = React.useMemo(() => buildData(seed, elevationGain, avgPace), [seed, elevationGain, avgPace]);\n  const reduced = React.useSyncExternalStore(subscribeMotion, () => window.matchMedia(RM_QUERY).matches, () => false);\n  const uid = React.useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const chartRef = React.useRef<HTMLDivElement>(null);\n  const curRef = React.useRef<HTMLDivElement>(null);\n  const dotRef = React.useRef<HTMLDivElement>(null);\n  const mapDot = React.useRef<SVGCircleElement>(null);\n  const mapHalo = React.useRef<SVGCircleElement>(null);\n  const kmRef = React.useRef<HTMLElement>(null);\n  const elRef = React.useRef<HTMLElement>(null);\n  const paceRef = React.useRef<HTMLElement>(null);\n  const cb = React.useRef(onScrub);\n\n  React.useEffect(() => {\n    cb.current = onScrub;\n  }, [onScrub]);\n\n  const ys = React.useMemo(() => data.meters.map((m) => 54 - ((m - data.lo) / (data.hi - data.lo || 1)) * 42), [data]);\n  const paceY = React.useMemo(() => {\n    const lo = Math.min(...data.pace);\n    const hi = Math.max(...data.pace);\n    return data.pace.map((p) => 12 + ((p - lo) / (hi - lo || 1)) * 40);\n  }, [data]);\n  const line = ys.map((y, i) => `${i ? \"L\" : \"M\"}${((i / (N - 1)) * 300).toFixed(2)} ${y.toFixed(2)}`).join(\" \");\n  const area = `${line} L300 60 L0 60 Z`;\n  const paceLine = paceY.map((y, i) => `${i ? \"L\" : \"M\"}${((i / (N - 1)) * 300).toFixed(2)} ${y.toFixed(2)}`).join(\" \");\n  const routeD = `${data.route.map(([x, y], i) => `${i ? \"L\" : \"M\"}${x.toFixed(2)} ${y.toFixed(2)}`).join(\" \")} Z`;\n\n  React.useEffect(() => {\n    const chart = chartRef.current;\n    if (!chart) return;\n    let f = reduced ? 0.62 : 0;\n    let raf = 0;\n    let last = 0;\n    let active = false;\n    let dragging = false;\n    let idleAt = 0;\n    let visible = true;\n\n    const apply = (v: number, notify: boolean) => {\n      f = Math.min(1, Math.max(0, v));\n      const x = f * (N - 1);\n      const i = Math.min(N - 2, Math.floor(x));\n      const t = x - i;\n      const lerp = (a: number, b: number) => a + (b - a) * t;\n      const rx = lerp(data.route[i][0], data.route[i + 1][0]);\n      const ry = lerp(data.route[i][1], data.route[i + 1][1]);\n      const y = lerp(ys[i], ys[i + 1]);\n      const m = lerp(data.meters[i], data.meters[i + 1]);\n      const p = lerp(data.pace[i], data.pace[i + 1]);\n      const km = f * distanceKm;\n      mapDot.current?.setAttribute(\"cx\", rx.toFixed(2));\n      mapDot.current?.setAttribute(\"cy\", ry.toFixed(2));\n      mapHalo.current?.setAttribute(\"cx\", rx.toFixed(2));\n      mapHalo.current?.setAttribute(\"cy\", ry.toFixed(2));\n      if (curRef.current) curRef.current.style.left = `${(f * 100).toFixed(2)}%`;\n      if (dotRef.current) {\n        dotRef.current.style.left = `${(f * 100).toFixed(2)}%`;\n        dotRef.current.style.top = `${((y / 60) * 100).toFixed(2)}%`;\n      }\n      if (kmRef.current) kmRef.current.textContent = km.toFixed(1);\n      if (elRef.current) elRef.current.textContent = String(Math.round(m));\n      if (paceRef.current) paceRef.current.textContent = clock(p);\n      chart.setAttribute(\"aria-valuenow\", String(Math.round(f * 100)));\n      chart.setAttribute(\"aria-valuetext\", `${km.toFixed(1)} km, ${Math.round(m)} m elevation, pace ${clock(p)} per km`);\n      if (notify) cb.current?.(f, km);\n    };\n\n    const frame = (t: number) => {\n      raf = 0;\n      const dt = last ? Math.min(0.05, (t - last) / 1000) : 0;\n      last = t;\n      if (!active && !dragging && t - idleAt > 1400) apply((f + dt / 16) % 1, false);\n      if (visible && !reduced) raf = requestAnimationFrame(frame);\n      else last = 0;\n    };\n    const kick = () => {\n      if (!raf && visible && !reduced) raf = requestAnimationFrame(frame);\n    };\n\n    const fromEvent = (e: PointerEvent) => {\n      const r = chart.getBoundingClientRect();\n      return (e.clientX - r.left) / Math.max(1, r.width);\n    };\n    const enter = () => {\n      active = true;\n    };\n    const move = (e: PointerEvent) => {\n      active = true;\n      apply(fromEvent(e), true);\n    };\n    const down = (e: PointerEvent) => {\n      dragging = true;\n      chart.setPointerCapture(e.pointerId);\n      apply(fromEvent(e), true);\n    };\n    const up = (e: PointerEvent) => {\n      dragging = false;\n      if (chart.hasPointerCapture(e.pointerId)) chart.releasePointerCapture(e.pointerId);\n      idleAt = performance.now();\n      if (e.pointerType !== \"mouse\") active = false;\n    };\n    const leave = () => {\n      if (dragging) return;\n      active = false;\n      idleAt = performance.now();\n    };\n    const key = (e: KeyboardEvent) => {\n      const step = e.shiftKey ? 0.1 : 0.01;\n      let next = -1;\n      if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") next = f + step;\n      else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") next = f - step;\n      else if (e.key === \"Home\") next = 0;\n      else if (e.key === \"End\") next = 1;\n      if (next < -0.5) return;\n      e.preventDefault();\n      active = true;\n      apply(next, true);\n    };\n    const blur = () => {\n      active = false;\n      idleAt = performance.now();\n    };\n\n    chart.addEventListener(\"pointerenter\", enter);\n    chart.addEventListener(\"pointermove\", move);\n    chart.addEventListener(\"pointerdown\", down);\n    chart.addEventListener(\"pointerup\", up);\n    chart.addEventListener(\"pointercancel\", up);\n    chart.addEventListener(\"pointerleave\", leave);\n    chart.addEventListener(\"keydown\", key);\n    chart.addEventListener(\"blur\", blur);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      kick();\n    });\n    io.observe(chart);\n    apply(f, false);\n    kick();\n    return () => {\n      chart.removeEventListener(\"pointerenter\", enter);\n      chart.removeEventListener(\"pointermove\", move);\n      chart.removeEventListener(\"pointerdown\", down);\n      chart.removeEventListener(\"pointerup\", up);\n      chart.removeEventListener(\"pointercancel\", up);\n      chart.removeEventListener(\"pointerleave\", leave);\n      chart.removeEventListener(\"keydown\", key);\n      chart.removeEventListener(\"blur\", blur);\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, [data, ys, distanceKm, reduced]);\n\n  const gradId = `hb-ub6-g${uid}`;\n  const stats: { k: string; v: string; u: string }[] = [\n    { k: \"Distance\", v: distanceKm.toFixed(2), u: \"km\" },\n    { k: \"Time\", v: clock(movingTimeSec), u: \"\" },\n    { k: \"Avg pace\", v: clock(avgPace), u: \"/km\" },\n    { k: \"Elevation\", v: `+${Math.round(elevationGain)}`, u: \"m\" },\n  ];\n\n  return (\n    <div className={`hb-ub6 ${className}`.trim()} style={{ \"--hb-ub6-a\": accent, \"--hb-ub6-pc\": paceColor, ...style } as React.CSSProperties} {...rest}>\n      <style href=\"hb-map-route-stats\" precedence=\"default\">\n        {css}\n      </style>\n      <div className=\"hb-ub6-hd\">\n        <span className=\"hb-ub6-tt\">{title}</span>\n        <span className=\"hb-ub6-mono\">{date}</span>\n      </div>\n      <div className=\"hb-ub6-mid\">\n        <div className=\"hb-ub6-map\">\n          <svg viewBox=\"0 0 100 100\" preserveAspectRatio=\"xMidYMid meet\" aria-hidden=\"true\">\n            <g stroke=\"#1a1f29\" strokeWidth=\"0.8\">\n              {[14, 31, 47, 66, 83].map((v) => (\n                <path key={`v${v}`} d={`M${v} -20 V120`} />\n              ))}\n              {[18, 36, 58, 77].map((v) => (\n                <path key={`h${v}`} d={`M-20 ${v} H120`} />\n              ))}\n            </g>\n            <path d=\"M-20 88 C 20 80, 50 96, 120 82\" fill=\"none\" stroke=\"#11284a\" strokeWidth=\"7\" />\n            <path className=\"hb-ub6-glow\" d={routeD} />\n            <path className=\"hb-ub6-path\" d={routeD} pathLength={1} />\n            <circle cx={data.route[0][0].toFixed(2)} cy={data.route[0][1].toFixed(2)} r=\"2.6\" fill=\"#0b0d12\" stroke=\"#ededed\" strokeWidth=\"1.2\" />\n            <circle ref={mapHalo} cx={data.route[0][0].toFixed(2)} cy={data.route[0][1].toFixed(2)} r=\"6\" fill=\"var(--hb-ub6-a)\" fillOpacity=\"0.25\" />\n            <circle ref={mapDot} cx={data.route[0][0].toFixed(2)} cy={data.route[0][1].toFixed(2)} r=\"3\" fill=\"#fff\" stroke=\"var(--hb-ub6-a)\" strokeWidth=\"1.6\" />\n          </svg>\n        </div>\n        <div className=\"hb-ub6-stats\">\n          {stats.map((s, i) => (\n            <div key={s.k} className=\"hb-ub6-st\" style={{ animationDelay: `${(0.15 + i * 0.07).toFixed(2)}s` }}>\n              <span className=\"hb-ub6-mono\">{s.k}</span>\n              <b>\n                {s.v}\n                {s.u ? <small>{s.u}</small> : null}\n              </b>\n            </div>\n          ))}\n        </div>\n      </div>\n      <div\n        ref={chartRef}\n        className=\"hb-ub6-ch\"\n        role=\"slider\"\n        tabIndex={0}\n        aria-label=\"Position along the route\"\n        aria-valuemin={0}\n        aria-valuemax={100}\n        aria-valuenow={0}\n      >\n        <svg viewBox=\"0 0 300 60\" preserveAspectRatio=\"none\" aria-hidden=\"true\">\n          <defs>\n            <linearGradient id={gradId} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n              <stop offset=\"0\" stopColor=\"var(--hb-ub6-a)\" stopOpacity=\"0.38\" />\n              <stop offset=\"1\" stopColor=\"var(--hb-ub6-a)\" stopOpacity=\"0\" />\n            </linearGradient>\n          </defs>\n          <g className=\"hb-ub6-area\">\n            <path d={area} fill={`url(#${gradId})`} />\n            <path d={line} fill=\"none\" stroke=\"var(--hb-ub6-a)\" strokeWidth=\"1.5\" vectorEffect=\"non-scaling-stroke\" />\n          </g>\n          <path d={paceLine} fill=\"none\" stroke=\"var(--hb-ub6-pc)\" strokeWidth=\"1\" strokeOpacity=\"0.75\" strokeDasharray=\"3 3\" vectorEffect=\"non-scaling-stroke\" />\n        </svg>\n        <div ref={curRef} className=\"hb-ub6-cur\" />\n        <div ref={dotRef} className=\"hb-ub6-dot\" />\n        <div className=\"hb-ub6-rd hb-ub6-mono\">\n          <span>\n            KM <em ref={kmRef}>0.0</em>\n          </span>\n          <span>\n            <em ref={elRef}>0</em> M\n          </span>\n          <span className=\"hb-ub6-rp\">\n            <em ref={paceRef}>0:00</em>/KM\n          </span>\n        </div>\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}