@classic-homes/maps-svelte 0.7.5 → 0.9.0

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.
@@ -27,8 +27,9 @@
27
27
  // Generate unique ID for this control instance
28
28
  const controlId = `coord-display-${Math.random().toString(36).slice(2, 9)}`;
29
29
 
30
- // Native control dimensions for offset calculations
31
- const NAV_CONTROL_WIDTH = 29;
30
+ // Width of MapLibre's own top-right controls (navigation, fullscreen …),
31
+ // measured: their size depends on the theme's touch-target CSS.
32
+ let topRightInset = $state(0);
32
33
 
33
34
  let cursorLngLat: LngLat | null = $state(null);
34
35
  let currentZoom = $state(0);
@@ -57,7 +58,7 @@
57
58
  const adjustedStyle = $derived(() => {
58
59
  if (position === 'top-right') {
59
60
  // Position horizontally to the left of native controls
60
- const rightOffset = NAV_CONTROL_WIDTH + STACK_GAP + EDGE_MARGIN;
61
+ const rightOffset = topRightInset ? topRightInset + STACK_GAP : EDGE_MARGIN;
61
62
  return `position: absolute; top: ${EDGE_MARGIN}px; right: ${rightOffset}px; z-index: 10;`;
62
63
  }
63
64
 
@@ -87,6 +88,33 @@
87
88
  return `${normalized.toFixed(0)}°`;
88
89
  }
89
90
 
91
+ // Track the native top-right controls so a top-right display sits beside them
92
+ $effect(() => {
93
+ const { map, loaded } = mapContext;
94
+ if (!map || !loaded || position !== 'top-right') return;
95
+ const container = map.getContainer();
96
+ const corner = container.querySelector('.maplibregl-ctrl-top-right');
97
+ if (!corner) return;
98
+ const measure = () => {
99
+ const controls = [...corner.children].filter((el) => el !== controlElement);
100
+ if (controls.length === 0) {
101
+ topRightInset = 0;
102
+ return;
103
+ }
104
+ const left = Math.min(...controls.map((el) => el.getBoundingClientRect().left));
105
+ topRightInset = Math.max(0, Math.round(container.getBoundingClientRect().right - left));
106
+ };
107
+ measure();
108
+ const resize = new ResizeObserver(measure);
109
+ resize.observe(corner);
110
+ const added = new MutationObserver(measure);
111
+ added.observe(corner, { childList: true });
112
+ return () => {
113
+ resize.disconnect();
114
+ added.disconnect();
115
+ };
116
+ });
117
+
90
118
  // Setup event listeners
91
119
  $effect(() => {
92
120
  const { map, loaded } = mapContext;
@@ -228,6 +228,32 @@
228
228
  }
229
229
  });
230
230
 
231
+ /**
232
+ * Stack the managed layers in list order: the top of the list draws on top.
233
+ * Outlines sit right above their fill. Only managed layers move; they keep
234
+ * their place relative to the app's other layers.
235
+ */
236
+ function applyDrawOrder(map: maplibregl.Map) {
237
+ const desired: string[] = []; // bottom to top
238
+ for (const state of getLayerStatesArray(layerStates).reverse()) {
239
+ const id = `lm-layer-${state.id}`;
240
+ if (map.getLayer(id)) desired.push(id);
241
+ if (map.getLayer(`${id}${OUTLINE_LAYER_SUFFIX}`))
242
+ desired.push(`${id}${OUTLINE_LAYER_SUFFIX}`);
243
+ }
244
+ if (desired.length < 2) return;
245
+ const all = map.getLayersOrder();
246
+ const managed = new Set(desired);
247
+ if (all.filter((id) => managed.has(id)).join('\n') === desired.join('\n')) return;
248
+ const top = Math.max(...desired.map((id) => all.indexOf(id)));
249
+ const before = all.slice(top + 1).find((id) => !managed.has(id));
250
+ try {
251
+ for (const id of desired) map.moveLayer(id, before);
252
+ } catch {
253
+ // The style changed underneath us; the next sync reapplies the order
254
+ }
255
+ }
256
+
231
257
  // Track pending map update
232
258
  let mapUpdateTimeout: ReturnType<typeof setTimeout> | null = null;
233
259
  // Theme each added layer's paint was last applied for
@@ -434,6 +460,8 @@
434
460
  }
435
461
  }
436
462
  }
463
+
464
+ applyDrawOrder(map);
437
465
  }, 0); // End of setTimeout callback
438
466
  });
439
467
 
@@ -804,6 +832,7 @@
804
832
  <Switch
805
833
  checked={state.visible}
806
834
  onchange={() => handleToggleLayer(layer.id)}
