Time slot picker
Pick a day and its times cascade in; picking a time splits it into time and Confirm, then a check books it.
It imports two small hooks from @/lib/motion and the colour tokens; the zip includes both.
Demo.tsx
"use client";
import { useEffect, useRef, useState, type CSSProperties, type KeyboardEvent } from "react";
import styles from "./demo.module.css";
const DAYS = [
{ name: "Mon", date: 11, slots: ["09:00", "09:30", "11:00", "13:30", "15:00"] },
{ name: "Tue", date: 12, slots: ["10:00", "10:30", "14:00", "16:30"] },
{ name: "Wed", date: 13, slots: [] },
{ name: "Thu", date: 14, slots: ["09:30", "10:30", "11:00", "13:00", "14:30", "16:00"] },
{ name: "Fri", date: 15, slots: ["09:00", "12:30", "15:30"] },
];
export default function Demo() {
const pills = useRef<(HTMLButtonElement | null)[]>([]);
const confirm = useRef<HTMLButtonElement>(null);
const change = useRef<HTMLButtonElement>(null);
const [day, setDay] = useState(3);
const [picked, setPicked] = useState<string | null>(null);
const [booked, setBooked] = useState(false);
const d = DAYS[day];
// Move focus to the control that just appeared: Confirm after picking, Change after booking.
useEffect(() => { if (picked && !booked) confirm.current?.focus({ preventScroll: true }); }, [picked, booked]);
useEffect(() => { if (booked) change.current?.focus({ preventScroll: true }); }, [booked]);
const pickDay = (i: number) => {
const next = (i + DAYS.length) % DAYS.length;
setDay(next);
setPicked(null);
setBooked(false);
pills.current[next]?.focus();
};
const dayKeys = (e: KeyboardEvent) => {
const moves: Record<string, number> = { ArrowRight: day + 1, ArrowLeft: day - 1, Home: 0, End: DAYS.length - 1 };
if (!(e.key in moves)) return;
e.preventDefault();
pickDay(moves[e.key]);
};
return (
<div className={styles.root}>
<div className={styles.card}>
<div className={styles.head}>
<p className={styles.title}>Intro call, 30 min</p>
<p className={styles.month}>November</p>
</div>
<div className={styles.days} role="radiogroup" aria-label="Day" onKeyDown={dayKeys}>
{DAYS.map((x, i) => (
<button
key={x.date}
ref={(el) => { pills.current[i] = el; }}
type="button"
role="radio"
aria-checked={i === day}
aria-label={`${x.name} ${x.date}${x.slots.length ? "" : ", fully booked"}`}
tabIndex={i === day ? 0 : -1}
className={styles.day}
data-empty={!x.slots.length}
onClick={() => pickDay(i)}
>
<span className={styles.dayName}>{x.name}</span>
<span className={styles.dayDate}>{x.date}</span>
</button>
))}
</div>
{booked && picked ? (
<div className={styles.done}>
<span className={styles.tick} aria-hidden="true">
<svg viewBox="0 0 24 24"><path d="M6 12.5l4 4 8-9" /></svg>
</span>
<p className={styles.doneText}>Booked for {d.name} {d.date}, {picked}</p>
<p className={styles.doneNote}>Invite sent to your calendar</p>
<button ref={change} type="button" className={styles.change} onClick={() => { setBooked(false); setPicked(null); }}>
Change time
</button>
</div>
) : d.slots.length ? (
<ul key={day} className={styles.slots} aria-label={`Times on ${d.name} ${d.date}`}>
{d.slots.map((t, i) => (
<li
key={t}
className={styles.slot}
data-picked={picked === t}
style={{ "--i": i } as CSSProperties}
onKeyDown={(e) => { if (e.key === "Escape" && picked === t) { setPicked(null); e.currentTarget.querySelector("button")?.focus(); } }}
>
<button
type="button"
className={styles.time}
aria-pressed={picked === t}
onClick={() => setPicked(picked === t ? null : t)}
>
{t}
</button>
{picked === t && (
<button ref={confirm} type="button" className={styles.confirm} onClick={() => setBooked(true)} aria-label={`Confirm ${d.name} ${d.date} at ${t}`}>
Confirm
</button>
)}
</li>
))}
</ul>
) : (
<p key={day} className={styles.empty}>Fully booked. Thursday has 6 times left.</p>
)}
{/* Always mounted, so screen readers announce the booking when its text appears. */}
<p className={styles.sr} role="status">{booked && picked ? `Booked for ${d.name} ${d.date}, ${picked}` : ""}</p>
</div>
</div>
);
}
demo.module.css
.root {
container-type: size; width: 100%; height: 100%; display: grid; grid-template-rows: minmax(0, 100%); place-items: center; padding: 12px;
box-sizing: border-box; background: var(--surface);
}
.card {
position: relative; width: min(380px, 100%); height: 100%; max-height: 400px; box-sizing: border-box; display: grid;
grid-template-rows: auto auto minmax(0, 1fr); gap: 10px; padding: 14px; border-radius: 16px;
border: 1px solid var(--line); background: var(--white);
}
.card p { margin: 0; }
.head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.title { font-size: 14px; font-weight: 700; }
.month { font-size: 12px; font-weight: 600; color: var(--muted); }
.days { display: flex; gap: 6px; }
.day {
flex: 1; display: grid; justify-items: center; gap: 1px; min-width: 0; min-height: 44px; padding: 5px 0; border-radius: 10px;
border: 1px solid var(--line); background: var(--white); color: var(--ink); font: inherit; cursor: pointer;
-webkit-tap-highlight-color: transparent; transition: background-color .25s, border-color .25s, color .25s, transform .18s var(--out);
}
.day:hover { border-color: var(--muted); }
.day:active { transform: scale(.95); }
.day:focus-visible, .time:focus-visible, .confirm:focus-visible, .change:focus-visible { outline: 2px solid var(--accent-text); outline-offset: 2px; }
.day[aria-checked="true"] { background: var(--ink); border-color: var(--ink); color: var(--white); }
.day[data-empty="true"] .dayDate { text-decoration: line-through; opacity: .55; }
.dayName { font-size: 11px; font-weight: 600; opacity: .7; }
.dayDate { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
/* Slots scroll inside the card so the picker fits short tiles. */
.slots {
list-style: none; margin: 0; padding: 2px; display: grid; align-content: start; gap: 6px; min-height: 0;
overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
/* A soft fade at the bottom hints there is more to scroll. Masks use only alpha, so any token works. */
mask-image: linear-gradient(to bottom, var(--ink) calc(100% - 14px), transparent); padding-bottom: 12px;
}
.slot { display: flex; gap: 6px; animation: cascade .4s var(--out) both; animation-delay: calc(var(--i) * 45ms); }
@keyframes cascade { from { opacity: 0; translate: 0 10px; } }
.time, .confirm {
min-height: 38px; border-radius: 9px; font: inherit; font-size: 14px; font-weight: 650; cursor: pointer;
-webkit-tap-highlight-color: transparent;
}
.time {
flex: 1 1 100%; min-width: 0; border: 1px solid var(--line); background: var(--white); color: var(--ink);
font-variant-numeric: tabular-nums; transition: flex-basis .35s var(--out), border-color .2s, background-color .2s;
}
.time:hover { border-color: var(--accent-text); }
/* Picking a time splits the row: the time shrinks to half and Confirm grows in beside it. */
.slot[data-picked="true"] .time { flex-basis: 50%; background: var(--surface); border-color: var(--line); color: var(--muted); }
.confirm {
flex: 1 1 50%; min-width: 0; overflow: hidden; white-space: nowrap; border: 0; background: var(--accent-text); color: var(--white);
animation: split .35s var(--out) both;
}
.confirm:active { scale: .97; }
@keyframes split { from { flex-basis: 0%; opacity: 0; } }
.empty { align-self: center; justify-self: center; font-size: 13px; color: var(--muted); text-align: center; animation: cascade .4s var(--out) both; }
.done { display: grid; justify-items: center; align-content: center; gap: 4px; text-align: center; min-height: 0; animation: cascade .4s var(--out) both; }
.tick { display: grid; place-items: center; width: 40px; height: 40px; margin-bottom: 4px; border-radius: 50%; background: var(--accent-pale); color: var(--accent-text); animation: pop .45s var(--out) both; }
.tick svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 20; animation: draw .45s var(--out) .15s both; }
@keyframes pop { from { scale: .4; opacity: 0; } }
@keyframes draw { from { stroke-dashoffset: 20; } to { stroke-dashoffset: 0; } }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.doneText { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
.doneNote { font-size: 12px; color: var(--muted); }
.change {
margin-top: 6px; min-height: 34px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--white);
color: var(--ink); font: inherit; font-size: 12px; font-weight: 650; cursor: pointer;
}
.change:hover { border-color: var(--muted); }
@container (max-height: 270px) {
.card { padding: 10px 12px; gap: 8px; }
.day { min-height: 40px; padding-block: 3px; }
.tick { width: 32px; height: 32px; margin-bottom: 0; }
.done { gap: 2px; }
}
@media (prefers-reduced-motion: reduce) {
.slot, .confirm, .empty, .done, .tick, .tick svg { animation: none; }
.time, .day { transition: none; }
}
Use it for
- Booking flows for calls, demos and appointments
- Compact pickers inside cards, sidebars or modals
- Any choose then confirm step that should not need a dialog
Avoid
- Calendars spanning many weeks, where a month grid fits better
- Flows where picking a time should book it at once
How it works
The slot list is keyed by the chosen day, so a new day remounts it and each row replays a cascade keyframe delayed by its index. Picking a time renders a Confirm button beside it: the time's flex-basis transitions from 100% to 50% while Confirm animates in from a flex-basis of 0, so the row visibly splits in two. Confirming swaps the list for a status block whose check draws with stroke-dashoffset.
Accessibility
Days form a radiogroup with arrow key navigation and a fully booked label, times are real buttons with aria-pressed, and focus moves to Confirm after picking and to Change time after booking. Escape cancels a pick and the booking is announced through role status. Reduced motion shows every state with no cascade, split or draw.