Planet
A globe of slanted-pixel dots with focusable markers.
Demo
Usage
import { Planet } from '@tuttn/slant'
const regions = [
{ id: 'fra', label: 'Frankfurt', lat: 50.11, lon: 8.68 },
{ id: 'iad', label: 'Virginia', lat: 38.95, lon: -77.45, tone: 'warning' },
{ id: 'nrt', label: 'Tokyo', lat: 35.68, lon: 139.69 },
]
// Uncontrolled: the globe keeps its own rotation and selection.
<Planet markers={regions} defaultValue="fra" onValueChange={openRegion} label="Edge regions" />
// Controlled: you own the selection and where the globe faces.
const [region, setRegion] = useState<string | null>('fra')
const [rotation, setRotation] = useState({ lon: 10, lat: 20 })
<Planet markers={regions} value={region} onValueChange={setRegion}
rotation={rotation} onRotationChange={setRotation} spin={false} label="Edge regions" />
// Smaller and finer, holding still.
<Planet size={200} resolution={2} running={false} label="Where we ship" />Props
| Prop | Type | Default | Description |
|---|---|---|---|
markers | readonly PlanetMarker[] | [] | Places on the globe, each a button named by its label. A marker on the far side is hidden, so it leaves the tab order until the globe turns it round. |
rotation | { lon: number; lat: number } | — | The point at the centre of the globe, in degrees. Pass it to control the globe; leave it out to let the globe keep its own. The idle spin drifts the view without reporting it, so pass `spin={false}` to show exactly this. |
defaultRotation | { lon: number; lat: number } | { lon: 10, lat: 20 } | Where an uncontrolled globe starts, and where Home turns it back to. It is also the resting frame under reduced motion. |
onRotationChange | (rotation: { lon: number; lat: number }) => void | — | Called with the new rotation when the arrow keys, Home, a drag or a focused marker turns the globe. |
value | string | null | — | The selected marker's id, or null for none. Pass it to control the selection. |
defaultValue | string | null | null | The marker an uncontrolled globe starts with selected. |
onValueChange | (id: string) => void | — | Called with a marker's id when a click, Enter or Space selects it. |
spin | boolean | true | Turns the globe slowly (6° a second) while it runs, unless someone is dragging it, hovering a marker or has focus inside it. |
resolution | number | 3 | Degrees between rows of dots, from 1 to 10. Smaller draws a finer globe. |
size | number | 320 | The diameter in pixels. The globe also shrinks to fit its container. |
label | string | messages.globe | The globe's accessible name. |
running | boolean | true | False holds the spin, and turns jump instead of gliding. Off screen, in a hidden tab or under reduced motion the globe holds anyway. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root, the globe group. |
Keyboard
The globe is one tab stop. Left and Right turn it 15° of longitude (swapped under RTL), Up and Down 10° of latitude, held within 70° of the equator, and Home turns it back to defaultRotation. Tab then moves through the markers facing you: focusing one turns the globe to face it, and Enter, Space or a click selects it. Markers on the far side are hidden, so they leave the tab order until the globe turns them round. A drag turns it too; a drag that ends on a marker does not select it.
Accessibility
The root is a role="group" with aria-roledescription="globe", named by label and described by a hidden hint (messages.globe and messages.globeHint, so a root's messages translates both). Each marker is a button named by its label, with aria-pressed on the selected one; the tooltip that shows the hovered, focused or selected marker's label repeats that name and is hidden from screen readers. The dots are a decorative canvas.
Motion
With spin, the globe turns 6° a second while nobody drags it, hovers a marker or has focus inside it. The spin drifts the view without reporting it; turns from the keys, a drag or a focused marker go through onRotationChange, and glide over the slowest motion token. Off screen, in a hidden tab or with running={false} no frame is drawn at all, and turns jump. Under reduced motion there is no spin and no gliding: the globe rests at defaultRotation, and the keys and a drag move it in steps.
Drawing
Dots sit on rows resolution degrees apart, each row's count following the cosine of its latitude so the poles don't bunch, projected orthographically and drawn as slanted pixels on a canvas sized for the screen's pixel ratio: land in --sl-accent, sea smaller in --sl-line, both fading toward the limb. The colours are read from the tokens and read again when the theme changes. The drawing never mirrors: under dir="rtl" the map keeps its orientation and only the arrow keys swap.
The land comes from Natural Earth's 1:110m land polygons (public domain), packed into a 2° bitmask of about 2 KB by scripts/build_land_mask.mjs. For a field of pixels that follows the pointer instead, see Pixel field.