{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "barcode-bar-chart",
  "title": "Barcode Bar Chart",
  "description": "A week of hours as a barcode: one thin line per hour, brighter when busier. A lens opens under the pointer, and the lines can rise into a regular bar chart.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/barcode-bar-chart/barcode-bar-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BarcodeSlot {\n  /** Read out on hover, e.g. \"Tue 14:00\". */\n  label: string;\n  value: number;\n}\n\nexport type BarcodeMode = \"barcode\" | \"bars\";\n\nexport interface BarcodeBarChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  data?: BarcodeSlot[];\n  /** Slots in each group, used for the labels underneath. */\n  groupSize?: number;\n  /** One label per group. */\n  groups?: string[];\n  defaultMode?: BarcodeMode;\n  /** How much wider the bars get under the pointer. */\n  lens?: number;\n  /** Colour of the busiest slots. */\n  color?: string;\n  /** Height of the plot, in px. */\n  height?: number;\n  /** Words after the value in the readout. */\n  unit?: [singular: string, plural: string];\n  formatValue?: (value: number) => string;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    surface: \"#0F1011\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    tip: \"#161718\",\n    tipShadow: \"0 4px 24px rgba(0,0,0,0.4)\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    accent: \"#5E6AD2\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    surface: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    tip: \"#FFFFFF\",\n    tipShadow: \"0 3px 12px rgba(8,9,10,0.09)\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    accent: \"#5E6AD2\",\n  },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst CARD_FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/** The chart is drawn at its real width, so its text stays the same size on a phone. */\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(700);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(Math.max(240, Math.round(entry.contentRect.width))),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ref]);\n  return width;\n}\n\nfunction Stat({\n  value,\n  label,\n  pal,\n}: {\n  value: string;\n  label: string;\n  pal: Palette;\n}) {\n  return (\n    <div className=\"text-left sm:text-right\">\n      <p className=\"m-0 text-[18px] leading-[1.2] font-[510] tracking-[-0.018em] tabular-nums\">\n        {value}\n      </p>\n      <p\n        className=\"m-0 mt-0.5 text-[12px] tracking-[-0.011em]\"\n        style={{ color: pal.sub }}\n      >\n        {label}\n      </p>\n    </div>\n  );\n}\n\nfunction Pills({\n  label,\n  options,\n  value,\n  onChange,\n  pal,\n}: {\n  label: string;\n  options: string[];\n  value: number;\n  onChange: (index: number) => void;\n  pal: Palette;\n}) {\n  return (\n    <div\n      className=\"flex rounded-full p-0.5\"\n      style={{ background: pal.chip }}\n      role=\"radiogroup\"\n      aria-label={label}\n    >\n      {options.map((option, i) => (\n        <button\n          key={option}\n          type=\"button\"\n          role=\"radio\"\n          aria-checked={i === value}\n          onClick={() => onChange(i)}\n          className=\"h-7 cursor-pointer rounded-full px-3 text-[13px] font-[510] tracking-[-0.011em] transition-colors outline-none focus-visible:ring-2\"\n          style={{\n            background: i === value ? pal.chipOn : \"transparent\",\n            color: i === value ? pal.text : pal.sub,\n          }}\n        >\n          {option}\n        </button>\n      ))}\n    </div>\n  );\n}\n\nfunction Tip({\n  x,\n  y,\n  width,\n  pal,\n  children,\n}: {\n  x: number;\n  y: number;\n  width: number;\n  pal: Palette;\n  children: React.ReactNode;\n}) {\n  return (\n    <div\n      aria-hidden=\"true\"\n      className=\"pointer-events-none absolute z-10 -translate-x-1/2 rounded-lg px-2.5 py-1.5 text-[12px] tracking-[-0.011em] whitespace-nowrap\"\n      style={{\n        left: Math.max(110, Math.min(width - 110, x)),\n        top: Math.max(0, y - 42),\n        background: pal.tip,\n        color: pal.sub,\n        boxShadow: `inset 0 0 0 1px ${pal.border}, ${pal.tipShadow}`,\n      }}\n    >\n      {children}\n    </div>\n  );\n}\n\nconst WEEK = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"];\nconst DAY_WEIGHT = [0.92, 1, 0.97, 0.88, 0.64, 0.16, 0.1];\n\n/** A week of hours: busy working days, quiet nights, with integer noise so server and client agree. */\nfunction makeWeek(): BarcodeSlot[] {\n  let state = 41;\n  return WEEK.flatMap((day, d) =>\n    Array.from({ length: 24 }, (_, hour) => {\n      state = (state * 9301 + 49297) % 233280;\n      const noise = state / 233280;\n      const away = Math.abs(hour - 14) / 7;\n      const shape = Math.max(0.02, 1 - away * away);\n      return {\n        label: `${day} ${String(hour).padStart(2, \"0\")}:00`,\n        value: Math.round(46 * DAY_WEIGHT[d] * shape * (0.55 + noise * 0.9)),\n      };\n    }),\n  );\n}\n\nconst DEFAULT_DATA = makeWeek();\nconst FORMAT = (n: number) => Math.round(n).toLocaleString(\"en-US\");\n\nexport function BarcodeBarChart({\n  theme = \"dark\",\n  title = \"Deploys by hour\",\n  subtitle = \"One line per hour. Point at the week to open it up.\",\n  data = DEFAULT_DATA,\n  groupSize = 24,\n  groups = WEEK,\n  defaultMode = \"barcode\",\n  lens = 3,\n  color,\n  height = 200,\n  unit = [\"deploy\", \"deploys\"],\n  formatValue = FORMAT,\n  className,\n  style,\n  ...props\n}: BarcodeBarChartProps) {\n  const pal = PALETTES[theme];\n  const accent = color ?? pal.accent;\n  const reduce = useReducedMotion();\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(boxRef);\n  const barRefs = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const centre = React.useRef<number | null>(null);\n  const start = React.useRef<() => void>(() => {});\n  const [mode, setMode] = React.useState(defaultMode === \"bars\" ? 1 : 0);\n  const [active, setActive] = React.useState<number | null>(null);\n\n  // On a phone two hours share a line, so the lines stay wide enough to tell apart.\n  const size = width < 520 ? 2 : 1;\n  const slots = React.useMemo(() => {\n    if (size === 1) return data;\n    const merged: BarcodeSlot[] = [];\n    for (let i = 0; i < data.length; i += size) {\n      const part = data.slice(i, i + size);\n      merged.push({\n        label: part[0].label,\n        value: part.reduce((n, s) => n + s.value, 0),\n      });\n    }\n    return merged;\n  }, [data, size]);\n\n  const count = slots.length;\n  const max = Math.max(1, ...slots.map((s) => s.value));\n  const total = data.reduce((n, s) => n + s.value, 0);\n  const busiest = data.reduce(\n    (best, s) => (s.value > best.value ? s : best),\n    data[0] ?? { label: \"None\", value: 0 },\n  );\n  const sums = groups.map((_, g) =>\n    data\n      .slice(g * groupSize, (g + 1) * groupSize)\n      .reduce((n, s) => n + s.value, 0),\n  );\n  const topGroup = sums.indexOf(Math.max(0, ...sums));\n  const slot = active === null ? null : slots[active];\n  const asBars = mode === 1;\n\n  // Bars ease toward their width from one loop that writes flex-grow directly and stops once it has settled.\n  React.useEffect(() => {\n    const grow = new Float32Array(count).fill(1);\n    let raf = 0;\n    const frame = () => {\n      let moving = false;\n      const c = centre.current;\n      for (let i = 0; i < count; i++) {\n        const d = c === null ? 9 : (i - c) / (count / 26);\n        const goal = 1 + (reduce ? 0 : lens) * Math.exp(-d * d);\n        const next = grow[i] + (goal - grow[i]) * 0.22;\n        const settled = Math.abs(goal - next) < 0.004;\n        grow[i] = settled ? goal : next;\n        if (!settled) moving = true;\n        const el = barRefs.current[i];\n        if (el) el.style.flexGrow = grow[i].toFixed(3);\n      }\n      raf = moving ? requestAnimationFrame(frame) : 0;\n    };\n    start.current = () => {\n      if (!raf) raf = requestAnimationFrame(frame);\n    };\n    return () => {\n      cancelAnimationFrame(raf);\n      raf = 0;\n    };\n  }, [count, lens, reduce]);\n\n  const aim = (i: number | null) => {\n    centre.current = i;\n    setActive(i);\n    start.current();\n  };\n\n  return (\n    <section\n      className={`w-full rounded-2xl p-5 sm:p-6 ${className ?? \"\"}`}\n      style={{\n        background: pal.card,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily: CARD_FONT,\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-x-8 gap-y-3\">\n        <div>\n          <h3 className=\"m-0 text-[17px] leading-[1.33] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-0.5 text-[13px] tracking-[-0.011em]\"\n            style={{ color: pal.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"flex gap-7\">\n          <Stat value={formatValue(total)} label=\"This week\" pal={pal} />\n          <Stat value={busiest.label} label=\"Busiest hour\" pal={pal} />\n          <Stat value={groups[topGroup] ?? \"—\"} label=\"Busiest day\" pal={pal} />\n        </div>\n      </div>\n\n      {!count ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No data available.\n        </p>\n      ) : (\n        <div\n          ref={boxRef}\n          className=\"relative mt-5 rounded-lg outline-none focus-visible:ring-2\"\n          tabIndex={0}\n          role=\"group\"\n          aria-label={`${title}. Use left and right arrow keys to read each slot.`}\n          onPointerMove={(e) => {\n            const r = e.currentTarget.getBoundingClientRect();\n            const i = Math.floor(((e.clientX - r.left) / r.width) * count);\n            aim(Math.max(0, Math.min(count - 1, i)));\n          }}\n          onPointerLeave={() => aim(null)}\n          onBlur={() => aim(null)}\n          onKeyDown={(e) => {\n            if (e.key !== \"ArrowLeft\" && e.key !== \"ArrowRight\") return;\n            e.preventDefault();\n            const dir = e.key === \"ArrowRight\" ? 1 : -1;\n            aim(\n              Math.max(\n                0,\n                Math.min(count - 1, (active ?? Math.floor(count / 2)) + dir),\n              ),\n            );\n          }}\n        >\n          <div\n            role=\"img\"\n            aria-label={`${formatValue(total)} ${unit[1]}. Busiest: ${busiest.label}, ${formatValue(busiest.value)}.`}\n            className=\"flex items-end gap-px\"\n            style={{ height }}\n          >\n            {slots.map((s, i) => {\n              const t = s.value / max;\n              return (\n                <span\n                  key={s.label}\n                  ref={(el) => {\n                    barRefs.current[i] = el;\n                  }}\n                  className=\"block min-w-0 motion-safe:transition-[height,opacity] motion-safe:duration-500\"\n                  style={{\n                    flex: \"1 1 0\",\n                    height: asBars ? `${Math.max(2, t * 100)}%` : \"100%\",\n                    background: active === i ? pal.soft : accent,\n                    opacity:\n                      active === i ? 1 : asBars ? 0.85 : 0.1 + t * t * 0.9,\n                    transitionDelay: `${Math.round((i / count) * 220)}ms`,\n                  }}\n                />\n              );\n            })}\n          </div>\n\n          <div\n            aria-hidden=\"true\"\n            className=\"mt-2 flex text-[11px]\"\n            style={{ color: pal.faint }}\n          >\n            {groups.map((g) => (\n              <span key={g} className=\"min-w-0 flex-1\">\n                {g}\n              </span>\n            ))}\n          </div>\n\n          {slot && active !== null && (\n            <Tip\n              x={((active + 0.5) / count) * width}\n              y={40}\n              width={width}\n              pal={pal}\n            >\n              <span className=\"font-[510]\" style={{ color: pal.text }}>\n                {formatValue(slot.value)} {unit[slot.value === 1 ? 0 : 1]}\n              </span>{\" \"}\n              on {slot.label}\n            </Tip>\n          )}\n        </div>\n      )}\n\n      <div className=\"mt-3 flex flex-wrap items-center justify-between gap-2\">\n        <Pills\n          label=\"Display\"\n          options={[\"Barcode\", \"Bars\"]}\n          value={mode}\n          onChange={setMode}\n          pal={pal}\n        />\n        <div\n          className=\"flex items-center gap-[3px] text-[12px]\"\n          style={{ color: pal.sub }}\n        >\n          <span className=\"mr-1\">Quiet</span>\n          {[0.14, 0.3, 0.5, 0.75, 1].map((o) => (\n            <span\n              key={o}\n              className=\"h-3 w-[3px]\"\n              style={{ background: accent, opacity: o }}\n            />\n          ))}\n          <span className=\"ml-1\">Busy</span>\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}