@dxos/react-ui-geo 0.8.4-main.f9ba587 → 0.8.4-main.fcfe5033a5

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 (77) hide show
  1. package/data/airports.ts +1 -1
  2. package/data/cities.ts +1 -1
  3. package/data/countries-110m.ts +1 -1
  4. package/data/countries-dots-3.ts +1 -1
  5. package/data/countries-dots-4.ts +1 -1
  6. package/dist/lib/browser/chunk-GMWLKTLN.mjs +9 -0
  7. package/dist/lib/browser/{countries-110m-37VAAFCK.mjs → countries-110m-ZM3ZIEFS.mjs} +1 -1
  8. package/dist/lib/browser/countries-110m-ZM3ZIEFS.mjs.map +7 -0
  9. package/dist/lib/browser/data.mjs +1 -1
  10. package/dist/lib/browser/index.mjs +400 -447
  11. package/dist/lib/browser/index.mjs.map +4 -4
  12. package/dist/lib/browser/meta.json +1 -1
  13. package/dist/lib/node-esm/{chunk-OPJPAAEK.mjs → chunk-JODBF4CC.mjs} +2 -2
  14. package/dist/lib/node-esm/{countries-110m-36TTKK5B.mjs → countries-110m-3SFASWVD.mjs} +1 -1
  15. package/dist/lib/node-esm/countries-110m-3SFASWVD.mjs.map +7 -0
  16. package/dist/lib/node-esm/data.mjs +1 -1
  17. package/dist/lib/node-esm/index.mjs +400 -447
  18. package/dist/lib/node-esm/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/types/src/components/Globe/Globe.d.ts +6 -4
  21. package/dist/types/src/components/Globe/Globe.d.ts.map +1 -1
  22. package/dist/types/src/components/Globe/Globe.stories.d.ts +27 -9
  23. package/dist/types/src/components/Globe/Globe.stories.d.ts.map +1 -1
  24. package/dist/types/src/components/Map/Map.d.ts +34 -18
  25. package/dist/types/src/components/Map/Map.d.ts.map +1 -1
  26. package/dist/types/src/components/Map/Map.stories.d.ts +14 -8
  27. package/dist/types/src/components/Map/Map.stories.d.ts.map +1 -1
  28. package/dist/types/src/components/Toolbar/Controls.d.ts.map +1 -1
  29. package/dist/types/src/components/index.d.ts +0 -1
  30. package/dist/types/src/components/index.d.ts.map +1 -1
  31. package/dist/types/src/hooks/context.d.ts +6 -8
  32. package/dist/types/src/hooks/context.d.ts.map +1 -1
  33. package/dist/types/src/hooks/useGlobeZoomHandler.d.ts +1 -1
  34. package/dist/types/src/hooks/useGlobeZoomHandler.d.ts.map +1 -1
  35. package/dist/types/src/hooks/useMapZoomHandler.d.ts +1 -1
  36. package/dist/types/src/hooks/useMapZoomHandler.d.ts.map +1 -1
  37. package/dist/types/src/hooks/useSpinner.d.ts +1 -1
  38. package/dist/types/src/hooks/useSpinner.d.ts.map +1 -1
  39. package/dist/types/src/hooks/useTour.d.ts +4 -3
  40. package/dist/types/src/hooks/useTour.d.ts.map +1 -1
  41. package/dist/types/src/index.d.ts +2 -1
  42. package/dist/types/src/index.d.ts.map +1 -1
  43. package/dist/types/src/translations.d.ts +12 -0
  44. package/dist/types/src/translations.d.ts.map +1 -0
  45. package/dist/types/src/types.d.ts +2 -1
  46. package/dist/types/src/types.d.ts.map +1 -1
  47. package/dist/types/src/util/path.d.ts +5 -8
  48. package/dist/types/src/util/path.d.ts.map +1 -1
  49. package/dist/types/src/util/render.d.ts +4 -4
  50. package/dist/types/src/util/render.d.ts.map +1 -1
  51. package/dist/types/tsconfig.tsbuildinfo +1 -1
  52. package/package.json +30 -23
  53. package/src/components/Globe/Globe.stories.tsx +82 -35
  54. package/src/components/Globe/Globe.tsx +124 -81
  55. package/src/components/Map/Map.stories.tsx +27 -15
  56. package/src/components/Map/Map.tsx +220 -94
  57. package/src/components/Toolbar/Controls.tsx +14 -20
  58. package/src/components/index.ts +0 -2
  59. package/src/hooks/context.tsx +11 -34
  60. package/src/hooks/useGlobeZoomHandler.ts +9 -3
  61. package/src/hooks/useMapZoomHandler.ts +1 -1
  62. package/src/hooks/useSpinner.ts +1 -1
  63. package/src/hooks/useTour.ts +10 -8
  64. package/src/index.ts +2 -1
  65. package/src/translations.ts +20 -0
  66. package/src/types.ts +3 -1
  67. package/src/util/inertia.ts +1 -1
  68. package/src/util/path.ts +5 -6
  69. package/src/util/render.ts +4 -3
  70. package/dist/lib/browser/chunk-CYCBMCOP.mjs +0 -9
  71. package/dist/lib/browser/countries-110m-37VAAFCK.mjs.map +0 -7
  72. package/dist/lib/node-esm/countries-110m-36TTKK5B.mjs.map +0 -7
  73. package/dist/types/src/components/types.d.ts +0 -15
  74. package/dist/types/src/components/types.d.ts.map +0 -1
  75. package/src/components/types.ts +0 -19
  76. /package/dist/lib/browser/{chunk-CYCBMCOP.mjs.map → chunk-GMWLKTLN.mjs.map} +0 -0
  77. /package/dist/lib/node-esm/{chunk-OPJPAAEK.mjs.map → chunk-JODBF4CC.mjs.map} +0 -0
