Collection
Mobility / In-Car Display · v0.2.2Validated
See the upcoming maneuver, its distance and recommended lanes on a schematic junction.
Upcoming Maneuver · Preview
Loading widget…
Preparing source code…
Idle
Mobility / In-Car Display · v0.2.2Validated
See the upcoming maneuver, its distance and recommended lanes on a schematic junction.
Loading widget…
Preparing source code…
Load the Lumo theme once in your app entry point. The example uses the local Lumo workspace packages.
import "@lumo/tokens/tokens.css";
import { NextTurnWidget } from "@lumo/widgets";
<NextTurnWidget />Props for integrating the React component in your app.
routereadonly VehicleManeuver[] | null (optional)simulateboolean (optional){
"route": [
{
"id": "r18",
"kind": "roundabout",
"distanceMetres": 420,
"instruction": "Take the second exit",
"street": "Quai des Forges",
"exitNumber": 2,
"lanes": [
{
"direction": "straight",
"recommended": false
},
{
"direction": "right",
"recommended": true
}
]
}
]
}import type { VehicleManeuver } from "@lumo/widgets";
type Step = Omit<VehicleManeuver, "distanceMetres"> & { distanceFromVehicleM: number | null };
export function toManeuvers(steps: readonly Step[]): VehicleManeuver[] {
return steps.map(({ distanceFromVehicleM, ...step }, index) => {
const previous = index ? steps[index - 1].distanceFromVehicleM : 0;
const distanceMetres = previous === null || distanceFromVehicleM === null ? null : distanceFromVehicleM - previous;
return { ...step, distanceMetres: distanceMetres !== null && distanceMetres >= 0 ? distanceMetres : null };
});
}"use client";
import { useState } from "react";
import { Navigation } from "lucide-react";
import { AnimatedNumber, Badge, Button, WidgetCard } from "@lumo/ui";
import { useAnimationInterval } from "@lumo/hooks";
import { useReducedMotion } from "motion/react";
import { LaneGuidance, ManeuverArrow } from "./ManeuverArt";
import { demoManeuvers, demoMetresPerTick } from "./next-turn.constants";
import type { NextTurnWidgetProps } from "./next-turn.types";
import styles from "./next-turn.module.css";
function distance(value: number | null | undefined) {
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null;
}
export function NextTurnWidget({ route, simulate = false }: NextTurnWidgetProps = {}) {
const [demo, update] = useState<{ index: number; remaining: number }>({ index: 0, remaining: demoManeuvers[0].distanceMetres });
const [peek, setPeek] = useState<{ from: string; to: string }>();
const reduced = useReducedMotion();
const steps = route === undefined ? demoManeuvers : route ?? [];
const index = route === undefined ? demo.index : 0;
const current = steps[index];
const next = steps[index + 1];
const preview = Boolean(peek && current?.id === peek.from && next?.id === peek.to);
if (peek && !preview) setPeek(undefined);
const shown = preview ? next : current;
const approach = route === undefined ? demo.remaining : distance(current?.distanceMetres);
const segment = distance(next?.distanceMetres);
const metres = preview && approach !== null && segment !== null ? distance(approach + segment) : preview ? null : approach;
const km = metres !== null && metres >= 1000;
useAnimationInterval(() => update(({ index, remaining }) => {
if (remaining > demoMetresPerTick) return { index, remaining: remaining - demoMetresPerTick };
const advance = (index + 1) % demoManeuvers.length;
return { index: advance, remaining: Math.max(0, demoManeuvers[advance].distanceMetres - (demoMetresPerTick - remaining)) };
}), simulate && route === undefined && !reduced ? 1000 : null);
return <WidgetCard title="Next turn" description="Route guidance" label="Next turn" icon={<Navigation />}
size="medium" className={styles.card}>
{shown ? <>
<div className={styles.guidance}>
<div className={styles.copy}>
<span className={styles.caption}>{preview ? "Following turn" : "Upcoming turn"}</span>
{metres === null ? <strong className={styles.unavailable}>Distance unavailable</strong> :
<div className={styles.reading} role="group" aria-label={km ? `${(metres / 1000).toFixed(1)} kilometres` : `${Math.round(metres)} metres`}>
<span aria-hidden="true"><AnimatedNumber className={styles.number} value={km ? metres / 1000 : metres} decimals={km ? 1 : 0} blur={false} /></span>
<small aria-hidden="true">{km ? "km" : "m"}</small>
</div>}
<strong className={styles.instruction}>{shown.instruction}</strong>
<span className={styles.street}>{shown.street.trim() || "Road name unavailable"}</span>
</div>
<div className={styles.map}>
<ManeuverArrow kind={shown.kind} />
{shown.kind === "roundabout" && shown.exitNumber !== undefined && Number.isSafeInteger(shown.exitNumber) &&
shown.exitNumber > 0 && <span className={styles.exit}><Badge size="sm" tone="info">Exit {shown.exitNumber}</Badge></span>}
</div>
</div>
<div className={styles.footer}>
{shown.lanes?.length ? <div className={styles.laneGuide}>
<span>{shown.lanes.some(lane => lane.recommended) ? "Recommended lanes" : "Lane directions"}</span>
<LaneGuidance lanes={shown.lanes} />
</div> :
<span className={styles.caption}>Lane guidance unavailable</span>}
{current && next && <Button type="button" variant="secondary" size="xs" aria-pressed={preview}
onClick={() => setPeek(preview ? undefined : { from: current.id, to: next.id })}>{preview ? "Current turn" : "Preview next"}</Button>}
</div>
</> : <p className={styles.empty}>{route === null ? "Route unavailable" : "No route in progress"}</p>}
</WidgetCard>;
}
lucide-reactmotionreactreact-dom