@zsherman/viz 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/LICENSE +373 -0
  3. package/README.md +13 -2
  4. package/THIRD_PARTY_NOTICES.md +79 -0
  5. package/dist/dom/flow/flow.js +370 -0
  6. package/dist/dom/flow/flow.styles.js +158 -0
  7. package/dist/dom/flow/index.js +4 -0
  8. package/dist/dom/flow/layout.js +51 -0
  9. package/dist/dom/geo-map/geo-map.js +1037 -0
  10. package/dist/dom/geo-map/geo-map.styles.js +184 -0
  11. package/dist/dom/geo-map/geometry.js +231 -0
  12. package/dist/dom/geo-map/index.js +3 -0
  13. package/dist/dom/geo-map/types.js +0 -0
  14. package/dist/dom/geo-map/world.js +15 -0
  15. package/dist/dom/index.js +6 -0
  16. package/dist/solid/flow/flow.jsx +180 -0
  17. package/dist/solid/flow/flow.styles.jsx +158 -0
  18. package/dist/solid/flow/index.jsx +4 -0
  19. package/dist/solid/flow/layout.jsx +51 -0
  20. package/dist/solid/geo-map/geo-map.jsx +698 -0
  21. package/dist/solid/geo-map/geo-map.styles.jsx +184 -0
  22. package/dist/solid/geo-map/geometry.jsx +231 -0
  23. package/dist/solid/geo-map/index.jsx +3 -0
  24. package/dist/solid/geo-map/types.jsx +0 -0
  25. package/dist/solid/geo-map/world.jsx +15 -0
  26. package/dist/solid/index.jsx +6 -0
  27. package/dist/styles.css +1 -0
  28. package/dist/types/flow/flow.d.ts +92 -0
  29. package/dist/types/flow/flow.styles.d.ts +137 -0
  30. package/dist/types/flow/index.d.ts +6 -0
  31. package/dist/types/flow/layout.d.ts +47 -0
  32. package/dist/types/geo-map/geo-map.d.ts +70 -0
  33. package/dist/types/geo-map/geo-map.styles.d.ts +188 -0
  34. package/dist/types/geo-map/geometry.d.ts +116 -0
  35. package/dist/types/geo-map/index.d.ts +4 -0
  36. package/dist/types/geo-map/types.d.ts +103 -0
  37. package/dist/types/geo-map/world.d.ts +11 -0
  38. package/dist/types/index.d.ts +2 -0
  39. package/docs/api.json +297 -0
  40. package/docs/flow.md +255 -0
  41. package/docs/geo-map.md +320 -0
  42. package/docs/llms.txt +43 -0
  43. package/docs/search-index.json +64 -0
  44. package/package.json +66 -4