package/package.json CHANGED
@@ -1,20 +1,26 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-geo",
3
- "version": "0.8.4-main.f9ba587",
3
+ "version": "0.8.4-main.fcfe5033a5",
4
4
  "description": "Geo components.",
5
5
  "homepage": "https://github.com/dxos",
6
6
  "bugs": "https://github.com/dxos/issues",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
7
11
  "license": "MIT",
8
12
  "author": "DXOS.org",
9
13
  "sideEffects": true,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  "./data": {
17
+ "source": "./src/data.ts",
13
18
  "types": "./dist/types/src/data.d.ts",
14
19
  "browser": "./dist/lib/browser/data.mjs",
15
20
  "node": "./dist/lib/node-esm/data.mjs"
16
21
  },
17
22
  ".": {
23
+ "source": "./src/index.ts",
18
24
  "types": "./dist/types/src/index.d.ts",
19
25
  "browser": "./dist/lib/browser/index.mjs",
20
26
  "node": "./dist/lib/node-esm/index.mjs"
@@ -34,51 +40,52 @@
34
40
  "src"
35
41
  ],
36
42
  "dependencies": {
37
- "@preact-signals/safe-react": "^0.9.0",
43
+ "@radix-ui/react-compose-refs": "1.1.1",
44
+ "@radix-ui/react-context": "1.1.1",
38
45
  "d3": "^7.9.0",
39
46
  "d3-geo-projection": "^4.0.0",
40
47
  "d3-hexbin": "^0.2.2",
41
48
  "geojson": "^0.5.0",
42
49
  "leaflet": "^1.9.4",
43
50
  "lodash.defaultsdeep": "^4.6.1",
44
- "react-leaflet": "^4.2.1",
51
+ "react-leaflet": "^5.0.0",
45
52
  "react-resize-detector": "^11.0.1",
46
53
  "topojson-client": "^3.1.0",
47
54
  "topojson-simplify": "^3.0.3",
48
55
  "versor": "^0.2.0",
49
- "@dxos/async": "0.8.4-main.f9ba587",
50
- "@dxos/log": "0.8.4-main.f9ba587",
51
- "@dxos/util": "0.8.4-main.f9ba587",
52
- "@dxos/debug": "0.8.4-main.f9ba587",
53
- "@dxos/node-std": "0.8.4-main.f9ba587"
56
+ "@dxos/async": "0.8.4-main.fcfe5033a5",
57
+ "@dxos/debug": "0.8.4-main.fcfe5033a5",
58
+ "@dxos/log": "0.8.4-main.fcfe5033a5",
59
+ "@dxos/node-std": "0.8.4-main.fcfe5033a5",
60
+ "@dxos/util": "0.8.4-main.fcfe5033a5"
54
61
  },
