仕組み
The form reads as the trip it produces, and the map shows it. Each slot is an inline field that opens a picker under itself: a list with descriptions, a grid of chips, or a month calendar. Above the sentence a flat map marks every port; the chosen route is an accent arc that draws itself from departure to arrival with a small boat on its tip, and the travel time settles on its middle. Browsing a port list previews the route as a dashed line. The picker is placed inside the visible area, the sentence rewrites itself after each choice and a live region reads it in full with the travel time. Availability, disabled options and invalid values come from the fields, so the sentence and the map can show why the trip cannot be booked yet.
向いている場面
- Trips between two places with a date and a party: ferries, trains, flights, shuttles, deliveries between depots.
- Places where the summary is the form itself and the route matters: a booking card, the top of a results page.
向かない場面
- Many stops or free-form addresses: use a map with search and a list of stops.
- Long forms, free-text fields or choices that need explanation: use a regular form.
- When the sentence cannot be rewritten for every language you support; translate the whole template, not word by word.
使い方
import { useState } from 'react'
import { RouteSentence } from './RouteSentence.jsx'
// Map units are your own (here 400 x 170): land as SVG paths, ports as points with a label side
const map = {
width: 400,
height: 170,
from: 'from',
to: 'to',
land: ['M-10 -10H58C48 40 104 60 78 96C64 120 140 140 150 180H-10Z', 'M232 44C230 26 300 18 304 42C306 60 234 64 232 44Z'],
ports: [
{ id: 'hbt', label: 'Harbourton', x: 80, y: 97, side: 'bottom' },
{ id: 'rdi', label: 'Reed Isle', x: 256, y: 65 },
{ id: 'fen', label: 'Fen Point', x: 222, y: 137 },
],
info: (values, a, b) => Math.round(Math.hypot(b.x - a.x, b.y - a.y) / 5) + ' min',
}
export function FerrySearch({ findSailings }) {
const [trip, setTrip] = useState({ from: 'hbt', to: 'rdi', date: '2027-07-17', party: 2 })
const [status, setStatus] = useState(null)
return (
<RouteSentence
title="Island ferries"
map={map}
template="From {from} to {to} on {date}, {party}."
fields={{
// No options: departure and arrival list the ports on the map
from: { type: 'options', label: 'From' },
to: { type: 'options', label: 'To' },
date: { type: 'date', label: 'Date', min: '2027-07-06' },
party: {
type: 'options',
label: 'Travellers',
columns: 4,
options: [1, 2, 3, 4, 5, 6, 7, 8].map((n) => ({ value: n, label: String(n) })),
format: (n) => (n === 1 ? '1 traveller' : n + ' travellers'),
},
}}
value={trip}
onChange={(next) => {
setTrip(next)
setStatus(null)
}}
submitLabel="Find sailings"
status={status}
// Return a promise: the button shows a spinner meanwhile, and a later change aborts it through the signal
onSubmit={async (values, { signal }) => {
const sailings = await findSailings(values, { signal })
setStatus(sailings.length ? { tone: 'success', text: sailings.length + ' sailings that day.' } : { tone: 'danger', text: 'No sailing that day.' })
}}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
template | string | 'From {from} to {to} on {date}, {party}.' | The sentence. Each {key} becomes a slot for fields[key]. Write one template per language, so word order and particles stay natural; punctuation after a slot stays on its line. |
fields | { [key]: { type: 'options' | 'date', label, ... } } | — | One entry per slot. Options: options [{ value, label, description?, disabled? }], columns (1 is a list, more is a grid of chips), format(value, option). Date: min, max, isDisabled(iso), format(iso). Both: invalid, placeholder. The departure and arrival fields list the map's ports when they have no options. Without fields a ferry trip with its map is shown. |
value | { [key]: any } | — | Controlled values, keyed like fields. Dates are 'YYYY-MM-DD' strings. Use with onChange; or pass defaultValue for an uncontrolled form. |
onChange | (values, key) => void | — | Called with all values and the changed key when a slot gets a new value. A swap changes both ports and reports the departure key. |
onSubmit | (values, { signal }) => void | Promise | — | Called by the button when every slot has a value and none is invalid. Return a promise to show the busy spinner; the signal aborts when a value changes or the form unmounts. |
submitLabel | string | null | 'Continue' | Text of the submit button. null hides the button, for a sentence that filters live. |
status | string | { tone: 'neutral' | 'success' | 'warning' | 'danger', text } | — | A message under the sentence in a polite status region, with an icon for its tone. Slots marked invalid point to it. |
note | ReactNode | — | Quiet fine print shown in the same place while there is no status. |
title | ReactNode | — | Optional heading above the sentence; it also names the form. |
description | ReactNode | — | One quieter line under the title. |
media | ReactNode | — | A 40px picture or icon beside the title, on an accent tint. |
map | { width, height, land: string[], ports: [{ id, label, x, y, side? }], from, to, info?(values, a, b), lanes?, swap? } | null | — | The route map above the sentence. Coordinates are in your own units (width x height); land is a list of SVG paths drawn flat with a coastline; each port is a dot with its label on the side you choose (right, left, top or bottom). from and to name the fields that hold the two port ids. The route between them is an accent arc that redraws when either changes; info returns the text on it, such as a travel time, which the live region also reads. lanes: false hides the dotted lanes, swap: false hides the swap button. Equal ports mark the arrival invalid. null shows no map; with your own fields there is none unless you pass one. |
size | 'lg' | 'md' | 'lg' | lg: 20px sentence, 36px slots, 44px button. md: 16px sentence, 28px slots, 36px button. |
variant | 'card' | 'plain' | 'card' | card draws the surface, border and corners; plain leaves them to the page. |
submitVariant | 'ink' | 'accent' | 'ink' | The button's colour, as base-button's ink or primary variant. |
locale | string | 'en' | BCP 47 locale for the calendar's month, weekday and day names and the default date format. |
weekStartsOn | 0 | 1 | … | 6 | 1 | First column of the calendar: 0 is Sunday, 1 is Monday. |
today | string | — | The 'YYYY-MM-DD' day marked as today in the calendar. Defaults to the device's date. |
labels | object | — | Built-in text: choose, submit, busy, prevMonth, nextMonth, today, unavailable, slot(label, text) for each slot's spoken name, swap and swapLabel for the swap button, samePort for equal ports, map(from, to, info) for the map's name and travel(info) for what the live region adds after the sentence. See the English defaults in the source. |
アクセシビリティ
- Each slot is a button named by its field and value, such as “From: Harbourton”. Tab moves between slots; Enter, Space, ArrowDown or ArrowUp opens its picker with focus on the current value.
- Lists and chip grids are listboxes: arrow keys move (up and down by row in a grid), Home and End jump to the ends, Enter or Space picks. Unavailable options stay reachable, are announced as unavailable and are struck through, not only greyed.
- The calendar is a grid in a non-modal dialog: arrows move by day and week, Home and End to the start and end of the week, Page Up and Page Down by month (with Shift by year). Days without a sailing are struck through and announced as unavailable.
- Escape closes a picker without a change and returns focus to its slot; Tab leaves it for the next slot or the button; a press outside closes it.
- The map is one image whose name states the route and its travel time, such as “Route map: Harbourton to Reed Isle, 35 min”. The swap button is named “Swap departure and arrival”.
- After each change a polite live region reads the whole sentence and then the travel time. The status line is a polite status region; an invalid slot shows an alert sign and is described by that message, and the route turns into a dashed danger line.
- On touch screens slots, rows, chips, days and the swap button get 44px targets, and the picker stays inside the screen.
モーション
When a port changes, the route draws itself from departure to arrival in 900ms with the reveal easing while a small boat rides its tip; the arrival dot pops in as the line reaches it and the travel time fades in at the middle. The picker scales in from its slot in 240ms and fades out in 160ms; a changed value rises 4px into place while its slot glows with the accent tint. With reduced motion the route, its ends and the travel time change at once, there is no boat, and the picker appears and goes without movement.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
RouteSentence.jsxroute-sentence.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。







