@mapcomponents/react-maplibre 0.1.92 → 0.1.94

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Change Log
2
2
 
3
+ ## [v0.1.93] - 2024-01-17
4
+
5
+ ### Fixed
6
+
7
+ - 0af148b: remove conditional use of useLayer hook for label layer in MlGeoJsonLayer
8
+ - 718afe6: fix useSource cleanup error
9
+ - c6856e6: fix MlTemporalController to work with useLayer, MlGeoJsonLayer adjustments
10
+
3
11
  ## [v0.1.92] - 2024-01-16
4
12
 
5
13
  ### Fixed
@@ -1,5 +1,5 @@
1
1
  import { useMapType } from './useMap';
2
- import { SourceSpecification, LayerSpecification, MapMouseEvent, GeoJSONFeature, Style, MapEventType, Map, VideoSourceSpecification, ImageSourceSpecification } from 'maplibre-gl';
2
+ import { GeoJSONSourceSpecification, LayerSpecification, MapMouseEvent, GeoJSONFeature, Style, MapEventType, Map, FilterSpecification } from 'maplibre-gl';
3
3
  import MapLibreGlWrapper from '../components/MapLibreMap/lib/MapLibreGlWrapper';
4
4
  import { GeoJSONObject } from '@turf/turf';
5
5
  type getLayerType = Style['getLayer'];