55
62
  "devDependencies": {
56
- "@react-three/drei": "^9.99.0",
57
- "@react-three/fiber": "^8.15.0",
63
+ "@react-three/drei": "^10.7.7",
64
+ "@react-three/fiber": "^9.5.0",
58
65
  "@types/d3": "^7.4.3",
59
66
  "@types/geojson": "^7946.0.14",
60
67
  "@types/leaflet": "^1.9.16",
61
- "@types/react": "~18.2.0",
62
- "@types/react-dom": "~18.2.0",
68
+ "@types/react": "~19.2.7",
69
+ "@types/react-dom": "~19.2.3",
63
70
  "@types/three": "0.165.0",
64
71
  "@types/topojson-client": "^3.1.4",
65
72
  "@types/topojson-simplify": "^3.0.3",
66
73
  "@types/topojson-specification": "^1.0.5",
67
74
  "JSONStream": "^1.3.5",
68
75
  "geojson2h3": "^1.2.0",
69
- "leva": "^0.9.35",
70
- "react": "~18.2.0",
71
- "react-dom": "~18.2.0",
72
- "three": "0.165.0",
73
- "@dxos/react-ui": "0.8.4-main.f9ba587",
74
- "@dxos/react-ui-theme": "0.8.4-main.f9ba587",
75
- "@dxos/storybook-utils": "0.8.4-main.f9ba587"
76
+ "leva": "^0.10.1",
77
+ "react": "~19.2.3",
78
+ "react-dom": "~19.2.3",
79
+ "three": "^0.178.0",
80
+ "@dxos/react-ui": "0.8.4-main.fcfe5033a5",
81
+ "@dxos/ui-theme": "0.8.4-main.fcfe5033a5",
82
+ "@dxos/storybook-utils": "0.8.4-main.fcfe5033a5"
76
83
  },
77
84
  "peerDependencies": {
78
- "react": "~18.2.0",
79
- "react-dom": "~18.2.0",
80
- "@dxos/react-ui": "0.8.4-main.f9ba587",
81
- "@dxos/react-ui-theme": "0.8.4-main.f9ba587"
85
+ "react": "~19.2.3",
86
+ "react-dom": "~19.2.3",
87
+ "@dxos/react-ui": "0.8.4-main.fcfe5033a5",
88
+ "@dxos/ui-theme": "0.8.4-main.fcfe5033a5"
82
89
  },
