{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"seat-map","type":"registry:block","title":"Seat map","description":"Pick seats while other people are picking too: yours rise with your initials, and a seat taken under you turns over with a ripple and points to the next best.","author":"Hairline UI","dependencies":[],"registryDependencies":["https://hairlineui.com/r/number-roll.json?as=dependency","https://hairlineui.com/r/text-morph.json?as=dependency"],"files":[{"path":"registry/seat-map.tsx","type":"registry:component","content":"// Hairline UI · seat-map · MIT License\n// Copyright (c) 2026 Hairline UI · https://hairlineui.com/license#components\n\n\"use client\";\n\nimport * as React from \"react\";\nimport { NumberRoll } from \"./number-roll\";\nimport { TextMorph } from \"./text-morph\";\n\n/* ─────────────────────────────────────────────────────────\n * SEAT MAP: pick seats while other people are picking too\n *\n *   pick      tap a seat and it rises a little as your initials\n *             drop into it; the seat list and the total roll\n *   live      when someone else takes a seat while you look, it\n *             turns over to taken and a ripple runs out from it\n *             through the seats around, so you notice where it went\n *   lost      if it was one of yours, it shakes, lets go of your\n *             initials, and the nearest free seats in the same tier\n *             pulse once as the next best\n *   limit     past the most seats you can hold, the count shakes\n *\n * Seats and their status are yours: pass rows of seats (null for an\n * aisle) and tiers with prices; the map tells you what's picked and\n * calls onContinue. Arrow keys move between seats.\n * ───────────────────────────────────────────────────────── */\n\nexport interface Seat {\n  id: string;\n  /** The seat's number in its row. */\n  label: string;\n  status: \"free\" | \"taken\";\n  /** A tier id; seats without one use the first tier. */\n  tier?: string;\n}\n\nexport interface SeatRow {\n  /** The row's letter. */\n  id: string;\n  /** Seats left to right; null leaves an aisle. */\n  seats: (Seat | null)[];\n}\n\nexport interface SeatTier {\n  id: string;\n  name: string;\n  price: number;\n  /** Any CSS colour. Defaults to your chart colours in turn. */\n  color?: string;\n}\n\nexport interface SeatMapProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"title\"> {\n  rows: SeatRow[];\n  tiers: SeatTier[];\n  selected?: string[];\n  defaultSelected?: string[];\n  onSelectedChange?: (ids: string[]) => void;\n  /** The most seats one person can hold. */\n  max?: number;\n  /** Shown in your seats. */\n  initials?: string;\n  /** What the seats face, e.g. \"Screen\" or \"Stage\". */\n  stage?: string;\n  title?: string;\n  subtitle?: string;\n  currency?: string;\n  locale?: string;\n  onContinue?: (ids: string[]) => void;\n}\n\nconst EASE = \"cubic-bezier(0.16,1,0.3,1)\";\nconst FOCUS = \"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/60 focus-visible:ring-offset-2 focus-visible:ring-offset-background\";\nconst NONE: string[] = [];\nconst SHAKE: Keyframe[] = [{ translate: \"0 0\" }, { translate: \"-3px 0\" }, { translate: \"3px 0\" }, { translate: \"-1.5px 0\" }, { translate: \"0 0\" }];\n\nconst reducedQuery = \"(prefers-reduced-motion: reduce)\";\nconst subscribeReduced = (onChange: () => void) => {\n  const query = window.matchMedia(reducedQuery);\n  query.addEventListener(\"change\", onChange);\n  return () => query.removeEventListener(\"change\", onChange);\n};\nconst useReducedMotion = () =>\n  React.useSyncExternalStore(subscribeReduced, () => window.matchMedia(reducedQuery).matches, () => false);\nconst subscribeNothing = () => () => {};\nconst useHydrated = () => React.useSyncExternalStore(subscribeNothing, () => true, () => false);\n\nconst mix = (color: string, amount: number, into = \"transparent\") => `color-mix(in oklab, ${color} ${amount}%, ${into})`;\n\nexport function SeatMap({\n  rows,\n  tiers,\n  selected: selectedProp,\n  defaultSelected = NONE,\n  onSelectedChange,\n  max = 6,\n  initials = \"You\",\n  stage = \"Screen\",\n  title,\n  subtitle,\n  currency = \"USD\",\n  locale,\n  onContinue,\n  className = \"\",\n  ...props\n}: SeatMapProps) {\n  const reduced = useReducedMotion();\n  const hydrated = useHydrated();\n  const loc = locale ?? (hydrated ? undefined : \"en-US\");\n  const [own, setOwn] = React.useState(defaultSelected);\n  const selected = selectedProp ?? own;\n  const [width, setWidth] = React.useState(0);\n  const [active, setActive] = React.useState<string | null>(null);\n  const [said, setSaid] = React.useState(\"\");\n\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const countRef = React.useRef<HTMLParagraphElement>(null);\n  const seatRefs = React.useRef(new Map<string, HTMLButtonElement>());\n  const before = React.useRef<Map<string, Seat[\"status\"]> | null>(null);\n\n  const tierOf = React.useMemo(() => {\n    const map = new Map(tiers.map((t, i) => [t.id, { ...t, color: t.color ?? `var(--chart-${(i % 5) + 1}, var(--primary))` }]));\n    return (seat: Seat) => map.get(seat.tier ?? tiers[0]?.id) ?? { id: \"\", name: \"\", price: 0, color: \"var(--primary)\" };\n  }, [tiers]);\n  const all = React.useMemo(() => {\n    const out = new Map<string, { seat: Seat; row: string; r: number; c: number }>();\n    rows.forEach((row, r) => row.seats.forEach((s, c) => s && out.set(s.id, { seat: s, row: row.id, r, c })));\n    return out;\n  }, [rows]);\n\n  const set = (ids: string[]) => {\n    if (selectedProp === undefined) setOwn(ids);\n    onSelectedChange?.(ids);\n  };\n\n  React.useLayoutEffect(() => {\n    const box = boxRef.current;\n    if (!box) return;\n    const measure = () => setWidth((w) => (w === box.offsetWidth ? w : box.offsetWidth));\n    measure();\n    const observer = new ResizeObserver(measure);\n    observer.observe(box);\n    return () => observer.disconnect();\n  }, []);\n\n  const cols = Math.max(...rows.map((r) => r.seats.length));\n  // Narrow maps keep the row letters on one side only and pack the seats a little closer.\n  const narrow = (width || 360) < 340;\n  const label = 16;\n  const gap = narrow ? 3 : 4;\n  const size = Math.max(13, Math.min(28, Math.floor(((width || 360) - label * (narrow ? 1 : 2) - 8 - gap * (cols - 1)) / cols)));\n  const live = React.useRef({ selected, set: (ids: string[]) => void ids });\n  React.useLayoutEffect(() => {\n    live.current.selected = selected;\n    live.current.set = (ids: string[]) => {\n      if (selectedProp === undefined) setOwn(ids);\n      onSelectedChange?.(ids);\n    };\n  });\n\n  // Seats other people took while you were looking: turn them over and ripple out; if it was yours, let it go.\n  React.useLayoutEffect(() => {\n    const selected = live.current.selected;\n    const ripple = (from: { r: number; c: number }, delay = 0) => {\n      if (reduced) return;\n      all.forEach((p, id) => {\n        const d = Math.hypot(p.r - from.r, p.c - from.c);\n        if (d === 0 || d > 3.6) return;\n        seatRefs.current.get(id)?.animate([{ scale: \"1\", filter: \"brightness(1)\" }, { scale: \"1.08\", filter: \"brightness(1.18)\" }, { scale: \"1\", filter: \"brightness(1)\" }], {\n          duration: 420,\n          delay: delay + d * 55,\n          easing: \"ease-in-out\",\n        });\n      });\n    };\n    const now = new Map([...all].map(([id, p]) => [id, p.seat.status]));\n    const prev = before.current;\n    before.current = now;\n    if (!prev) return;\n    const lost: string[] = [];\n    now.forEach((status, id) => {\n      if (prev.get(id) !== \"free\" || status !== \"taken\") return;\n      const p = all.get(id)!;\n      const el = seatRefs.current.get(id);\n      if (!reduced) el?.animate([{ transform: \"rotateX(0)\" }, { transform: \"rotateX(90deg)\", offset: 0.5 }, { transform: \"rotateX(0)\" }], { duration: 460, easing: \"ease-in-out\" });\n      ripple(p, 120);\n      if (selected.includes(id)) lost.push(id);\n    });\n    if (!lost.length) return;\n    lost.forEach((id) => {\n      const p = all.get(id)!;\n      if (!reduced) seatRefs.current.get(id)?.animate(SHAKE, { duration: 380, easing: \"ease-out\" });\n      // The next best: the nearest free seats in the same tier.\n      const tier = tierOf(p.seat).id;\n      [...all.values()]\n        .filter((q) => q.seat.status === \"free\" && tierOf(q.seat).id === tier && !selected.includes(q.seat.id))\n        .sort((a, b) => Math.hypot(a.r - p.r, a.c - p.c) - Math.hypot(b.r - p.r, b.c - p.c))\n        .slice(0, 2)\n        .forEach((q, i) => {\n          if (reduced) return;\n          seatRefs.current.get(q.seat.id)?.animate(\n            [{ boxShadow: `0 0 0 0px ${mix(tierOf(q.seat).color, 60)}` }, { boxShadow: `0 0 0 4px ${mix(tierOf(q.seat).color, 0)}` }],\n            { duration: 900, delay: 420 + i * 120, easing: EASE, iterations: 2 },\n          );\n        });\n    });\n    const next = selected.filter((id) => !lost.includes(id));\n    // Hand the change back on the next frame, outside this layout pass.\n    requestAnimationFrame(() => live.current.set(next));\n    setSaid(`${lost.map((id) => `${all.get(id)!.row}${all.get(id)!.seat.label}`).join(\", \")} was just taken. Pick another seat.`);\n  }, [all, reduced, tierOf]);\n\n  const toggle = (seat: Seat) => {\n    if (seat.status === \"taken\") return;\n    const p = all.get(seat.id)!;\n    if (selected.includes(seat.id)) {\n      set(selected.filter((id) => id !== seat.id));\n      setSaid(`Row ${p.row} seat ${seat.label} removed.`);\n      return;\n    }\n    if (selected.length >= max) {\n      if (!reduced) countRef.current?.animate(SHAKE, { duration: 380, easing: \"ease-out\" });\n      setSaid(`You can hold up to ${max} seats.`);\n      return;\n    }\n    set([...selected, seat.id]);\n    setSaid(`Row ${p.row} seat ${seat.label} added, ${fmt(tierOf(seat).price)}.`);\n  };\n\n  // Arrow keys move between seats; a roving tab stop keeps the map one stop in the tab order.\n  const first = [...all.values()].find((p) => p.seat.status === \"free\")?.seat.id ?? [...all.keys()][0];\n  const stop = active && all.has(active) ? active : first;\n  const key = (e: React.KeyboardEvent, id: string) => {\n    const p = all.get(id);\n    if (!p) return;\n    const dir = { ArrowLeft: [0, -1], ArrowRight: [0, 1], ArrowUp: [-1, 0], ArrowDown: [1, 0] }[e.key];\n    if (!dir) return;\n    e.preventDefault();\n    let best: string | null = null;\n    let bestD = Infinity;\n    all.forEach((q, qid) => {\n      const dr = q.r - p.r;\n      const dc = q.c - p.c;\n      if ((dir[0] && Math.sign(dr) !== dir[0]) || (dir[1] && Math.sign(dc) !== dir[1])) return;\n      const d = dir[0] ? Math.abs(dr) * 10 + Math.abs(dc) : Math.abs(dc) * 10 + Math.abs(dr);\n      if (d < bestD) {\n        bestD = d;\n        best = qid;\n      }\n    });\n    if (best) {\n      setActive(best);\n      seatRefs.current.get(best)?.focus();\n    }\n  };\n\n  const fmt = (v: number) => new Intl.NumberFormat(loc, { style: \"currency\", currency, maximumFractionDigits: v % 1 ? 2 : 0 }).format(v);\n  const picked = selected.map((id) => all.get(id)).filter(Boolean) as { seat: Seat; row: string }[];\n  const total = picked.reduce((s, p) => s + tierOf(p.seat).price, 0);\n  const list = picked.length ? picked.map((p) => `${p.row}${p.seat.label}`).join(\", \") : \"No seats yet\";\n  const onSeat = (c: string) => `oklch(from ${c} clamp(0.2, (0.7 - l) * 100, 0.99) 0 h)`;\n\n  return (\n    <div className={`w-full rounded-[22px] bg-background shadow-[0_0_0_1px_var(--border)] ${className}`} {...props}>\n      <div aria-hidden=\"true\" data-natural-width className=\"grid h-0\" style={{ gridTemplateColumns: \"minmax(0, 460px)\" }} />\n      <div className=\"p-4 sm:p-5\">\n        {(title || subtitle) && (\n          <div className=\"mb-3\">\n            {title && <p className=\"text-[15px] font-medium tracking-[-0.01em] text-foreground\">{title}</p>}\n            {subtitle && <p className=\"mt-0.5 text-[12px] text-muted-foreground\">{subtitle}</p>}\n          </div>\n        )}\n        <div className=\"flex flex-wrap items-center gap-x-4 gap-y-1 text-[11.5px] text-muted-foreground\">\n          {tiers.map((t, i) => {\n            const color = t.color ?? `var(--chart-${(i % 5) + 1}, var(--primary))`;\n            return (\n              <span key={t.id} className=\"inline-flex items-center gap-1.5\">\n                <span className=\"size-2.5 rounded-[3px]\" style={{ background: mix(color, 16), boxShadow: `inset 0 0 0 1px ${mix(color, 60)}` }} />\n                {t.name} <span className=\"tabular-nums text-foreground\">{fmt(t.price)}</span>\n              </span>\n            );\n          })}\n          <span className=\"inline-flex items-center gap-1.5\">\n            <span className=\"size-2.5 rounded-[3px]\" style={{ background: mix(\"var(--foreground)\", 12) }} />\n            Taken\n          </span>\n        </div>\n\n        {/* What the seats face. */}\n        <div aria-hidden=\"true\" className=\"mt-4 flex flex-col items-center\">\n          <svg viewBox=\"0 0 200 16\" className=\"h-4 w-[72%]\" preserveAspectRatio=\"none\">\n            <path d=\"M2 14 Q100 -6 198 14\" fill=\"none\" stroke=\"color-mix(in oklab, var(--foreground) 35%, transparent)\" strokeWidth=\"1.5\" vectorEffect=\"non-scaling-stroke\" strokeLinecap=\"round\" />\n          </svg>\n          <span className=\"mt-0.5 text-[10.5px] uppercase tracking-[0.12em] text-muted-foreground\">{stage}</span>\n        </div>\n\n        <div ref={boxRef} role=\"grid\" aria-label=\"Seats\" className=\"mt-3 flex w-full flex-col items-center\" style={{ gap, perspective: 600 }}>\n          {width > 0 &&\n            rows.map((row) => (\n              <div key={row.id} role=\"row\" className=\"flex items-center\" style={{ gap }}>\n                <span aria-hidden=\"true\" className=\"text-center text-[10.5px] tabular-nums text-muted-foreground\" style={{ width: label }}>\n                  {row.id}\n                </span>\n                {row.seats.map((seat, c) =>\n                  seat ? (\n                    <SeatButton\n                      key={seat.id}\n                      seat={seat}\n                      row={row.id}\n                      size={size}\n                      color={tierOf(seat).color}\n                      price={fmt(tierOf(seat).price)}\n                      mine={selected.includes(seat.id)}\n                      initials={initials}\n                      ink={onSeat(tierOf(seat).color)}\n                      tabStop={seat.id === stop}\n                      reduced={reduced}\n                      onToggle={toggle}\n                      onKey={key}\n                      onFocusSeat={setActive}\n                      register={(el) => {\n                        if (el) seatRefs.current.set(seat.id, el);\n                        else seatRefs.current.delete(seat.id);\n                      }}\n                    />\n                  ) : (\n                    <span key={`aisle-${c}`} aria-hidden=\"true\" style={{ width: size * 0.6 }} />\n                  ),\n                )}\n                {!narrow && (\n                  <span aria-hidden=\"true\" className=\"text-center text-[10.5px] tabular-nums text-muted-foreground\" style={{ width: label }}>\n                    {row.id}\n                  </span>\n                )}\n              </div>\n            ))}\n        </div>\n      </div>\n\n      <div className=\"flex items-center justify-between gap-3 border-t border-border px-4 py-3 sm:px-5\">\n        <div className=\"min-w-0\">\n          <p ref={countRef} className=\"text-[12px] text-muted-foreground tabular-nums\">\n            <TextMorph>{picked.length ? `${picked.length} of ${max} seats` : `Up to ${max} seats`}</TextMorph>\n          </p>\n          <p className=\"truncate text-[13px] font-medium text-foreground\">\n            <TextMorph>{list}</TextMorph>\n          </p>\n        </div>\n        <div className=\"flex shrink-0 items-center gap-3\">\n          <p className=\"text-[17px] font-medium tracking-[-0.02em] text-foreground tabular-nums\">\n            <NumberRoll value={total} format={{ style: \"currency\", currency, maximumFractionDigits: 0 }} locales={loc} duration={700} />\n          </p>\n          <button\n            type=\"button\"\n            disabled={!picked.length}\n            onClick={() => onContinue?.(selected)}\n            className={`h-9 cursor-pointer rounded-full bg-primary px-4 text-[13px] font-medium text-primary-foreground transition-[opacity,scale] duration-200 active:scale-[0.97] disabled:cursor-default disabled:opacity-40 disabled:active:scale-100 ${FOCUS}`}\n          >\n            Continue\n          </button>\n        </div>\n      </div>\n      <p className=\"sr-only\" aria-live=\"polite\">\n        {said}\n      </p>\n    </div>\n  );\n}\n\ninterface SeatButtonProps {\n  seat: Seat;\n  row: string;\n  size: number;\n  color: string;\n  price: string;\n  mine: boolean;\n  initials: string;\n  ink: string;\n  tabStop: boolean;\n  reduced: boolean;\n  onToggle: (seat: Seat) => void;\n  onKey: (e: React.KeyboardEvent, id: string) => void;\n  onFocusSeat: (id: string) => void;\n  register: (el: HTMLButtonElement | null) => void;\n}\n\nconst SeatButton = React.memo(function SeatButton({ seat, row, size, color, price, mine, initials, ink, tabStop, reduced, onToggle, onKey, onFocusSeat, register }: SeatButtonProps) {\n  const taken = seat.status === \"taken\";\n  const letters = initials.slice(0, 2).toUpperCase();\n  return (\n    <button\n      ref={register}\n      type=\"button\"\n      role=\"gridcell\"\n      aria-selected={mine}\n      aria-disabled={taken}\n      aria-label={`Row ${row}, seat ${seat.label}, ${taken ? \"taken\" : mine ? \"yours\" : price}`}\n      tabIndex={tabStop ? 0 : -1}\n      onClick={() => onToggle(seat)}\n      onKeyDown={(e) => onKey(e, seat.id)}\n      onFocus={() => onFocusSeat(seat.id)}\n      className={`relative flex shrink-0 items-center justify-center overflow-hidden rounded-b-[4px] rounded-t-[8px] transition-[background-color,box-shadow,translate] duration-300 ${taken ? \"cursor-default\" : \"cursor-pointer\"} ${FOCUS}`}\n      style={{\n        width: size,\n        height: size,\n        background: taken ? mix(\"var(--foreground)\", 12) : mine ? color : mix(color, 14),\n        boxShadow: taken ? \"none\" : mine ? `inset 0 -2px 0 ${mix(\"black\", 18)}` : `inset 0 0 0 1px ${mix(color, 55)}`,\n        translate: mine ? \"0 -2px\" : \"0 0\",\n        transitionTimingFunction: EASE,\n      }}\n    >\n      <span\n        aria-hidden=\"true\"\n        className=\"text-[9px] font-semibold leading-none\"\n        style={{\n          color: ink,\n          opacity: mine ? 1 : 0,\n          transform: mine ? \"none\" : \"translateY(-8px)\",\n          transition: reduced ? \"none\" : `transform 380ms cubic-bezier(0.34,1.36,0.64,1) 60ms, opacity 200ms ${EASE} 60ms`,\n          fontSize: Math.max(8, size * 0.36),\n        }}\n      >\n        {size >= 20 ? letters : letters.slice(0, 1)}\n      </span>\n    </button>\n  );\n});\n"}],"css":{},"docs":"Docs and live preview: https://hairlineui.com/ui/seat-map. MIT licensed: https://hairlineui.com/license#components","meta":{"license":"MIT"}}