@@ -0,0 +1,698 @@
1
+ import { boundsOf, clampFlat, facesViewer, fitFlat, fittedProjection, globeArc, globeFit, globeTransition, rotationFacing, screenArc } from "./geometry.jsx";
2
+ import { styles } from "./geo-map.styles.jsx";
3
+ import { loadWorld } from "./world.jsx";
4
+ import * as stylex from "@stylexjs/stylex";
5
+ import { Icon } from "@zsherman/kit/icon";
6
+ import { Minus, Plus, Scan } from "@zsherman/kit/icons";
7
+ import { useMessages } from "@zsherman/kit/locale-provider";
8
+ import { For, Show, createEffect, createMemo, createSignal, createUniqueId, omit, onSettled } from "solid-js";
9
+ import { IconButton } from "@zsherman/kit/button";
10
+ import { announce, createRovingFocus, createViewport, spatialMove } from "@ui-sdk/core";
11
+ import { geoGraticule10, geoPath } from "d3-geo";
12
+ // src/geo-map/geo-map.tsx
13
+ /**
14
+ * GeoMap: a world map of markers and the connections between them, flat or a globe. A Solid rewrite of
15
+ * hyper-ui's GeoMap and ConnectionMap (one component for both): Solid renders the SVG and the markers,
16
+ * d3-geo only projects (geometry.ts). Pan and zoom on a flat map are ui-sdk's `createViewport`; a globe
17
+ * turns with a drag (geometry.ts `globeTransition`, after hyper-ui's map-mode reducer). The markers are a
18
+ * listbox with one tab stop (ui-sdk `createRovingFocus`, arrows to the nearest marker on screen), and the
19
+ * kit's Tooltip names what the pointer or the focus is on: a chunk of its own (the popup core is most of a
20
+ * tooltip's weight), loaded when the browser is idle after the map mounts, or at the first hover or focus.
21
+ */
22
+ const DEFAULT_LABELS = {
23
+ map: "map",
24
+ markers: "Locations",
25
+ zoomIn: "Zoom in",
26
+ zoomOut: "Zoom out",
27
+ fit: "Show all",
28
+ zoom: (percent) => `Zoom ${percent}%`,
29
+ connections: (count) => count === 1 ? "1 connection" : `${count} connections`
30
+ };
31
+ const TONES = /* @__PURE__ */ new Set([
32
+ "accent",
33
+ "info",
34
+ "success",
35
+ "warning",
36
+ "danger"
37
+ ]);
38
+ const CHART = /^chart-[1-8]$/;
39
+ /** A marker or connection color as CSS: token names become their custom properties. */
40
+ function geoMapColor(c) {
41
+ if (CHART.test(c)) return `var(--sui-${c})`;
42
+ if (TONES.has(c)) return `var(--sui-${c})`;
43
+ if (c === "neutral") return "var(--sui-text-muted)";
44
+ return c;
45
+ }
46
+ const DEFAULT_RADIUS = 5;
47
+ const DEFAULT_WEIGHT = 1.5;
48
+ /** The size the map lays out for before it's measured (on the server, the first render). */
49
+ const FALLBACK_WIDTH = 960;
50
+ const ZOOM_STEP = 1.5;
51
+ const MOTION_MS = 450;
52
+ const [tooltipKit, setTooltipKit] = createSignal(null);
53
+ let tooltipLoading;
54
+ /** Loads the kit's Tooltip once per page; every map shares it. */
55
+ function loadTooltip() {
56
+ tooltipLoading ??= import("@zsherman/kit/tooltip").then((kit) => void setTooltipKit(() => kit), () => {
57
+ tooltipLoading = void 0;
58
+ });
59
+ }
60
+ const reducedMotion = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
61
+ /** A store that ui-sdk's helpers read and write, backed by plain values and a signal for rendering. */
62
+ function createLocalStore(initial, onSet) {
63
+ const state = { ...initial };
64
+ const listeners = /* @__PURE__ */ new Set();
65
+ return {
66
+ get: () => state,
67
+ set: (patch) => {
68
+ if (!Object.keys(patch).some((k) => !Object.is(state[k], patch[k]))) return;
69
+ Object.assign(state, patch);
70
+ onSet(state);
71
+ for (const listener of listeners) listener();
72
+ },
73
+ subscribe: (listener) => {
74
+ listeners.add(listener);
75
+ return () => void listeners.delete(listener);
76
+ }
77
+ };
78
+ }
79
+ /**
80
+ * A world map of markers and the connections between them: flat (pan and zoom) or a globe (turn and zoom).
81
+ * Markers are a listbox: Tab reaches the map's markers, arrows move to the nearest one on screen, Enter
82
+ * selects; + and − zoom. Land comes from Natural Earth (loaded on demand) unless `worldData` is given.
83
+ *
84
+ * @example
85
+ * <GeoMap
86
+ * label="Edge servers and the users on them"
87
+ * markers={[{ id: "fra", name: "Frankfurt", latitude: 50.1, longitude: 8.7, group: "server" }]}
88
+ * connections={[{ source: "u1", target: "fra" }]}
89
+ * />
90
+ */
91
+ function GeoMap(props) {
92
+ const rest = omit(props, "label", "markers", "connections", "projection", "arcStyle", "graticule", "worldData", "center", "initialView", "interactive", "controls", "height", "selected", "defaultSelected", "onMarkerSelect", "onConnectionSelect", "highlighted", "animated", "tooltip", "labels", "controller", "class", "style");
93
+ const messages = useMessages("geoMap");
94
+ const label = (key) => props.labels?.[key] ?? messages()?.[key] ?? DEFAULT_LABELS[key];
95
+ const uid = createUniqueId();
96
+ let root;
97
+ const markerEls = /* @__PURE__ */ new Map();
98
+ const globe = () => props.projection === "orthographic";
99
+ const interactive = () => props.interactive !== false;
100
+ const markers = () => props.markers ?? [];
101
+ const [measured, setMeasured] = createSignal(null);
102
+ const box = createMemo(() => {
103
+ const m = measured();
104
+ if (m) return m;
105
+ const h = typeof props.height === "number" ? props.height : FALLBACK_WIDTH / 2;
106
+ return {
107
+ w: typeof props.height === "number" ? h * 2 : FALLBACK_WIDTH,
108
+ h
109
+ };
110
+ }, { equals: (a, b) => a.w === b.w && a.h === b.h });
111
+ const [loaded, setLoaded] = createSignal(null);
112
+ const world = () => props.worldData ?? loaded();
113
+ createEffect(() => props.worldData === void 0, (need) => {
114
+ if (need) loadWorld().then(setLoaded, () => void 0);
115
+ });
116
+ const lonLats = () => markers().map((m) => [m.longitude, m.latitude]);
117
+ const globeDefault = createMemo(() => {
118
+ const fitted = props.initialView === "markers" ? globeFit(lonLats()) : null;
119
+ const center = props.center ?? [10, 25];
120
+ return fitted ? {
121
+ ...fitted,
122
+ drag: null
123
+ } : {
124
+ rotation: rotationFacing(center),
125
+ scale: 1,
126
+ drag: null
127
+ };
128
+ });
129
+ let globeNow = null;
130
+ const [globeMoved, setGlobeMoved] = createSignal(null, { ownedWrite: true });
131
+ const globeView = () => globeMoved() ?? globeDefault();
132
+ const setGlobe = (next) => {
133
+ globeNow = next;
134
+ setGlobeMoved(next);
135
+ };
136
+ const dispatchGlobe = (event) => {
137
+ const now = globeNow ?? globeDefault();
138
+ const next = globeTransition(now, event);
139
+ if (next !== now) setGlobe(next);
140
+ };
141
+ /** Projections for the flat map (fixed: pans and zooms are a transform) and for a globe (turns with it). */
142
+ const projection = createMemo(() => {
143
+ const { w, h } = box();
144
+ if (globe()) {
145
+ const v = globeView();
146
+ return fittedProjection("orthographic", w, h, v.rotation, v.scale);
147
+ }
148
+ return fittedProjection(props.projection ?? "naturalEarth", w, h);
149
+ });
150
+ const path = createMemo(() => geoPath(projection()));
151
+ const sphere = createMemo(() => path()({ type: "Sphere" }) ?? "");
152
+ const land = createMemo(() => {
153
+ const w = world();
154
+ return w ? path()(w) ?? "" : "";
155
+ });
156
+ const graticule = createMemo(() => props.graticule ? path()(geoGraticule10()) ?? "" : "");
157
+ const placed = createMemo(() => {
158
+ const p = projection();
159
+ const rotation = globe() ? p.rotate() : null;
160
+ const groups = /* @__PURE__ */ new Map();
161
+ return markers().map((marker) => {
162
+ const lonLat = [marker.longitude, marker.latitude];
163
+ const at = p(lonLat) ?? [NaN, NaN];
164
+ let tone = 1;
165
+ if (marker.group !== void 0) {
166
+ if (!groups.has(marker.group)) groups.set(marker.group, groups.size);
167
+ tone = groups.get(marker.group) % 8 + 1;
168
+ }
169
+ return {
170
+ marker,
171
+ at,
172
+ shown: rotation ? facesViewer(lonLat, rotation) : Number.isFinite(at[0]),
173
+ color: geoMapColor(marker.color ?? `chart-${tone}`)
174
+ };
175
+ });
176
+ });
177
+ const byId = createMemo(() => new Map(placed().map((p) => [p.marker.id, p])));
178
+ /** The markers' ids, in order: the list renders one option per id and reads the rest through `byId`. */
179
+ const ids = createMemo(() => markers().map((m) => m.id), { equals: (a, b) => a.length === b.length && a.every((id, i) => id === b[i]) });
180
+ const links = createMemo(() => {
181
+ const out = /* @__PURE__ */ new Map();
182
+ const ids = new Set(markers().map((m) => m.id));
183
+ for (const c of props.connections ?? []) {
184
+ if (!ids.has(c.source) || !ids.has(c.target)) continue;
185
+ if (!out.has(c.source)) out.set(c.source, /* @__PURE__ */ new Set());
186
+ if (!out.has(c.target)) out.set(c.target, /* @__PURE__ */ new Set());
187
+ out.get(c.source).add(c.target);
188
+ out.get(c.target).add(c.source);
189
+ }
190
+ return out;
191
+ });
192
+ const hasConnections = () => (props.connections?.length ?? 0) > 0;
193
+ const drawn = createMemo(() => {
194
+ const p = projection();
195
+ const ids = byId();
196
+ const style = props.arcStyle ?? "greatCircle";
197
+ const out = [];
198
+ (props.connections ?? []).forEach((connection, index) => {
199
+ const a = ids.get(connection.source);
200
+ const b = ids.get(connection.target);
201
+ if (!a || !b) return;
202
+ let d;
203
+ if (globe()) {
204
+ if (style === "greatCircle") d = globeArc(p, [a.marker.longitude, a.marker.latitude], [b.marker.longitude, b.marker.latitude]);
205
+ else d = a.shown && b.shown ? screenArc(style, a.at, b.at) : "";
206
+ } else d = screenArc(style, a.at, b.at);
207
+ if (!d) return;
208
+ out.push({
209
+ connection,
210
+ index,
211
+ d,
212
+ color: connection.color ? geoMapColor(connection.color) : a.color
213
+ });
214
+ });
215
+ return out;
216
+ });
217
+ const flatDefault = createMemo(() => {
218
+ const { w, h } = box();
219
+ if (globe() || props.initialView !== "markers") return {
220
+ x: 0,
221
+ y: 0,
222
+ zoom: 1
223
+ };
224
+ const rect = boundsOf(placed().filter((m) => m.shown).map((m) => m.at));
225
+ return rect ? fitFlat(rect, w, h) : {
226
+ x: 0,
227
+ y: 0,
228
+ zoom: 1
229
+ };
230
+ }, { equals: (a, b) => a.x === b.x && a.y === b.y && a.zoom === b.zoom });
231
+ let flatNow = null;
232
+ const [flatMoved, setFlatMoved] = createSignal(null, { ownedWrite: true });
233
+ const flat = () => flatMoved() ?? flatDefault();
234
+ const setFlat = (next) => {
235
+ const { w, h } = box();
236
+ flatNow = clampFlat(next, w, h);
237
+ setFlatMoved(flatNow);
238
+ };
239
+ const viewport = createViewport({
240
+ store: {
241
+ get: () => ({ viewport: flatNow ?? flatDefault() }),
242
+ set: () => {}
243
+ },
244
+ onDestroy: (cleanup) => onSettled(() => cleanup)
245
+ }, {
246
+ key: "viewport",
247
+ surface: () => root,
248
+ minZoom: () => 1,
249
+ maxZoom: () => 12,
250
+ message: (zoom) => label("zoom")(Math.round(zoom * 100)),
251
+ set: (value) => {
252
+ setFlat(value);
253
+ return true;
254
+ }
255
+ });
256
+ let tween = 0;
257
+ const turnTo = (rotation, scale) => {
258
+ cancelAnimationFrame(tween);
259
+ const from = globeNow ?? globeDefault();
260
+ const to = {
261
+ rotation,
262
+ scale: scale ?? from.scale,
263
+ drag: null
264
+ };
265
+ if (reducedMotion() || typeof requestAnimationFrame !== "function") return setGlobe(to);
266
+ const dLon = ((to.rotation[0] - from.rotation[0]) % 360 + 540) % 360 - 180;
267
+ const start = performance.now();
268
+ const step = (now) => {
269
+ const t = Math.min(1, (now - start) / MOTION_MS);
270
+ const e = 1 - (1 - t) ** 3;
271
+ setGlobe({
272
+ rotation: [
273
+ from.rotation[0] + dLon * e,
274
+ from.rotation[1] + (to.rotation[1] - from.rotation[1]) * e,
275
+ from.rotation[2]
276
+ ],
277
+ scale: from.scale + (to.scale - from.scale) * e,
278
+ drag: null
279
+ });
280
+ if (t < 1) tween = requestAnimationFrame(step);
281
+ };
282
+ tween = requestAnimationFrame(step);
283
+ };
284
+ const fit = (target = "markers") => {
285
+ const chosen = target === "world" ? [] : target === "markers" ? placed() : placed().filter((p) => target.includes(p.marker.id));
286
+ if (globe()) {
287
+ const fitted = globeFit(chosen.map((p) => [p.marker.longitude, p.marker.latitude]));
288
+ if (fitted) turnTo(fitted.rotation, fitted.scale);
289
+ else turnTo((globeNow ?? globeDefault()).rotation, 1);
290
+ return;
291
+ }
292
+ const { w, h } = box();
293
+ const rect = boundsOf(chosen.filter((p) => p.shown).map((p) => p.at));
294
+ setFlat(rect ? fitFlat(rect, w, h) : {
295
+ x: 0,
296
+ y: 0,
297
+ zoom: 1
298
+ });
299
+ };
300
+ const zoomBy = (factor) => {
301
+ if (globe()) {
302
+ dispatchGlobe({
303
+ type: "scaleBy",
304
+ factor
305
+ });
306
+ announce(label("zoom")(Math.round((globeNow ?? globeDefault()).scale * 100)));
307
+ } else viewport.zoomBy(factor);
308
+ };
309
+ const centerOn = (target) => {
310
+ const lonLat = typeof target === "string" ? (() => {
311
+ const m = byId().get(target)?.marker;
312
+ return m ? [m.longitude, m.latitude] : void 0;
313
+ })() : [target[0], target[1]];
314
+ if (!lonLat) return;
315
+ if (globe()) return turnTo(rotationFacing(lonLat, (globeNow ?? globeDefault()).rotation[2]));
316
+ const at = projection()(lonLat);
317
+ if (!at) return;
318
+ const { w, h } = box();
319
+ const { zoom } = flat();
320
+ setFlat({
321
+ zoom,
322
+ x: w / 2 - at[0] * zoom,
323
+ y: h / 2 - at[1] * zoom
324
+ });
325
+ };
326
+ const view = () => {
327
+ if (globe()) {
328
+ const v = globeNow ?? globeDefault();
329
+ return {
330
+ zoom: v.scale,
331
+ center: [-v.rotation[0], -v.rotation[1]]
332
+ };
333
+ }
334
+ const { w, h } = box();
335
+ const v = flat();
336
+ const inverted = projection().invert?.([(w / 2 - v.x) / v.zoom, (h / 2 - v.y) / v.zoom]);
337
+ return {
338
+ zoom: v.zoom,
339
+ center: inverted ? [inverted[0], inverted[1]] : [0, 0]
340
+ };
341
+ };
342
+ const [ownSelected, setOwnSelected] = createSignal(null);
343
+ const [selectionTouched, setSelectionTouched] = createSignal(false);
344
+ const selected = () => props.selected !== void 0 ? props.selected : selectionTouched() ? ownSelected() : props.defaultSelected ?? null;
345
+ const select = (id) => {
346
+ const next = selected() === id ? null : id;
347
+ setOwnSelected(next);
348
+ setSelectionTouched(true);
349
+ props.onMarkerSelect?.(next === null ? null : byId().get(id)?.marker ?? null);
350
+ };
351
+ const [hovered, setHovered] = createSignal(null);
352
+ const [focused, setFocused] = createSignal(null);
353
+ const active = () => props.highlighted !== void 0 ? props.highlighted : (() => {
354
+ const h = hovered();
355
+ if (h?.kind === "marker") return h.id;
356
+ return focused()?.id ?? null;
357
+ })();
358
+ const dimmedMarker = (id) => {
359
+ const a = active();
360
+ return a !== null && a !== id && !links().get(a)?.has(id);
361
+ };
362
+ const connectionState = (c) => {
363
+ const a = active();
364
+ if (a === null) return "rest";
365
+ return c.source === a || c.target === a ? "active" : "dimmed";
366
+ };
367
+ const tip = () => {
368
+ const h = hovered();
369
+ if (h) return h;
370
+ const f = focused();
371
+ return f?.visible ? {
372
+ kind: "marker",
373
+ id: f.id,
374
+ el: f.el
375
+ } : null;
376
+ };
377
+ const tipMarker = () => {
378
+ const t = tip();
379
+ return t?.kind === "marker" ? byId().get(t.id) : void 0;
380
+ };
381
+ const tipConnection = () => {
382
+ const t = tip();
383
+ return t?.kind === "connection" ? props.connections?.[t.index] : void 0;
384
+ };
385
+ const tipAnchor = () => {
386
+ const t = tip();
387
+ if (!t) return null;
388
+ return t.kind === "marker" ? t.el : {
389
+ x: t.x,
390
+ y: t.y
391
+ };
392
+ };
393
+ const markerName = (m) => hasConnections() ? `${m.name}, ${label("connections")(links().get(m.id)?.size ?? 0)}` : m.name;
394
+ const [stop, setStop] = createSignal(null, { ownedWrite: true });
395
+ const rovingStore = createLocalStore({ focused: null }, (s) => setStop(s.focused));
396
+ const spatial = spatialMove({ rect: (_el, key) => {
397
+ const p = byId().get(key);
398
+ const [x, y] = p ? p.at : [0, 0];
399
+ return {
400
+ left: x,
401
+ right: x,
402
+ top: y,
403
+ bottom: y
404
+ };
405
+ } });
406
+ const listMove = (event, { from, items }) => {
407
+ const i = items.findIndex((item) => item.key === from.key);
408
+ switch (event.key) {
409
+ case "ArrowRight":
410
+ case "ArrowDown": return items[Math.min(items.length - 1, i + 1)]?.key;
411
+ case "ArrowLeft":
412
+ case "ArrowUp": return items[Math.max(0, i - 1)]?.key;
413
+ case "Home": return items[0]?.key;
414
+ case "End": return items.at(-1)?.key;
415
+ default: return;
416
+ }
417
+ };
418
+ const roving = createRovingFocus({
419
+ store: rovingStore,
420
+ onDestroy: (cleanup) => onSettled(() => cleanup)
421
+ }, {
422
+ slot: "marker",
423
+ key: "focused",
424
+ preferred: (item) => item.key === selected(),
425
+ move: (event, context) => globe() ? listMove(event, context) : spatial(event, context)
426
+ });
427
+ const onMarkerFocus = (id, el) => {
428
+ setFocused({
429
+ id,
430
+ el,
431
+ visible: el.matches(":focus-visible")
432
+ });
433
+ const p = byId().get(id);
434
+ if (!p) return;
435
+ if (globe()) {
436
+ if (!p.shown) turnTo(rotationFacing([p.marker.longitude, p.marker.latitude], globeView().rotation[2]));
437
+ } else {
438
+ const [x, y] = p.at;
439
+ viewport.reveal({
440
+ left: x,
441
+ right: x,
442
+ top: y,
443
+ bottom: y
444
+ }, { padding: 40 });
445
+ }
446
+ };
447
+ const onPart = (target) => target instanceof Element && !!target.closest("[data-slot=\"geo-map-marker\"], [data-slot=\"geo-map-controls\"], [data-hit]");
448
+ const onPointerDown = (event) => {
449
+ if (!interactive() || event.button !== 0 || onPart(event.target)) return;
450
+ if (!globe()) return viewport.pointerdown(event);
451
+ cancelAnimationFrame(tween);
452
+ root?.setPointerCapture(event.pointerId);
453
+ dispatchGlobe({
454
+ type: "dragStart",
455
+ x: event.clientX,
456
+ y: event.clientY
457
+ });
458
+ };
459
+ const onPointerMove = (event) => {
460
+ if (globe() && globeNow?.drag) dispatchGlobe({
461
+ type: "dragMove",
462
+ x: event.clientX,
463
+ y: event.clientY
464
+ });
465
+ };
466
+ const onPointerUp = () => {
467
+ if (globe()) dispatchGlobe({ type: "dragEnd" });
468
+ };
469
+ const onWheel = (event) => {
470
+ if (!interactive()) return;
471
+ const zoomIntent = event.ctrlKey || event.metaKey;
472
+ if (globe()) {
473
+ if (!zoomIntent) return;
474
+ event.preventDefault();
475
+ dispatchGlobe({
476
+ type: "scaleBy",
477
+ factor: Math.exp(-event.deltaY / 200)
478
+ });
479
+ return;
480
+ }
481
+ if (zoomIntent || flat().zoom > 1) viewport.wheel(event);
482
+ };
483
+ const onKeyDown = (event) => {
484
+ if (!interactive() || event.defaultPrevented) return;
485
+ if (!globe()) {
486
+ viewport.keydown(event);
487
+ return;
488
+ }
489
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
490
+ if (event.key === "+" || event.key === "=") zoomBy(ZOOM_STEP);
491
+ else if (event.key === "-" || event.key === "_") zoomBy(1 / ZOOM_STEP);
492
+ else if (event.key === "0") dispatchGlobe({
493
+ type: "scaleTo",
494
+ scale: 1
495
+ });
496
+ else return;
497
+ event.preventDefault();
498
+ };
499
+ onSettled(() => {
500
+ const el = root;
501
+ const observer = new ResizeObserver(() => {
502
+ const w = el.clientWidth;
503
+ const h = el.clientHeight;
504
+ if (w <= 0 || h <= 0) return;
505
+ const was = measured();
506
+ setMeasured({
507
+ w,
508
+ h
509
+ });
510
+ if (was && flatNow) {
511
+ flatNow = clampFlat({
512
+ zoom: flatNow.zoom,
513
+ x: flatNow.x * w / was.w,
514
+ y: flatNow.y * h / was.h
515
+ }, w, h);
516
+ setFlatMoved(flatNow);
517
+ }
518
+ });
519
+ observer.observe(el);
520
+ const idle = typeof requestIdleCallback === "function" ? requestIdleCallback(loadTooltip) : setTimeout(loadTooltip, 200);
521
+ props.controller?.({
522
+ fit,
523
+ zoomBy,
524
+ centerOn,
525
+ focus: (id) => roving.focus(id),
526
+ view
527
+ });
528
+ return () => {
529
+ observer.disconnect();
530
+ cancelAnimationFrame(tween);
531
+ if (typeof cancelIdleCallback === "function") cancelIdleCallback(idle);
532
+ else clearTimeout(idle);
533
+ };
534
+ });
535
+ const pct = (value, of) => `${value / of * 100}%`;
536
+ const worldTransform = () => {
537
+ if (globe()) return void 0;
538
+ const v = flat();
539
+ return v.zoom === 1 && v.x === 0 && v.y === 0 ? void 0 : `translate(${v.x} ${v.y}) scale(${v.zoom})`;
540
+ };
541
+ const markerLeft = (at) => {
542
+ const { w } = box();
543
+ if (globe()) return pct(at[0], w);
544
+ const v = flat();
545
+ return pct(v.x + at[0] * v.zoom, w);
546
+ };
547
+ const markerTop = (at) => {
548
+ const { h } = box();
549
+ if (globe()) return pct(at[1], h);
550
+ const v = flat();
551
+ return pct(v.y + at[1] * v.zoom, h);
552
+ };
553
+ const descriptionId = (id) => `${uid}-d-${id.replace(/[^\w-]/g, "_")}`;
554
+ const rootStyle = () => {
555
+ const own = {};
556
+ if (props.height !== void 0) own.height = typeof props.height === "number" ? `${props.height}px` : props.height;
557
+ return typeof props.style === "object" && props.style ? {
558
+ ...own,
559
+ ...props.style
560
+ } : own;
561
+ };
562
+ return <div {...rest} ref={(el) => root = el} role="group" aria-roledescription={label("map")} aria-label={props.label} data-slot="geo-map" data-projection={props.projection ?? "naturalEarth"} data-loading={world() ? void 0 : ""} class={[stylex.props(styles.root, props.height === void 0 && styles.aspect, props.interactive !== false && styles.interactive, props.projection === "orthographic" && styles.globe).className, props.class]} style={rootStyle()} onPointerEnter={loadTooltip} onFocusIn={loadTooltip} onPointerDown={onPointerDown} onPointerMove={onPointerMove} onPointerUp={onPointerUp} onPointerCancel={onPointerUp} onWheel={onWheel} onKeyDown={onKeyDown}>
563
+ <svg data-slot="geo-map-base" aria-hidden="true" viewBox={`0 0 ${box().w} ${box().h}`} preserveAspectRatio="none" {...stylex.attrs(styles.svg)}>
564
+ <g transform={worldTransform()}>
565
+ <path data-slot="geo-map-water" d={sphere()} {...stylex.attrs(styles.water)} />
566
+ <Show when={graticule()}>
567
+ {(d) => <path data-slot="geo-map-graticule" d={d()} {...stylex.attrs(styles.graticule)} />}
568
+ </Show>
569
+ <Show when={land()}>
570
+ {(d) => <path data-slot="geo-map-land" d={d()} {...stylex.attrs(styles.land)} />}
571
+ </Show>
572
+ <g data-slot="geo-map-connections">
573
+ <For each={drawn()} keyed={(c) => `${c.index}:${c.connection.source}>${c.connection.target}`}>
574
+ {(c) => <g data-slot="geo-map-connection" data-state={connectionState(c().connection)} style={{
575
+ "--geo-map-color": c().color,
576
+ "--geo-map-weight": String(c().connection.weight ?? DEFAULT_WEIGHT)
577
+ }}>
578
+ <path d={c().d} class={stylex.props(styles.connection, connectionState(c().connection) === "active" && styles.connectionActive, connectionState(c().connection) === "dimmed" && styles.connectionDimmed, props.animated === true && styles.flowing).className} />
579
+ <path d={c().d} data-hit="" {...stylex.attrs(styles.connectionHit)} onPointerEnter={(event) => setHovered({
580
+ kind: "connection",
581
+ index: c().index,
582
+ x: event.clientX,
583
+ y: event.clientY
584
+ })} onPointerLeave={() => setHovered(null)} onClick={() => props.onConnectionSelect?.(c().connection)} />
585
+ </g>}
586
+ </For>
587
+ </g>
588
+ </g>
589
+ </svg>
590
+ <Show when={markers().length > 0}>
591
+ <div role="listbox" aria-label={label("markers")} data-slot="geo-map-markers" {...stylex.attrs(styles.markers)}>
592
+ <For each={ids()}>
593
+ {(id) => {
594
+ let last;
595
+ const p = createMemo(() => last = byId().get(id) ?? last);
596
+ const part = {
597
+ key: id,
598
+ props: { value: id }
599
+ };
600
+ let el;
601
+ onSettled(() => {
602
+ markerEls.set(part.key, el);
603
+ const leave = roving.bind(el, part);
604
+ return () => {
605
+ leave();
606
+ markerEls.delete(part.key);
607
+ };
608
+ });
609
+ return <div ref={(node) => el = node} role="option" aria-label={markerName(p().marker)} aria-selected={selected() === p().marker.id ? "true" : "false"} aria-describedby={p().marker.description ? descriptionId(p().marker.id) : void 0} tabindex={roving.tabIndex({ focused: stop() }, part)} data-slot="geo-map-marker" data-id={p().marker.id} data-group={p().marker.group} data-active={active() === p().marker.id ? "" : void 0} data-dimmed={dimmedMarker(p().marker.id) ? "" : void 0} data-hidden={p().shown ? void 0 : ""} class={stylex.props(styles.marker, !p().shown && styles.farSide).className} style={{
610
+ left: markerLeft(p().at),
611
+ top: markerTop(p().at),
612
+ "--geo-map-color": p().color,
613
+ "--geo-map-radius": String(p().marker.radius ?? DEFAULT_RADIUS)
614
+ }} onPointerEnter={() => setHovered({
615
+ kind: "marker",
616
+ id: part.key,
617
+ el
618
+ })} onPointerLeave={() => setHovered(null)} onFocus={() => onMarkerFocus(part.key, el)} onBlur={() => setFocused(null)} onClick={() => select(part.key)} onKeyDown={(event) => {
619
+ if (event.key === "Enter" || event.key === " ") {
620
+ event.preventDefault();
621
+ select(part.key);
622
+ return;
623
+ }
624
+ if (event.key === "Escape" && focused()?.visible) {
625
+ setFocused({
626
+ ...focused(),
627
+ visible: false
628
+ });
629
+ return;
630
+ }
631
+ roving.keydown(event, el);
632
+ }}>
633
+ <Show when={p().marker.description}>
634
+ {(description) => <span id={descriptionId(p().marker.id)} hidden>
635
+ {description()}
636
+ </span>}
637
+ </Show>
638
+ </div>;
639
+ }}
640
+ </For>
641
+ </div>
642
+ </Show>
643
+ <Show when={interactive() && props.controls !== false}>
644
+ <div data-slot="geo-map-controls" {...stylex.attrs(styles.controls)}>
645
+ <IconButton size="sm" variant="secondary" label={label("zoomIn")} onClick={() => zoomBy(ZOOM_STEP)}>
646
+ <Icon icon={Plus} />
647
+ </IconButton>
648
+ <IconButton size="sm" variant="secondary" label={label("zoomOut")} onClick={() => zoomBy(1 / ZOOM_STEP)}>
649
+ <Icon icon={Minus} />
650
+ </IconButton>
651
+ <IconButton size="sm" variant="secondary" label={label("fit")} onClick={() => fit()}>
652
+ <Icon icon={Scan} />
653
+ </IconButton>
654
+ </div>
655
+ </Show>
656
+ <Show when={tooltipKit()} keyed>
657
+ {(kit) => <kit.Tooltip open={tip() !== null} onOpenChange={(open) => {
658
+ if (open) return;
659
+ setHovered(null);
660
+ const f = focused();
661
+ if (f) setFocused({
662
+ ...f,
663
+ visible: false
664
+ });
665
+ }}>
666
+ <kit.TooltipContent anchor={tipAnchor()} data-slot="geo-map-tooltip">
667
+ <span {...stylex.attrs(styles.tooltipBody)}>
668
+ <Show when={tipMarker()}>
669
+ {(p) => props.tooltip ? props.tooltip(p().marker) : <>
670
+ <span {...stylex.attrs(styles.tooltipTitle)}>{p().marker.name}</span>
671
+ <Show when={p().marker.description}>
672
+ {(description) => <span {...stylex.attrs(styles.tooltipLine)}>{description()}</span>}
673
+ </Show>
674
+ <Show when={hasConnections()}>
675
+ <span {...stylex.attrs(styles.tooltipLine)}>
676
+ {label("connections")(links().get(p().marker.id)?.size ?? 0)}
677
+ </span>
678
+ </Show>
679
+ </>}
680
+ </Show>
681
+ <Show when={tipConnection()}>
682
+ {(c) => <>
683
+ <span {...stylex.attrs(styles.tooltipTitle)}>
684
+ {byId().get(c().source)?.marker.name ?? c().source} →{" "}
685
+ {byId().get(c().target)?.marker.name ?? c().target}
686
+ </span>
687
+ <Show when={c().label}>
688
+ {(text) => <span {...stylex.attrs(styles.tooltipLine)}>{text()}</span>}
689
+ </Show>
690
+ </>}
691
+ </Show>
692
+ </span>
693
+ </kit.TooltipContent>
694
+ </kit.Tooltip>}
695
+ </Show>
696
+ </div>;
697
+ }
698
+ export { GeoMap, geoMapColor };