@macrostrat/map-interface 0.1.0 → 0.2.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.
@@ -1,8 +1,8 @@
1
1
  import hyper from "@macrostrat/hyper";
2
2
  import {
3
3
  useMapRef,
4
- useMapStatus,
5
4
  useMapDispatch,
5
+ useMapPosition,
6
6
  } from "@macrostrat/mapbox-react";
7
7
  import {
8
8
  mapViewInfo,
@@ -11,7 +11,7 @@ import {
11
11
  } from "@macrostrat/mapbox-utils";
12
12
  import classNames from "classnames";
13
13
  import mapboxgl from "mapbox-gl";
14
- import { useEffect, useRef } from "react";
14
+ import { useEffect, useRef, useCallback } from "react";
15
15
  import styles from "./main.module.sass";
16
16
  import rootStyles from "../main.module.sass";
17
17
  import { enable3DTerrain } from "./terrain";
@@ -21,6 +21,7 @@ import {
21
21
  MapPaddingManager,
22
22
  MapResizeManager,
23
23
  } from "../helpers";
24
+ import "mapbox-gl/dist/mapbox-gl.css";
24
25
 
25
26
  const h = hyper.styled({ ...styles, ...rootStyles });
26
27
 
@@ -36,9 +37,16 @@ export interface MapViewProps extends MapboxCoreOptions {
36
37
  //style: mapboxgl.Style | string;
37
38
  //transformRequest?: mapboxgl.TransformRequestFunction;
38
39
  mapPosition?: MapPosition;
40
+ onMapLoad?: (map: mapboxgl.Map) => void;
39
41
  }
40
42
 
41
- function initializeMap(container, args: MapboxCoreOptions = {}) {
43
+ export interface MapboxOptionsExt extends MapboxCoreOptions {
44
+ mapPosition?: MapPosition;
45
+ }
46
+
47
+ function defaultInitializeMap(container, args: MapboxOptionsExt = {}) {
48
+ const { mapPosition, ...rest } = args;
49
+
42
50
  const map = new mapboxgl.Map({
43
51
  container,
44
52
  maxZoom: 18,
@@ -47,9 +55,14 @@ function initializeMap(container, args: MapboxCoreOptions = {}) {
47
55
  trackResize: true,
48
56
  antialias: true,
49
57
  optimizeForTerrain: true,
50
- ...args,
58
+ ...rest,
51
59
  });
52
60
 
61
+ // set initial map position
62
+ if (mapPosition != null) {
63
+ setMapPosition(map, mapPosition);
64
+ }
65
+
53
66
  //setMapPosition(map, mapPosition);
54
67
  return map;
55
68
  }
@@ -67,12 +80,16 @@ export function MapView(props: MapViewProps) {
67
80
  const {
68
81
  enableTerrain = true,
69
82
  style,
70
- transformRequest,
71
83
  mapPosition = defaultMapPosition,
84
+ initializeMap = defaultInitializeMap,
72
85
  children,
73
86
  accessToken,
74
87
  infoMarkerPosition,
88
+ transformRequest,
75
89
  projection,
90
+ onMapLoaded = null,
91
+ onStyleLoaded = null,
92
+ ...rest
76
93
  } = props;
77
94
  if (enableTerrain) {
78
95
  terrainSourceID ??= "mapbox-3d-dem";
@@ -90,35 +107,40 @@ export function MapView(props: MapViewProps) {
90
107
  // Keep track of map position for reloads
91
108
 
92
109
  useEffect(() => {
93
- if (style == null || ref.current == null || dispatch == null) return;
94
- if (mapRef?.current != null) return;
95
- console.log("Initializing map");
110
+ if (style == null) return;
111
+ if (mapRef.current != null) {
112
+ console.log("Setting style", style);
113
+ mapRef.current.setStyle(style);
114
+ return;
115
+ }
96
116
  const map = initializeMap(ref.current, {
97
117
  style,
98
- transformRequest,
99
118
  projection,
119
+ mapPosition,
120
+ ...rest,
121
+ });
122
+ map.on("style.load", () => {
123
+ onStyleLoaded?.(map);
124
+ dispatch({ type: "set-style-loaded", payload: true });
100
125
  });
126
+ onMapLoaded?.(map);
101
127
  dispatch({ type: "set-map", payload: map });
102
- console.log("Map initialized");
103
- return () => {
104
- map.remove();
105
- dispatch({ type: "set-map", payload: null });
106
- };
107
- }, [transformRequest, dispatch, style]);
128
+ }, [style]);
108
129
 
109
130
  // Map style updating
110
- useEffect(() => {
111
- if (mapRef?.current == null || style == null) return;
112
- mapRef?.current?.setStyle(style);
113
- }, [mapRef.current, style]);
114
-
115
- useEffect(() => {
116
- const map = mapRef.current;
117
- if (map == null || mapPosition == null) return;
118
- setMapPosition(map, mapPosition);
119
- }, [mapRef.current]);
120
-
121
- const { mapPosition: _computedMapPosition } = useMapStatus();
131
+ // useEffect(() => {
132
+ // if (mapRef?.current == null || style == null) return;
133
+ // mapRef?.current?.setStyle(style);
134
+ // }, [mapRef.current, style]);
135
+
136
+ // Set map position if it changes
137
+ // useEffect(() => {
138
+ // const map = mapRef.current;
139
+ // if (map == null || mapPosition == null) return;
140
+ // setMapPosition(map, mapPosition);
141
+ // }, [mapPosition]);
142
+
143
+ const _computedMapPosition = useMapPosition();
122
144
  const { mapUse3D, mapIsRotated } = mapViewInfo(_computedMapPosition);
123
145
 
124
146
  // Get map projection
@@ -3,6 +3,16 @@
3
3
  position: relative
4
4
  overflow: hidden
5
5
 
6
+ .mapbox-map
7
+ position: absolute
8
+ top: 0
9
+ bottom: 0
10
+ left: 0
11
+ right: 0
12
+ &:global(.mapboxgl-map)
13
+ // override the default mapbox position: relative in all cases
14
+ position: absolute
15
+
6
16
  /* Desktop styling is necessarily much more complicated than mobile
7
17
  to handle a two-column layout. */
8
18
  @media screen and (min-width: 768px)