Seat Selection
Controlled seat selection with compact map and list presentations, taken and accessible seat states.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add seat-selectionSource files
Expand a file to read the source the installer copies into your app.
components/blocks/seat-selection/SeatSelection.tsx
import { useState } from 'react'
import { Pressable, useWindowDimensions, View, type ViewProps } from 'react-native'
import { Accessibility, Check } from 'lucide-react-native'
import { Icon } from '@/components/ui/icon'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { SegmentedControl, SegmentedControlItem } from '@/components/ui/segmented-control'
import { Text } from '@/components/ui/text'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
export interface SeatSelectionSeat {
id: string
label: string
accessible?: boolean
taken?: boolean
}
export interface SeatSelectionRow {
id: string
label: string
seats: readonly SeatSelectionSeat[]
}
interface SeatSelectionLabels {
presentation: string
map: string
list: string
taken: string
takenLegend: string
accessible: string
choose: string
empty: string
unavailable: string
row: (label: string) => string
selected: (label: string) => string
}
export interface SeatSelectionProps extends Omit<ViewProps, 'children'> {
className?: string
title?: string
description?: string
frontLabel?: string
/** Ordered rows. Seat IDs must be unique and nonempty across all rows. */
rows?: readonly SeatSelectionRow[]
/** Insert an aisle after this number of seats in each map row. */
aisleAfter?: number
value: string | null
onValueChange: (seatId: string) => void
labels?: Partial<SeatSelectionLabels>
}
const SEAT_SIZE = 48
const SEAT_GAP = 8
const ROW_LABEL_WIDTH = 40
const AISLE_WIDTH = 12
export function SeatSelection({
className,
title = 'Choose a seat',
description,
frontLabel,
rows = [],
aisleAfter,
value,
onValueChange,
labels,
...props
}: SeatSelectionProps) {
const { width, fontScale } = useWindowDimensions()
const [contentWidth, setContentWidth] = useState<number>()
const [presentation, setPresentation] = useState<'map' | 'list'>('map')
const visibleRows = rows.filter((row) => row.seats.length > 0)
const allSeats = visibleRows.flatMap((row) => row.seats)
const selected = allSeats.find((seat) => seat.id === value && !seat.taken)
const taken = labels?.taken ?? 'Taken'
const accessibleSeat = labels?.accessible ?? 'Accessible seat'
const empty = allSeats.length === 0 || allSeats.every((seat) => seat.taken)
const unavailable = value !== null && !selected && !empty
const status = empty
? (labels?.empty ?? 'No seats available.')
: unavailable
? (labels?.unavailable ?? 'Your selected seat is unavailable. Choose another seat.')
: selected
? (labels?.selected?.(selected.label) ?? `Selected seat: ${selected.label}`)
: (labels?.choose ?? 'Select an available seat.')
useAnnouncement(unavailable ? status : undefined)
const split = aisleAfter ?? 0
const requiredWidth = visibleRows.reduce((widest, { seats }) => {
const aisle = split > 0 && split < seats.length ? AISLE_WIDTH : 0
return Math.max(widest, ROW_LABEL_WIDTH + seats.length * (SEAT_SIZE + SEAT_GAP) + aisle)
}, 0)
// Compact seat codes fit map cells. The list preserves full labels and larger type.
const mapFits =
requiredWidth <= Math.min(width, contentWidth ?? width) &&
fontScale < 1.4 &&
visibleRows.every((row) => row.label.length <= 3 && row.seats.every((seat) => seat.label.length <= 3))
const list = !mapFits || presentation === 'list'
return (
<View {...props} className={cn('gap-[16px]', className)}>
<View className="gap-[4px]">
<Text size="title" accessibilityRole="header">
{title}
</Text>
{description ? <Text variant="muted">{description}</Text> : null}
</View>
<View className="gap-[16px]" onLayout={({ nativeEvent }) => setContentWidth(nativeEvent.layout.width)}>
{mapFits && allSeats.length > 0 ? (
<SegmentedControl
accessibilityLabel={labels?.presentation ?? 'Seat presentation'}
value={presentation}
onValueChange={(next) => {
if (next === 'map' || next === 'list') setPresentation(next)
}}
>
<SegmentedControlItem value="map">{labels?.map ?? 'Map'}</SegmentedControlItem>
<SegmentedControlItem value="list">{labels?.list ?? 'List'}</SegmentedControlItem>
</SegmentedControl>
) : null}
{allSeats.length > 0 ? (
<RadioGroup
accessibilityLabel={title}
value={selected?.id ?? ''}
onValueChange={onValueChange}
className={list ? 'gap-[16px]' : 'items-center gap-[8px]'}
>
{!list && frontLabel ? (
<Text
size="sm"
variant="muted"
className="mb-[8px] self-stretch rounded-md bg-muted px-[12px] py-[8px] text-center"
>
{frontLabel}
</Text>
) : null}
{visibleRows.map((row) => {
const rowLabel = labels?.row?.(row.label) ?? `Row ${row.label}`
return (
<View key={row.id} className={list ? 'gap-[4px]' : 'flex-row items-center gap-[8px]'}>
<Text
size="sm"
variant="muted"
accessibilityRole={list ? 'header' : undefined}
aria-hidden={!list}
className={list ? 'font-medium' : 'text-center font-semibold'}
style={list ? undefined : { width: ROW_LABEL_WIDTH }}
>
{list ? rowLabel : row.label}
</Text>
{row.seats.map((seat, index) => {
const checked = seat.id === selected?.id
const detail = [seat.accessible ? accessibleSeat : '', seat.taken ? taken : '']
.filter(Boolean)
.join(', ')
const accessibilityLabel = [seat.label, rowLabel, detail].filter(Boolean).join(', ')
return list ? (
<RadioGroupItem
key={seat.id}
value={seat.id}
label={seat.label}
disabled={seat.taken}
accessibilityLabel={accessibilityLabel}
className={cn('border border-border bg-card focus:border-ring', checked && 'border-primary')}
>
{detail ? (
<Text size="sm" variant="muted">
{detail}
</Text>
) : null}
</RadioGroupItem>
) : (
<Pressable
key={seat.id}
accessibilityLabel={accessibilityLabel}
accessibilityRole="radio"
accessibilityState={{ checked, disabled: !!seat.taken }}
disabled={seat.taken}
className={cn(
'shrink-0 items-center justify-center rounded-md border border-border bg-card focus:border-ring active:bg-muted',
seat.taken && 'border-transparent bg-muted',
checked && 'border-primary bg-primary active:bg-primary',
)}
style={{
width: SEAT_SIZE,
height: SEAT_SIZE,
marginStart: index === split && split > 0 ? AISLE_WIDTH : 0,
}}
onPress={() => {
if (!seat.taken && !checked) onValueChange(seat.id)
}}
>
{checked || seat.accessible ? (
<View aria-hidden className="flex-row gap-[4px]">
{seat.accessible ? (
<Icon
icon={Accessibility}
size={14}
className={checked ? 'text-primary-foreground' : 'text-foreground'}
/>
) : null}
{checked ? <Icon icon={Check} size={14} className="text-primary-foreground" /> : null}
</View>
) : null}
<Text
aria-hidden
size="xs"
numberOfLines={1}
adjustsFontSizeToFit
minimumFontScale={0.75}
className={cn(
'max-w-full px-[2px] text-center font-semibold',
checked && 'text-primary-foreground',
seat.taken && 'text-muted-foreground line-through',
)}
>
{seat.label}
</Text>
</Pressable>
)
})}
</View>
)
})}
</RadioGroup>
) : null}
{!list && allSeats.some((seat) => seat.taken || seat.accessible) ? (
<View className="flex-row flex-wrap items-center gap-x-[12px] gap-y-[4px]">
{allSeats.some((seat) => seat.accessible) ? (
<View className="flex-row items-center gap-[4px]">
<Icon icon={Accessibility} size={16} className="text-muted-foreground" />
<Text size="sm" variant="muted">
{accessibleSeat}
</Text>
</View>
) : null}
{allSeats.some((seat) => seat.taken) ? (
<Text size="sm" variant="muted">
{labels?.takenLegend ?? 'Struck-through seats are taken'}
</Text>
) : null}
</View>
) : null}
<Text size="sm" className={cn('border-t border-border pt-[12px]', unavailable && 'text-destructive')}>
{status}
</Text>
</View>
</View>
)
}
components/blocks/seat-selection/index.ts
export { SeatSelection, type SeatSelectionProps, type SeatSelectionRow, type SeatSelectionSeat } from './SeatSelection'
Dependencies
Registry items
npm packages
lucide-react-native