shadcn-svelte map
DocsComponentsBlocks
All blocks

Flight route network

Data

An airline network as arcs from each hub, with flights a week and distance per route.

Use the arrow keys to pan, plus and minus to zoom, and Shift with the arrow keys to rotate.

London network

8 destinations · 252 flights a week

Pick a route on the map or below. Longest is 10,881 km.

Install

Adds the block to src/lib/components/route-network and the map if you do not have it yet.

pnpm dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/route-network.json
npx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/route-network.json
yarn dlx shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/route-network.json
bunx --bun shadcn-svelte@latest add https://shadcn-svelte-map.vercel.app/r/route-network.json

Code

<script lang="ts">
	import type { FeatureCollection, Point } from 'geojson';
	import PlaneIcon from '@lucide/svelte/icons/plane';
	import { cn } from '$lib/utils.js';
	import { Button } from '$lib/components/ui/button/index.js';
	import { ButtonGroup } from '$lib/components/ui/button-group/index.js';
	import * as Card from '$lib/components/ui/card/index.js';
	import {
		Map,
		MapArc,
		MapControlContainer,
		MapMarker,
		MapMarkerContent,
		MapMarkerLabel,
		MapMarkerTooltip,
		MapPointLayer,
		MapZoomControl,
		distanceMeters,
		type LngLatTuple,
		type MapStyle
	} from '$lib/components/ui/map/index.js';
	import { AIRPORTS, HUBS, ROUTES, VIEWPORT, type Airport, type Hub } from './data.js';

	interface Props {
		/** Basemap. Defaults to OpenFreeMap. */
		mapStyle?: MapStyle;
		class?: string;
	}

	let { mapStyle, class: className }: Props = $props();

	interface Leg {
		from: LngLatTuple;
		to: LngLatTuple;
		code: string;
		city: string;
		weekly: number;
		km: number;
	}

	const METRES_PER_KM = 1_000;
	const ARC_CURVATURE = 0.18;
	const airports: Record<string, Airport> = Object.fromEntries(
		AIRPORTS.map((airport) => [airport.code, airport])
	);
	const numbers = new Intl.NumberFormat('en');

	let hub = $state<Hub>(HUBS[0]);
	let selectedCode = $state<string | null>(null);

	const legs: Leg[] = $derived(
		ROUTES.filter((route) => route.from === hub)
			.flatMap((route): Leg[] => {
				const origin: Airport | undefined = airports[route.from];
				const destination: Airport | undefined = airports[route.to];
				// A route to an airport missing from the list is left out rather than breaking the map.
				if (!origin || !destination) return [];
				return [
					{
						from: origin.lngLat,
						to: destination.lngLat,
						code: destination.code,
						city: destination.city,
						weekly: route.weekly,
						km: Math.round(distanceMeters(origin.lngLat, destination.lngLat) / METRES_PER_KM)
					}
				];
			})
			.sort((a, b) => b.weekly - a.weekly)
	);

	const selected = $derived(legs.find((leg) => leg.code === selectedCode) ?? null);
	const weekly = $derived(legs.reduce((sum, leg) => sum + leg.weekly, 0));
	const longest = $derived(legs.reduce((most, leg) => Math.max(most, leg.km), 0));

	// Only the current hub's destinations are shown, so every point on the map picks a route.
	const destinations: FeatureCollection<Point, { code: string; city: string }> = $derived({
		type: 'FeatureCollection',
		features: legs.map((leg) => ({
			type: 'Feature',
			properties: { code: leg.code, city: leg.city },
			geometry: { type: 'Point', coordinates: leg.to }
		}))
	});

	function pickHub(next: Hub) {
		hub = next;
		selectedCode = null;
	}

	function pickLeg(code: string) {
		selectedCode = legs.some((leg) => leg.code === code) ? code : null;
	}
</script>

