@classic-homes/maps-svelte 0.2.4 → 0.2.6

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,13 +1,7 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy, untrack } from 'svelte';
3
- import type {
4
- BasemapSwitcherProps,
5
- BasemapDefinition,
6
- ControlPosition,
7
- } from '@classic-homes/maps-core';
2
+ import { getContext } from 'svelte';
3
+ import type { BasemapSwitcherProps, BasemapDefinition } from '@classic-homes/maps-core';
8
4
  import {
9
- computePositionClasses,
10
- computeVerticalOffsetStyle,
11
5
  FREE_BASEMAPS,
12
6
  getAvailableBasemaps,
13
7
  resolveBasemapStyle,
@@ -16,11 +10,9 @@
16
10
  import '@classic-homes/maps-core/styles/basemap-switcher.css';
17
11
  import { cn } from '../../utils.js';
18
12
  import type maplibregl from 'maplibre-gl';
13
+ import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
19
14
 
20
15
  interface MapContext {
21
- readonly controlOffsets?: Record<string, number>;
22
- registerControl?: (id: string, position: string, height: number) => number;
23
- unregisterControl?: (id: string, position: string) => void;
24
16
  setMapStyle?: (style: string | maplibregl.StyleSpecification) => void;
25
17
  }
26
18
 
@@ -44,7 +36,17 @@
44
36
 
45
37
  let isOpen = $state(false);
46
38
  let internalActiveId = $state<string | undefined>(undefined);
47
- let customControlOffset = $state(0);
39
+ let containerElement: HTMLDivElement;
40
+
41
+ // Use positioning coordinator
42
+ const controlPosition = useControlPosition(() => containerElement, {
43
+ id: switcherId,
44
+ type: 'basemap-switcher',
45
+ position: position as 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right',
46
+ priority: 'normal',
47
+ zLayer: 'interactive',
48
+ hasExpandedState: true,
49
+ });
48
50
 
49
51
  // Initialize with defaultBasemapId (reactive to prop changes)
50
52
  $effect(() => {
@@ -52,7 +54,6 @@
52
54
  internalActiveId = defaultBasemapId;
53
55
  }
54
56
  });
55
- let containerElement: HTMLDivElement;
56
57
 
57
58
  // Compute available basemaps
58
59
  const sourceBasemaps = $derived(basemaps ?? FREE_BASEMAPS);
@@ -68,17 +69,6 @@
68
69
  activeBasemapId ?? internalActiveId ?? availableBasemaps()[0]?.id
69
70
  );
70
71
 
71
- // Register this control with the map context
72
- $effect(() => {
73
- const registerControl = mapContext?.registerControl;
74
- if (!containerElement || !registerControl) return;
75
-
76
- const height = containerElement.offsetHeight;
77
- untrack(() => {
78
- customControlOffset = registerControl(switcherId, position, height);
79
- });
80
- });
81
-
82
72
  // Close on outside click
83
73
  $effect(() => {
84
74
  if (!isOpen) return;
@@ -86,6 +76,7 @@
86
76
  const handleMouseDown = (e: MouseEvent) => {
87
77
  if (containerElement && !containerElement.contains(e.target as Node)) {
88
78
  isOpen = false;
79
+ controlPosition.setExpanded(false);
89
80
  }
90
81
  };
91
82
 
@@ -103,6 +94,7 @@
103
94
  const handleKeyDown = (e: KeyboardEvent) => {
104
95
  if (e.key === 'Escape') {
105
96
  isOpen = false;
97
+ controlPosition.setExpanded(false);
106
98
  }
107
99
  };
108
100
 
@@ -114,38 +106,24 @@
114
106
  });
115
107
 
116
108
  function handleSelect(basemap: BasemapDefinition) {
117
- const style = resolveBasemapStyle(basemap, apiKeys);
118
- if (!style) return;
109
+ const resolvedStyle = resolveBasemapStyle(basemap, apiKeys);
110
+ if (!resolvedStyle) return;
119
111
 
120
112
  if (!activeBasemapId) {
121
113
  internalActiveId = basemap.id;
122
114
  }
123
115
 
124
- mapContext?.setMapStyle?.(style as string | maplibregl.StyleSpecification);
116
+ mapContext?.setMapStyle?.(resolvedStyle as string | maplibregl.StyleSpecification);
125
117
  onBasemapChange?.(basemap);
126
118
  isOpen = false;
119
+ controlPosition.setExpanded(false);
127
120
  }
