@vinctus/react-maplibre 0.1.4 → 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.
package/dist/Map.d.ts CHANGED
@@ -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
- mapRef: React.MutableRefObject<maplibre.Map | null>;
22
+ setMap: React.Dispatch<React.SetStateAction<maplibre.Map | null>>;
23
23
  mapLoaded: boolean;
24
24
  setMapLoaded: React.Dispatch<React.SetStateAction<boolean>>;
25
25
  };
@@ -27,7 +27,6 @@ export declare const MapProvider: FC<{
27
27
  children?: ReactNode;
28
28
  }>;
29
29
  export type MapProps = {
30
- reuseMaps?: boolean;
31
30
  accessToken?: string;
32
31
  antialias?: boolean;
33
32
  children?: ReactNode | ReactNode[];
@@ -45,6 +44,5 @@ export type MapProps = {
45
44
  ref?: RefAttributes<maplibre.Map | null>;
46
45
  style?: CSSProperties;
47
46
  zoom: number;
48
- options?: any;
49
47
  };
50
48
  export declare const Map: React.ForwardRefExoticComponent<Omit<MapProps, "ref"> & React.RefAttributes<maplibre.Map | null>>;
package/dist/Map.js CHANGED
@@ -1,3 +1,14 @@
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
+ };
1
12
  import { jsx as _jsx } from "react/jsx-runtime";
2
13
  import React, { useRef, useEffect, useState, useContext, useImperativeHandle, useMemo, } from 'react';
3
14
  import maplibre from 'maplibre-gl';
@@ -5,7 +16,10 @@ import { ErrorBoundary } from './ErrorBoundary'; // eslint-disable-line import/n
5
16
  const DEFAULT_STYLE = 'https://demotiles.maplibre.org/style.json';
6
17
  const DISABLE_PITCH = 0;
7
18
  const MapContext = React.createContext({
8
- mapRef: { current: null },
19
+ map: null,
20
+ setMap: () => {
21
+ throw new Error('Use <MapProvider>');
22
+ },
9
23
  mapLoaded: false,
10
24
  setMapLoaded: () => {
11
25
  throw new Error('Use <MapProvider>');
@@ -16,92 +30,52 @@ export const viewStateFromMap = (map) => ({
16
30
  latitude: map.getCenter().lat,
17
31
  zoom: map.getZoom(),
18
32
  });
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
- };
33
+ export const useMap = () => useContext(MapContext);
32
34
  export const MapProvider = ({ children }) => {
33
- const mapRef = useRef(null);
35
+ const [map, setMap] = useState(null);
34
36
  const [mapLoaded, setMapLoaded] = useState(false);
35
- return (_jsx(MapContext.Provider, Object.assign({ value: { mapRef, mapLoaded, setMapLoaded } }, { children: children })));
37
+ return (_jsx(MapContext.Provider, { value: { map, setMap, mapLoaded, setMapLoaded }, children: children }));
36
38
  };
37
39
  function addEventHandler(map, type, handler) {
38
40
  map.on(type, (ev) => handler(Object.assign({ viewState: viewStateFromMap(map) }, ev)));
39
41
  }
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) => {
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"]);
41
44
  const mapContainer = useRef(null);
42
- const { mapRef, mapLoaded, setMapLoaded } = useMap();
43
- useImperativeHandle(ref, () => mapRef.current, [
44
- mapRef,
45
- ]);
45
+ const { map, setMap, setMapLoaded } = useMap();
46
+ useImperativeHandle(ref, () => map, [map]);
46
47
  useEffect(() => {
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)) {
48
+ if (map)
49
+ return; // initialize map only once
50
+ if (mapContainer.current) {
56
51
  const m = new maplibre.Map(Object.assign({ container: mapContainer.current, style: mapStyle, center: [longitude, latitude], zoom }, options));
57
52
  if (onDragEnd)
58
53
  addEventHandler(m, 'dragend', onDragEnd);
59
54
  if (onLoad)
60
55
  addEventHandler(m, 'load', (ev) => {
61
56
  onLoad(ev);
62
- if (!mapLoaded)
63
- setMapLoaded(true);
57
+ setMapLoaded(true);
64
58
  });
65
59
  if (onMoveEnd)
66
60
  addEventHandler(m, 'moveend', onMoveEnd);
67
61
  if (onZoomEnd)
68
62
  addEventHandler(m, 'zoomend', onZoomEnd);
69
- mapRef.current = m;
63
+ setMap(m);
70
64
  }
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
- // }
92
65
  }, [
93
- reuseMaps,
66
+ map,
94
67
  onDragEnd,
95
68
  onLoad,
96
69
  onMoveEnd,
97
70
  onZoomEnd,
71
+ setMap,
98
72
  setMapLoaded,
99
73
  mapStyle,
100
74
  longitude,
101
75
  latitude,
102
76
  zoom,
103
- // options, // investigate using a useRef for this (other Map options)
77
+ options,
104
78
  ]);
105
79
  const memoizedStyle = useMemo(() => (Object.assign({ position: 'relative', width: '100%', height: '100%' }, style)), [style]);
106
- return (_jsx(ErrorBoundary, Object.assign({ fallback: fallback }, { children: _jsx("div", Object.assign({ id: id, ref: mapContainer, style: memoizedStyle }, { children: children })) })));
80
+ return (_jsx(ErrorBoundary, { fallback: fallback, children: _jsx("div", { id: id, ref: mapContainer, style: memoizedStyle, children: children }) }));
107
81
  });
@@ -0,0 +1 @@
1
+ {"root":["../src/ErrorBoundary.tsx","../src/GeolocateControl.tsx","../src/Layer.tsx","../src/Map.tsx","../src/Marker.tsx","../src/Popup.tsx","../src/ScaleControl.tsx","../src/index.tsx","../src/types.tsx"],"version":"5.7.2"}
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@vinctus/react-maplibre",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "description": "Minimalist MapLibre GL JS React wrapper library",
5
+ "type": "module",
5
6
  "types": "dist/index.d.ts",
6
7
  "publishConfig": {
7
8
  "access": "public"
@@ -13,25 +14,21 @@
13
14
  ".": "./dist/index.js",
14
15
  "./dist/*": "./dist/*.js"
15
16
  },
16
- "dependencies": {
17
- "react-scripts": "5.0.1"
18
- },
19
17
  "peerDependencies": {
20
- "maplibre-gl": "^4.5.2",
18
+ "maplibre-gl": "^4.7.1",
21
19
  "react": "^18.3.1",
22
20
  "react-dom": "^18.3.1"
23
21
  },
24
22
  "devDependencies": {
25
- "@types/node": "^18.14.6",
23
+ "@types/node": "^22.10.2",
26
24
  "@types/react": "^18.3.3",
27
25
  "@types/react-dom": "^18.3.0",
28
- "typescript": "^4.9.5"
26
+ "typescript": "^5.7.2",
27
+ "vite": "^6.0.3"
29
28
  },
30
29
  "scripts": {
31
- "start": "react-scripts start",
32
- "build": "react-scripts build",
33
- "test": "react-scripts test",
34
- "eject": "react-scripts eject",
30
+ "dev": "vite",
31
+ "build": "tsc -b && vite build",
35
32
  "prepublishOnly": "tsc"
36
33
  },
37
34
  "eslintConfig": {