maplibre-gl-layer-control 0.7.0 → 0.7.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/index.cjs CHANGED
@@ -631,9 +631,13 @@ function createBackgroundGroupSymbolSVG(size = 16) {
631
631
  class LayerControl {
632
632
  constructor(options = {}) {
633
633
  __publicField(this, "map");
634
+ __publicField(this, "mapContainer");
634
635
  __publicField(this, "container");
635
636
  __publicField(this, "button");
636
637
  __publicField(this, "panel");
638
+ // Panel positioning
639
+ __publicField(this, "resizeHandler", null);
640
+ __publicField(this, "mapResizeHandler", null);
637
641
  // State management
638
642
  __publicField(this, "state");
639
643
  __publicField(this, "targetLayers");
@@ -688,6 +692,7 @@ class LayerControl {
688
692
  */
689
693
  onAdd(map) {
690
694
  this.map = map;
695
+ this.mapContainer = map.getContainer();
691
696
  if (Object.keys(this.state.layerStates).length === 0) {
692
697
  this.autoDetectLayers();
693
698
  }
@@ -695,17 +700,22 @@ class LayerControl {
695
700
  this.button = this.createToggleButton();
696
701
  this.panel = this.createPanel();
697
702
  this.container.appendChild(this.button);
698
- this.container.appendChild(this.panel);
703
+ this.mapContainer.appendChild(this.panel);
699
704
  this.updateWidthDisplay();
700
705
  this.setupEventListeners();
701
706
  this.buildLayerItems();
707
+ if (!this.state.collapsed) {
708
+ requestAnimationFrame(() => {
709
+ this.updatePanelPosition();
710
+ });
711
+ }
702
712
  return this.container;
703
713
  }
704
714
  /**
705
715
  * Called when the control is removed from the map
706
716
  */
707
717
  onRemove() {
708
- var _a;
718
+ var _a, _b;
709
719
  if (this.customLayerUnsubscribe) {
710
720
  this.customLayerUnsubscribe();
711
721
  this.customLayerUnsubscribe = null;
@@ -714,7 +724,16 @@ class LayerControl {
714
724
  this.customLayerRegistry.destroy();
715
725
  this.customLayerRegistry = null;
716
726
  }
717
- (_a = this.container.parentNode) == null ? void 0 : _a.removeChild(this.container);
727
+ if (this.resizeHandler) {
728
+ window.removeEventListener("resize", this.resizeHandler);
729
+ this.resizeHandler = null;
730
+ }
731
+ if (this.mapResizeHandler) {
732
+ this.map.off("resize", this.mapResizeHandler);
733
+ this.mapResizeHandler = null;
734
+ }
735
+ (_a = this.panel.parentNode) == null ? void 0 : _a.removeChild(this.panel);
736
+ (_b = this.container.parentNode) == null ? void 0 : _b.removeChild(this.container);
718
737
  }
719
738
  /**
720
739
  * Auto-detect layers from the map and populate layerStates
@@ -970,6 +989,56 @@ class LayerControl {
970
989
  panel.appendChild(actionButtons);
971
990
  return panel;
972
991
  }
992
+ /**
993
+ * Detect which corner the control is positioned in
994
+ * @returns The position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
995
+ */
996
+ getControlPosition() {
997
+ const parent = this.container.parentElement;
998
+ if (!parent) return "top-right";
999
+ if (parent.classList.contains("maplibregl-ctrl-top-left")) return "top-left";
1000
+ if (parent.classList.contains("maplibregl-ctrl-top-right")) return "top-right";
1001
+ if (parent.classList.contains("maplibregl-ctrl-bottom-left")) return "bottom-left";
1002
+ if (parent.classList.contains("maplibregl-ctrl-bottom-right")) return "bottom-right";
1003
+ return "top-right";
1004
+ }
1005
+ /**
1006
+ * Update the panel position based on button location and control corner
1007
+ * Positions the panel next to the button, expanding in the appropriate direction
1008
+ */
1009
+ updatePanelPosition() {
1010
+ if (!this.button || !this.panel || !this.mapContainer) return;
1011
+ const buttonRect = this.button.getBoundingClientRect();
1012
+ const mapRect = this.mapContainer.getBoundingClientRect();
1013
+ const position = this.getControlPosition();
1014
+ const buttonTop = buttonRect.top - mapRect.top;
1015
+ const buttonBottom = mapRect.bottom - buttonRect.bottom;
1016
+ const buttonLeft = buttonRect.left - mapRect.left;
1017
+ const buttonRight = mapRect.right - buttonRect.right;
1018
+ const panelGap = 5;
1019
+ this.panel.style.top = "";
1020
+ this.panel.style.bottom = "";
1021
+ this.panel.style.left = "";
1022
+ this.panel.style.right = "";
1023
+ switch (position) {
1024
+ case "top-left":
1025
+ this.panel.style.top = `${buttonTop + buttonRect.height + panelGap}px`;
1026
+ this.panel.style.left = `${buttonLeft}px`;
1027
+ break;
1028
+ case "top-right":
1029
+ this.panel.style.top = `${buttonTop + buttonRect.height + panelGap}px`;
1030
+ this.panel.style.right = `${buttonRight}px`;
1031
+ break;
1032
+ case "bottom-left":
1033
+ this.panel.style.bottom = `${buttonBottom + buttonRect.height + panelGap}px`;
1034
+ this.panel.style.left = `${buttonLeft}px`;
1035
+ break;
1036
+ case "bottom-right":
1037
+ this.panel.style.bottom = `${buttonBottom + buttonRect.height + panelGap}px`;
1038
+ this.panel.style.right = `${buttonRight}px`;
1039
+ break;
1040
+ }
1041
+ }
973
1042
  /**
974
1043
  * Create action buttons for Show All / Hide All
975
1044
  */
@@ -1201,10 +1270,23 @@ class LayerControl {
1201
1270
  setupEventListeners() {
1202
1271
  this.button.addEventListener("click", () => this.toggle());
1203
1272
  document.addEventListener("click", (e) => {
1204
- if (!this.container.contains(e.target)) {
1273
+ const target = e.target;
1274
+ if (!this.container.contains(target) && !this.panel.contains(target)) {
1205
1275
  this.collapse();
1206
1276
  }
1207
1277
  });
1278
+ this.resizeHandler = () => {
1279
+ if (!this.state.collapsed) {
1280
+ this.updatePanelPosition();
1281
+ }
1282
+ };
1283
+ window.addEventListener("resize", this.resizeHandler);
1284
+ this.mapResizeHandler = () => {
1285
+ if (!this.state.collapsed) {
1286
+ this.updatePanelPosition();
1287
+ }
1288
+ };
1289
+ this.map.on("resize", this.mapResizeHandler);
1208
1290
  this.setupLayerChangeListeners();
1209
1291
  }
1210
1292
  /**
@@ -1254,6 +1336,7 @@ class LayerControl {
1254
1336
  expand() {
1255
1337
  this.state.collapsed = false;
1256
1338
  this.panel.classList.add("expanded");
1339
+ this.updatePanelPosition();
1257
1340
  }
1258
1341
  /**
1259
1342
  * Collapse the panel