835
+ aria-label={`Toggle ${layer.name}`}
807
836
  class="scale-75"
808
837
  />
809
838
 
@@ -926,6 +955,7 @@
926
955
  <Switch
927
956
  checked={state.visible}
928
957
  onchange={() => handleToggleLayer(layer.id)}
958
+ aria-label={`Toggle ${layer.name}`}
929
959
  class="scale-75"
930
960
  />
931
961
 
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { onMount, onDestroy, setContext } from 'svelte';
2
+ import { onMount, onDestroy, setContext, untrack } from 'svelte';
3
3
  import type { Snippet } from 'svelte';
4
4
  import * as maplibregl from 'maplibre-gl';
5
5
  import maplibreStyles from 'maplibre-gl/dist/maplibre-gl.css?inline';
@@ -215,6 +215,7 @@
215
215
  });
216
216
 
217
217
  map = newMap;
218
+ appliedView = readViewProps();
218
219
 
219
220
  // Track style changes for layer re-initialization
220
221
  styleLoadHandler = () => {
@@ -500,6 +501,51 @@
500
501
  }
501
502
  });
502
503
 
504
+ // `center`, `zoom`, `bearing` and `pitch` follow the parent after mount. A
505
+ // prop that changed since the last run moves the map, and only where it
506
+ // differs from what the map already shows: a parent that mirrors onMove
507
+ // back into these props then never fights the user's own panning, and an
508
+ // unchanged prop never snaps back a view the user moved.
509
+ type ViewProps = { center: LngLat; zoom: number; bearing: number; pitch: number };
510
+ let appliedView: ViewProps | null = null;
511
+ function readViewProps(): ViewProps {
512
+ const [lng, lat] = center as LngLat;
513
+ return { center: [lng, lat], zoom, bearing, pitch };
514
+ }
515
+ const angleDelta = (a: number, b: number) => Math.abs(((((a - b) % 360) + 540) % 360) - 180);
516
+
517
+ $effect(() => {
518
+ // Read the props before any guard, so the effect tracks them from its first run
519
+ const next = readViewProps();
520
+ if (!map || !mapLoaded) return;
521
+ const target = map;
522
+ untrack(() => {
523
+ const prev = appliedView;
524
+ appliedView = next;
525
+ if (!prev) return;
526
+ const view: { center?: LngLat; zoom?: number; bearing?: number; pitch?: number } = {};
527
+ const shown = target.getCenter();
528
+ if (
529
+ (next.center[0] !== prev.center[0] || next.center[1] !== prev.center[1]) &&
530
+ (Math.abs(shown.lng - next.center[0]) > 1e-9 || Math.abs(shown.lat - next.center[1]) > 1e-9)
531
+ ) {
532
+ view.center = next.center;
533
+ }
534
+ if (next.zoom !== prev.zoom && Math.abs(target.getZoom() - next.zoom) > 1e-6) {
535
+ view.zoom = next.zoom;
536
+ }
537
+ if (next.bearing !== prev.bearing && angleDelta(target.getBearing(), next.bearing) > 1e-6) {
538
+ view.bearing = next.bearing;
539
+ }
540
+ if (next.pitch !== prev.pitch && Math.abs(target.getPitch() - next.pitch) > 1e-6) {
541
+ view.pitch = next.pitch;
542
+ }
543
+ if (Object.keys(view).length === 0) return;
544
+ if (animation && !reducedMotion.value) target.easeTo(view);
545
+ else target.jumpTo(view);
546
+ });
547
+ });
548
+
503
549
  // Clear programmatic override when customStyle prop changes
