@dxos/react-ui-geo 0.7.5-labs.ff2ff30 → 0.7.5-main.b19bfc8

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 (38) hide show
  1. package/dist/lib/browser/index.mjs +146 -81
  2. package/dist/lib/browser/index.mjs.map +3 -3
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node/index.cjs +123 -58
  5. package/dist/lib/node/index.cjs.map +3 -3
  6. package/dist/lib/node/meta.json +1 -1
  7. package/dist/lib/node-esm/index.mjs +146 -81
  8. package/dist/lib/node-esm/index.mjs.map +3 -3
  9. package/dist/lib/node-esm/meta.json +1 -1
  10. package/dist/types/src/components/Globe/Globe.d.ts +5 -5
  11. package/dist/types/src/components/Globe/Globe.d.ts.map +1 -1
  12. package/dist/types/src/components/Globe/Globe.stories.d.ts +9 -10
  13. package/dist/types/src/components/Globe/Globe.stories.d.ts.map +1 -1
  14. package/dist/types/src/components/Map/Map.d.ts +9 -10
  15. package/dist/types/src/components/Map/Map.d.ts.map +1 -1
  16. package/dist/types/src/components/Map/Map.stories.d.ts +6 -2
  17. package/dist/types/src/components/Map/Map.stories.d.ts.map +1 -1
  18. package/dist/types/src/components/Toolbar/Controls.d.ts +2 -3
  19. package/dist/types/src/components/Toolbar/Controls.d.ts.map +1 -1
  20. package/dist/types/src/components/types.d.ts +2 -1
  21. package/dist/types/src/components/types.d.ts.map +1 -1
  22. package/dist/types/src/hooks/context.d.ts +2 -2
  23. package/dist/types/src/hooks/context.d.ts.map +1 -1
  24. package/dist/types/src/hooks/useTour.d.ts +8 -3
  25. package/dist/types/src/hooks/useTour.d.ts.map +1 -1
  26. package/dist/types/src/util/debug.d.ts.map +1 -1
  27. package/dist/types/src/util/render.d.ts +2 -2
  28. package/dist/types/src/util/render.d.ts.map +1 -1
  29. package/package.json +12 -10
  30. package/src/components/Globe/Globe.stories.tsx +8 -11
  31. package/src/components/Globe/Globe.tsx +56 -32
  32. package/src/components/Map/Map.stories.tsx +28 -6
  33. package/src/components/Map/Map.tsx +93 -82
  34. package/src/components/types.ts +2 -1
  35. package/src/hooks/useDrag.ts +1 -1
  36. package/src/hooks/useTour.ts +31 -19
  37. package/src/util/debug.ts +1 -0
  38. package/src/util/render.ts +18 -4
@@ -3,29 +3,39 @@
3
3
  //
4
4
 
5
5
  import * as d3 from 'd3';
6
- import { useEffect, useState } from 'react';
6
+ import { type Dispatch, type SetStateAction, useEffect, useState } from 'react';
7
7
  import versor from 'versor';
8
8
 
9
+ import { log } from '@dxos/log';
10
+
9
11
  import type { GlobeController } from '../components';
10
- import { type Features, geoToPosition, type LatLng, positionToRotation, type StyleSet } from '../util';
12
+ import { geoToPosition, type LatLng, positionToRotation, type StyleSet } from '../util';
11
13
 
12
14
  const TRANSITION_NAME = 'globe-tour';
13
15
 
14
16
  const defaultDuration = 1_500;
15
17
 
16
18
  export type TourOptions = {
19
+ running?: boolean;
17
20
  disabled?: boolean;
18
- styles?: StyleSet;
19
21
  duration?: number;
22
+ loop?: boolean;
23
+ tilt?: number;
24
+ autoRotate?: boolean;
25
+ styles?: StyleSet;
20
26
  };
21
27
 
22
28
  /**
23
29
  * Iterates between points.
24
30
  * Inspired by: https://observablehq.com/@mbostock/top-100-cities
25
31
  */
