Specification
- Catalogue
- No. 4 of 110
- Section
- Pickers and booking
- Interaction
- Press
- Element
- Picker
- Preview
- Live component
- Imports
- react 19.3.1, @number-flow/react 0.6.2
- Added
Notes
Choose seats on a plan of the room, see whether they sit together, or pick from a plain list instead.
Groups want to sit side by side, and the plan shows that at a glance. The list view gives the same choice to anyone who cannot use the map.
Taken seats carry a cross, chosen seats a tick: shape as well as colour. Says whether the chosen seats are side by side (the aisle breaks a pair). Seats are 30px targets so ten fit a phone; the list view has full-size targets. Up to four seats per booking; prices by row.
The names, figures and pictures in the demo are sample content; a real build uses the client's own.
Code
React and plain CSS"use client";
import { useState } from "react";
import { radioArrows } from "@/components/pieces/shared";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./seat-map.css";
const ROWS = ["A", "B", "C", "D", "E", "F"];
const SEATS = 10;
const TAKEN = new Set(["A3", "A4", "A5", "A6", "B1", "B2", "B8", "C5", "C6", "C7", "D2", "D3", "D9", "D10", "E4", "E5", "F1", "F6", "F7", "F8"]);
const MAX_SEATS = 4;
const seatPrice = (row: string) => (row <= "C" ? 18 : 14);
function seatsText(ids: string[]): string {
const byRow = new Map<string, number[]>();
for (const id of ids) byRow.set(id.charAt(0), [...(byRow.get(id.charAt(0)) ?? []), Number(id.slice(1))]);
return [...byRow.entries()]
.sort()
.map(([r, ns]) => `Row ${r}, ${ns.sort((a, b) => a - b).join(" and ")}`)
.join("; ");
}
function together(ids: string[]): boolean {
if (ids.length < 2) return true;
const row = ids[0]!.charAt(0);
if (ids.some((id) => id.charAt(0) !== row)) return false;
const ns = ids.map((id) => Number(id.slice(1))).sort((a, b) => a - b);
// The aisle sits between seats 5 and 6.
return ns.every((n, i) => i === 0 || (n === ns[i - 1]! + 1 && !(ns[i - 1] === 5 && n === 6)));
}
function SeatMapDemo() {
const [picked, setPicked] = useState<string[]>([]);
const [view, setView] = useState<"map" | "list">("map");
const [note, setNote] = useState("");
const toggle = (id: string) => {
setPicked((p) => {
if (p.includes(id)) return p.filter((x) => x !== id);
if (p.length >= MAX_SEATS) {
setNote(`Up to ${MAX_SEATS} seats per booking.`);
return p;
}
setNote("");
return [...p, id];
});
};
const total = picked.reduce((n, id) => n + seatPrice(id.charAt(0)), 0);
return (
<div className="dm">
<div className="seats">
<div className="seats-top">
<div className="seats-view" role="radiogroup" aria-label="Choose seats by" onKeyDown={radioArrows}>
{(["map", "list"] as const).map((v) => (
<button key={v} type="button" role="radio" aria-checked={view === v} onClick={() => setView(v)}>
{v === "map" ? "Map" : "List"}
</button>
))}
</div>
<span className="seats-key">
<i data-k="free" /> £18 front, £14 back <i data-k="taken" /> Taken
</span>
</div>
{view === "map" ? (
<div className="seats-map">
<div className="seats-stage" aria-hidden="true">
Stage
</div>
{ROWS.map((r) => (
<div key={r} className="seats-row" role="group" aria-label={`Row ${r}`}>
<span className="seats-rowname" aria-hidden="true">
{r}
</span>
{Array.from({ length: SEATS }, (_, i) => {
const id = `${r}${i + 1}`;
const taken = TAKEN.has(id);
const mine = picked.includes(id);
return (
<button
key={id}
type="button"
className="seat"
data-aisle={i === 5 || undefined}
disabled={taken}
aria-pressed={mine}
aria-label={`Row ${r} seat ${i + 1}, £${seatPrice(r)}${taken ? ", taken" : ""}`}
onClick={() => toggle(id)}
>
{taken ? (
<svg viewBox="0 0 12 12" width="10" height="10" aria-hidden="true">
<path d="M3 3l6 6M9 3l-6 6" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
</svg>
) : mine ? (
<svg viewBox="0 0 12 12" width="11" height="11" aria-hidden="true">
<path d="M2.5 6.5l2.2 2.2L9.5 3.8" stroke="currentColor" strokeWidth="1.8" fill="none" strokeLinecap="round" strokeLinejoin="round" />
</svg>
) : null}
</button>
);
})}
</div>
))}
</div>
) : (
<div className="seats-list">
{ROWS.map((r) => (
<fieldset key={r}>
<legend>
Row {r} · £{seatPrice(r)}
</legend>
<div>
{Array.from({ length: SEATS }, (_, i) => `${r}${i + 1}`)
.filter((id) => !TAKEN.has(id))
.map((id) => (
<label key={id} className="seats-check">
<input type="checkbox" checked={picked.includes(id)} onChange={() => toggle(id)} />
{id.slice(1)}
</label>
))}
</div>
</fieldset>
))}
</div>
)}
<p className="seats-sum" aria-live="polite">
{note ||
(picked.length === 0 ? (
"Pick up to four seats"
) : (
<>
<strong>
{picked.length} {picked.length === 1 ? "seat" : "seats"}, £{total}
</strong>{" "}
{seatsText(picked)}
{picked.length > 1 && (together(picked) ? ", side by side" : ", not together")}
</>
))}
</p>
</div>
</div>
);
}
// What a hovered card shows: the piece used the way a visitor would.
const TOUR: Tour = [
{ click: ".seat:not(:disabled)", nth: 20, wait: 700 },
{ click: ".seat:not(:disabled)", nth: 21, wait: 1600 },
];
export default function SeatMapEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Seat map with a list view" sample tour={TOUR}>
<SeatMapDemo />
</PieceStage>
);
}