@classic-homes/maps-svelte 0.2.16 → 0.4.2
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/lib/components/controls/BasemapSwitcher.svelte +61 -22
- package/dist/lib/components/core/Map.svelte +97 -19
- package/dist/lib/components/core/Map.svelte.d.ts +0 -3
- package/dist/lib/components/core/mapErrors.d.ts +3 -0
- package/dist/lib/components/core/mapErrors.js +10 -0
- package/dist/lib/components/layers/useLayer.svelte.js +8 -0
- package/dist/lib/inline-css.d.ts +4 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import { getContext } from 'svelte';
|
|
2
|
+
import { getContext, untrack } from 'svelte';
|
|
3
3
|
import type { BasemapSwitcherProps, BasemapDefinition } from '@classic-homes/maps-core';
|
|
4
4
|
import {
|
|
5
5
|
FREE_BASEMAPS,
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
|
|
14
14
|
|
|
15
15
|
interface MapContext {
|
|
16
|
+
map?: maplibregl.Map;
|
|
16
17
|
setMapStyle?: (style: string | maplibregl.StyleSpecification) => void;
|
|
17
18
|
}
|
|
18
19
|
|
|
@@ -38,6 +39,43 @@
|
|
|
38
39
|
let isOpen = $state(false);
|
|
39
40
|
let internalActiveId = $state<string | undefined>(undefined);
|
|
40
41
|
let containerElement: HTMLDivElement;
|
|
42
|
+
let toggleButton: HTMLButtonElement;
|
|
43
|
+
let panelStyle = $state('');
|
|
44
|
+
let panelColumns = $state(1);
|
|
45
|
+
let compactPanel = $state(false);
|
|
46
|
+
|
|
47
|
+
// Constrain the flyout to its MAP, not the browser window. Its control may
|
|
48
|
+
// already be partway down a short card after the navigation buttons.
|
|
49
|
+
$effect(() => {
|
|
50
|
+
if (!isOpen || !containerElement) return;
|
|
51
|
+
controlPosition.style;
|
|
52
|
+
const mapElement = mapContext?.map?.getContainer();
|
|
53
|
+
if (!mapElement) return;
|
|
54
|
+
const measure = () => {
|
|
55
|
+
const box = mapElement.getBoundingClientRect();
|
|
56
|
+
const button = containerElement.getBoundingClientRect();
|
|
57
|
+
const available = Math.max(44, box.width - 24);
|
|
58
|
+
panelColumns = Math.max(
|
|
59
|
+
1,
|
|
60
|
+
Math.min(columns, Math.floor((available - 16) / (thumbnailSize + 12)))
|
|
61
|
+
);
|
|
62
|
+
const width = Math.min(available, panelColumns * (thumbnailSize + 12) + 16);
|
|
63
|
+
compactPanel = box.height < 400 || available < 180;
|
|
64
|
+
const height = Math.min(
|
|
65
|
+
box.height - 24,
|
|
66
|
+
32 +
|
|
67
|
+
Math.ceil(availableBasemaps().length / panelColumns) *
|
|
68
|
+
(compactPanel ? 48 : thumbnailSize + 36)
|
|
69
|
+
);
|
|
70
|
+
const left = Math.max(12, Math.min(button.left - box.left, box.width - width - 12));
|
|
71
|
+
const top = Math.max(12, Math.min(button.bottom - box.top + 4, box.height - height - 12));
|
|
72
|
+
panelStyle = `width:${width}px;max-height:${Math.max(44, height)}px;left:${left - (button.left - box.left)}px;top:${top - (button.top - box.top)}px;overflow:auto;`;
|
|
73
|
+
};
|
|
74
|
+
untrack(measure);
|
|
75
|
+
const observer = new ResizeObserver(measure);
|
|
76
|
+
observer.observe(mapElement);
|
|
77
|
+
return () => observer.disconnect();
|
|
78
|
+
});
|
|
41
79
|
|
|
42
80
|
// Use positioning coordinator with getter for reactive position updates
|
|
43
81
|
const controlPosition = useControlPosition(
|
|
@@ -99,6 +137,9 @@
|
|
|
99
137
|
if (e.key === 'Escape') {
|
|
100
138
|
isOpen = false;
|
|
101
139
|
controlPosition.setExpanded(false);
|
|
140
|
+
toggleButton?.focus();
|
|
141
|
+
e.preventDefault();
|
|
142
|
+
e.stopPropagation();
|
|
102
143
|
}
|
|
103
144
|
};
|
|
104
145
|
|
|
@@ -128,6 +169,7 @@
|
|
|
128
169
|
onBasemapChange?.(basemap);
|
|
129
170
|
isOpen = false;
|
|
130
171
|
controlPosition.setExpanded(false);
|
|
172
|
+
toggleButton?.focus();
|
|
131
173
|
}
|
|
132
174
|
|
|
133
175
|
function handleToggle() {
|
|
@@ -135,9 +177,6 @@
|
|
|
135
177
|
isOpen = newState;
|
|
136
178
|
controlPosition.setExpanded(newState);
|
|
137
179
|
}
|
|
138
|
-
|
|
139
|
-
const isTop = $derived(position.startsWith('top'));
|
|
140
|
-
const panelWidth = $derived(columns * (thumbnailSize + 12) + 16);
|
|
141
180
|
</script>
|
|
142
181
|
|
|
143
182
|
<div
|
|
@@ -151,41 +190,40 @@
|
|
|
151
190
|
class={cn(
|
|
152
191
|
'absolute classic-basemap-panel-enter',
|
|
153
192
|
'bg-card rounded-lg shadow-lg border border-black dark:border-gray-400',
|
|
154
|
-
'p-2'
|
|
155
|
-
isTop ? 'top-full mt-1' : 'bottom-full mb-1',
|
|
156
|
-
position.includes('right') ? 'right-0' : 'left-0'
|
|
193
|
+
'p-2'
|
|
157
194
|
)}
|
|
158
|
-
style=
|
|
195
|
+
style={panelStyle}
|
|
159
196
|
>
|
|
160
197
|
<div
|
|
161
|
-
role="
|
|
198
|
+
role="group"
|
|
162
199
|
aria-label="Select basemap"
|
|
163
200
|
class="grid gap-1.5"
|
|
164
|
-
style="grid-template-columns: repeat({
|
|
201
|
+
style="grid-template-columns: repeat({panelColumns}, minmax(0, 1fr));"
|
|
165
202
|
>
|
|
166
203
|
{#each availableBasemaps() as basemap (basemap.id)}
|
|
167
204
|
{@const isActive = basemap.id === currentActiveId}
|
|
168
205
|
{@const thumbUrl = resolveBasemapThumbnail(basemap, apiKeys)}
|
|
169
206
|
<button
|
|
170
207
|
type="button"
|
|
171
|
-
|
|
172
|
-
aria-selected={isActive}
|
|
208
|
+
aria-pressed={isActive}
|
|
173
209
|
class={cn(
|
|
174
|
-
'flex flex-col items-center gap-1 p-1 rounded cursor-pointer',
|
|
210
|
+
'flex min-h-[44px] min-w-0 flex-col items-center gap-1 p-1 rounded cursor-pointer',
|
|
175
211
|
'hover:bg-muted transition-colors',
|
|
176
212
|
isActive && 'ring-2 ring-primary'
|
|
177
213
|
)}
|
|
178
214
|
onclick={() => handleSelect(basemap)}
|
|
179
215
|
title={basemap.name}
|
|
180
216
|
>
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
217
|
+
{#if !compactPanel}
|
|
218
|
+
<img
|
|
219
|
+
src={thumbUrl}
|
|
220
|
+
alt=""
|
|
221
|
+
loading="lazy"
|
|
222
|
+
class="rounded object-cover"
|
|
223
|
+
style="width: {thumbnailSize}px; height: {thumbnailSize}px;"
|
|
224
|
+
/>
|
|
225
|
+
{/if}
|
|
226
|
+
<span class="text-xs text-foreground truncate w-full text-center leading-tight">
|
|
189
227
|
{basemap.label ?? basemap.name}
|
|
190
228
|
</span>
|
|
191
229
|
</button>
|
|
@@ -196,9 +234,10 @@
|
|
|
196
234
|
|
|
197
235
|
<!-- Toggle button -->
|
|
198
236
|
<button
|
|
237
|
+
bind:this={toggleButton}
|
|
199
238
|
type="button"
|
|
200
239
|
class={cn(
|
|
201
|
-
'w-[
|
|
240
|
+
'w-[44px] h-[44px] flex items-center justify-center',
|
|
202
241
|
'bg-card rounded shadow-lg border border-black dark:border-gray-400',
|
|
203
242
|
'hover:bg-muted transition-colors cursor-pointer',
|
|
204
243
|
'text-foreground'
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
import { onMount, onDestroy, setContext } from 'svelte';
|
|
3
3
|
import type { Snippet } from 'svelte';
|
|
4
4
|
import maplibregl from 'maplibre-gl';
|
|
5
|
-
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
6
|
-
import '@classic-homes/maps-core/styles/controls.css';
|
|
7
|
-
import '@classic-homes/maps-core/styles/marker.css';
|
|
5
|
+
import maplibreStyles from 'maplibre-gl/dist/maplibre-gl.css?inline';
|
|
6
|
+
import controlStyles from '@classic-homes/maps-core/styles/controls.css?inline';
|
|
7
|
+
import markerStyles from '@classic-homes/maps-core/styles/marker.css?inline';
|
|
8
8
|
|
|
9
9
|
import type {
|
|
10
10
|
MapProps,
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
import { useMapTheme } from '../../composables/useMapTheme.svelte.js';
|
|
24
24
|
import { useReducedMotion } from '../../composables/useReducedMotion.svelte.js';
|
|
25
25
|
import MapContainer from '../base/MapContainer.svelte';
|
|
26
|
+
import { isRecoverableMapError } from './mapErrors.js';
|
|
26
27
|
|
|
27
28
|
interface Props extends Omit<MapProps, 'class'> {
|
|
28
29
|
class?: string;
|
|
@@ -91,6 +92,8 @@
|
|
|
91
92
|
let mapLoaded = $state(false);
|
|
92
93
|
let mapError = $state<string | null>(null);
|
|
93
94
|
let styleVersion = $state(0);
|
|
95
|
+
let resourceWarning = $state(false);
|
|
96
|
+
let styleReady = false;
|
|
94
97
|
let activeStyleOverride: string | maplibregl.StyleSpecification | null = $state(null);
|
|
95
98
|
let styleLoadHandler: (() => void) | null = null;
|
|
96
99
|
let initTimeoutId: ReturnType<typeof setTimeout> | null = null;
|
|
@@ -100,15 +103,22 @@
|
|
|
100
103
|
|
|
101
104
|
function setMapStyle(style: string | maplibregl.StyleSpecification) {
|
|
102
105
|
activeStyleOverride = style;
|
|
103
|
-
if (map)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
106
|
+
if (!map) return;
|
|
107
|
+
const target = map;
|
|
108
|
+
// Every styleVersion bump makes Source and the layer hooks tear down and
|
|
109
|
+
// re-add themselves, so a style swap must bump exactly once. MapLibre fires
|
|
110
|
+
// `style.load` from a diffed setStyle() too (Style.setState), and the
|
|
111
|
+
// handler registered in onMount turns that into the bump. The idle listener
|
|
112
|
+
// is only a fallback for a swap that never fired `style.load` (older
|
|
113
|
+
// MapLibre, or a diff that applied nothing): bumping unconditionally here
|
|
114
|
+
// landed a second bump after the layers had been re-added, and the hooks
|
|
115
|
+
// then found their layers present and skipped re-wiring the click and hover
|
|
116
|
+
// handlers - the map kept rendering, but nothing on it responded.
|
|
117
|
+
const versionBefore = styleVersion;
|
|
118
|
+
target.setStyle(style);
|
|
119
|
+
target.once('idle', () => {
|
|
120
|
+
if (styleVersion === versionBefore) styleVersion++;
|
|
121
|
+
});
|
|
112
122
|
}
|
|
113
123
|
|
|
114
124
|
const reducedMotion = useReducedMotion();
|
|
@@ -146,6 +156,26 @@
|
|
|
146
156
|
onMount(() => {
|
|
147
157
|
if (!containerElement) return;
|
|
148
158
|
|
|
159
|
+
// Inline CSS stays in the lazy map module rather than SvelteKit's route
|
|
160
|
+
// stylesheet graph. Install once before constructing the first map.
|
|
161
|
+
if (!document.getElementById('classic-map-base-styles')) {
|
|
162
|
+
const sheet = document.createElement('style');
|
|
163
|
+
sheet.id = 'classic-map-base-styles';
|
|
164
|
+
sheet.textContent =
|
|
165
|
+
maplibreStyles +
|
|
166
|
+
controlStyles +
|
|
167
|
+
markerStyles +
|
|
168
|
+
`
|
|
169
|
+
.maplibregl-map { container-type: size; }
|
|
170
|
+
.maplibregl-map .maplibregl-ctrl-group button { width:44px !important; height:44px !important; }
|
|
171
|
+
@container (max-height: 400px) {
|
|
172
|
+
.maplibregl-ctrl-group { display:flex; }
|
|
173
|
+
.maplibregl-ctrl-group button + button { border-top:0 !important; border-left:1px solid hsl(var(--border)); }
|
|
174
|
+
}
|
|
175
|
+
.maplibregl-ctrl-group button:focus-visible { outline:2px solid currentColor; outline-offset:-3px; }
|
|
176
|
+
`;
|
|
177
|
+
document.head.appendChild(sheet);
|
|
178
|
+
}
|
|
149
179
|
try {
|
|
150
180
|
const newMap = new maplibregl.Map({
|
|
151
181
|
container: containerElement,
|
|
@@ -172,13 +202,15 @@
|
|
|
172
202
|
|
|
173
203
|
// Track style changes for layer re-initialization
|
|
174
204
|
styleLoadHandler = () => {
|
|
205
|
+
styleReady = true;
|
|
175
206
|
styleVersion++;
|
|
207
|
+
finishLoading();
|
|
176
208
|
};
|
|
177
209
|
newMap.on('style.load', styleLoadHandler);
|
|
178
210
|
|
|
179
211
|
// Map initialization timeout (30 seconds)
|
|
180
212
|
initTimeoutId = setTimeout(() => {
|
|
181
|
-
if (!
|
|
213
|
+
if (!mapLoaded) {
|
|
182
214
|
const errorMessage = 'Map initialization timeout - style may be unreachable';
|
|
183
215
|
mapError = errorMessage;
|
|
184
216
|
onError?.({
|
|
@@ -204,7 +236,7 @@
|
|
|
204
236
|
id: 'maplibre-navigation-control',
|
|
205
237
|
type: 'navigation',
|
|
206
238
|
isBuiltIn: true,
|
|
207
|
-
dimensions: { width:
|
|
239
|
+
dimensions: { width: 44, height: 134 },
|
|
208
240
|
position: {
|
|
209
241
|
primary: navigationControlPosition as
|
|
210
242
|
| 'top-left'
|
|
@@ -238,13 +270,20 @@
|
|
|
238
270
|
}
|
|
239
271
|
|
|
240
272
|
if (fullscreenControl) {
|
|
241
|
-
newMap.addControl(
|
|
273
|
+
newMap.addControl(
|
|
274
|
+
new maplibregl.FullscreenControl({
|
|
275
|
+
container:
|
|
276
|
+
containerElement.closest<HTMLElement>('[data-testid="map-container"]') ??
|
|
277
|
+
containerElement,
|
|
278
|
+
}),
|
|
279
|
+
fullscreenControlPosition
|
|
280
|
+
);
|
|
242
281
|
// Register with coordinator - FullscreenControl is ~29x29px
|
|
243
282
|
positioningCoordinator.register({
|
|
244
283
|
id: 'maplibre-fullscreen-control',
|
|
245
284
|
type: 'custom',
|
|
246
285
|
isBuiltIn: true,
|
|
247
|
-
dimensions: { width:
|
|
286
|
+
dimensions: { width: 44, height: 44 },
|
|
248
287
|
position: {
|
|
249
288
|
primary: fullscreenControlPosition as
|
|
250
289
|
| 'top-left'
|
|
@@ -270,7 +309,7 @@
|
|
|
270
309
|
id: 'maplibre-geolocate-control',
|
|
271
310
|
type: 'custom',
|
|
272
311
|
isBuiltIn: true,
|
|
273
|
-
dimensions: { width:
|
|
312
|
+
dimensions: { width: 44, height: 44 },
|
|
274
313
|
position: {
|
|
275
314
|
primary: geolocateControlPosition as
|
|
276
315
|
| 'top-left'
|
|
@@ -284,7 +323,8 @@
|
|
|
284
323
|
}
|
|
285
324
|
|
|
286
325
|
// Map load handler
|
|
287
|
-
|
|
326
|
+
function finishLoading() {
|
|
327
|
+
if (mapLoaded) return;
|
|
288
328
|
if (initTimeoutId) {
|
|
289
329
|
clearTimeout(initTimeoutId);
|
|
290
330
|
initTimeoutId = null;
|
|
@@ -299,10 +339,17 @@
|
|
|
299
339
|
}
|
|
300
340
|
|
|
301
341
|
onLoad?.({ type: 'load', loaded: true });
|
|
302
|
-
}
|
|
342
|
+
}
|
|
343
|
+
newMap.on('load', finishLoading);
|
|
303
344
|
|
|
304
345
|
// Error handler
|
|
305
346
|
newMap.on('error', (e) => {
|
|
347
|
+
if (isRecoverableMapError(e, styleReady)) {
|
|
348
|
+
// Keep rendered data and controls usable when one resource fails.
|
|
349
|
+
// The initialization deadline still catches styles that never load.
|
|
350
|
+
resourceWarning = true;
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
306
353
|
const errorMessage = e.error?.message || 'Map error';
|
|
307
354
|
if (initTimeoutId) {
|
|
308
355
|
clearTimeout(initTimeoutId);
|
|
@@ -456,6 +503,12 @@
|
|
|
456
503
|
|
|
457
504
|
const updateDimensions = () => {
|
|
458
505
|
if (!containerElement) return;
|
|
506
|
+
const navigation = containerElement.querySelector('.maplibregl-ctrl-group');
|
|
507
|
+
if (navigationControl && navigation instanceof HTMLElement) {
|
|
508
|
+
positioningCoordinator.update('maplibre-navigation-control', {
|
|
509
|
+
dimensions: { width: navigation.offsetWidth, height: navigation.offsetHeight },
|
|
510
|
+
});
|
|
511
|
+
}
|
|
459
512
|
positioningCoordinator.setMapDimensions({
|
|
460
513
|
width: containerElement.offsetWidth,
|
|
461
514
|
height: containerElement.offsetHeight,
|
|
@@ -509,6 +562,31 @@
|
|
|
509
562
|
{#snippet children()}
|
|
510
563
|
<!-- Map canvas container -->
|
|
511
564
|
<div bind:this={containerElement} class="absolute inset-0"></div>
|
|
565
|
+
{#if resourceWarning && !combinedError}
|
|
566
|
+
<div
|
|
567
|
+
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"
|
|
568
|
+
role="status"
|
|
569
|
+
>
|
|
570
|
+
Some map details could not load.
|
|
571
|
+
<button
|
|
572
|
+
type="button"
|
|
573
|
+
class="min-h-[44px] underline px-2"
|
|
574
|
+
onclick={() => {
|
|
575
|
+
resourceWarning = false;
|
|
576
|
+
if (map)
|
|
577
|
+
map.setStyle(
|
|
578
|
+
(activeStyleOverride ?? mapStyle) as string | maplibregl.StyleSpecification,
|
|
579
|
+
{ diff: false }
|
|
580
|
+
);
|
|
581
|
+
}}>Retry map details</button
|
|
582
|
+
>
|
|
583
|
+
<button
|
|
584
|
+
type="button"
|
|
585
|
+
class="min-h-[44px] underline px-2"
|
|
586
|
+
onclick={() => (resourceWarning = false)}>Dismiss</button
|
|
587
|
+
>
|
|
588
|
+
</div>
|
|
589
|
+
{/if}
|
|
512
590
|
<!-- Custom controls overlay - positioned above map canvas -->
|
|
513
591
|
{#if mapLoaded && children}
|
|
514
592
|
<div class="absolute inset-0 pointer-events-none [&>*]:pointer-events-auto">
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
|
-
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
3
|
-
import '@classic-homes/maps-core/styles/controls.css';
|
|
4
|
-
import '@classic-homes/maps-core/styles/marker.css';
|
|
5
2
|
import type { MapProps } from '@classic-homes/maps-core';
|
|
6
3
|
interface Props extends Omit<MapProps, 'class'> {
|
|
7
4
|
class?: string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Resource errors are recoverable once a style is ready, or when a source/tile
|
|
2
|
+
* is identified. Map retains a deadline for styles that never initialize. */
|
|
3
|
+
export function isRecoverableMapError(event, styleReady) {
|
|
4
|
+
if (styleReady)
|
|
5
|
+
return true;
|
|
6
|
+
if (!event || typeof event !== 'object')
|
|
7
|
+
return false;
|
|
8
|
+
return (('sourceId' in event && typeof event.sourceId === 'string') ||
|
|
9
|
+
('tile' in event && event.tile != null));
|
|
10
|
+
}
|
|
@@ -98,6 +98,14 @@ export function useLayer(getOptions) {
|
|
|
98
98
|
function addLayer() {
|
|
99
99
|
if (layerAdded || map.getLayer(id)) {
|
|
100
100
|
layerAdded = true;
|
|
101
|
+
// The layer is on the map but this hook may not own its events: a
|
|
102
|
+
// styleVersion bump drops the handlers and marks the layer as not added,
|
|
103
|
+
// and if the layer survived (or a second bump lands after the first one
|
|
104
|
+
// re-added it) this branch is the only path back. Without the re-attach,
|
|
105
|
+
// the layer renders but every onClick/onHover is silently dead.
|
|
106
|
+
if (!eventHandlerCleanup) {
|
|
107
|
+
eventHandlerCleanup = setupEventHandlers(map, id, source, sourceLayer, onClick, onHover);
|
|
108
|
+
}
|
|
101
109
|
return;
|
|
102
110
|
}
|
|
103
111
|
try {
|