504
550
  $effect(() => {
505
551
  if (customStyle) {
@@ -573,7 +619,13 @@
573
619
  >
574
620
  {#snippet children()}
575
621
  <!-- Map canvas container -->
576
- <div bind:this={containerElement} class="absolute inset-0"></div>
622
+ <!-- Inline position: MapLibre's own `.maplibregl-map { position: relative }`
623
+ (injected after the app stylesheet) outranks the `absolute` utility,
624
+ which collapses the map to 0 px high. Inline style always wins. -->
625
+ <div
626
+ bind:this={containerElement}
627
+ style="position: absolute; top: 0; right: 0; bottom: 0; left: 0"
628
+ ></div>
577
629
  {#if resourceWarning && !combinedError}
578
630
  <div
579
631
  class="absolute bottom-8 inset-x-14 z-30 mx-auto max-w-sm rounded border bg-card p-2 text-xs text-foreground shadow"
@@ -8,6 +8,8 @@
8
8
  applyMarkerChildProtection,
9
9
  removeMarkerProtection,
10
10
  markerColor,
11
+ applyMarkerAccessibility,
12
+ handleMarkerKeydown,
11
13
  } from '@classic-homes/maps-core';
12
14
  import type { MapContextValue } from '../../composables/useMapContext.svelte';
13
15
 
@@ -30,6 +32,7 @@
30
32
  let {
31
33
  id,
32
34
  coordinates,
35
+ label,
33
36
  color = markerColor,
34
37
  anchor = 'bottom',
35
38
  offset,
@@ -82,9 +85,15 @@
82
85
  markerOptions.element = markerElement;
83
86
  }
84
87
 
85
- const newMarker = new maplibregl.Marker(markerOptions)
86
- .setLngLat(coordinates as [number, number])
87
- .addTo(map);
88
+ const newMarker = new maplibregl.Marker(markerOptions);
89
+ // Before addTo: MapLibre keeps a label and role that are already set
90
+ applyMarkerAccessibility(newMarker.getElement(), {
91
+ label,
92
+ clickable: Boolean(onClick),
93
+ draggable,
94
+ custom: Boolean(children),
95
+ });
96
+ newMarker.setLngLat(coordinates as [number, number]).addTo(map);
88
97
 
89
98
  // Protect against CSS framework interference that causes marker sliding
90
99
  const el = newMarker.getElement();
@@ -167,6 +176,22 @@
167
176
  };
168
177
  });
169
178
 
179
+ // Accessible name, role and focus follow label, onClick and draggable
180
+ $effect(() => {
181
+ if (!marker) return;
182
+ const element = marker.getElement();
183
+ applyMarkerAccessibility(element, {
184
+ label,
185
+ clickable: Boolean(onClick),
186
+ draggable,
187
+ custom: Boolean(children),
188
+ });
189
+ if (!onClick) return;
190
+ // Enter and Space activate a clickable marker, as for any button
191
+ element.addEventListener('keydown', handleMarkerKeydown);
192
+ return () => element.removeEventListener('keydown', handleMarkerKeydown);
193
+ });
194
+
170
195
  // Update position when coordinates change
171
196
  $effect(() => {
172
197
  if (marker && coordinates) {
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy, tick } from 'svelte';
2
+ import { getContext, onDestroy, untrack } from 'svelte';
3
3
  import type { Snippet } from 'svelte';
4
4
  import * as maplibregl from 'maplibre-gl';
5
5
  import type { PopupProps } from '@classic-homes/maps-core';
@@ -51,6 +51,8 @@
51
51
  let containerElement: HTMLDivElement | null = $state(null);
52
52
  let contentElement: HTMLDivElement | null = $state(null);
53
53
  let autoPanRafHandle: number | null = null;
54
+ // Removes a fit waiting for the current camera move to end
55
+ let pendingAutoPan: (() => void) | null = null;
54
56
 
55
57
  // Create container for content
56
58
  $effect(() => {
@@ -60,27 +62,52 @@
60
62
  }
61
63
  });
62
64
 
65
+ // MapLibre reads these options only in its constructor, so a change
66
+ // rebuilds the popup (an open one reopens at once, without onClose/onOpen).
67
+ const optionsKey = $derived(
68
+ JSON.stringify([closeButton, closeOnMove, anchor, offset, maxWidth, focusAfterOpen])
69
+ );
70
+ // Where the popup is shown, as last applied; plain, read by the click guard
71
+ let shownKey = '';
72
+ // Set while an open popup is torn down for a rebuild
73
+ let rebuildingOpen = false;
74
+ const keyOf = (c: unknown) => (Array.isArray(c) ? c.join(',') : JSON.stringify(c));
75
+
63
76
  // Create and manage popup
64
77
  $effect(() => {
65
78
  const { map, loaded } = mapContext;
66
-
67
- if (!map || !loaded || !containerElement || popup) return;
68
-
69
- const newPopup = new maplibregl.Popup({
70
- closeButton,
71
- closeOnClick,
72
- closeOnMove,
73
- anchor: anchor as Anchor | undefined,
74
- offset: offset as number | maplibregl.PointLike | undefined,
75
- maxWidth,
76
- focusAfterOpen,
77
- });
78
-
79
- newPopup.setDOMContent(containerElement);
80
- popup = newPopup;
79
+ void optionsKey;
80
+
81
+ if (!map || !loaded || !containerElement) return;
82
+
83
+ const element = containerElement;
84
+ const newPopup = untrack(
85
+ () =>
86
+ new maplibregl.Popup({
87
+ closeButton,
88
+ // Map clicks are handled below, so a click that moves the popup to
89
+ // another feature does not also close it
90
+ closeOnClick: false,
91
+ closeOnMove,
92
+ anchor: anchor as Anchor | undefined,
93
+ offset: offset as number | maplibregl.PointLike | undefined,
94
+ maxWidth,
95
+ focusAfterOpen,
96
+ })
97
+ );
98
+
99
+ newPopup.setDOMContent(element);
100
+ // A rebuild of an open popup reopens it: not a new open for the caller
101
+ let silent = rebuildingOpen;
102
+ rebuildingOpen = false;
103
+ let tearingDown = false;
81
104
 
82
105
  // Event handlers
83
106
  newPopup.on('open', () => {
107
+ if (silent) {
108
+ silent = false;
109
+ return;
110
+ }
84
111
  onOpen?.({
85
112
  type: 'open',
86
113
  lngLat: coordinates,
@@ -88,20 +115,52 @@
88
115
  });
89
116
 
90
117
  newPopup.on('close', () => {
118
+ if (tearingDown) return;
91
119
  onClose?.({
92
120
  type: 'close',
93
121
  lngLat: coordinates,
94
122
  });
95
123
  });
124
+
125
+ popup = newPopup;
126
+
127
+ return () => {
128
+ tearingDown = true;
129
+ rebuildingOpen = newPopup.isOpen();
130
+ newPopup.remove();
131
+ if (untrack(() => popup) === newPopup) popup = null;
132
+ };
96
133
  });
97
134
 
98
135
  // Update popup position
99
136
  $effect(() => {
100
137
  if (popup && coordinates) {
101
138
  popup.setLngLat(coordinates as [number, number]);
139
+ shownKey = keyOf(coordinates);
102
140
  }
103
141
  });
104
142
 
143
+ // closeOnClick: a map click closes the popup once the click's own handlers
144
+ // have run, unless one of them moved the popup (a click on another feature
145
+ // that the caller shows in this popup)
146
+ $effect(() => {
147
+ const { map } = mapContext;
148
+ const target = popup;
149
+ if (!map || !target || typeof map.on !== 'function') return;
150
+ const onMapClick = () => {
151
+ if (!closeOnClick || !target.isOpen()) return;
152
+ const before = shownKey;
153
+ setTimeout(() => {
154
+ if (popup !== target || !target.isOpen() || shownKey !== before) return;
155
+ target.remove();
156
+ }, 0);
157
+ };
158
+ map.on('click', onMapClick);
159
+ return () => {
160
+ map.off('click', onMapClick);
161
+ };
162
+ });
163
+
105
164
  // Open/close popup
106
165
  $effect(() => {
107
166
  const { map } = mapContext;
@@ -110,6 +169,7 @@
110
169
 
111
170
  if (open) {
112
171
  popup.setLngLat(coordinates as [number, number]);
172
+ shownKey = keyOf(coordinates);
113
173
  // addTo() removes an existing attachment and emits close. Coordinate or
114
174
  // callback updates must move the current popup without clearing selection.
115
175
  if (!popup.isOpen()) popup.addTo(map);
@@ -123,8 +183,21 @@
123
183
  // Wait for popup to render before measuring
124
184
  autoPanRafHandle = requestAnimationFrame(() => {
125
185
  autoPanRafHandle = null;
126
- if (popup && map) {
127
- panToFitPopup(map, popup, normalizePadding(autoPanPadding));
186
+ const target = popup;
187
+ if (!target || !map) return;
188
+ const pan = () => {
189
+ if (popup === target && target.isOpen()) {
190
+ panToFitPopup(map, target, normalizePadding(autoPanPadding));
191
+ }
192
+ };
193
+ // A pan now would cancel a camera move already under way (the
194
+ // caller moving the map to the popup's spot): fit once it lands
195
+ if (typeof map.isMoving === 'function' && map.isMoving()) {
196
+ pendingAutoPan?.();
197
+ map.once('moveend', pan);
198
+ pendingAutoPan = () => map.off('moveend', pan);
199
+ } else {
200
+ pan();
128
201
  }
129
202
  });
130
203
  }
@@ -149,11 +222,8 @@
149
222
  cancelAnimationFrame(autoPanRafHandle);
150
223
  autoPanRafHandle = null;
151
224
  }
152
-
153
- if (popup) {
154
- popup.remove();
155
- popup = null;
156
- }
225
+ pendingAutoPan?.();
226
+ pendingAutoPan = null;
157
227
  });
158
228
  </script>
159
229
 
@@ -25,6 +25,7 @@
25
25
  beforeId,
26
26
  onClick,
27
27
  onHover,
28
+ hoverThrottleMs,
28
29
  // Circle-specific props
29
30
  circleRadius,
30
31
  circleColor,
@@ -73,6 +74,7 @@
73
74
  layout: {},
74
75
  onClick,
75
76
  onHover,
77
+ hoverThrottleMs,
76
78
  };
77
79
  });
78
80
  </script>
@@ -25,6 +25,7 @@
25
25
  beforeId,
26
26
  onClick,
27
27
  onHover,
28
+ hoverThrottleMs,
28
29
  // Fill extrusion-specific props
29
30
  fillExtrusionHeight,
30
31
  fillExtrusionBase,
@@ -70,6 +71,7 @@
70
71
  layout: {},
71
72
  onClick,
72
73
  onHover,
74
+ hoverThrottleMs,
73
75
  };