@@ -21,8 +21,9 @@ export interface useLayerProps {
21
21
  insertBeforeFirstSymbolLayer?: boolean;
22
22
  geojson?: GeoJSONObject;
23
23
  options: Partial<LayerSpecification & {
24
- source?: Partial<Exclude<SourceSpecification, VideoSourceSpecification | ImageSourceSpecification>>;
24
+ source?: GeoJSONSourceSpecification | string;
25
25
  id?: string;
26
+ filter?: FilterSpecification;
26
27
  }>;
27
28
  onHover?: (ev: MapEventType & unknown) => Map | void;
28
29
  onClick?: (ev: MapEventType & unknown) => Map | void;
package/dist/index.esm.js CHANGED
@@ -2437,7 +2437,7 @@ function useLayer(props) {
2437
2437
  var initializedRef = React.useRef(false);
2438
2438
  var layerId = React.useRef(props.layerId || (props.idPrefix ? props.idPrefix : 'Layer-') + mapHook.componentId);
2439
2439
  var createLayer = React.useCallback(function () {
2440
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
2440
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
2441
2441
  if (!mapHook.map || (mapHook === null || mapHook === void 0 ? void 0 : mapHook.map.cancelled))
2442
2442
  return;
2443
2443
  if (mapHook.map.map.getLayer(layerId.current)) {
@@ -2451,26 +2451,35 @@ function useLayer(props) {
2451
2451
  return;
2452
2452
  }
2453
2453
  }
2454
+ if (typeof ((_b = props === null || props === void 0 ? void 0 : props.options) === null || _b === void 0 ? void 0 : _b.source) !== 'string' &&
2455
+ !props.geojson &&
2456
+ !((_d = (_c = props === null || props === void 0 ? void 0 : props.options) === null || _c === void 0 ? void 0 : _c.source) === null || _d === void 0 ? void 0 : _d.data)) {
2457
+ return;
2458
+ }
2454
2459
  if (typeof props.options.type === 'undefined') {
2455
2460
  return;
2456
2461
  }
2457
2462
  initializedRef.current = true;
2458
2463
  try {
2459
2464
  mapHook.map.addLayer(__assign(__assign(__assign(__assign({}, props.options), (props.geojson &&
2460
- (!((_b = props.options) === null || _b === void 0 ? void 0 : _b.source) ||
2461
- (((_d = (_c = props.options) === null || _c === void 0 ? void 0 : _c.source) === null || _d === void 0 ? void 0 : _d.attribution) && !((_f = (_e = props.options) === null || _e === void 0 ? void 0 : _e.source) === null || _f === void 0 ? void 0 : _f.type))) // if either options.source isn't defined or only options.source.attribution is defined
2465
+ (!((_e = props.options) === null || _e === void 0 ? void 0 : _e.source) ||
2466
+ (typeof ((_f = props === null || props === void 0 ? void 0 : props.options) === null || _f === void 0 ? void 0 : _f.source) !== 'string' &&
2467
+ ((_h = (_g = props.options) === null || _g === void 0 ? void 0 : _g.source) === null || _h === void 0 ? void 0 : _h.attribution) &&
2468
+ !((_k = (_j = props.options) === null || _j === void 0 ? void 0 : _j.source) === null || _k === void 0 ? void 0 : _k.type))) // if either options.source isn't defined or only options.source.attribution is defined
2462
2469
  ? {
2463
2470
  source: {
2464
2471
  type: 'geojson',
2465
2472
  data: props.geojson,
2466
- attribution: ((_g = props.options.source) === null || _g === void 0 ? void 0 : _g.attribution)
2467
- ? (_h = props.options.source) === null || _h === void 0 ? void 0 : _h.attribution
2473
+ attribution: typeof ((_l = props === null || props === void 0 ? void 0 : props.options) === null || _l === void 0 ? void 0 : _l.source) !== 'string' && ((_m = props.options.source) === null || _m === void 0 ? void 0 : _m.attribution)
2474
+ ? (_o = props.options.source) === null || _o === void 0 ? void 0 : _o.attribution
2468
2475
  : '',
2469
2476
  },
2477
+ // eslint-disable-next-line no-mixed-spaces-and-tabs
2470
2478
  }
2471
- : {})), (typeof ((_j = props.options) === null || _j === void 0 ? void 0 : _j.source) === 'string'
2479
+ : {})), (typeof ((_p = props.options) === null || _p === void 0 ? void 0 : _p.source) === 'string'
2472
2480
  ? {
2473
2481
  source: props.options.source,
2482
+ // eslint-disable-next-line no-mixed-spaces-and-tabs
2474
2483
  }
2475
2484
  : {})), { id: layerId.current }), props.insertBeforeLayer
2476
2485
  ? props.insertBeforeLayer
@@ -2495,18 +2504,19 @@ function useLayer(props) {
2495
2504
  mapHook.map.on('styledata', function () {
2496
2505
  var _a;
2497
2506
  if (initializedRef.current && !((_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map.getLayer(layerId.current))) {
2498
- console.log('Recreate Layer');
2499
2507
  createLayer();
2500
2508
  }
2501
2509
  }, mapHook.componentId);
2502
- layerPaintConfRef.current = JSON.stringify((_k = props.options) === null || _k === void 0 ? void 0 : _k.paint);
2503
- layerLayoutConfRef.current = JSON.stringify((_l = props.options) === null || _l === void 0 ? void 0 : _l.layout);
2510
+ layerPaintConfRef.current = JSON.stringify((_q = props.options) === null || _q === void 0 ? void 0 : _q.paint);
2511
+ layerLayoutConfRef.current = JSON.stringify((_r = props.options) === null || _r === void 0 ? void 0 : _r.layout);
2504
2512
  layerTypeRef.current = props.options.type;
2505
- }, [props, mapHook.map]);
2513
+ }, [props, mapHook]);
2506
2514
  React.useEffect(function () {
2507
2515
  var _a, _b, _c, _d;
2508
2516
  if (!mapHook.map)
2509
2517
  return;
2518
+ if (!props.geojson && !props.options.source)
2519
+ return;
2510
2520
  if (((_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.cancelled) === false &&
2511
2521
  initializedRef.current &&
2512
2522
  ((_d = (_c = (_b = mapHook === null || mapHook === void 0 ? void 0 : mapHook.map) === null || _b === void 0 ? void 0 : _b.map) === null || _c === void 0 ? void 0 : _c.getLayer) === null || _d === void 0 ? void 0 : _d.call(_c, layerId.current)) &&
@@ -2516,7 +2526,7 @@ function useLayer(props) {
2516
2526
  return;
2517
2527
  }
2518
2528
  createLayer();
2519
- }, [mapHook.map, mapHook.mapIsReady, props.options, createLayer]);
2529
+ }, [mapHook.map, mapHook.mapIsReady, props, createLayer]);
2520
2530
  React.useEffect(function () {
2521
2531
  var _a, _b, _c, _d, _e, _f;
2522
2532
  if (((_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.cancelled) === true ||
@@ -2705,27 +2715,41 @@ function useSource(props) {
2705
2715
  var mapHook = useMap({
2706
2716
  mapId: props.mapId,
2707
2717
  });
2708
- var initializedRef = React.useRef(false);
2709
2718
  var _b = React.useState(), source = _b[0], setSource = _b[1];
2710
2719
  var sourceId = React.useRef(props.sourceId || (props.idPrefix ? props.idPrefix : 'Source-') + mapHook.componentId);
2720
+ var removeSource = React.useCallback(function () {
2721
+ var _a, _b;
2722
+ if (mapHook.map && ((_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.style) === null || _b === void 0 ? void 0 : _b._layers)) {
2723
+ for (var _i = 0, _c = Object.entries(mapHook.map.map.style._layers); _i < _c.length; _i++) {
2724
+ var _d = _c[_i], layerId = _d[0], layer = _d[1];
2725
+ if (layer.source === sourceId.current) {
2726
+ mapHook.map.removeLayer(layerId);
2727
+ }
2728
+ }
2729
+ if (mapHook.map.getSource(sourceId.current)) {
2730
+ mapHook.map.removeSource(sourceId.current);
2731
+ }
2732
+ }
2733
+ }, [mapHook.map]);
2711
2734
  var createSource = React.useCallback(function () {
2712
2735
  var _a;
2713
2736
  if (!mapHook.map)
2714
2737
  return;
2715
- initializedRef.current = true;
2738
+ if (props.source.type === 'geojson' && !props.source.data)
2739
+ return;
2716
2740
  if (mapHook.map.map.getSource(sourceId.current)) {
2717
- mapHook.cleanup();
2741
+ removeSource();
2718
2742
  }
2719
- (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.addSource(sourceId.current, __assign({}, props.source), mapHook.componentId);
2743
+ (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.addSource(sourceId.current, __assign({}, props.source));
2720
2744
  setSource(mapHook.map.map.getSource(sourceId.current));
2721
- }, [props, mapHook.map]);
2745
+ }, [props, mapHook, removeSource]);
2722
2746
  React.useEffect(function () {
2723
2747
  var _a, _b, _c, _d;
2724
- if (!initializedRef.current || !((_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.getSource(props.sourceId)))
2748
+ if (!((_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.getSource(sourceId.current)))
2725
2749
  return;
2726
2750
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2727
2751
  //@ts-ignore setData only exists on GeoJsonSource
2728
- (_d = (_c = mapHook.map.map.getSource(props.sourceId)) === null || _c === void 0 ? void 0 : _c.setData) === null || _d === void 0 ? void 0 : _d.call(_c, props.source.data);
2752
+ (_d = (_c = mapHook.map.map.getSource(sourceId.current)) === null || _c === void 0 ? void 0 : _c.setData) === null || _d === void 0 ? void 0 : _d.call(_c, props.source.data);
2729
2753
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2730
2754
  //@ts-ignore data only exists on GeoJsonSource
2731
2755
  }, [(_a = props.source) === null || _a === void 0 ? void 0 : _a.data]);
@@ -2739,19 +2763,9 @@ function useSource(props) {
2739
2763
  //cleanup
2740
2764
  React.useEffect(function () {
2741
2765
  return function () {
2742
- var _a, _b, _c;
2743
- initializedRef.current = false;
2744
- if (mapHook.map && ((_c = (_b = (_a = mapHook.map) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.style) === null || _c === void 0 ? void 0 : _c._layers)) {
2745
- for (var _i = 0, _d = Object.entries(mapHook.map.map.style._layers); _i < _d.length; _i++) {
2746
- var _e = _d[_i], layerId = _e[0], layer = _e[1];
2747
- if (layer.source === sourceId.current) {
2748
- mapHook.map.map.removeLayer(layerId);
2749
- }
2750
- }
2751
- mapHook.map.map.removeSource(sourceId.current);
2752
- }
2766
+ removeSource();
2753
2767
  };
2754
- }, [mapHook.map]);
2768
+ }, [removeSource]);
2755
2769
  return {
2756
2770
  map: mapHook.map,
2757
2771
  source: source,
@@ -2766,7 +2780,7 @@ function useSource(props) {
2766
2780
  * @component
2767
2781
  */
2768
2782
  var MlGeoJsonLayer = function (props) {
2769
- var _a, _b, _c;
2783
+ var _a, _b, _c, _d, _e;
2770
2784
  var layerType = props.type || getDefaulLayerTypeByGeometry(props.geojson);
2771
2785
  var layerId = React.useRef(props.layerId || 'MlGeoJsonLayer-' + uuid.v4());
2772
2786
  var labelLayerId = "label-".concat(layerId.current);
@@ -2780,7 +2794,7 @@ var MlGeoJsonLayer = function (props) {
2780
2794
  }, [props.layerId]);
2781
2795
  useSource({
2782
2796
  mapId: props.mapId,
2783
- sourceId: layerId.current,
2797
+ sourceId: "source-" + layerId.current,
2784
2798
  source: {
2785
2799
  type: 'geojson',
2786
2800
  data: props.geojson,
@@ -2789,22 +2803,22 @@ var MlGeoJsonLayer = function (props) {
2789
2803
  useLayer({
2790
2804
  mapId: props.mapId,
2791
2805
  layerId: layerId.current,
2792
- options: __assign(__assign({ source: layerId.current }, props.options), { paint: __assign(__assign({}, (props.paint || getDefaultPaintPropsByType(layerType, props.defaultPaintOverrides))), (_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.paint), layout: __assign(__assign({}, ((props === null || props === void 0 ? void 0 : props.layout) || {})), (_b = props === null || props === void 0 ? void 0 : props.options) === null || _b === void 0 ? void 0 : _b.layout), type: layerType }),
2806
+ options: __assign(__assign({ source: "source-" + layerId.current }, props.options), { paint: __assign(__assign({}, (props.paint || getDefaultPaintPropsByType(layerType, props.defaultPaintOverrides))), (_a = props === null || props === void 0 ? void 0 : props.options) === null || _a === void 0 ? void 0 : _a.paint), layout: __assign(__assign({}, ((props === null || props === void 0 ? void 0 : props.layout) || {})), (_b = props === null || props === void 0 ? void 0 : props.options) === null || _b === void 0 ? void 0 : _b.layout), type: layerType }),
2793
2807
  insertBeforeLayer: props.insertBeforeLayer,
2794
2808
  onHover: props.onHover,
2795
2809
  onClick: props.onClick,
2796
2810
  onLeave: props.onLeave,
2797
2811
  });
2798
- if (props.labelProp) {
2799
- useLayer({
2800
- mapId: props.mapId,
2801
- options: __assign(__assign({ source: props.labelProp ? layerId.current : undefined, id: labelLayerId, type: 'symbol', maxzoom: 24, minzoom: 1 }, ((props === null || props === void 0 ? void 0 : props.labelOptions) ? props.labelOptions : {})), { layout: __assign({ 'text-font': ['Open Sans Regular'], 'text-field': "{".concat(props.labelProp, "}") }, (((_c = props === null || props === void 0 ? void 0 : props.labelOptions) === null || _c === void 0 ? void 0 : _c.layout) ? props.labelOptions.layout : {})), paint: {
2802
- 'text-halo-width': 1,
2803
- 'text-halo-color': '#121212',
2804
- 'text-color': '#fefefe',
2805
- } }),
2806
- });
2807
- }
2812
+ // Label useLayer hook can't be called conditionally.
2813
+ // Using it with geojson and options.source undefined will cause it to return without creating a layer.
2814
+ useLayer({
2815
+ mapId: props.mapId,
2816
+ options: __assign(__assign(__assign({ source: props.labelProp ? "source-" + layerId.current : undefined, id: labelLayerId, type: 'symbol', maxzoom: 24, minzoom: 1 }, ((props === null || props === void 0 ? void 0 : props.labelOptions) ? props.labelOptions : {})), (((_c = props === null || props === void 0 ? void 0 : props.options) === null || _c === void 0 ? void 0 : _c.filter) ? { filter: props.options.filter } : {})), { layout: __assign(__assign({ 'text-font': ['Open Sans Regular'], 'text-field': "{".concat(props.labelProp, "}"), 'text-size': 12, 'text-anchor': 'top' }, (((_d = props === null || props === void 0 ? void 0 : props.labelOptions) === null || _d === void 0 ? void 0 : _d.layout) ? props.labelOptions.layout : {})), (((_e = props === null || props === void 0 ? void 0 : props.layout) === null || _e === void 0 ? void 0 : _e.visibility) ? { visibility: props.layout.visibility } : {})), paint: {
2817
+ 'text-halo-width': 1,
2818
+ 'text-halo-color': '#fefefe',
2819
+ 'text-color': '#121212',
2820
+ } }),
2821
+ });
2808
2822
  return React__default["default"].createElement(React__default["default"].Fragment, null);
2809
2823
  };
2810
2824
 
@@ -6253,13 +6267,18 @@ var MlTemporalController = function (props) {
6253
6267
  };
6254
6268
  }, [mapHook.map, props.onClick, props.onHover, props.onLeave]);
6255
6269
  return (React__default["default"].createElement(React__default["default"].Fragment, null,
6256
- filteredData && props.ownLayer && (React__default["default"].createElement(MlGeoJsonLayer, { type: props.type, mapId: props.mapId, layerId: "timeController", insertBeforeLayer: props.insertBeforeLayer || 'timeControllerLabels', geojson: filteredData, paint: props.paint ||
6270
+ filteredData && props.ownLayer && (React__default["default"].createElement(MlGeoJsonLayer, { type: props.type, mapId: props.mapId,
6271
+ //layerId="timeController"
6272
+ //insertBeforeLayer={props.insertBeforeLayer || 'timeControllerLabels'}
6273
+ paint: props.paint ||
6257
6274
  paint, options: {
6258
6275
  source: {
6276
+ type: 'geojson',
6259
6277
  attribution: props.attribution,
6278
+ data: filteredData
6260
6279
  },
6261
6280
  } })),
6262
- props.label && (React__default["default"].createElement(MlTemporalControllerLabels, { data: filteredData, currentVal: currentVal, fadeIn: props.labelFadeIn, fadeOut: props.labelFadeOut, step: props.step, labelField: labelField, labelColor: labelColor, timeField: props.timeField, minVal: minVal, accumulate: props.accumulate, isPlaying: isPlaying })),
6281
+ filteredData && props.label && (React__default["default"].createElement(MlTemporalControllerLabels, { data: filteredData, currentVal: currentVal, fadeIn: props.labelFadeIn, fadeOut: props.labelFadeOut, step: props.step, labelField: labelField, labelColor: labelColor, timeField: props.timeField, minVal: minVal, accumulate: props.accumulate, isPlaying: isPlaying })),
6263
6282
  React__default["default"].createElement(TemporalControllerPlayer, { currentVal: currentVal, isPlaying: isPlaying, step: props.step, interval: props.interval, minVal: minVal, maxVal: maxVal, returnCurrent: setCurrentVal, returnPlaying: setIsPlaying, open: false, fadeIn: props.fadeIn, fadeOut: props.fadeOut, featuresColor: featuresColor, labels: props.label, labelColor: labelColor, labelFadeIn: props.labelFadeIn, labelFadeOut: props.labelFadeOut, accumulate: props.accumulate, display: props.displayCurrentValue })));
6264
6283
  };
6265
6284
  MlTemporalController.defaultProps = {