83
90
  "publishConfig": {
84
91
  "access": "public"
@@ -2,21 +2,20 @@
2
2
  // Copyright 2018 DXOS.org
3
3
  //
4
4
 
5
- import '@dxos-theme';
6
-
7
- import { type Meta } from '@storybook/react-vite';
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
8
6
  import { type FeatureCollection, type Geometry, type Position } from 'geojson';
9
7
  import { Leva } from 'leva';
10
8
  import React, { useMemo, useRef, useState } from 'react';
11
9
  import { type Topology } from 'topojson-specification';
12
10
 
13
11
  import { useAsyncState } from '@dxos/react-ui';
14
- import { withTheme, withLayout } from '@dxos/storybook-utils';
12
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
15
13
 
16
- import { Globe, type GlobeCanvasProps, type GlobeController, type GlobeRootProps } from './Globe';
17
- import { useDrag, useGlobeZoomHandler, useSpinner, useTour, type Vector } from '../../hooks';
18
- import { closestPoint, type LatLng, type StyleSet } from '../../util';
14
+ import { type Vector, useDrag, useGlobeZoomHandler, useSpinner, useTour } from '../../hooks';
15
+ import { type LatLngLiteral } from '../../types';
16
+ import { type StyleSet, closestPoint } from '../../util';
19
17
  import { type ControlProps } from '../Toolbar';
18
+ import { Globe, type GlobeCanvasProps, type GlobeController, type GlobeRootProps } from './Globe';
20
19
 
21
20
  // TODO(burdon): Load from JSON at runtime?
22
21
  const useTopology = () => {
@@ -95,8 +94,11 @@ const createTrip = (
95
94
  routes: Record<string, string[]>,
96
95
  points: Position[] = [],
97
96
  ) => {
98
- let previousHub: LatLng;
99
- return Object.entries(routes).reduce<{ points: LatLng[]; lines: { source: LatLng; target: LatLng }[] }>(
97
+ let previousHub: LatLngLiteral;
98
+ return Object.entries(routes).reduce<{
99
+ points: LatLngLiteral[];
100
+ lines: { source: LatLngLiteral; target: LatLngLiteral }[];
101
+ }>(
100
102
  (features, [hub, regional]) => {
101
103
  const hubAirport = airports.features.find(({ properties }) => properties.iata === hub);
102
104
  if (hubAirport) {
@@ -125,7 +127,7 @@ const createTrip = (
125
127
  );
126
128
  };
127
129
 
128
- type StoryProps = Pick<GlobeRootProps, 'scale' | 'translation' | 'rotation'> &
130
+ type DefaultStoryProps = Pick<GlobeRootProps, 'zoom' | 'translation' | 'rotation'> &
129
131
  Pick<GlobeCanvasProps, 'projection' | 'styles'> & {
130
132
  drag?: boolean;
131
133
  spin?: boolean;
@@ -133,8 +135,8 @@ type StoryProps = Pick<GlobeRootProps, 'scale' | 'translation' | 'rotation'> &
133
135
  xAxis?: boolean;
134
136
  };
135
137
 
136
- const Story = ({
137
- scale: _scale = 1,
138
+ const DefaultStory = ({
139
+ zoom: zoomProp = 1,
138
140
  translation,
139
141
  rotation = [0, 0, 0],
140
142
  projection,
@@ -143,7 +145,7 @@ const Story = ({
143
145
  spin = false,
144
146
  tour = false,
145
147
  xAxis = false,
146
- }: StoryProps) => {
148
+ }: DefaultStoryProps) => {
147
149
  const controller = useRef<GlobeController>(null);
148
150
  const [dots] = useAsyncState(async () => {
149
151
  const points = (await import('../../../data/countries-dots-3.ts')).default;
@@ -154,6 +156,7 @@ const Story = ({
154
156
  });
155
157
  const [topology] = useTopology();
156
158
  const [airports] = useAsyncState(async () => (await import('../../../data/airports.ts')).default);
159
+
157
160
  const features = useMemo(() => {
158
161
  return airports ? createTrip(airports, routes, (dots?.objects.dots as any)?.geometries[0].coordinates) : undefined;
159
162
  }, [airports, routes, dots]);
@@ -188,24 +191,24 @@ const Story = ({
188
191
  break;
189
192
  }
190
193
  case 'zoom-in': {
191
- controller.current.setScale((scale) => scale * 1.1);
194
+ controller.current.setZoom((scale) => scale * 1.1);
192
195
  break;
193
196
  }
194
197
  case 'zoom-out': {
195
- controller.current.setScale((scale) => scale * 0.9);
198
+ controller.current.setZoom((scale) => scale * 0.9);
196
199
  break;
197
200
  }
198
201
  }
199
202
  };
200
203
 
201
204
  return (
202
- <Globe.Root classNames='absolute inset-0' scale={_scale} translation={translation} rotation={rotation}>
205
+ <Globe.Root zoom={zoomProp} translation={translation} rotation={rotation}>
203
206
  <Globe.Canvas
204
- ref={controller}
205
207
  topology={styles?.dots ? dots : topology}
206
208
  projection={projection}
207
209
  styles={styles}
208
210
  features={tour ? { points: features?.points ?? [] } : features}
211
+ ref={controller}
209
212
  />
210
213
  <Globe.Zoom onAction={handleAction} />
211
214
  <Globe.Action onAction={handleAction} />
@@ -219,11 +222,15 @@ const Story = ({
219
222
 
220
223
  const initialRotation: Vector = [0, -40, 0];
221
224
 
222
- const meta: Meta = {
225
+ const meta = {
223
226
  title: 'ui/react-ui-geo/Globe',
224
227
  component: Globe.Root,
225
- decorators: [withTheme, withLayout({ fullscreen: true, classNames: 'bg-[#000]' })],
226
- };
228
+ render: DefaultStory,
229
+ decorators: [withTheme(), withLayout({ layout: 'fullscreen' })],
230
+ parameters: {
231
+ layout: 'fullscreen',
232
+ },
233
+ } satisfies Meta;
227
234
 
228
235
  export default meta;
229
236
 
@@ -234,7 +241,7 @@ export const Earth1 = () => {
234
241
  useDrag(controller);
235
242
 
236
243
  return (
237
- <Globe.Root scale={1.2} rotation={[Math.random() * 360, 0, 0]}>
244
+ <Globe.Root zoom={1.2} rotation={[Math.random() * 360, 0, 0]}>
238
245
  <Globe.Canvas ref={setController} topology={topology} styles={defaultStyles} />
239
246
  <Globe.Zoom onAction={handleAction} />
240
247
  </Globe.Root>
@@ -249,7 +256,7 @@ export const Earth2 = () => {
249
256
 
250
257
  return (
251
258
  <div className='absolute bottom-0 left-0 right-0 '>
252
- <Globe.Root classNames='h-[400px]' scale={2.8} translation={{ x: 0, y: 400 }}>
259
+ <Globe.Root classNames='h-[400px]' zoom={2.8} translation={{ x: 0, y: 400 }}>
253
260
  <Globe.Canvas ref={setController} topology={topology} styles={defaultStyles} />
254
261
  <Globe.Zoom onAction={handleAction} />
255
262
  </Globe.Root>
@@ -283,33 +290,73 @@ export const Mercator = () => {
283
290
  useDrag(controller);
284
291
 
285
292
  return (
286
- <Globe.Root classNames='flex grow overflow-hidden' scale={0.7} rotation={initialRotation}>
293
+ <Globe.Root classNames='flex grow overflow-hidden' zoom={0.7} rotation={initialRotation}>
287
294
  <Globe.Canvas ref={setController} topology={topology} projection='mercator' styles={monochrome} />
288
295
  <Globe.Zoom onAction={handleAction} />
289
296
  </Globe.Root>
290
297
  );
291
298
  };
292
299
 
293
- export const Globe1 = () => {
294
- return <Story drag projection='mercator' scale={0.8} rotation={initialRotation} styles={defaultStyles} />;
300
+ type Story = StoryObj<typeof DefaultStory>;
301
+
302
+ export const Globe1: Story = {
303
+ args: {
304
+ drag: true,
305
+ projection: 'mercator',
306
+ zoom: 0.8,
307
+ rotation: initialRotation,
308
+ styles: defaultStyles,
309
+ },
295
310
  };
296
311
 
297
- export const Globe2 = () => {
298
- return <Story drag projection='transverse-mercator' scale={0.8} rotation={initialRotation} styles={defaultStyles} />;
312
+ export const Globe2: Story = {
313
+ args: {
314
+ drag: true,
315
+ projection: 'transverse-mercator',
316
+ zoom: 0.8,
317
+ rotation: initialRotation,
318
+ styles: defaultStyles,
319
+ },
299
320
  };
300
321
 
301
- export const Globe3 = () => {
302
- return <Story drag spin scale={1.5} rotation={initialRotation} styles={defaultStyles} />;
322
+ export const Globe3: Story = {
323
+ args: {
324
+ drag: true,
325
+ spin: true,
326
+ zoom: 1.5,
327
+ rotation: initialRotation,
328
+ styles: defaultStyles,
329
+ },
303
330
  };
304
331
 
305
- export const Globe4 = () => {
306
- return <Story drag tour scale={2} rotation={initialRotation} styles={defaultStyles} />;
332
+ export const Globe4: Story = {
333
+ args: {
334
+ drag: true,
335
+ tour: true,
336
+ zoom: 2,
337
+ rotation: initialRotation,
338
+ styles: defaultStyles,
339
+ },
307
340
  };
308
341
 
309
- export const Globe5 = () => {
310
- return <Story drag tour scale={0.9} rotation={initialRotation} styles={dotStyles} />;
342
+ export const Globe5: Story = {
343
+ args: {
344
+ drag: true,
345
+ tour: true,
346
+ zoom: 0.9,
347
+ rotation: initialRotation,
348
+ styles: dotStyles,
349
+ },
311
350
  };
312
351
 
313
- export const Globe6 = () => {
314
- return <Story drag xAxis tour scale={2} translation={{ x: 0, y: 600 }} rotation={[0, -20, 0]} styles={dotStyles} />;
352
+ export const Globe6: Story = {
353
+ args: {
354
+ drag: true,
355
+ xAxis: true,
356
+ tour: true,
357
+ zoom: 2,
358
+ translation: { x: 0, y: 600 },
359
+ rotation: [0, -20, 0],
360
+ styles: dotStyles,
361
+ },
315
362
  };
@@ -4,14 +4,14 @@
4
4
 
5
5
  import {
6
6
  type GeoProjection,
7
+ easeLinear,
8
+ easeSinOut,
7
9
  geoMercator,
8
10
  geoOrthographic,
9
11
  geoPath,
10
12
  geoTransverseMercator,
11
13
  interpolateNumber,
12
14
  transition,
13
- easeLinear,
14
- easeSinOut,
15
15
  } from 'd3';
16
16
  import { type ControlPosition } from 'leaflet';
17
17
  import React, {
@@ -26,15 +26,17 @@ import React, {
26
26
  import { useResizeDetector } from 'react-resize-detector';
27
27
  import { type Topology } from 'topojson-specification';
28
28
 
29
- import { type ThemedClassName, type ThemeMode, useDynamicRef, useThemeContext } from '@dxos/react-ui';
30
- import { mx } from '@dxos/react-ui-theme';
31
-
32
29
  import {
33
- GlobeContextProvider,
34
- type GlobeContextProviderProps,
35
- type GlobeContextType,
36
- useGlobeContext,
37
- } from '../../hooks';
30
+ type ThemeMode,
31
+ type ThemedClassName,
32
+ useComposedRefs,
33
+ useControlledState,
34
+ useDynamicRef,
35
+ useThemeContext,
36
+ } from '@dxos/react-ui';
37
+ import { composable, composableProps, mx } from '@dxos/ui-theme';
38
+
39
+ import { GlobeContext, type GlobeContextType, type Point, type Vector, useGlobeContext } from '../../hooks';
38
40
  import {
39
41
  type Features,
40
42
  type StyleSet,
@@ -44,7 +46,7 @@ import {
44
46
  renderLayers,
45
47
  timer,
46
48
  } from '../../util';
47
- import { ZoomControls, ActionControls, type ControlProps, controlPositions } from '../Toolbar';
49
+ import { ActionControls, type ControlProps, ZoomControls, controlPositions } from '../Toolbar';
48
50
 
49
51
  /**
50
52
  * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
@@ -103,7 +105,7 @@ const defaultStyles: Record<ThemeMode, StyleSet> = {
103
105
  export type GlobeController = {
104
106
  canvas: HTMLCanvasElement;
105
107
  projection: GeoProjection;
106
- } & Pick<GlobeContextType, 'scale' | 'translation' | 'rotation' | 'setScale' | 'setTranslation' | 'setRotation'>;
108
+ } & Pick<GlobeContextType, 'zoom' | 'translation' | 'rotation' | 'setZoom' | 'setTranslation' | 'setRotation'>;
107
109
 
108
110
  export type ProjectionType = 'orthographic' | 'mercator' | 'transverse-mercator';
109
111
 
@@ -126,18 +128,43 @@ const getProjection = (type: GlobeCanvasProps['projection'] = 'orthographic'): G
126
128
  // Root
127
129
  //
128
130
 
129
- type GlobeRootProps = PropsWithChildren<ThemedClassName<GlobeContextProviderProps>>;
130
-
131
- const GlobeRoot = ({ classNames, children, ...props }: GlobeRootProps) => {
132
- const { ref, width, height } = useResizeDetector<HTMLDivElement>();
133
- return (
134
- <div ref={ref} className={mx('relative flex grow overflow-hidden', classNames)}>
135
- <GlobeContextProvider size={{ width, height }} {...props}>
136
- {children}
137
- </GlobeContextProvider>
138
- </div>
139
- );
140
- };
131
+ type GlobeRootProps = Partial<Pick<GlobeContextType, 'center' | 'zoom' | 'translation' | 'rotation'>>;
132
+
133
+ const GlobeRoot = composable<HTMLDivElement, GlobeRootProps>(
134
+ (
135
+ { children, center: centerProp, zoom: zoomProp, translation: translationProp, rotation: rotationProp, ...props },
136
+ forwardedRef,
137
+ ) => {
138
+ const localRef = useRef<HTMLDivElement>(null);
139
+ const composedRef = useComposedRefs<HTMLDivElement>(localRef, forwardedRef);
140
+ const { width, height } = useResizeDetector<HTMLDivElement>({ targetRef: localRef });
141
+
142
+ const [center, setCenter] = useControlledState(centerProp);
143
+ const [zoom, setZoom] = useControlledState(zoomProp ?? 4);
144
+ const [translation, setTranslation] = useControlledState<Point>(translationProp);
145
+ const [rotation, setRotation] = useControlledState<Vector>(rotationProp);
146
+
147
+ return (
148
+ <GlobeContext.Provider
149
+ value={{
150
+ size: { width, height },
151
+ center,
152
+ zoom,
153
+ translation,
154
+ rotation,
155
+ setCenter,
156
+ setZoom,
157
+ setTranslation,
158
+ setRotation,
159
+ }}
160
+ >
161
+ <div {...composableProps(props, { classNames: 'relative dx-container' })} ref={composedRef}>
162
+ {children}
163
+ </div>
164
+ </GlobeContext.Provider>
165
+ );
166
+ },
167
+ );
141
168
 
142
169
  //
143
170
  // Canvas
@@ -154,17 +181,18 @@ type GlobeCanvasProps = {
154
181
  * Basic globe renderer.
155
182
  * https://github.com/topojson/world-atlas
156
183
  */
184
+ // TODO(burdon): Move controller to root.
157
185
  const GlobeCanvas = forwardRef<GlobeController, GlobeCanvasProps>(
158
- ({ projection: _projection, topology, features, styles: _styles }, forwardRef) => {
186
+ ({ projection: projectionProp, topology, features, styles: stylesProp }, forwardRef) => {
159
187
  const { themeMode } = useThemeContext();
160
- const styles = useMemo(() => _styles ?? defaultStyles[themeMode], [_styles, themeMode]);
188
+ const styles = useMemo(() => stylesProp ?? defaultStyles[themeMode], [stylesProp, themeMode]);
161
189
 
162
190
  // Canvas.
163
191
  const [canvas, setCanvas] = useState<HTMLCanvasElement>(null);
164
192
  const canvasRef = (canvas: HTMLCanvasElement) => setCanvas(canvas);
165
193
 
166
194
  // Projection.
167
- const projection = useMemo(() => getProjection(_projection), [_projection]);
195
+ const projection = useMemo(() => getProjection(projectionProp), [projectionProp]);
168
196
 
169
197
  // Layers.
170
198
  // TODO(burdon): Generate on the fly based on what is visible.
@@ -173,55 +201,50 @@ const GlobeCanvas = forwardRef<GlobeController, GlobeCanvasProps>(
173
201
  }, [topology, features, styles]);
174
202
 
175
203
  // State.
176
- const { size, center, scale, translation, rotation, setCenter, setScale, setTranslation, setRotation } =
204
+ const { size, center, zoom, translation, rotation, setCenter, setZoom, setTranslation, setRotation } =
177
205
  useGlobeContext();
178
-
179
- const scaleRef = useDynamicRef(scale);
206
+ const zoomRef = useDynamicRef(zoom);
180
207
 
181
208
  // Update rotation.
182
209
  useEffect(() => {
183
210
  if (center) {
184
- setScale(1);
211
+ setZoom(1);
185
212
  setRotation(positionToRotation(geoToPosition(center)));
186
213
  }
187
214
  }, [center]);
188
215
 
189
216
  // External controller.
190
217
  const zooming = useRef(false);
191
- useImperativeHandle<GlobeController, GlobeController>(
192
- forwardRef,
193
- () => {
194
- return {
195
- canvas,
196
- projection,
197
- center,
198
- get scale() {
199
- return scaleRef.current;
200
- },
201
- translation,
202
- rotation,
203
- setCenter,
204
- setScale: (s) => {
205
- if (typeof s === 'function') {
206
- const is = interpolateNumber(scaleRef.current, s(scaleRef.current));
207
- // Stop easing if already zooming.
208
- transition()
209
- .ease(zooming.current ? easeLinear : easeSinOut)
210
- .duration(200)
211
- .tween('scale', () => (t) => setScale(is(t)))
212
- .on('end', () => {
213
- zooming.current = false;
214
- });
215
- } else {
216
- setScale(s);
217
- }
218
- },
219
- setTranslation,
220
- setRotation,
221
- };
222
- },
223
- [canvas],
224
- );
218
+ useImperativeHandle<GlobeController, GlobeController>(forwardRef, () => {
219
+ return {
220
+ canvas,
221
+ projection,
222
+ center,
223
+ get zoom() {
224
+ return zoomRef.current;
225
+ },
226
+ translation,
227
+ rotation,
228
+ setCenter,
229
+ setZoom: (state) => {
230
+ if (typeof state === 'function') {
231
+ const is = interpolateNumber(zoomRef.current, state(zoomRef.current));
232
+ // Stop easing if already zooming.
233
+ transition()
234
+ .ease(zooming.current ? easeLinear : easeSinOut)
235
+ .duration(200)
236
+ .tween('scale', () => (t) => setZoom(is(t)))
237
+ .on('end', () => {
238
+ zooming.current = false;
239
+ });
240
+ } else {
241
+ setZoom(state);
242
+ }
243
+ },
244
+ setTranslation,
245
+ setRotation,
246
+ };
247
+ }, [canvas]);
225
248
 
226
249
  // https://d3js.org/d3-geo/path#geoPath
227
250
  // https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext
@@ -236,14 +259,14 @@ const GlobeCanvas = forwardRef<GlobeController, GlobeCanvasProps>(
236
259
  timer(() => {
237
260
  // https://d3js.org/d3-geo/projection
238
261
  projection
239
- .scale((Math.min(size.width, size.height) / 2) * scale)
262
+ .scale((Math.min(size.width, size.height) / 2) * zoom)
240
263
  .translate([size.width / 2 + (translation?.x ?? 0), size.height / 2 + (translation?.y ?? 0)])
241
264
  .rotate(rotation ?? [0, 0, 0]);
242
265
 
243
- renderLayers(generator, layers, scale, styles);
266
+ renderLayers(generator, layers, zoom, styles);
244
267
  });
245
268
  }
246
- }, [generator, size, scale, translation, rotation, layers]);
269
+ }, [generator, size, zoom, translation, rotation, layers]);
247
270
 
248
271
  if (!size.width || !size.height) {
249
272
  return null;
@@ -253,22 +276,30 @@ const GlobeCanvas = forwardRef<GlobeController, GlobeCanvasProps>(
253
276
  },
254
277
  );
255
278
 
279
+ //
280
+ // Debug
281
+ //
282
+
256
283
  const GlobeDebug = ({ position = 'topleft' }: { position?: ControlPosition }) => {
257
- const { size, scale, translation, rotation } = useGlobeContext();
284
+ const { size, zoom, translation, rotation } = useGlobeContext();
258
285
  return (
259
286
  <div
260
287
  className={mx(
261
- 'z-10 absolute w-96 p-2 overflow-hidden border border-green-700 rounded',
288
+ 'z-10 absolute w-96 p-2 overflow-hidden border border-green-700 rounded-sm',
262
289
  controlPositions[position],
263
290
  )}
264
291
  >
265
292
  <pre className='font-mono text-xs text-green-700'>
266
- {JSON.stringify({ size, scale, translation, rotation }, null, 2)}
293
+ {JSON.stringify({ size, zoom, translation, rotation }, null, 2)}
267
294
  </pre>
268
295
  </div>
269
296
  );
270
297
  };
271
298
 
299
+ //
300
+ // Panel
301
+ //
302
+
272
303
  const GlobePanel = ({
273
304
  position,
274
305
  classNames,
@@ -277,25 +308,37 @@ const GlobePanel = ({
277
308
  return <div className={mx('z-10 absolute overflow-hidden', controlPositions[position], classNames)}>{children}</div>;
278
309
  };
279
310
 
311
+ //
312
+ // Controls
313
+ //
314
+
280
315
  const CustomControl = ({ position, children }: PropsWithChildren<{ position: ControlPosition }>) => {
281
316
  return <div className={mx('z-10 absolute overflow-hidden', controlPositions[position])}>{children}</div>;
282
317
  };
283
318
 
284
319
  type GlobeControlProps = { position?: ControlPosition } & Pick<ControlProps, 'onAction'>;
285
320
 
321
+ const GlobeZoom = ({ onAction, position = 'bottomleft', ...props }: GlobeControlProps) => (
322
+ <CustomControl position={position} {...props}>
323
+ <ZoomControls onAction={onAction} />
324
+ </CustomControl>
325
+ );
326
+
327
+ const GlobeAction = ({ onAction, position = 'bottomright', ...props }: GlobeControlProps) => (
328
+ <CustomControl position={position} {...props}>
329
+ <ActionControls onAction={onAction} />
330
+ </CustomControl>
331
+ );
332
+
333
+ //
334
+ // Globe
335
+ //
336
+
286
337
  export const Globe = {
287
338
  Root: GlobeRoot,
288
339
  Canvas: GlobeCanvas,
289
- Zoom: ({ onAction, position = 'bottomleft', ...props }: GlobeControlProps) => (
290
- <CustomControl position={position} {...props}>
291
- <ZoomControls onAction={onAction} />
292
- </CustomControl>
293
- ),
294
- Action: ({ onAction, position = 'bottomright', ...props }: GlobeControlProps) => (
295
- <CustomControl position={position} {...props}>
296
- <ActionControls onAction={onAction} />
297
- </CustomControl>
298
- ),
340
+ Zoom: GlobeZoom,
341
+ Action: GlobeAction,
299
342
  Debug: GlobeDebug,
300
343
  Panel: GlobePanel,
301
344
  };