@classic-homes/maps-svelte 0.1.3 → 0.1.5

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.
@@ -0,0 +1,7 @@
1
+ import type { LayerManagerProps } from '@classic-homes/maps-core';
2
+ import '@classic-homes/maps-core/styles/layer-manager.css';
3
+ interface Props extends LayerManagerProps {
4
+ }
5
+ declare const LayerManager: import("svelte").Component<Props, {}, "">;
6
+ type LayerManager = ReturnType<typeof LayerManager>;
7
+ export default LayerManager;
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy } from 'svelte';
2
+ import { getContext, onDestroy, untrack } from 'svelte';
3
3
  import type { LegendProps, LegendItem, ControlPosition } from '@classic-homes/maps-core';
4
4
  import { computePositionClasses, computeVerticalOffsetStyle } from '@classic-homes/maps-core';
5
5
  import { cn } from '../../utils.js';
@@ -43,13 +43,16 @@
43
43
 
44
44
  // Register this control with the map context and get vertical offset
45
45
  $effect(() => {
46
- if (!legendElement || !mapContext?.registerControl) return;
46
+ const registerControl = mapContext?.registerControl;
47
+ if (!legendElement || !registerControl) return;
47
48
 
48
49
  // Get the element's height
49
50
  const height = legendElement.offsetHeight;
50
51
 
51
- // Register and get offset
52
- customControlOffset = mapContext.registerControl(legendId, position, height);
52
+ // Register and get offset - wrap in untrack to prevent infinite loop
53
+ untrack(() => {
54
+ customControlOffset = registerControl(legendId, position, height);
55
+ });
53
56
  });
54
57
 