<div class={cn('grid min-h-[560px] gap-4 p-4 lg:grid-cols-[minmax(0,1fr)_20rem]', className)}>
	<Map
		class="min-h-[360px] rounded-lg border"
		aria-label="Route map"
		{mapStyle}
		viewport={VIEWPORT}
	>
		<MapArc
			id="network-legs"
			arcs={legs}
			curvature={ARC_CURVATURE}
			opacity={selected ? 0.35 : 0.8}
			width={1.5}
			onclick={(leg) => pickLeg(leg.code)}
		/>
		{#if selected}
			<MapArc id="network-selected" arcs={[selected]} curvature={ARC_CURVATURE} width={3} />
		{/if}
		<MapPointLayer
			id="network-airports"
			data={destinations}
			radius={3.5}
			onclick={(feature) => pickLeg(String(feature.properties.code))}
		/>
		{#each HUBS as code (code)}
			{@const airport = airports[code]}
			<MapMarker
				lngLat={airport.lngLat}
				aria-label="{airport.city} hub"
				onclick={() => pickHub(code)}
			>
				<MapMarkerContent>
					<span
						class={[
							'grid size-6 place-items-center rounded-full border-2 border-background shadow-md',
							hub === code ? 'bg-primary text-primary-foreground' : 'bg-muted text-muted-foreground'
						]}
					>
						<PlaneIcon class="size-3" />
					</span>
				</MapMarkerContent>
				<MapMarkerLabel>{code}</MapMarkerLabel>
				<MapMarkerTooltip>{airport.city}</MapMarkerTooltip>
			</MapMarker>
		{/each}
		<MapControlContainer position="top-left">
			<ButtonGroup aria-label="Hub">
				{#each HUBS as code (code)}
					<Button
						variant={hub === code ? 'default' : 'outline'}
						size="sm"
						aria-pressed={hub === code}
						onclick={() => pickHub(code)}
					>
						{code}
					</Button>
				{/each}
			</ButtonGroup>
		</MapControlContainer>
		<MapControlContainer position="top-right">
			<MapZoomControl />
		</MapControlContainer>
	</Map>

	<Card.Root class="min-h-0 gap-4 overflow-y-auto">
		<Card.Header>
			<Card.Title>{airports[hub].city} network</Card.Title>
			<Card.Description data-testid="network-summary">
				{legs.length} destinations · {numbers.format(weekly)} flights a week
			</Card.Description>
		</Card.Header>
		<Card.Content class="flex flex-col gap-4">
			<div class="rounded-md border p-3 text-sm" data-testid="leg-detail" aria-live="polite">
				{#if selected}
					<p class="font-medium">{hub} → {selected.code} · {selected.city}</p>
					<p class="text-muted-foreground">
						{numbers.format(selected.km)} km · {selected.weekly} flights a week
					</p>
				{:else}
					<p class="text-muted-foreground">
						Pick a route on the map or below. Longest is {numbers.format(longest)} km.
					</p>
				{/if}
			</div>
			<ol class="flex flex-col gap-1" aria-label="Routes from {hub}">
				{#each legs as leg (leg.code)}
					<li>
						<button
							type="button"
							class={[
								'flex w-full items-center justify-between gap-2 rounded-md px-2 py-1.5 text-left text-sm hover:bg-accent',
								leg.code === selectedCode && 'bg-accent'
							]}
							aria-pressed={leg.code === selectedCode}
							onclick={() => pickLeg(leg.code)}
							data-testid="leg"
						>
							<span><span class="font-medium">{leg.code}</span> {leg.city}</span>
							<span class="text-muted-foreground tabular-nums">{leg.weekly}/wk</span>
						</button>
					</li>
				{/each}
			</ol>
		</Card.Content>
	</Card.Root>
</div>
import type { LngLatTuple, MapViewport } from '$lib/components/ui/map/index.js';

export const VIEWPORT: MapViewport = {
	center: [15, 22],
	zoom: 1,
	bearing: 0,
	pitch: 0
};

/** Every airport the network can fly to, so a route can only name one of these. */
export type AirportCode =
	| 'LHR'
	| 'FRA'
	| 'DXB'
	| 'JFK'
	| 'ORD'
	| 'GRU'
	| 'JNB'
	| 'NBO'
	| 'DEL'
	| 'BOM'
	| 'SIN'
	| 'HND'
	| 'SYD'
	| 'IST'
	| 'MAD';

export interface Airport {
	code: AirportCode;
	city: string;
	lngLat: LngLatTuple;
}

export const AIRPORTS: Airport[] = [
	{ code: 'LHR', city: 'London', lngLat: [-0.4543, 51.47] },
	{ code: 'FRA', city: 'Frankfurt', lngLat: [8.5622, 50.0379] },
	{ code: 'DXB', city: 'Dubai', lngLat: [55.3644, 25.2532] },
	{ code: 'JFK', city: 'New York', lngLat: [-73.7781, 40.6413] },
	{ code: 'ORD', city: 'Chicago', lngLat: [-87.9073, 41.9742] },
	{ code: 'GRU', city: 'São Paulo', lngLat: [-46.4731, -23.4356] },
	{ code: 'JNB', city: 'Johannesburg', lngLat: [28.2425, -26.1392] },
	{ code: 'NBO', city: 'Nairobi', lngLat: [36.9278, -1.3192] },
	{ code: 'DEL', city: 'Delhi', lngLat: [77.1, 28.5562] },
	{ code: 'BOM', city: 'Mumbai', lngLat: [72.8656, 19.0896] },
	{ code: 'SIN', city: 'Singapore', lngLat: [103.9915, 1.3644] },
	{ code: 'HND', city: 'Tokyo', lngLat: [139.7798, 35.5494] },
	{ code: 'SYD', city: 'Sydney', lngLat: [151.1772, -33.9399] },
	{ code: 'IST', city: 'Istanbul', lngLat: [28.8146, 40.9769] },
	{ code: 'MAD', city: 'Madrid', lngLat: [-3.5676, 40.4983] }
];

/** Airports the airline runs its network from. */
export const HUBS = ['LHR', 'FRA', 'DXB'] as const satisfies readonly AirportCode[];

export type Hub = (typeof HUBS)[number];

export interface Route {
	from: Hub;
	to: AirportCode;
	/** Flights a week in each direction. */
	weekly: number;
}

export const ROUTES: Route[] = [
	{ from: 'LHR', to: 'JFK', weekly: 84 },
	{ from: 'LHR', to: 'ORD', weekly: 28 },
	{ from: 'LHR', to: 'GRU', weekly: 7 },
	{ from: 'LHR', to: 'JNB', weekly: 14 },
	{ from: 'LHR', to: 'DEL', weekly: 21 },
	{ from: 'LHR', to: 'SIN', weekly: 14 },
	{ from: 'LHR', to: 'MAD', weekly: 56 },
	{ from: 'LHR', to: 'IST', weekly: 28 },
	{ from: 'FRA', to: 'JFK', weekly: 35 },
	{ from: 'FRA', to: 'ORD', weekly: 21 },
	{ from: 'FRA', to: 'GRU', weekly: 7 },
	{ from: 'FRA', to: 'DEL', weekly: 14 },
	{ from: 'FRA', to: 'HND', weekly: 14 },
	{ from: 'FRA', to: 'IST', weekly: 42 },
	{ from: 'FRA', to: 'MAD', weekly: 35 },
	{ from: 'DXB', to: 'LHR', weekly: 42 },
	{ from: 'DXB', to: 'JNB', weekly: 21 },
	{ from: 'DXB', to: 'NBO', weekly: 21 },
	{ from: 'DXB', to: 'BOM', weekly: 56 },
	{ from: 'DXB', to: 'DEL', weekly: 49 },
	{ from: 'DXB', to: 'SIN', weekly: 28 },
	{ from: 'DXB', to: 'SYD', weekly: 14 },
	{ from: 'DXB', to: 'HND', weekly: 14 }
];

Handcrafted by Oggy. The source code is on GitHub.