74
76
  });
75
77
  </script>
@@ -25,6 +25,7 @@
25
25
  beforeId,
26
26
  onClick,
27
27
  onHover,
28
+ hoverThrottleMs,
28
29
  // Fill-specific props
29
30
  fillColor,
30
31
  fillOpacity,
@@ -65,6 +66,7 @@
65
66
  layout: {},
66
67
  onClick,
67
68
  onHover,
69
+ hoverThrottleMs,
68
70
  };
69
71
  });
70
72
  </script>
@@ -25,6 +25,7 @@
25
25
  beforeId,
26
26
  onClick,
27
27
  onHover,
28
+ hoverThrottleMs,
28
29
  // Heatmap-specific props
29
30
  heatmapIntensity,
30
31
  heatmapWeight,
@@ -63,6 +64,7 @@
63
64
  layout: {},
64
65
  onClick,
65
66
  onHover,
67
+ hoverThrottleMs,
66
68
  };
67
69
  });
68
70
  </script>
@@ -25,6 +25,7 @@
25
25
  beforeId,
26
26
  onClick,
27
27
  onHover,
28
+ hoverThrottleMs,
28
29
  // Line-specific props
29
30
  lineColor,
30
31
  lineWidth,
@@ -76,6 +77,7 @@
76
77
  layout,
