Display
World map
Where your numbers come from: each country shaded by its value in five steps of ink, with a tooltip, a click to pick a country, pinch and drag to zoom, and soft pulsing dots for what's happening now.
Installation
npx sinaui add world-mapCopy src/lib/ui/WorldMap.svelte, world-map-shapes.ts, Button, Icon and tokens.css. Needs @hugeicons/core-free-icons.
The country outlines are Natural Earth's (public domain), projected ahead of time into plain SVG paths: about 40 KB gzipped, loaded the first time a map is on screen, so pages without one don't pay for them. Countries too small to draw at this scale, such as Singapore and Bahrain, show as a dot when they have data.
Examples
Pick a country
Hover or focus a country for its name, value and share. Clicking one calls onselect; pass the code back as selected to outline it.
Readers of the House of Wisdom catalog in the last 30 days.
| Country | Readers | Share |
|---|---|---|
| Iraq | 4,210 | 25.7% |
| Egypt | 3,105 | 18.9% |
| Spain | 2,380 | 14.5% |
| Iran | 1,940 | 11.8% |
| Morocco | 1,210 | 7.4% |
| Syria | 980 | 6% |
| Uzbekistan | 760 | 4.6% |
| Türkiye | 690 | 4.2% |
| Tunisia | 410 | 2.5% |
| India | 380 | 2.3% |
| Saudi Arabia | 240 | 1.5% |
| Bahrain | 64 | 0.4% |
| Singapore | 41 | 0.2% |
Pick a country to filter by it.
<script lang="ts">
import WorldMap from '#lib/ui/WorldMap.svelte';
const readers = [
{ code: 'IQ', value: 4210 },
{ code: 'EG', value: 3105 },
{ code: 'ES', value: 2380 },
{ code: 'IR', value: 1940 },
{ code: 'MA', value: 1210 },
{ code: 'SY', value: 980 },
{ code: 'UZ', value: 760 },
{ code: 'TR', value: 690 },
{ code: 'TN', value: 410 },
{ code: 'IN', value: 380 },
{ code: 'SA', value: 240 },
{ code: 'BH', value: 64 },
{ code: 'SG', value: 41 }
];
let picked = $state<string | null>(null);
const names = new Intl.DisplayNames(['en-US'], { type: 'region' });
</script>
<WorldMap
data={readers}
title="Library readers by country"
description="Readers of the House of Wisdom catalog in the last 30 days."
valueLabel="Readers"
selected={picked}
onselect={(code) => (picked = picked === code ? null : code)}
/>
<p class="note">
{picked ? `Showing readers from ${names.of(picked)}.` : 'Pick a country to filter by it.'}
</p>
<style>
.note {
margin: 0.75rem 0 0;
color: var(--ui-muted);
font-size: 0.875rem;
}
</style>Happening now
Pings pulse softly over the countries where something is happening now, and new ones pop in. Reduced motion keeps them as still dots.
| Country | Readers | Share |
|---|---|---|
| Iraq | 820 | 35.7% |
| Egypt | 610 | 26.5% |
| Spain | 450 | 19.6% |
| Morocco | 300 | 13% |
| Uzbekistan | 120 | 5.2% |
<script lang="ts">
import WorldMap from '#lib/ui/WorldMap.svelte';
const today = [
{ code: 'IQ', value: 820 },
{ code: 'EG', value: 610 },
{ code: 'ES', value: 450 },
{ code: 'MA', value: 300 },
{ code: 'UZ', value: 120 }
];
// Who is reading right now; somewhere a new reader pops in every few seconds.
const places = ['IQ', 'EG', 'ES', 'MA', 'UZ', 'IR', 'SY', 'TR'];
let now = $state([
{ code: 'IQ', value: 6 },
{ code: 'EG', value: 3 }
]);
$effect(() => {
const timer = setInterval(() => {
const code = places[Math.floor(Math.random() * places.length)];
const rest = now.filter((p) => p.code !== code).slice(-4);
now = [...rest, { code, value: 1 + Math.floor(Math.random() * 6) }];
}, 3000);
return () => clearInterval(timer);
});
</script>
<WorldMap data={today} pings={now} title="Readers today, and reading now" valueLabel="Readers" />A rate
For a rate or a score, format the value your way and turn share off.
| Country | Read to the end |
|---|---|
| Spain | 71% |
| Iran | 66% |
| Iraq | 62% |
| Egypt | 55% |
| Morocco | 48% |
| Uzbekistan | 39% |
<script lang="ts">
import WorldMap from '#lib/ui/WorldMap.svelte';
// A rate, not a count: no share of the total, and values shown as percentages.
const finished = [
{ code: 'IQ', value: 0.62 },
{ code: 'EG', value: 0.55 },
{ code: 'ES', value: 0.71 },
{ code: 'MA', value: 0.48 },
{ code: 'IR', value: 0.66 },
{ code: 'UZ', value: 0.39 }
];
const percent = new Intl.NumberFormat('en-US', { style: 'percent' }).format;
</script>
<WorldMap
data={finished}
title="Books read to the end, by country"
valueLabel="Read to the end"
format={percent}
share={false}
/>Props
| Prop | Type | Default |
|---|---|---|
data | { code, value, label? }[] (required); code is ISO 3166-1 alpha-2,
and anything else (an "Other" row) is left out | |
title | string (required): read as the map's name | |
description | string | |
valueLabel | string: the value column in the table screen readers get | 'Value' |
format | (value: number) => string | whole numbers, en-US |
share | boolean: each country's share of the total | true |
onselect | (code: string) => void | |
selected | string | null: outlined | null |
pings | { code, value? }[]: pulsing dots, sized by value | [] |
empty | string: shown when there's no data | 'Nothing to show yet.' |
Interaction
- Mouse: hover for a tooltip, click to select, double-click to zoom in (Shift to zoom out), drag to pan once zoomed, and Ctrl or ⌘ with the wheel (or a trackpad pinch) to zoom.
- Touch: tap to select, pinch to zoom, and drag to pan once zoomed; until then a finger scrolls the page as usual.
- The buttons in the corner zoom in and out, and show the whole map again.
Accessibility
- Countries with data are one tab stop: arrow keys move between them from the largest value down, Home and End jump to either end, Enter or Space selects, plus and minus zoom, and 0 shows the whole map. Focus shows the tooltip, and a country off screen slides into view.
- Each country is named with its value and share, and a visually hidden table lists them all for screen readers.
- Shading is never the only cue: the tooltip and the table carry every number. Reduced motion turns off the fill-in, the pulses and the zoom glide.