128
121
 
129
- // Cleanup on destroy
130
- onDestroy(() => {
131
- mapContext?.unregisterControl?.(switcherId, position);
132
- });
133
-
134
- const hasControlsAtPosition = $derived(() => {
135
- const offset = mapContext?.controlOffsets?.[position] ?? 0;
136
- return offset > 0;
137
- });
138
-
139
- const getPositionClasses = $derived(() => {
140
- return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
141
- });
142
-
143
- const verticalOffsetStyle = $derived(() => {
144
- const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
145
- return Object.entries(styleObj)
146
- .map(([key, value]) => `${key}: ${value}`)
147
- .join('; ');
148
- });
122
+ function handleToggle() {
123
+ const newState = !isOpen;
124
+ isOpen = newState;
125
+ controlPosition.setExpanded(newState);
126
+ }
149
127
 
150
128
  const isTop = $derived(position.startsWith('top'));
151
129
  const panelWidth = $derived(columns * (thumbnailSize + 12) + 16);
@@ -153,8 +131,8 @@
153
131
 
154
132
  <div
155
133
  bind:this={containerElement}
156
- class={cn('absolute z-10', getPositionClasses(), className)}
157
- style={verticalOffsetStyle()}
134
+ class={cn('absolute', !controlPosition.visible && 'invisible', className)}
135
+ style={controlPosition.style}
158
136
  >
159
137
  <!-- Flyout panel -->
160
138
  {#if isOpen}
@@ -214,7 +192,7 @@
214
192
  'hover:bg-muted transition-colors cursor-pointer',
215
193
  'text-foreground'
216
194
  )}
217
- onclick={() => (isOpen = !isOpen)}
195
+ onclick={handleToggle}
218
196
  aria-label="Switch basemap"
219
197
  aria-expanded={isOpen}
220
198
  aria-haspopup="true"
@@ -1,21 +1,14 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy, untrack } from 'svelte';
2
+ import { getContext } from 'svelte';
3
3
  import type maplibregl from 'maplibre-gl';
4
- import type { CoordinateDisplayProps, LngLat, ControlPosition } from '@classic-homes/maps-core';
5
- import {
6
- formatCoordinates,
7
- computePositionClasses,
8
- computeVerticalOffsetStyle,
9
- } from '@classic-homes/maps-core';
4
+ import type { CoordinateDisplayProps, LngLat } from '@classic-homes/maps-core';
5
+ import { formatCoordinates, EDGE_MARGIN, STACK_GAP } from '@classic-homes/maps-core';
10
6
  import { cn } from '../../utils.js';
7
+ import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
11
8
 
12
9
  interface MapContext {
13
10
  readonly map: maplibregl.Map | null;
14
11
  readonly loaded: boolean;
15
- readonly controlOffsets?: Record<string, number>;
16
- registerControl?: (id: string, position: string, height: number) => number;
17
- unregisterControl?: (id: string, position: string) => void;
18
- getCustomControlOffset?: (id: string, position: string) => number;
19
12
  }
20
13
 
21
14
  interface Props extends CoordinateDisplayProps {}
@@ -34,6 +27,9 @@
34
27
  // Generate unique ID for this control instance
35
28
  const controlId = `coord-display-${Math.random().toString(36).slice(2, 9)}`;
36
29
 
30
+ // Native control dimensions for offset calculations
31
+ const NAV_CONTROL_WIDTH = 29;
32
+
37
33
  let cursorLngLat: LngLat | null = $state(null);
38
34
  let currentZoom = $state(0);
39
35
  let currentBearing = $state(0);
@@ -41,45 +37,33 @@
41
37
  let zoomHandler: (() => void) | null = null;
42
38
  let rotateHandler: (() => void) | null = null;
43
39
  let controlElement: HTMLDivElement;
44
- let customControlOffset = $state(0);
45
-
46
- // Check if controls exist at this position - if so, offset horizontally toward center
47
- const hasControlsAtPosition = $derived(() => {
48
- const offset = mapContext?.controlOffsets?.[position] ?? 0;
49
- return offset > 0;
50
- });
51
40
 
