Hotel booking
A booking widget for a fictional mountain lodge: check-in and check-out Inputs of type date, steppers for adults and children, a RadioGroup of rooms styled as selectable cards, and a price summary with nights × rate, a cleaning fee, tax and a total beside a Reserve Button.
Alder Ridge Lodge
4.8 · 212 stays · Silverpine, ColoradoRoom
Your stay
Jan 15 to Jan 18, Ridge King Suite- $264 × 3 nights
- $792.00
- Cleaning fee
- $45.00
- Lodging tax (11.5%)
- $96.26
- Total
- $933.26
Source · recipes/hotel-booking.tsx
'use client'
import {
BodySmall,
Button,
Heading,
Input,
Label,
Radio,
RadioGroup,
Separator,
Stack,
Text,
toastSuccess,
} from '@blocks/design-system'
import { Minus, MountainSnow, Plus, Star } from 'lucide-react'
import { useId, useState } from 'react'
import styles from './hotel-booking.module.css'
interface Room {
id: string
name: string
details: string
sleeps: number
rate: number
}
// A non-empty tuple, so rooms[0] is always a Room.
const rooms: [Room, ...Room[]] = [
{ id: 'forest-queen', name: 'Forest Queen', details: 'Queen bed, forest view', sleeps: 2, rate: 189 },
{ id: 'ridge-king', name: 'Ridge King Suite', details: 'King bed, fireplace, balcony', sleeps: 3, rate: 264 },
{ id: 'family-loft', name: 'Family Loft', details: 'King and two twins, kitchenette', sleeps: 5, rate: 329 },
]
const cleaningFee = 45
const taxRate = 0.115
const dayMs = 24 * 60 * 60 * 1000
const usd = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
const usdCents = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
const shortDate = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC' })
// Date inputs give "YYYY-MM-DD". Reading them as UTC keeps the night count
// free of time zones and daylight saving.
function toUtc(value: string) {
const [year, month, day] = value.split('-').map(Number)
return year && month && day ? Date.UTC(year, month - 1, day) : Number.NaN
}
function plusOneDay(value: string) {
const time = toUtc(value)
return Number.isNaN(time) ? undefined : new Date(time + dayMs).toISOString().slice(0, 10)
}
// An app-owned counter: a labelled group with − and + buttons. At a limit the
// buttons are aria-disabled rather than disabled, so focus stays on them.
function Stepper(props: {
label: string
hint: string
value: number
min: number
max: number
onChange: (value: number) => void
}) {
const { label, hint, value, min, max, onChange } = props
const labelId = useId()
const noun = label.toLowerCase()
return (
<fieldset className={styles.stepper} aria-labelledby={labelId}>
<Stack gap="none">
<Text id={labelId} fontSize="sm" fontWeight="semibold">
{label}
</Text>
<Text fontSize="xs" color="text-muted">
{hint}
</Text>
</Stack>
<div className={styles.stepperControls}>
<Button
type="button"
size="sm"
borderRadius="circle"
aria-label={`Fewer ${noun}`}
aria-disabled={value <= min}
onClick={() => value > min && onChange(value - 1)}
>
<Minus aria-hidden size={14} />
</Button>
<output className={styles.count}>
{value}
<span className={styles.visuallyHidden}> {noun}</span>
</output>
<Button
type="button"
size="sm"
borderRadius="circle"
aria-label={`More ${noun}`}
aria-disabled={value >= max}
onClick={() => value < max && onChange(value + 1)}
>
<Plus aria-hidden size={14} />
</Button>
</div>
</fieldset>
)
}
// A booking widget for a fictional lodge: dates, guests and a room, with a
// price breakdown that updates as each one changes.
export default function HotelBooking() {
const titleId = useId()
const checkInId = useId()
const checkOutId = useId()
const dateErrorId = useId()
const roomLabelId = useId()
const summaryId = useId()
const [checkIn, setCheckIn] = useState('2027-01-15')
const [checkOut, setCheckOut] = useState('2027-01-18')
const [adults, setAdults] = useState(2)
const [children, setChildren] = useState(0)
const [roomId, setRoomId] = useState('ridge-king')
const guests = adults + children
const largestRoom = Math.max(...rooms.map((room) => room.sleeps))
const room = rooms.find((option) => option.id === roomId) ?? rooms[0]
const nights = Math.round((toUtc(checkOut) - toUtc(checkIn)) / dayMs)
const datesValid = Number.isFinite(nights) && nights > 0
const lodging = datesValid ? room.rate * nights : 0
const taxes = (lodging + cleaningFee) * taxRate
const total = lodging + cleaningFee + taxes
const stay = `${shortDate.format(toUtc(checkIn))} to ${shortDate.format(toUtc(checkOut))}`
const freeCancellationUntil = shortDate.format(toUtc(checkIn) - 7 * dayMs)
// Changing the party moves the booking to a room that still fits.
function setParty(nextAdults: number, nextChildren: number) {
setAdults(nextAdults)
setChildren(nextChildren)
if (room.sleeps < nextAdults + nextChildren) {
const fits = rooms.find((option) => option.sleeps >= nextAdults + nextChildren)
if (fits) setRoomId(fits.id)
}
}
function reserve() {
if (!datesValid) {
document.getElementById(checkOutId)?.focus()
return
}
const party = `${guests} ${guests === 1 ? 'guest' : 'guests'}`
toastSuccess(`${room.name} reserved`, { description: `${stay} · ${party} · ${usdCents.format(total)}` })
}
return (
<section className={styles.booking} aria-labelledby={titleId}>
<div className={styles.layout}>
<Stack gap="lg">
<Stack direction="row" gap="md" align="center">
<span className={styles.logo}>
<MountainSnow aria-hidden size={22} />
</span>
<Stack gap="none">
<Heading as="h3" id={titleId} fontSize="lg" margin="none">
Alder Ridge Lodge
</Heading>
<Text fontSize="sm" color="text-muted">
<Star aria-hidden size={12} className={styles.star} /> 4.8 · 212 stays · Silverpine, Colorado
</Text>
</Stack>
</Stack>
<div className={styles.dates}>
<div>
<Label htmlFor={checkInId}>Check-in</Label>
<Input
id={checkInId}
type="date"
value={checkIn}
min="2027-01-01"
onChange={(event) => setCheckIn(event.currentTarget.value)}
/>
</div>
<div>
<Label htmlFor={checkOutId}>Check-out</Label>
<Input
id={checkOutId}
type="date"
value={checkOut}
min={plusOneDay(checkIn)}
variant={datesValid ? 'default' : 'error'}
aria-describedby={datesValid ? undefined : dateErrorId}
onChange={(event) => setCheckOut(event.currentTarget.value)}
/>
</div>
{!datesValid && (
<BodySmall id={dateErrorId} color="danger-text" margin="none" dangerouslySetClassName={styles.dateError}>
Check-out must be at least one night after check-in.
</BodySmall>
)}
</div>
<div className={styles.guests}>
<Stepper
label="Adults"
hint="Ages 13 and up"
value={adults}
min={1}
max={largestRoom - children}
onChange={(value) => setParty(value, children)}
/>
<Separator />
<Stepper
label="Children"
hint="Ages 2 to 12"
value={children}
min={0}
max={Math.min(4, largestRoom - adults)}
onChange={(value) => setParty(adults, value)}
/>
</div>
<Stack gap="sm">
<Text id={roomLabelId} fontSize="sm" fontWeight="semibold">
Room
</Text>
<RadioGroup
aria-labelledby={roomLabelId}
value={roomId}
onValueChange={(value) => setRoomId(String(value))}
dangerouslySetClassName={styles.rooms}
>
{rooms.map((option) => {
const fits = option.sleeps >= guests
return (
<Radio key={option.id} value={option.id} disabled={!fits}>
<span className={styles.roomText}>
<span className={styles.roomName}>{option.name}</span>
<span className={styles.roomMeta}>
{option.details} · sleeps {option.sleeps}
{!fits && ' · too small for your party'}
</span>
</span>
<span className={styles.roomPrice}>
<strong>{usd.format(option.rate)}</strong> per night
</span>
</Radio>
)
})}
</RadioGroup>
</Stack>
</Stack>
<section className={styles.summary} aria-labelledby={summaryId}>
<Heading as="h4" id={summaryId} fontSize="md" margin="none">
Your stay
</Heading>
<Text fontSize="sm" color="text-muted">
{datesValid ? `${stay}, ${room.name}` : 'Choose your dates'}
</Text>
<dl className={styles.breakdown}>
<div>
<dt>
{usd.format(room.rate)} × {datesValid ? nights : 0} {nights === 1 ? 'night' : 'nights'}
</dt>
<dd>{usdCents.format(lodging)}</dd>
</div>
<div>
<dt>Cleaning fee</dt>
<dd>{usdCents.format(cleaningFee)}</dd>
</div>
<div>
<dt>Lodging tax (11.5%)</dt>
<dd>{usdCents.format(taxes)}</dd>
</div>
<div className={styles.total} aria-live="polite">
<dt>Total</dt>
<dd>{usdCents.format(total)}</dd>
</div>
</dl>
<Button type="button" variant="primary" size="lg" onClick={reserve}>
Reserve
</Button>
<Text fontSize="xs" color="text-muted" dangerouslySetClassName={styles.centered}>
You won’t be charged until check-in.
{datesValid && ` Free cancellation until ${freeCancellationUntil}.`}
</Text>
</section>
</div>
</section>
)
}How it works
- Dates are read as UTC. The night count comes from the inputs’
YYYY-MM-DDvalues parsed withDate.UTC, so time zones and daylight saving can’t change it, and nothing reads the current date during render. Check-out’sminfollows check-in, and an invalid range marks the input and explains itself. - Steppers keep focus at their limits. Each stepper is a labelled
<fieldset>; at a limit its button isaria-disabledrather thandisabled, so focus doesn’t vanish. The count is an<output>, announced with its noun, “3 adults”. - Rooms that can’t fit are disabled and say so. Growing the party past a room’s size moves the booking to the smallest room that fits, and the smaller rooms explain why they’re unavailable.
- The summary reflows. A container query puts the summary in a sticky column beside the form when there’s room, and under it on a phone.
App-owned CSS
The two-column layout, steppers, room cards and the price breakdown.
/* The booking form beside its price summary, room cards and a guest stepper. */
.booking {
container-type: inline-size;
}
.layout {
display: grid;
gap: var(--ds-space-xl);
align-items: start;
}
@container (min-width: 38rem) {
.layout {
grid-template-columns: minmax(0, 1fr) 17rem;
}
.summary {
position: sticky;
top: var(--ds-space-xxl);
}
}
.logo {
display: grid;
place-items: center;
flex: none;
width: var(--ds-control-height-lg);
height: var(--ds-control-height-lg);
color: var(--ds-color-accent-fg);
background: var(--ds-color-accent);
border-radius: var(--ds-border-radius-md);
}
.star {
color: var(--ds-color-warning);
fill: currentColor;
vertical-align: -1px;
}
.dates {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: var(--ds-space-lg);
/* Grid items and date inputs default to their content's width; let them
shrink so the row lines up with the cards below. */
& > div {
display: grid;
min-width: 0;
}
& input {
width: 100%;
min-width: 0;
}
}
.dateError {
grid-column: 1 / -1;
}
.guests {
display: grid;
gap: var(--ds-space-md);
padding: var(--ds-space-md) var(--ds-space-lg);
border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
border-radius: var(--ds-border-radius-md);
}
.stepper {
display: flex;
min-width: 0;
margin: 0;
padding: 0;
border: 0;
align-items: center;
justify-content: space-between;
gap: var(--ds-space-lg);
}
.stepperControls {
display: flex;
align-items: center;
gap: var(--ds-space-md);
/* Round icon buttons: as wide as they are tall. */
& > button {
width: var(--ds-control-height-sm);
padding-inline: 0;
}
}
.count {
min-width: var(--ds-space-lg);
font-weight: var(--ds-font-weight-semibold);
font-variant-numeric: tabular-nums;
text-align: center;
}
.visuallyHidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
/* Each Radio's label becomes a selectable card. */
.rooms > label {
display: flex;
align-items: center;
gap: var(--ds-space-md);
padding: var(--ds-space-md) var(--ds-space-lg);
border: var(--ds-border-width-sm) solid var(--ds-color-border);
border-radius: var(--ds-border-radius-md);
transition:
border-color var(--ds-duration-fast) var(--ds-easing-standard),
background-color var(--ds-duration-fast) var(--ds-easing-standard);
&:hover:not(:has([data-disabled])) {
border-color: var(--ds-color-border-strong);
}
&:has([data-checked]) {
background: var(--ds-color-accent-subtle);
border-color: var(--ds-color-accent);
}
}
.roomText {
display: grid;
flex: 1;
min-width: 0;
}
.roomName {
font-weight: var(--ds-font-weight-semibold);
}
.roomMeta {
font-size: var(--ds-font-size-xs);
color: var(--ds-color-text-muted);
}
.roomPrice {
font-size: var(--ds-font-size-xs);
color: var(--ds-color-text-muted);
white-space: nowrap;
& strong {
display: block;
font-size: var(--ds-font-size-sm);
color: var(--ds-color-text);
text-align: right;
}
}
.summary {
display: grid;
gap: var(--ds-space-md);
padding: var(--ds-space-lg);
background: var(--ds-color-bg);
border: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
border-radius: var(--ds-border-radius-xl);
box-shadow: var(--ds-shadow-md);
}
.breakdown {
display: grid;
gap: var(--ds-space-sm);
margin: var(--ds-space-sm) 0;
font-size: var(--ds-font-size-sm);
& div {
display: flex;
justify-content: space-between;
gap: var(--ds-space-md);
}
& dt {
color: var(--ds-color-text-muted);
}
& dd {
margin: 0;
font-variant-numeric: tabular-nums;
}
}
.total {
padding-top: var(--ds-space-md);
margin-top: var(--ds-space-sm);
font-size: var(--ds-font-size-md);
font-weight: var(--ds-font-weight-semibold);
border-top: var(--ds-border-width-sm) solid var(--ds-color-border-subtle);
& dt {
color: var(--ds-color-text);
}
}
.centered {
text-align: center;
}
@media (prefers-reduced-motion: reduce) {
.rooms > label {
transition: none;
}
}