55
58
  // Cleanup on destroy
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy } from 'svelte';
2
+ import { getContext, onDestroy, untrack } from 'svelte';
3
3
  import type {
4
4
  LegendGroupProps,
5
5
  LegendConfig,
@@ -69,18 +69,16 @@
69
69
 
70
70
  // Register this control with the map context and get vertical offset
71
71
  $effect(() => {
72
- const registerFn = mapContext?.registerControl;
73
- if (!containerElement || !registerFn) return;
74
-
75
- // Small delay to ensure DOM has updated after expansion change
76
- const timeoutId = setTimeout(() => {
77
- if (containerElement) {
78
- const height = containerElement.offsetHeight;
79
- customControlOffset = registerFn(groupId, position, height);
80
- }
81
- }, 50);
82
-
83
- return () => clearTimeout(timeoutId);
72
+ const registerControl = mapContext?.registerControl;
73
+ if (!containerElement || !registerControl) return;
74
+
75
+ // Get the element's height
76
+ const height = containerElement.offsetHeight;
77
+
78
+ // Register and get offset - wrap in untrack to prevent infinite loop
79
+ untrack(() => {
80
+ customControlOffset = registerControl(groupId, position, height);
81
+ });
84
82
  });
85
83
 
86
84
  // Cleanup on destroy
@@ -1,3 +1,5 @@
1
1
  export { default as Legend } from './Legend.svelte';
2
2
  export { default as LegendGroup } from './LegendGroup.svelte';
3
3
  export { default as CoordinateDisplay } from './CoordinateDisplay.svelte';
4
+ export { default as BasemapSwitcher } from './BasemapSwitcher.svelte';
5
+ export { default as LayerManager } from './LayerManager.svelte';
@@ -1,3 +1,5 @@
1
1
  export { default as Legend } from './Legend.svelte';
2
2
  export { default as LegendGroup } from './LegendGroup.svelte';
3
3
  export { default as CoordinateDisplay } from './CoordinateDisplay.svelte';
4
+ export { default as BasemapSwitcher } from './BasemapSwitcher.svelte';
5
+ export { default as LayerManager } from './LayerManager.svelte';
@@ -85,29 +85,41 @@
85
85
  let map: maplibregl.Map | null = $state(null);
86
86
  let mapLoaded = $state(false);
87
87
  let mapError = $state<string | null>(null);
88
+ let styleVersion = $state(0);
89
+ let activeStyleOverride: string | maplibregl.StyleSpecification | null = $state(null);
88
90
 
89
- // Registry for custom controls to coordinate positioning
91
+ function setMapStyle(style: string | maplibregl.StyleSpecification) {
92
+ activeStyleOverride = style;
93
+ map?.setStyle(style);
94
+ }
95
+
96
+ // Non-reactive registry for custom controls to coordinate positioning (like React's useRef pattern)
90
97
  // Each entry tracks controls at a position with their heights
91
98
  type ControlEntry = { id: string; height: number };
92
- const customControlRegistry = $state<Record<string, ControlEntry[]>>({
99
+ const customControlRegistryRef: Record<string, ControlEntry[]> = {
93
100
  'top-left': [],
94
101
  'top-right': [],
95
102
  'bottom-left': [],
96
103
  'bottom-right': [],
97
- });
104
+ };
105
+
106
+ // Only trigger re-render when registry structure changes (new controls added/removed)
107
+ let controlRegistryVersion = $state(0);
98
108
 
99
109
  // Register a custom control and return its vertical offset
100
110
  function registerControl(id: string, position: string, height: number): number {
101
- const positionControls = customControlRegistry[position] || [];
111
+ const positionControls = customControlRegistryRef[position] || [];
102
112
 
103
- // Check if already registered
104
113
  const existingIndex = positionControls.findIndex((c) => c.id === id);
105
114
  if (existingIndex >= 0) {
115
+ // Just update height - no reactivity trigger needed
106
116
  positionControls[existingIndex].height = height;
107
117
  } else {
118
+ // New control - add and trigger update
108
119
  positionControls.push({ id, height });
120
+ customControlRegistryRef[position] = positionControls;
121
+ controlRegistryVersion++;
109
122
  }
110
- customControlRegistry[position] = positionControls;
111
123
 
112
124
  // Calculate offset (sum of heights of controls before this one)
113
125
  let offset = 0;
@@ -120,13 +132,17 @@
120
132
 
121
133
  // Unregister a custom control
122
134
  function unregisterControl(id: string, position: string): void {
123
- const positionControls = customControlRegistry[position] || [];
124
- customControlRegistry[position] = positionControls.filter((c) => c.id !== id);
135
+ const positionControls = customControlRegistryRef[position] || [];
136
+ const filtered = positionControls.filter((c) => c.id !== id);
137
+ if (filtered.length !== positionControls.length) {
138
+ customControlRegistryRef[position] = filtered;
139
+ controlRegistryVersion++;
140
+ }
125
141
  }
126
142
 
127
143
  // Get offset for a control at a position
128
144
  function getCustomControlOffset(id: string, position: string): number {
129
- const positionControls = customControlRegistry[position] || [];
145
+ const positionControls = customControlRegistryRef[position] || [];
130
146
  let offset = 0;
131
147
  for (const control of positionControls) {
132
148
  if (control.id === id) break;
@@ -194,9 +210,13 @@
194
210
  get controlOffsets() {
195
211
  return controlOffsets();
196
212
  },
213
+ get styleVersion() {
214
+ return styleVersion;
215
+ },
197
216
  registerControl,
198
217
  unregisterControl,
199
218
  getCustomControlOffset,
219
+ setMapStyle,
200
220
  });
201
221
 
202
222
  const combinedError = $derived(error || mapError);
@@ -227,6 +247,11 @@
227
247
 
228
248
  map = newMap;
229
249
 
250
+ // Track style changes for layer re-initialization
251
+ newMap.on('style.load', () => {
252
+ styleVersion++;
253
+ });
254
+
230
255
  // Add controls
231
256
  if (navigationControl) {
232
257
  newMap.addControl(
@@ -379,8 +404,9 @@
379
404
  });
380
405
 
381
406
  // Track initial style to avoid unnecessary setStyle calls
382
- let initialStyleApplied = $state(false);
383
- let lastAppliedStyle: string | null = $state(null);
407
+ // These are NOT reactive - they're just internal tracking flags
408
+ let initialStyleApplied = false;
409
+ let lastAppliedStyle: string | null = null;
384
410
 
385
411
  // Update style when theme changes (but not on initial load)
386
412
  $effect(() => {
@@ -396,6 +422,9 @@
396
422
  return;
397
423
  }
398
424
 
425
+ // Don't override a programmatic style change (basemap switcher)
426
+ if (activeStyleOverride) return;
427
+
399
428
  // Only update if style actually changed
400
429
  if (styleKey !== lastAppliedStyle) {
401
430
  lastAppliedStyle = styleKey;
@@ -403,6 +432,13 @@
403
432
  }
404
433
  });
405
434
 
435
+ // Clear programmatic override when customStyle prop changes
436
+ $effect(() => {
437
+ if (customStyle) {
438
+ activeStyleOverride = null;
439
+ }
440
+ });
441
+
406
442
  onDestroy(() => {
407
443
  if (map) {
408
444
  map.remove();
@@ -15,6 +15,7 @@
15
15
  interface MapContext {
16
16
  readonly map: maplibregl.Map | null;
17
17
  readonly loaded: boolean;
18
+ readonly styleVersion: number;
18
19
  }
19
20
 
20
21
  /**
@@ -109,6 +110,22 @@
109
110
  let lastDataFingerprint: GeoJSONFingerprint | null = null;
110
111
  let pendingIdleCallback: number | null = null;
111
112
 
113
+ // Track style version for re-initialization after basemap changes
114
+ let lastKnownStyleVersion = -1;
115
+
116
+ $effect(() => {
117
+ const sv = mapContext.styleVersion ?? 0;
118
+ if (lastKnownStyleVersion === -1) {
119
+ lastKnownStyleVersion = sv;
120
+ return;
121
+ }
122
+ if (sv !== lastKnownStyleVersion) {
123
+ lastKnownStyleVersion = sv;
124
+ sourceAdded = false;
125
+ lastDataFingerprint = null;
126
+ }
127
+ });
128
+
112
129
  // Add source effect
113
130
  $effect(() => {
114
131
  const { map, loaded } = mapContext;
@@ -2,4 +2,4 @@ export { MapContainer, MapSkeleton, MapError, MapEmpty } from './base/index.js';
2
2
  export { Map, Source, Marker, Popup, PopupCard, PopupHeader, PopupContent, PopupFooter, } from './core/index.js';
3
3
  export { FillLayer, LineLayer, CircleLayer, SymbolLayer, FillExtrusionLayer, HeatmapLayer, type SvelteFeatureClickEventParams, type SvelteFeatureHoverEventParams, } from './layers/index.js';
4
4
  export { DeckOverlay } from './deck/index.js';
5
- export { Legend, LegendGroup, CoordinateDisplay } from './controls/index.js';
5
+ export { Legend, LegendGroup, CoordinateDisplay, BasemapSwitcher, LayerManager, } from './controls/index.js';
@@ -7,4 +7,4 @@ export { FillLayer, LineLayer, CircleLayer, SymbolLayer, FillExtrusionLayer, Hea
7
7
  // Deck.gl components
8
8
  export { DeckOverlay } from './deck/index.js';
9
9
  // Control components
10
- export { Legend, LegendGroup, CoordinateDisplay } from './controls/index.js';
10
+ export { Legend, LegendGroup, CoordinateDisplay, BasemapSwitcher, LayerManager, } from './controls/index.js';
@@ -14,6 +14,23 @@ export function useLayer(getOptions) {
14
14
  let lastAppliedVisible = undefined;
15
15
  let lastAppliedPaint = undefined;
16
16
  let lastAppliedFilterSerialized = '';
17
+ // Track style version for re-initialization after basemap changes
18
+ let lastKnownStyleVersion = -1;
19
+ $effect(() => {
20
+ const sv = mapContext.styleVersion ?? 0;
21
+ if (lastKnownStyleVersion === -1) {
22
+ lastKnownStyleVersion = sv;
23
+ return;
24
+ }
25
+ if (sv !== lastKnownStyleVersion) {
26
+ lastKnownStyleVersion = sv;
27
+ layerAdded = false;
28
+ hoveredFeatureId = null;
29
+ lastAppliedVisible = undefined;
30
+ lastAppliedPaint = undefined;
31
+ lastAppliedFilterSerialized = '';
32
+ }
33
+ });
17
34
  const createFeatureParams = (e, options, map) => {
18
35
  const feature = e.features?.[0];
19
36
  if (!feature)
@@ -22,6 +22,10 @@ export interface MapContextValue {
22
22
  unregisterControl?: (id: string, position: string) => void;
23
23
  /** Get the offset for a specific custom control */
24
24
  getCustomControlOffset?: (id: string, position: string) => number;
25
+ /** Counter incremented on each style.load event, used to re-initialize sources/layers */
26
+ readonly styleVersion: number;
27
+ /** Set the map style programmatically (e.g. basemap switching) */
28
+ setMapStyle?: (style: string | maplibregl.StyleSpecification) => void;
25
29
  }
26
30
  /**
27
31
  * Get the map context from a parent Map component
@@ -3,10 +3,11 @@
3
3
  *
4
4
  * Svelte MapLibre components for the Classic Theme design system
5
5
  */
6
- export { MapContainer, MapSkeleton, MapError, MapEmpty, Map, Source, Marker, Popup, PopupCard, PopupHeader, PopupContent, PopupFooter, FillLayer, LineLayer, CircleLayer, SymbolLayer, FillExtrusionLayer, HeatmapLayer, Legend, LegendGroup, CoordinateDisplay, DeckOverlay, type SvelteFeatureClickEventParams, type SvelteFeatureHoverEventParams, } from './components/index.js';
6
+ export { MapContainer, MapSkeleton, MapError, MapEmpty, Map, Source, Marker, Popup, PopupCard, PopupHeader, PopupContent, PopupFooter, FillLayer, LineLayer, CircleLayer, SymbolLayer, FillExtrusionLayer, HeatmapLayer, Legend, LegendGroup, CoordinateDisplay, BasemapSwitcher, LayerManager, DeckOverlay, type SvelteFeatureClickEventParams, type SvelteFeatureHoverEventParams, } from './components/index.js';
7
7
  export { useMapTheme, useReducedMotion, useMapContext, tryUseMapContext, type MapContextValue, } from './composables/index.js';
8
8
  export { cn } from './utils.js';
9
- export type { LngLat, MapBounds, BoundsArray, MapViewport, GeoJSONFeature, GeoJSONFeatureCollection, MarkerData, MapClickEventParams, MapHoverEventParams, FeatureClickEventParams, FeatureHoverEventParams, MarkerClickEventParams, MapMoveEventParams, BaseMapProps, MapProps, SourceProps, FillLayerProps, LineLayerProps, CircleLayerProps, SymbolLayerProps, FillExtrusionLayerProps, HeatmapLayerProps, MarkerProps, PopupProps, LegendItem, GradientStop, LegendProps, CoordinateDisplayProps, LegendConfig, LegendGroupProps, } from '@classic-homes/maps-core';
10
- export { THEME_LIGHT, THEME_DARK, lightTheme, darkTheme, lightBasicStyle, darkBasicStyle, generateMapDescription, formatCoordinates, formatDistance, formatArea, createHoverExpression, createSelectionExpression, getHighlightStyle, getSelectionStyle, calculateBounds, extendBounds, combineBounds, isValidBounds, loadGeoJSON, validateGeoJSON, calculateCentroid, countFeaturesByType, filterByGeometryType, } from '@classic-homes/maps-core';
9
+ export type { LngLat, MapBounds, BoundsArray, MapViewport, GeoJSONFeature, GeoJSONFeatureCollection, MarkerData, MapClickEventParams, MapHoverEventParams, FeatureClickEventParams, FeatureHoverEventParams, MarkerClickEventParams, MapMoveEventParams, BaseMapProps, MapProps, SourceProps, FillLayerProps, LineLayerProps, CircleLayerProps, SymbolLayerProps, FillExtrusionLayerProps, HeatmapLayerProps, MarkerProps, PopupProps, LegendItem, GradientStop, LegendProps, CoordinateDisplayProps, LegendConfig, LegendGroupProps, BasemapProvider, BasemapCategory, BasemapDefinition, BasemapApiKeys, BasemapSwitcherProps, OverlayLayerCategory, OverlayLayerDefinition, OverlayLayerState, OverlayApiKeys, LayerManagerProps, } from '@classic-homes/maps-core';
10
+ export { CATEGORY_META, SCHOOL_DISTRICTS, CITY_LIMITS, ZONING_DISTRICTS, PARKS, TRAILS, SPORTS_FACILITIES, TRANSIT_ROUTES, BIKE_LANES, FLOOD_ZONES, WATERSHEDS, BUILTIN_OVERLAY_LAYERS, resolveLayerSourceUrl, getLayerStyleForTheme, searchLayers, getLayersByCategory, groupLayersByCategory, getLayerCategories, createInitialLayerStates, toggleLayerVisibility, setLayerOpacity, } from '@classic-homes/maps-core';
11
+ export { THEME_LIGHT, THEME_DARK, lightTheme, darkTheme, lightBasicStyle, darkBasicStyle, generateMapDescription, formatCoordinates, formatDistance, formatArea, createHoverExpression, createSelectionExpression, getHighlightStyle, getSelectionStyle, calculateBounds, extendBounds, combineBounds, isValidBounds, loadGeoJSON, validateGeoJSON, calculateCentroid, countFeaturesByType, filterByGeometryType, BUILTIN_BASEMAPS, FREE_BASEMAPS, OSM_STANDARD, resolveBasemapStyle, getAvailableBasemaps, } from '@classic-homes/maps-core';
11
12
  export type { LoadGeoJSONOptions, LoadGeoJSONResult, ValidationResult, ClusterFeatureProperties, ClusterExpandOptions, } from '@classic-homes/maps-core';
12
13
  export { isClusterFeature, getClusterExpansionZoom, getClusterChildren, getClusterLeaves, expandCluster, getClusterBounds, expandClusterToBounds, } from '@classic-homes/maps-core';
package/dist/lib/index.js CHANGED
@@ -12,18 +12,22 @@ Map, Source, Marker, Popup, PopupCard, PopupHeader, PopupContent, PopupFooter,
12
12
  // Layers
13
13
  FillLayer, LineLayer, CircleLayer, SymbolLayer, FillExtrusionLayer, HeatmapLayer,
14
14
  // Controls
15
- Legend, LegendGroup, CoordinateDisplay,
15
+ Legend, LegendGroup, CoordinateDisplay, BasemapSwitcher, LayerManager,
16
16
  // Deck.gl
17
17
  DeckOverlay, } from './components/index.js';
18
18
  // Composables
19
19
  export { useMapTheme, useReducedMotion, useMapContext, tryUseMapContext, } from './composables/index.js';
20
20
  // Utils
21
21
  export { cn } from './utils.js';
22
+ // Re-export layer presets and utilities from maps-core
23
+ export { CATEGORY_META, SCHOOL_DISTRICTS, CITY_LIMITS, ZONING_DISTRICTS, PARKS, TRAILS, SPORTS_FACILITIES, TRANSIT_ROUTES, BIKE_LANES, FLOOD_ZONES, WATERSHEDS, BUILTIN_OVERLAY_LAYERS, resolveLayerSourceUrl, getLayerStyleForTheme, searchLayers, getLayersByCategory, groupLayersByCategory, getLayerCategories, createInitialLayerStates, toggleLayerVisibility, setLayerOpacity, } from '@classic-homes/maps-core';
22
24
  // Re-export theme and utilities from maps-core
23
25
  export { THEME_LIGHT, THEME_DARK, lightTheme, darkTheme, lightBasicStyle, darkBasicStyle, generateMapDescription, formatCoordinates, formatDistance, formatArea, createHoverExpression, createSelectionExpression, getHighlightStyle, getSelectionStyle,
24
26
  // Bounds utilities
25
27
  calculateBounds, extendBounds, combineBounds, isValidBounds,
26
28
  // Data utilities
27
- loadGeoJSON, validateGeoJSON, calculateCentroid, countFeaturesByType, filterByGeometryType, } from '@classic-homes/maps-core';
29
+ loadGeoJSON, validateGeoJSON, calculateCentroid, countFeaturesByType, filterByGeometryType,
30
+ // Basemap utilities
31
+ BUILTIN_BASEMAPS, FREE_BASEMAPS, OSM_STANDARD, resolveBasemapStyle, getAvailableBasemaps, } from '@classic-homes/maps-core';
28
32
  // Re-export cluster utilities from maps-core
29
33
  export { isClusterFeature, getClusterExpansionZoom, getClusterChildren, getClusterLeaves, expandCluster, getClusterBounds, expandClusterToBounds, } from '@classic-homes/maps-core';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/maps-svelte",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Svelte MapLibre components for Classic Theme",
5
5
  "keywords": [
6
6
  "maps",
@@ -36,6 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@classic-homes/maps-core": "*",
39
+ "@classic-homes/theme-svelte": "*",
39
40
  "@classic-homes/theme-tokens": "*",
40
41
  "clsx": "^2.1.0",
41
42
  "tailwind-merge": "^2.2.0"