52
- // Get combined position classes using shared utility
53
- const getPositionClasses = $derived(() => {
54
- return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
41
+ // Use positioning coordinator with 'critical' priority to be at the edge
42
+ const controlPosition = useControlPosition(() => controlElement, {
43
+ id: controlId,
44
+ type: 'coordinate-display',
45
+ position: position as 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right',
46
+ priority: 'critical',
47
+ zLayer: 'base',
55
48
  });
56
49
 
57
- // Register this control with the map context and get vertical offset
58
- $effect(() => {
59
- const registerControl = mapContext?.registerControl;
60
- if (!controlElement || !registerControl) return;
50
+ // Adjust style based on position
51
+ const ATTRIBUTION_HEIGHT = 32;
61
52
 
62
- // Get the element's height
63
- const height = controlElement.offsetHeight;
53
+ const adjustedStyle = $derived(() => {
54
+ if (position === 'top-right') {
55
+ // Position horizontally to the left of native controls
56
+ const rightOffset = NAV_CONTROL_WIDTH + STACK_GAP + EDGE_MARGIN;
57
+ return `position: absolute; top: ${EDGE_MARGIN}px; right: ${rightOffset}px; z-index: 10;`;
58
+ }
64
59
 
65
- // Register and get offset - wrap in untrack to prevent infinite loop
66
- untrack(() => {
67
- customControlOffset = registerControl(controlId, position, height);
68
- });
69
- });
70
-
71
- // Cleanup on destroy
72
- onDestroy(() => {
73
- mapContext?.unregisterControl?.(controlId, position);
74
- });
60
+ if (position === 'bottom-right') {
61
+ // Add extra offset to not cover the MapLibre attribution
62
+ const bottomOffset = EDGE_MARGIN + ATTRIBUTION_HEIGHT;
63
+ return `position: absolute; bottom: ${bottomOffset}px; right: ${EDGE_MARGIN}px; z-index: 10;`;
64
+ }
75
65
 
76
- // Calculate vertical offset style for stacking with other custom controls
77
- // Uses top/bottom properties instead of margins for correct absolute positioning
78
- const verticalOffsetStyle = $derived(() => {
79
- const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
80
- return Object.entries(styleObj)
81
- .map(([key, value]) => `${key}: ${value}`)
82
- .join('; ');
66
+ return controlPosition.style;
83
67
  });
84
68
 
85
69
  // Format the coordinate based on settings
@@ -139,14 +123,14 @@
139
123
  <div
140
124
  bind:this={controlElement}
141
125
  class={cn(
142
- 'absolute z-10 bg-card rounded px-2 py-1',
126
+ 'absolute bg-card rounded px-2 py-1',
143
127
  'text-xs font-mono text-muted-foreground',
144
128
  'border border-black dark:border-gray-400 shadow-sm',
145
129
  'flex items-center gap-3',
146
- getPositionClasses(),
130
+ !controlPosition.visible && 'invisible',
147
131
  className
148
132
  )}
149
- style={verticalOffsetStyle()}
133
+ style={adjustedStyle()}
150
134
  >
151
135
  {#if cursorLngLat}
152
136
  <span title="Cursor coordinates">{formatCoord(cursorLngLat)}</span>
@@ -0,0 +1,96 @@
1
+ <script lang="ts" module>
2
+ import type {
3
+ ControlPriority,
4
+ ExtendedControlPosition,
5
+ ZIndexLayer,
6
+ } from '@classic-homes/maps-core';
7
+
8
+ export interface CustomControlProps {
9
+ /** Position on the map (default: 'top-right') */
10
+ position?: ExtendedControlPosition;
11
+ /** Fallback positions if primary is occupied */
12
+ fallbacks?: ExtendedControlPosition[];
13
+ /** Priority for collision resolution (default: 'normal') */
14
+ priority?: ControlPriority;
15
+ /** Z-index layer (default: 'interactive') */
16
+ zLayer?: ZIndexLayer;
17
+ /** Optional unique ID (auto-generated if not provided) */
18
+ id?: string;
19
+ /** Whether the control has an expandable state */
20
+ hasExpandedState?: boolean;
21
+ /** Additional CSS class names */
22
+ class?: string;
23
+ /** Inline styles (merged with positioning styles) */
24
+ style?: string;
25
+ }
26
+ </script>
27
+
28
+ <script lang="ts">
29
+ import { cn } from '../../utils.js';
30
+ import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
31
+ import type { Snippet } from 'svelte';
32
+ import { setContext } from 'svelte';
33
+
34
+ interface Props extends CustomControlProps {
35
+ children: Snippet;
36
+ }
37
+
38
+ let {
39
+ position = 'top-right',
40
+ fallbacks,
41
+ priority = 'normal',
42
+ zLayer = 'interactive',
43
+ id,
44
+ hasExpandedState = false,
45
+ class: className,
46
+ style: userStyle,
47
+ children,
48
+ }: Props = $props();
49
+
50
+ const controlId = id ?? `custom-control-${Math.random().toString(36).slice(2, 9)}`;
51
+ let controlElement: HTMLDivElement;
52
+
53
+ const controlPosition = useControlPosition(() => controlElement, {
54
+ id: controlId,
55
+ type: 'custom',
56
+ position,
57
+ fallbacks,
58
+ priority,
59
+ zLayer,
60
+ hasExpandedState,
61
+ });
62
+
63
+ const mergedStyle = $derived(
64
+ userStyle ? `${controlPosition.style}; ${userStyle}` : controlPosition.style
65
+ );
66
+
67
+ // Expose setExpanded via context
68
+ setContext('custom-control', {
69
+ setExpanded: controlPosition.setExpanded,
70
+ });
71
+ </script>
72
+
73
+ <!--
74
+ CustomControl component for positioning arbitrary content on the map
75
+
76
+ This component integrates with the map's positioning coordinator for
77
+ collision avoidance and proper stacking with other controls.
78
+
79
+ @example
80
+ ```svelte
81
+ <Map>
82
+ <CustomControl position="top-right">
83
+ <div class="bg-white p-2 rounded shadow">
84
+ Custom widget content
85
+ </div>
86
+ </CustomControl>
87
+ </Map>
88
+ ```
89
+ -->
90
+ <div
91
+ bind:this={controlElement}
92
+ class={cn(!controlPosition.visible && 'invisible', className)}
93
+ style={mergedStyle}
94
+ >
95
+ {@render children()}
96
+ </div>
@@ -0,0 +1,26 @@
1
+ import type { ControlPriority, ExtendedControlPosition, ZIndexLayer } from '@classic-homes/maps-core';
2
+ export interface CustomControlProps {
3
+ /** Position on the map (default: 'top-right') */
4
+ position?: ExtendedControlPosition;
5
+ /** Fallback positions if primary is occupied */
6
+ fallbacks?: ExtendedControlPosition[];
7
+ /** Priority for collision resolution (default: 'normal') */
8
+ priority?: ControlPriority;
9
+ /** Z-index layer (default: 'interactive') */
10
+ zLayer?: ZIndexLayer;
11
+ /** Optional unique ID (auto-generated if not provided) */
12
+ id?: string;
13
+ /** Whether the control has an expandable state */
14
+ hasExpandedState?: boolean;
15
+ /** Additional CSS class names */
16
+ class?: string;
17
+ /** Inline styles (merged with positioning styles) */
18
+ style?: string;
19
+ }
20
+ import type { Snippet } from 'svelte';
21
+ interface Props extends CustomControlProps {
22
+ children: Snippet;
23
+ }
24
+ declare const CustomControl: import("svelte").Component<Props, {}, "">;
25
+ type CustomControl = ReturnType<typeof CustomControl>;
26
+ export default CustomControl;
@@ -5,20 +5,13 @@
5
5
  OverlayLayerDefinition,
6
6
  OverlayLayerState,
7
7
  OverlayLayerCategory,
8
- ControlPosition,
9
8
  } from '@classic-homes/maps-core';
10
9
  import {
11
- computePositionClasses,
12
- computeVerticalOffsetStyle,
13
10
  CATEGORY_META,
14
11
  getLayerStyleForTheme,
15
12
  searchLayers,
16
13
  getLayerCategories,
17
14
  groupLayersByCategory,
18
- createInitialLayerStates,
19
- toggleLayerVisibility,
20
- setLayerOpacity,
21
- reorderLayers,
22
15
  getLayerStatesArray,
23
16
  resolveLayerSourceUrl,
24
17
  } from '@classic-homes/maps-core';
@@ -26,14 +19,12 @@
26
19
  import { Switch } from '@classic-homes/theme-svelte';
27
20
  import { cn } from '../../utils.js';
28
21
  import type maplibregl from 'maplibre-gl';
22
+ import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
29
23
 
30
24
  interface MapContext {
31
25
  readonly map: maplibregl.Map | null;
32
26
  readonly loaded: boolean;
33
27
  readonly styleVersion: number;
34
- readonly controlOffsets?: Record<string, number>;
35
- registerControl?: (id: string, position: string, height: number) => number;
36
- unregisterControl?: (id: string, position: string) => void;
37
28
  }
38
29
 
39
30
  interface Props extends LayerManagerProps {}
@@ -129,12 +120,21 @@
129
120
 
130
121
  let searchQuery = $state('');
131
122
  let activeCategory = $state<OverlayLayerCategory | 'all'>('all');
132
- let customControlOffset = $state(0);
133
123
  let expandedOpacity = $state<string | null>(null);
134
124
  let draggedId = $state<string | null>(null);
135
125
  let dragOver = $state<{ id: string; position: 'above' | 'below' } | null>(null);
136
126
  let containerElement: HTMLDivElement;
137
127
 
128
+ // Use positioning coordinator
129
+ const controlPosition = useControlPosition(() => containerElement, {
130
+ id: managerId,
131
+ type: 'layer-manager',
132
+ position: position as 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right',
133
+ priority: 'high',
134
+ zLayer: 'interactive',
135
+ hasExpandedState: true,
136
+ });
137
+
138
138
  // Track style version for re-initialization after basemap changes
139
139
  let lastKnownStyleVersion = -1;
140
140
  let addedSources = new Set<string>();
@@ -204,17 +204,6 @@
204
204
  return getLayerStatesArray(layerStates).filter((s) => s.visible);
205
205
  });
206
206
 
207
- // Register this control with the map context
208
- $effect(() => {
209
- const registerControl = mapContext?.registerControl;
210
- if (!containerElement || !registerControl) return;
211
-
212
- const height = containerElement.offsetHeight;
213
- untrack(() => {
214
- customControlOffset = registerControl(managerId, position, height);
215
- });
216
- });
217
-
218
207
  // Reset on style version change
219
208
  $effect(() => {
220
209
  const sv = mapContext.styleVersion ?? 0;
@@ -402,6 +391,7 @@
402
391
  const handleMouseDown = (e: MouseEvent) => {
403
392
  if (containerElement && !containerElement.contains(e.target as Node)) {
404
393
  isOpen = false;
394
+ controlPosition.setExpanded(false);
405
395
  }
406
396
  };
407
397
 
@@ -419,6 +409,7 @@
419
409
  const handleKeyDown = (e: KeyboardEvent) => {
420
410
  if (e.key === 'Escape') {
421
411
  isOpen = false;
412
+ controlPosition.setExpanded(false);
422
413
  }
423
414
  };
424
415
 
@@ -553,8 +544,6 @@
553
544
 
554
545
  // Cleanup on destroy
555
546
  onDestroy(() => {
556
- mapContext?.unregisterControl?.(managerId, position);
557
-
558
547
  const { map } = mapContext;
559
548
  if (!map) return;
560
549
 
@@ -580,29 +569,19 @@
580
569
  }
581
570
  });
582
571
 
583
- const hasControlsAtPosition = $derived.by(() => {
584
- const offset = mapContext?.controlOffsets?.[position] ?? 0;
585
- return offset > 0;
586
- });
587
-
588
- const getPositionClasses = $derived.by(() => {
589
- return computePositionClasses(position as ControlPosition, hasControlsAtPosition);
590
- });
591
-
592
- const verticalOffsetStyle = $derived.by(() => {
593
- const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
594
- return Object.entries(styleObj)
595
- .map(([key, value]) => `${key}: ${value}`)
596
- .join('; ');
597
- });
572
+ function handleToggle() {
573
+ const newState = !isOpen;
574
+ isOpen = newState;
575
+ controlPosition.setExpanded(newState);
576
+ }
598
577
 
599
578
  const isTop = $derived(position.startsWith('top'));
600
579
  </script>
601
580
 
602
581
  <div
603
582
  bind:this={containerElement}
604
- class={cn('absolute z-10', getPositionClasses, className)}
605
- style={verticalOffsetStyle}
583
+ class={cn('absolute', !controlPosition.visible && 'invisible', className)}
584
+ style={controlPosition.style}
606
585
  >
607
586
  <!-- Flyout panel -->
608
587
  {#if isOpen}
@@ -941,7 +920,7 @@
941
920
  'hover:bg-muted transition-colors cursor-pointer',
942
921
  'text-foreground'
943
922
  )}
944
- onclick={() => (isOpen = !isOpen)}
923
+ onclick={handleToggle}
945
924
  aria-label="Toggle layer manager"
946
925
  aria-expanded={isOpen}
947
926
  aria-haspopup="true"
@@ -1,15 +1,7 @@
1
1
  <script lang="ts">
2
- import { getContext, onDestroy, untrack } from 'svelte';
3
- import type { LegendProps, LegendItem, ControlPosition } from '@classic-homes/maps-core';
4
- import { computePositionClasses, computeVerticalOffsetStyle } from '@classic-homes/maps-core';
2
+ import type { LegendProps, LegendItem } from '@classic-homes/maps-core';
5
3
  import { cn } from '../../utils.js';
6
-
7
- interface MapContext {
8
- readonly controlOffsets?: Record<string, number>;
9
- registerControl?: (id: string, position: string, height: number) => number;
10
- unregisterControl?: (id: string, position: string) => void;
11
- getCustomControlOffset?: (id: string, position: string) => number;
12
- }
4
+ import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
13
5
 
14
6
  interface Props extends LegendProps {}
15
7
 
@@ -27,8 +19,6 @@
27
19
  onItemClick,
28
20
  }: Props = $props();
29
21
 
30
- const mapContext = getContext<MapContext>('maplibre-map');
31
-
32
22
  // Generate unique ID for this legend instance
33
23
  const legendId = `legend-${Math.random().toString(36).slice(2, 9)}`;
34
24
 
@@ -36,49 +26,19 @@
36
26
  let userToggled = $state(false);
37
27
  let localCollapsed = $state(false);
38
28
  let legendElement: HTMLDivElement;
39
- let customControlOffset = $state(0);
40
29
 
41
- // Determine effective collapsed state - user toggle takes precedence
42
- const isCollapsed = $derived(userToggled ? localCollapsed : collapsed);
43
-
44
- // Register this control with the map context and get vertical offset
45
- $effect(() => {
46
- const registerControl = mapContext?.registerControl;
47
- if (!legendElement || !registerControl) return;
48
-
49
- // Get the element's height
50
- const height = legendElement.offsetHeight;
51
-
52
- // Register and get offset - wrap in untrack to prevent infinite loop
53
- untrack(() => {
54
- customControlOffset = registerControl(legendId, position, height);
55
- });
30
+ // Use positioning coordinator
31
+ const controlPosition = useControlPosition(() => legendElement, {
32
+ id: legendId,
33
+ type: 'legend',
34
+ position: position as 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right',
35
+ priority: 'normal',
36
+ zLayer: 'interactive',
37
+ hasExpandedState: collapsible,
56
38
  });
57
39
 
58
- // Cleanup on destroy
59
- onDestroy(() => {
60
- mapContext?.unregisterControl?.(legendId, position);
61
- });
62
-
63
- // Check if controls exist at this position - if so, offset horizontally toward center
64
- const hasControlsAtPosition = $derived(() => {
65
- const offset = mapContext?.controlOffsets?.[position] ?? 0;
66
- return offset > 0;
67
- });
68
-
69
- // Get combined position classes using shared utility
70
- const getPositionClasses = $derived(() => {
71
- return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
72
- });
73
-
74
- // Calculate vertical offset style for stacking with other custom controls
75
- // Uses top/bottom properties instead of margins for correct absolute positioning
76
- const verticalOffsetStyle = $derived(() => {
77
- const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
78
- return Object.entries(styleObj)
79
- .map(([key, value]) => `${key}: ${value}`)
80
- .join('; ');
81
- });
40
+ // Determine effective collapsed state - user toggle takes precedence
41
+ const isCollapsed = $derived(userToggled ? localCollapsed : collapsed);
82
42
 
83
43
  function handleItemClick(item: LegendItem, index: number) {
84
44
  if (onItemClick) {
@@ -104,12 +64,12 @@
104
64
  <div
105
65
  bind:this={legendElement}
106
66
  class={cn(
107
- 'absolute z-10 bg-card rounded-lg shadow-lg border border-black dark:border-gray-400',
67
+ 'absolute bg-card rounded-lg shadow-lg border border-black dark:border-gray-400',
108
68
  'text-sm min-w-[140px] max-w-[280px]',
109
- getPositionClasses(),
69
+ !controlPosition.visible && 'invisible',
110
70
  className
111
71
  )}
112
- style={verticalOffsetStyle()}
72
+ style={controlPosition.style}
113
73
  >
114
74
  {#if title || collapsible}
115
75
  <button