@classic-homes/maps-svelte 0.3.1 → 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.
@@ -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="width: {panelWidth}px;"
195
+ style={panelStyle}
159
196
  >
160
197
  <div
161
- role="listbox"
198
+ role="group"
162
199
  aria-label="Select basemap"
163
200
  class="grid gap-1.5"
164
- style="grid-template-columns: repeat({columns}, 1fr);"
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
- role="option"
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
- <img
182
- src={thumbUrl}
183
- alt={basemap.name}
184
- loading="lazy"
185
- class="rounded object-cover"
186
- style="width: {thumbnailSize}px; height: {thumbnailSize}px;"
187
- />
188
- <span class="text-[10px] text-foreground truncate w-full text-center leading-tight">
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-[40px] h-[40px] flex items-center justify-center',
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;
@@ -153,6 +156,26 @@
153
156
  onMount(() => {
154
157
  if (!containerElement) return;
155
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
+ }
156
179
  try {
157
180
  const newMap = new maplibregl.Map({
158
181
  container: containerElement,
@@ -179,13 +202,15 @@
179
202
 
180
203
  // Track style changes for layer re-initialization
181
204
  styleLoadHandler = () => {
205
+ styleReady = true;
182
206
  styleVersion++;
207
+ finishLoading();
183
208
  };
184
209
  newMap.on('style.load', styleLoadHandler);
185
210
 
186
211
  // Map initialization timeout (30 seconds)
187
212
  initTimeoutId = setTimeout(() => {
188
- if (!map?.loaded()) {
213
+ if (!mapLoaded) {
189
214
  const errorMessage = 'Map initialization timeout - style may be unreachable';
190
215
  mapError = errorMessage;
191
216
  onError?.({
@@ -211,7 +236,7 @@
211
236
  id: 'maplibre-navigation-control',
212
237
  type: 'navigation',
213
238
  isBuiltIn: true,
214
- dimensions: { width: 29, height: 91 },
239
+ dimensions: { width: 44, height: 134 },
215
240
  position: {
216
241
  primary: navigationControlPosition as
217
242
  | 'top-left'
@@ -245,13 +270,20 @@
245
270
  }
246
271
 
247
272
  if (fullscreenControl) {
248
- newMap.addControl(new maplibregl.FullscreenControl(), fullscreenControlPosition);
273
+ newMap.addControl(
274
+ new maplibregl.FullscreenControl({
275
+ container:
276
+ containerElement.closest<HTMLElement>('[data-testid="map-container"]') ??
277
+ containerElement,
278
+ }),
279
+ fullscreenControlPosition
280
+ );
249
281
  // Register with coordinator - FullscreenControl is ~29x29px
250
282
  positioningCoordinator.register({
251
283
  id: 'maplibre-fullscreen-control',
252
284
  type: 'custom',
253
285
  isBuiltIn: true,
254
- dimensions: { width: 29, height: 29 },
286
+ dimensions: { width: 44, height: 44 },
255
287
  position: {
256
288
  primary: fullscreenControlPosition as
257
289
  | 'top-left'
@@ -277,7 +309,7 @@
277
309
  id: 'maplibre-geolocate-control',
278
310
  type: 'custom',
279
311
  isBuiltIn: true,
280
- dimensions: { width: 29, height: 29 },
312
+ dimensions: { width: 44, height: 44 },
281
313
  position: {
282
314
  primary: geolocateControlPosition as
283
315
  | 'top-left'
@@ -291,7 +323,8 @@
291
323
  }
292
324
 
293
325
  // Map load handler
294
- newMap.on('load', () => {
326
+ function finishLoading() {
327
+ if (mapLoaded) return;
295
328
  if (initTimeoutId) {
296
329
  clearTimeout(initTimeoutId);
297
330
  initTimeoutId = null;
@@ -306,10 +339,17 @@
306
339
  }
307
340
 
308
341
  onLoad?.({ type: 'load', loaded: true });
309
- });
342
+ }
343
+ newMap.on('load', finishLoading);
310
344
 
311
345
  // Error handler
312
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
+ }
313
353
  const errorMessage = e.error?.message || 'Map error';
314
354
  if (initTimeoutId) {
315
355
  clearTimeout(initTimeoutId);
@@ -463,6 +503,12 @@
463
503
 
464
504
  const updateDimensions = () => {
465
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
+ }
466
512
  positioningCoordinator.setMapDimensions({
467
513
  width: containerElement.offsetWidth,
468
514
  height: containerElement.offsetHeight,
@@ -516,6 +562,31 @@
516
562
  {#snippet children()}
517
563
  <!-- Map canvas container -->
518
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}
519
590
  <!-- Custom controls overlay - positioned above map canvas -->
520
591
  {#if mapLoaded && children}
521
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,3 @@
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 declare function isRecoverableMapError(event: unknown, styleReady: boolean): boolean;
@@ -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
+ }
@@ -0,0 +1,4 @@
1
+ declare module '*.css?inline' {
2
+ const css: string;
3
+ export default css;
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-svelte",
3
- "version": "0.3.1",
3
+ "version": "0.4.2",
4
4
  "description": "Svelte MapLibre components for Classic Theme",
5
5
  "keywords": [
6
6
  "maps",