@vinctus/react-maplibre 0.1.1 → 0.1.3

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.
package/dist/Map.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React, { FC, RefAttributes, ReactNode, CSSProperties } from 'react';
2
- import maplibre, { MapLibreEvent, StyleSpecification } from 'maplibre-gl';
2
+ import maplibre, { MapLibreEvent, MapOptions, StyleSpecification } from 'maplibre-gl';
3
3
  export type ViewState = {
4
4
  longitude: number;
5
5
  latitude: number;
@@ -19,7 +19,7 @@ export declare const viewStateFromMap: (map: maplibre.Map) => {
19
19
  };
20
20
  export declare const useMap: () => {
21
21
  map: maplibre.Map | null;
22
- setMap: React.Dispatch<React.SetStateAction<maplibre.Map | null>>;
22
+ mapRef: React.MutableRefObject<maplibre.Map | null>;
23
23
  mapLoaded: boolean;
24
24
  setMapLoaded: React.Dispatch<React.SetStateAction<boolean>>;
25
25
  };
@@ -27,6 +27,7 @@ export declare const MapProvider: FC<{
27
27
  children?: ReactNode;
28
28
  }>;
29
29
  export type MapProps = {
30
+ reuseMaps?: boolean;
30
31
  accessToken?: string;
31
32
  antialias?: boolean;
32
33
  children?: ReactNode | ReactNode[];
@@ -44,5 +45,6 @@ export type MapProps = {
44
45
  ref?: RefAttributes<maplibre.Map | null>;
45
46
  style?: CSSProperties;
46
47
  zoom: number;
48
+ options?: MapOptions;
47
49
  };
48
50
  export declare const Map: React.ForwardRefExoticComponent<Omit<MapProps, "ref"> & React.RefAttributes<maplibre.Map | null>>;
package/dist/Map.js CHANGED
@@ -1,14 +1,3 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
1
  import { jsx as _jsx } from "react/jsx-runtime";
13
2
  import React, { useRef, useEffect, useState, useContext, useImperativeHandle, useMemo, } from 'react';
14
3
  import maplibre from 'maplibre-gl';
@@ -16,10 +5,7 @@ import { ErrorBoundary } from './ErrorBoundary'; // eslint-disable-line import/n
16
5
  const DEFAULT_STYLE = 'https://demotiles.maplibre.org/style.json';
17
6
  const DISABLE_PITCH = 0;
18
7
  const MapContext = React.createContext({
19
- map: null,
20
- setMap: () => {
21
- throw new Error('Use <MapProvider>');
22
- },
8
+ mapRef: { current: null },
23
9
  mapLoaded: false,
24
10
  setMapLoaded: () => {
25
11
  throw new Error('Use <MapProvider>');
@@ -30,51 +16,91 @@ export const viewStateFromMap = (map) => ({
30
16
  latitude: map.getCenter().lat,
31
17
  zoom: map.getZoom(),
32
18
  });
33
- export const useMap = () => useContext(MapContext);
19
+ export const useMap = () => {
20
+ const context = useContext(MapContext);
21
+ if (!context) {
22
+ throw new Error('useMap must be used within a MapProvider');
23
+ }
24
+ const { mapRef, mapLoaded, setMapLoaded } = context;
25
+ return {
26
+ map: mapRef.current,
27
+ mapRef,
28
+ mapLoaded,
29
+ setMapLoaded,
30
+ };
31
+ };
34
32
  export const MapProvider = ({ children }) => {
35
- const [map, setMap] = useState(null);
33
+ const mapRef = useRef(null);
36
34
  const [mapLoaded, setMapLoaded] = useState(false);
37
- return (_jsx(MapContext.Provider, Object.assign({ value: { map, setMap, mapLoaded, setMapLoaded } }, { children: children })));
35
+ return (_jsx(MapContext.Provider, Object.assign({ value: { mapRef, mapLoaded, setMapLoaded } }, { children: children })));
38
36
  };
39
37
  function addEventHandler(map, type, handler) {
40
38
  map.on(type, (ev) => handler(Object.assign({ viewState: viewStateFromMap(map) }, ev)));
41
39
  }
42
- export const Map = React.forwardRef((_a, ref) => {
43
- var { children, style, id, longitude, latitude, maxPitch = DISABLE_PITCH, mapStyle = DEFAULT_STYLE, zoom, onDragEnd, onLoad, onMoveEnd, onZoomEnd, fallback } = _a, options = __rest(_a, ["children", "style", "id", "longitude", "latitude", "maxPitch", "mapStyle", "zoom", "onDragEnd", "onLoad", "onMoveEnd", "onZoomEnd", "fallback"]);
40
+ export const Map = React.forwardRef(({ reuseMaps, children, style, id, longitude, latitude, maxPitch = DISABLE_PITCH, mapStyle = DEFAULT_STYLE, zoom, onDragEnd, onLoad, onMoveEnd, onZoomEnd, fallback, options = {}, }, ref) => {
44
41
  const mapContainer = useRef(null);
45
- const { map, setMap, setMapLoaded } = useMap();
46
- useImperativeHandle(ref, () => map, [map]);
42
+ const { mapRef, mapLoaded, setMapLoaded } = useMap();
43
+ useImperativeHandle(ref, () => mapRef.current, [
44
+ mapRef,
45
+ ]);
47
46
  useEffect(() => {
48
- if (map)
49
- return; // initialize map only once
50
- if (mapContainer.current) {
47
+ if (reuseMaps && mapRef.current) {
48
+ // Reattach the existing map instance to the new container
49
+ if (mapContainer.current && mapRef.current.getContainer() !== mapContainer.current) {
50
+ mapContainer.current.appendChild(mapRef.current.getContainer());
51
+ mapRef.current.resize(); // resize map in case container dimensions have changed
52
+ }
53
+ return;
54
+ }
55
+ if (!reuseMaps || (mapContainer.current && !mapRef.current)) {
51
56
  const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, options));
52
57
  if (onDragEnd)
53
58
  addEventHandler(m, 'dragend', onDragEnd);
54
59
  if (onLoad)
55
60
  addEventHandler(m, 'load', (ev) => {
56
61
  onLoad(ev);
57
- setMapLoaded(true);
62
+ if (!mapLoaded)
63
+ setMapLoaded(true);
58
64
  });
59
65
  if (onMoveEnd)
60
66
  addEventHandler(m, 'moveend', onMoveEnd);
61
67
  if (onZoomEnd)
62
68
  addEventHandler(m, 'zoomend', onZoomEnd);
63
- setMap(m);
69
+ mapRef.current = m;
64
70
  }
71
+ // further investigation: calling .remove() causes the following error:
72
+ //
73
+ // Cannot read properties of undefined (reading 'off')
74
+ // TypeError: Cannot read properties of undefined (reading 'off')
75
+ // at t.GeolocateControl.onRemove (http://localhost:3002/static/js/bundle.js:107201:393)
76
+ // at t.Map.removeControl (http://localhost:3002/static/js/bundle.js:107351:57)
77
+ // at http://localhost:3002/static/js/bundle.js:853:53
78
+ // at safelyCallDestroy (http://localhost:3002/static/js/bundle.js:39324:9)
79
+ // at commitHookEffectListUnmount (http://localhost:3002/static/js/bundle.js:39462:15)
80
+ // at commitPassiveUnmountInsideDeletedTreeOnFiber (http://localhost:3002/static/js/bundle.js:41143:15)
81
+ // at commitPassiveUnmountEffectsInsideOfDeletedTree_begin (http://localhost:3002/static/js/bundle.js:41099:9)
82
+ // at commitPassiveUnmountEffects_begin (http://localhost:3002/static/js/bundle.js:41021:15)
83
+ // at commitPassiveUnmountEffects (http://localhost:3002/static/js/bundle.js:41009:7)
84
+ // at flushPassiveEffectsImpl (http://localhost:3002/static/js/bundle.js:42830:7)
85
+ //
86
+ // return () => {
87
+ // if (!reuseMaps && mapRef.current) {
88
+ // mapRef.current.remove()
89
+ // mapRef.current = null
90
+ // }
91
+ // }
65
92
  }, [
66
- map,
93
+ reuseMaps,
67
94
  onDragEnd,
68
95
  onLoad,
69
96
  onMoveEnd,
70
97
  onZoomEnd,
71
- setMap,
72
98
  setMapLoaded,
73
99
  mapStyle,
74
100
  longitude,
75
101
  latitude,
76
102
  zoom,
77
- options,
103
+ // options, // investigate using a useRef for this (other Map options)
78
104
  ]);
79
105
  const memoizedStyle = useMemo(() => (Object.assign({ position: 'relative', width: '100%', height: '100%' }, style)), [style]);
80
106
  return (_jsx(ErrorBoundary, Object.assign({ fallback: fallback }, { children: _jsx("div", Object.assign({ id: id, ref: mapContainer, style: memoizedStyle }, { children: children })) })));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vinctus/react-maplibre",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Minimalist MapLibre GL JS React wrapper library",
5
5
  "types": "dist/index.d.ts",
6
6
  "publishConfig": {