26
- export const useTour = (controller?: GlobeController | null, features?: Features, options: TourOptions = {}) => {
32
+ export const useTour = (
33
+ controller?: GlobeController | null,
34
+ points?: LatLng[],
35
+ options: TourOptions = {},
36
+ ): [boolean, Dispatch<SetStateAction<boolean>>] => {
27
37
  const selection = d3.selection();
28
- const [running, setRunning] = useState(false);
38
+ const [running, setRunning] = useState(options.running ?? false);
29
39
  useEffect(() => {
30
40
  if (!running) {
31
41
  selection.interrupt(TRANSITION_NAME);
@@ -39,10 +49,15 @@ export const useTour = (controller?: GlobeController | null, features?: Features
39
49
  const context = canvas.getContext('2d', { alpha: false });
40
50
  const path = d3.geoPath(projection, context).pointRadius(2);
41
51
 
42
- const tilt = 0;
52
+ const tilt = options.tilt ?? 0;
43
53
  let last: LatLng;
44
54
  try {
45
- for (const next of features.points) {
55
+ const p = [...points];
56
+ if (options.loop) {
57
+ p.push(p[0]);
58
+ }
59
+
60
+ for (const next of p) {
46
61
  if (!running) {
47
62
  break;
48
63
  }
@@ -83,8 +98,10 @@ export const useTour = (controller?: GlobeController | null, features?: Features
83
98
  context.restore();
84
99
 
85
100
  // TODO(burdon): This has to come after rendering above. Add to features to correct order?
86
- projection.rotate(iv(t));
87
- setRotation(projection.rotate());
101
+ if (options.autoRotate) {
102
+ projection.rotate(iv(t));
103
+ setRotation(projection.rotate());
104
+ }
88
105
  });
89
106
 
90
107
  // Throws if interrupted.
@@ -92,6 +109,8 @@ export const useTour = (controller?: GlobeController | null, features?: Features
92
109
  last = next;
93
110
  }
94
111
  } catch (err) {
112
+ log.catch(err);
113
+ } finally {
95
114
  setRunning(false);
96
115
  }
97
116
  });
@@ -101,14 +120,7 @@ export const useTour = (controller?: GlobeController | null, features?: Features
101
120
  selection.interrupt(TRANSITION_NAME);
102
121
  };
103
122
  }
104
- }, [controller, running]);
105
-
106
- return [
107
- () => {
108
- if (!options.disabled) {
109
- setRunning(true);
110
- }
111
- },
112
- () => setRunning(false),
113
- ];
123
+ }, [controller, running, JSON.stringify(options)]);
124
+
125
+ return [running, setRunning];
114
126
  };
package/src/util/debug.ts CHANGED
@@ -12,5 +12,6 @@ export const timer = <T = void>(cb: () => T): T => {
12
12
  // eslint-disable-next-line no-console
13
13
  console.log({ t, data });
14
14
  }
15
+
15
16
  return data;
16
17
  };
@@ -11,7 +11,17 @@ import { type LatLng, geoLine, geoPoint } from './path';
11
11
 
12
12
  export type Styles = Record<string, any>;
13
13
 
14
- export type Style = 'water' | 'graticule' | 'land' | 'border' | 'dots' | 'point' | 'line' | 'cursor' | 'arc';
14
+ export type Style =
15
+ | 'background'
16
+ | 'water'
17
+ | 'graticule'
18
+ | 'land'
19
+ | 'border'
20
+ | 'dots'
21
+ | 'point'
22
+ | 'line'
23
+ | 'cursor'
24
+ | 'arc';
15
25
 
16
26
  export type StyleSet = Partial<Record<Style, Styles>>;
17
27
 
@@ -108,16 +118,20 @@ export const createLayers = (topology: Topology, features: Features, styles: Sty
108
118
  /**
109
119
  * Render layers created above.
110
120
  */
111
- export const renderLayers = (generator: GeoPath, layers: Layer[] = [], scale: number) => {
121
+ export const renderLayers = (generator: GeoPath, layers: Layer[] = [], scale: number, styles: StyleSet) => {
112
122
  const context: CanvasRenderingContext2D = generator.context();
113
123
  const {
114
124
  canvas: { width, height },
115
125
  } = context;
116
126
  context.reset();
117
127
 
118
- // TODO(burdon): Option.
119
128
  // Clear background.
120
- context.clearRect(0, 0, width, height);
129
+ if (styles.background) {
130
+ context.fillStyle = styles.background.fillStyle;
131
+ context.fillRect(0, 0, width, height);
132
+ } else {
133
+ context.clearRect(0, 0, width, height);
134
+ }
121
135
 
122
136
  // Render features.
123
137
  // https://github.com/d3/d3-geo#_path