77
78
  onClick,
78
79
  onHover,
80
+ hoverThrottleMs,
79
81
  };
80
82
  });
81
83
  </script>
@@ -25,6 +25,7 @@
25
25
  beforeId,
26
26
  onClick,
27
27
  onHover,
28
+ hoverThrottleMs,
28
29
  // Symbol-specific props
29
30
  iconImage,
30
31
  iconSize,
@@ -103,6 +104,7 @@
103
104
  layout,
104
105
  onClick,
105
106
  onHover,
107
+ hoverThrottleMs,
106
108
  };
107
109
  });
108
110
  </script>
@@ -432,6 +432,18 @@ export function useLayer(getOptions) {
432
432
  map.on('mousemove', id, handleMouseMove);
433
433
  map.on('mouseleave', id, handleMouseLeave);
434
434
  }
435
+ // A click-only layer still shows it is clickable
436
+ const pointerOn = () => {
437
+ map.getCanvas().style.cursor = 'pointer';
438
+ };
439
+ const pointerOff = () => {
440
+ map.getCanvas().style.cursor = '';
441
+ };
442
+ const clickOnly = Boolean(onClick) && !onHover;
443
+ if (clickOnly) {
444
+ map.on('mouseenter', id, pointerOn);
445
+ map.on('mouseleave', id, pointerOff);
446
+ }
435
447
  // Return cleanup function to remove all handlers
436
448
  return () => {
437
449
  if (onClick) {
@@ -442,6 +454,10 @@ export function useLayer(getOptions) {
442
454
  map.off('mousemove', id, handleMouseMove);
443
455
  map.off('mouseleave', id, handleMouseLeave);
444
456
  }
457
+ if (clickOnly) {
458
+ map.off('mouseenter', id, pointerOn);
459
+ map.off('mouseleave', id, pointerOff);
460
+ }
445
461
  };
446
462
  }
447
463
  // Cleanup on destroy (the handler effect detaches its handlers itself)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-svelte",
3
- "version": "0.7.5",
3
+ "version": "0.9.0",
4
4
  "description": "Svelte MapLibre components for Classic Theme",
5
5
  "keywords": [
6
6
  "maps",
@@ -37,7 +37,7 @@
37
37
  "test": "vitest run"
38
38
  },
39
39
  "dependencies": {
40
- "@classic-homes/maps-core": "^0.7.5",
40
+ "@classic-homes/maps-core": "^0.9.0",
41
41
  "@classic-homes/theme-svelte": "*",
42
42
  "@classic-homes/theme-tokens": "*",
43
43
  "clsx": "^2.1.0",