@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.
- package/dist/lib/components/controls/BasemapSwitcher.svelte +28 -50
- package/dist/lib/components/controls/CoordinateDisplay.svelte +31 -47
- package/dist/lib/components/controls/CustomControl.svelte +96 -0
- package/dist/lib/components/controls/CustomControl.svelte.d.ts +26 -0
- package/dist/lib/components/controls/LayerManager.svelte +21 -42
- package/dist/lib/components/controls/Legend.svelte +15 -55
- package/dist/lib/components/controls/LegendGroup.svelte +18 -59
- package/dist/lib/components/controls/index.d.ts +1 -0
- package/dist/lib/components/controls/index.js +1 -0
- package/dist/lib/components/core/Map.svelte +103 -99
- package/dist/lib/components/index.d.ts +1 -1
- package/dist/lib/components/index.js +1 -1
- package/dist/lib/composables/index.d.ts +1 -0
- package/dist/lib/composables/index.js +1 -0
- package/dist/lib/composables/useControlPosition.svelte.d.ts +75 -0
- package/dist/lib/composables/useControlPosition.svelte.js +179 -0
- package/dist/lib/composables/useMapContext.svelte.d.ts +3 -8
- package/dist/lib/composables/useMapTheme.svelte.js +6 -4
- package/dist/lib/index.d.ts +4 -4
- package/dist/lib/index.js +3 -3
- package/package.json +1 -1
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
LegendGroupProps,
|
|
5
|
-
LegendConfig,
|
|
6
|
-
LegendItem,
|
|
7
|
-
ControlPosition,
|
|
8
|
-
} from '@classic-homes/maps-core';
|
|
9
|
-
import { computePositionClasses, computeVerticalOffsetStyle } from '@classic-homes/maps-core';
|
|
2
|
+
import type { LegendGroupProps, LegendConfig, LegendItem } from '@classic-homes/maps-core';
|
|
10
3
|
import { cn } from '../../utils.js';
|
|
11
|
-
|
|
12
|
-
interface MapContext {
|
|
13
|
-
readonly controlOffsets?: Record<string, number>;
|
|
14
|
-
registerControl?: (id: string, position: string, height: number) => number;
|
|
15
|
-
unregisterControl?: (id: string, position: string) => void;
|
|
16
|
-
getCustomControlOffset?: (id: string, position: string) => number;
|
|
17
|
-
}
|
|
4
|
+
import { useControlPosition } from '../../composables/useControlPosition.svelte.js';
|
|
18
5
|
|
|
19
6
|
interface Props extends LegendGroupProps {}
|
|
20
7
|
|
|
@@ -28,8 +15,6 @@
|
|
|
28
15
|
class: className,
|
|
29
16
|
}: Props = $props();
|
|
30
17
|
|
|
31
|
-
const mapContext = getContext<MapContext>('maplibre-map');
|
|
32
|
-
|
|
33
18
|
// Generate unique ID for this legend group instance
|
|
34
19
|
const groupId = `legend-group-${Math.random().toString(36).slice(2, 9)}`;
|
|
35
20
|
|
|
@@ -42,7 +27,16 @@
|
|
|
42
27
|
|
|
43
28
|
let internalExpanded = $state<string | null>(getInitialExpanded());
|
|
44
29
|
let containerElement: HTMLDivElement;
|
|
45
|
-
|
|
30
|
+
|
|
31
|
+
// Use positioning coordinator
|
|
32
|
+
const controlPosition = useControlPosition(() => containerElement, {
|
|
33
|
+
id: groupId,
|
|
34
|
+
type: 'legend-group',
|
|
35
|
+
position: position as 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right',
|
|
36
|
+
priority: 'normal',
|
|
37
|
+
zLayer: 'interactive',
|
|
38
|
+
hasExpandedState: true,
|
|
39
|
+
});
|
|
46
40
|
|
|
47
41
|
// Use controlled or internal state
|
|
48
42
|
const expandedId = $derived(
|
|
@@ -57,6 +51,9 @@
|
|
|
57
51
|
internalExpanded = newExpanded;
|
|
58
52
|
}
|
|
59
53
|
|
|
54
|
+
// Notify coordinator of expanded state change
|
|
55
|
+
controlPosition.setExpanded(newExpanded !== null);
|
|
56
|
+
|
|
60
57
|
onExpandedChange?.(newExpanded);
|
|
61
58
|
}
|
|
62
59
|
|
|
@@ -67,44 +64,6 @@
|
|
|
67
64
|
}
|
|
68
65
|
});
|
|
69
66
|
|
|
70
|
-
// Register this control with the map context and get vertical offset
|
|
71
|
-
$effect(() => {
|
|
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
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
// Cleanup on destroy
|
|
85
|
-
onDestroy(() => {
|
|
86
|
-
mapContext?.unregisterControl?.(groupId, position);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// Check if controls exist at this position
|
|
90
|
-
const hasControlsAtPosition = $derived(() => {
|
|
91
|
-
const offset = mapContext?.controlOffsets?.[position] ?? 0;
|
|
92
|
-
return offset > 0;
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// Get combined position classes using shared utility
|
|
96
|
-
const getPositionClasses = $derived(() => {
|
|
97
|
-
return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// Calculate vertical offset style for stacking with other custom controls
|
|
101
|
-
const verticalOffsetStyle = $derived(() => {
|
|
102
|
-
const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
|
|
103
|
-
return Object.entries(styleObj)
|
|
104
|
-
.map(([key, value]) => `${key}: ${value}`)
|
|
105
|
-
.join('; ');
|
|
106
|
-
});
|
|
107
|
-
|
|
108
67
|
// Generate gradient CSS
|
|
109
68
|
function getGradientStyle(legend: LegendConfig): string {
|
|
110
69
|
if (legend.type !== 'gradient' || !legend.gradient?.length) return '';
|
|
@@ -120,12 +79,12 @@
|
|
|
120
79
|
<div
|
|
121
80
|
bind:this={containerElement}
|
|
122
81
|
class={cn(
|
|
123
|
-
'absolute
|
|
82
|
+
'absolute bg-card rounded-lg shadow-lg border border-black dark:border-gray-400',
|
|
124
83
|
'text-sm min-w-[160px] max-w-[280px]',
|
|
125
|
-
|
|
84
|
+
!controlPosition.visible && 'invisible',
|
|
126
85
|
className
|
|
127
86
|
)}
|
|
128
|
-
style={
|
|
87
|
+
style={controlPosition.style}
|
|
129
88
|
>
|
|
130
89
|
{#each legends as legend, legendIndex}
|
|
131
90
|
{@const isExpanded = expandedId === legend.id}
|
|
@@ -3,3 +3,4 @@ export { default as LegendGroup } from './LegendGroup.svelte';
|
|
|
3
3
|
export { default as CoordinateDisplay } from './CoordinateDisplay.svelte';
|
|
4
4
|
export { default as BasemapSwitcher } from './BasemapSwitcher.svelte';
|
|
5
5
|
export { default as LayerManager } from './LayerManager.svelte';
|
|
6
|
+
export { default as CustomControl, type CustomControlProps } from './CustomControl.svelte';
|
|
@@ -3,3 +3,4 @@ export { default as LegendGroup } from './LegendGroup.svelte';
|
|
|
3
3
|
export { default as CoordinateDisplay } from './CoordinateDisplay.svelte';
|
|
4
4
|
export { default as BasemapSwitcher } from './BasemapSwitcher.svelte';
|
|
5
5
|
export { default as LayerManager } from './LayerManager.svelte';
|
|
6
|
+
export { default as CustomControl } from './CustomControl.svelte';
|
|
@@ -13,7 +13,11 @@
|
|
|
13
13
|
MapMoveEventParams,
|
|
14
14
|
LngLat,
|
|
15
15
|
} from '@classic-homes/maps-core';
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
generateMapDescription,
|
|
18
|
+
createPositioningCoordinator,
|
|
19
|
+
type PositioningCoordinator,
|
|
20
|
+
} from '@classic-homes/maps-core';
|
|
17
21
|
|
|
18
22
|
import { cn } from '../../utils.js';
|
|
19
23
|
import { useMapTheme } from '../../composables/useMapTheme.svelte.js';
|
|
@@ -88,69 +92,14 @@
|
|
|
88
92
|
let styleVersion = $state(0);
|
|
89
93
|
let activeStyleOverride: string | maplibregl.StyleSpecification | null = $state(null);
|
|
90
94
|
|
|
95
|
+
// Create positioning coordinator instance
|
|
96
|
+
const positioningCoordinator: PositioningCoordinator = createPositioningCoordinator();
|
|
97
|
+
|
|
91
98
|
function setMapStyle(style: string | maplibregl.StyleSpecification) {
|
|
92
99
|
activeStyleOverride = style;
|
|
93
100
|
map?.setStyle(style);
|
|
94
101
|
}
|
|
95
102
|
|
|
96
|
-
// Non-reactive registry for custom controls to coordinate positioning (like React's useRef pattern)
|
|
97
|
-
// Each entry tracks controls at a position with their heights
|
|
98
|
-
type ControlEntry = { id: string; height: number };
|
|
99
|
-
const customControlRegistryRef: Record<string, ControlEntry[]> = {
|
|
100
|
-
'top-left': [],
|
|
101
|
-
'top-right': [],
|
|
102
|
-
'bottom-left': [],
|
|
103
|
-
'bottom-right': [],
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
// Only trigger re-render when registry structure changes (new controls added/removed)
|
|
107
|
-
let controlRegistryVersion = $state(0);
|
|
108
|
-
|
|
109
|
-
// Register a custom control and return its vertical offset
|
|
110
|
-
function registerControl(id: string, position: string, height: number): number {
|
|
111
|
-
const positionControls = customControlRegistryRef[position] || [];
|
|
112
|
-
|
|
113
|
-
const existingIndex = positionControls.findIndex((c) => c.id === id);
|
|
114
|
-
if (existingIndex >= 0) {
|
|
115
|
-
// Just update height - no reactivity trigger needed
|
|
116
|
-
positionControls[existingIndex].height = height;
|
|
117
|
-
} else {
|
|
118
|
-
// New control - add and trigger update
|
|
119
|
-
positionControls.push({ id, height });
|
|
120
|
-
customControlRegistryRef[position] = positionControls;
|
|
121
|
-
controlRegistryVersion++;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// Calculate offset (sum of heights of controls before this one)
|
|
125
|
-
let offset = 0;
|
|
126
|
-
for (const control of positionControls) {
|
|
127
|
-
if (control.id === id) break;
|
|
128
|
-
offset += control.height + 8; // 8px gap between controls
|
|
129
|
-
}
|
|
130
|
-
return offset;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Unregister a custom control
|
|
134
|
-
function unregisterControl(id: string, position: string): void {
|
|
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
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Get offset for a control at a position
|
|
144
|
-
function getCustomControlOffset(id: string, position: string): number {
|
|
145
|
-
const positionControls = customControlRegistryRef[position] || [];
|
|
146
|
-
let offset = 0;
|
|
147
|
-
for (const control of positionControls) {
|
|
148
|
-
if (control.id === id) break;
|
|
149
|
-
offset += control.height + 8;
|
|
150
|
-
}
|
|
151
|
-
return offset;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
103
|
const reducedMotion = useReducedMotion();
|
|
155
104
|
const themeState = useMapTheme(() => themePref);
|
|
156
105
|
|
|
@@ -166,39 +115,6 @@
|
|
|
166
115
|
})
|
|
167
116
|
);
|
|
168
117
|
|
|
169
|
-
// Calculate control offsets for each position
|
|
170
|
-
// MapLibre controls are ~29px per button with ~5px margin
|
|
171
|
-
const controlOffsets = $derived(() => {
|
|
172
|
-
const offsets: Record<string, number> = {
|
|
173
|
-
'top-left': 0,
|
|
174
|
-
'top-right': 0,
|
|
175
|
-
'bottom-left': 0,
|
|
176
|
-
'bottom-right': 0,
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
// Navigation control is ~91px (3 buttons)
|
|
180
|
-
if (navigationControl) {
|
|
181
|
-
offsets[navigationControlPosition] += 100;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// Scale control is ~28px
|
|
185
|
-
if (scaleControl) {
|
|
186
|
-
offsets[scaleControlPosition] += 28;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// Fullscreen control is ~29px
|
|
190
|
-
if (fullscreenControl) {
|
|
191
|
-
offsets[fullscreenControlPosition] += 38;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Geolocate control is ~29px
|
|
195
|
-
if (geolocateControl) {
|
|
196
|
-
offsets[geolocateControlPosition] += 38;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
return offsets;
|
|
200
|
-
});
|
|
201
|
-
|
|
202
118
|
// Set context for child components
|
|
203
119
|
setContext('maplibre-map', {
|
|
204
120
|
get map() {
|
|
@@ -207,16 +123,11 @@
|
|
|
207
123
|
get loaded() {
|
|
208
124
|
return mapLoaded;
|
|
209
125
|
},
|
|
210
|
-
get controlOffsets() {
|
|
211
|
-
return controlOffsets();
|
|
212
|
-
},
|
|
213
126
|
get styleVersion() {
|
|
214
127
|
return styleVersion;
|
|
215
128
|
},
|
|
216
|
-
registerControl,
|
|
217
|
-
unregisterControl,
|
|
218
|
-
getCustomControlOffset,
|
|
219
129
|
setMapStyle,
|
|
130
|
+
positioningCoordinator,
|
|
220
131
|
});
|
|
221
132
|
|
|
222
133
|
const combinedError = $derived(error || mapError);
|
|
@@ -252,7 +163,7 @@
|
|
|
252
163
|
styleVersion++;
|
|
253
164
|
});
|
|
254
165
|
|
|
255
|
-
// Add controls
|
|
166
|
+
// Add controls and register with positioning coordinator
|
|
256
167
|
if (navigationControl) {
|
|
257
168
|
newMap.addControl(
|
|
258
169
|
new maplibregl.NavigationControl({
|
|
@@ -262,14 +173,62 @@
|
|
|
262
173
|
}),
|
|
263
174
|
navigationControlPosition
|
|
264
175
|
);
|
|
176
|
+
// Register with coordinator - NavigationControl is ~29x91px with visualizePitch
|
|
177
|
+
positioningCoordinator.register({
|
|
178
|
+
id: 'maplibre-navigation-control',
|
|
179
|
+
type: 'navigation',
|
|
180
|
+
isBuiltIn: true,
|
|
181
|
+
dimensions: { width: 29, height: 91 },
|
|
182
|
+
position: {
|
|
183
|
+
primary: navigationControlPosition as
|
|
184
|
+
| 'top-left'
|
|
185
|
+
| 'top-right'
|
|
186
|
+
| 'bottom-left'
|
|
187
|
+
| 'bottom-right',
|
|
188
|
+
},
|
|
189
|
+
priority: 'critical',
|
|
190
|
+
zLayer: 'base',
|
|
191
|
+
});
|
|
265
192
|
}
|
|
266
193
|
|
|
267
194
|
if (scaleControl) {
|
|
268
195
|
newMap.addControl(new maplibregl.ScaleControl({ unit: 'metric' }), scaleControlPosition);
|
|
196
|
+
// Register with coordinator - ScaleControl is ~100x20px
|
|
197
|
+
positioningCoordinator.register({
|
|
198
|
+
id: 'maplibre-scale-control',
|
|
199
|
+
type: 'scale',
|
|
200
|
+
isBuiltIn: true,
|
|
201
|
+
dimensions: { width: 100, height: 20 },
|
|
202
|
+
position: {
|
|
203
|
+
primary: scaleControlPosition as
|
|
204
|
+
| 'top-left'
|
|
205
|
+
| 'top-right'
|
|
206
|
+
| 'bottom-left'
|
|
207
|
+
| 'bottom-right',
|
|
208
|
+
},
|
|
209
|
+
priority: 'critical',
|
|
210
|
+
zLayer: 'base',
|
|
211
|
+
});
|
|
269
212
|
}
|
|
270
213
|
|
|
271
214
|
if (fullscreenControl) {
|
|
272
215
|
newMap.addControl(new maplibregl.FullscreenControl(), fullscreenControlPosition);
|
|
216
|
+
// Register with coordinator - FullscreenControl is ~29x29px
|
|
217
|
+
positioningCoordinator.register({
|
|
218
|
+
id: 'maplibre-fullscreen-control',
|
|
219
|
+
type: 'custom',
|
|
220
|
+
isBuiltIn: true,
|
|
221
|
+
dimensions: { width: 29, height: 29 },
|
|
222
|
+
position: {
|
|
223
|
+
primary: fullscreenControlPosition as
|
|
224
|
+
| 'top-left'
|
|
225
|
+
| 'top-right'
|
|
226
|
+
| 'bottom-left'
|
|
227
|
+
| 'bottom-right',
|
|
228
|
+
},
|
|
229
|
+
priority: 'critical',
|
|
230
|
+
zLayer: 'base',
|
|
231
|
+
});
|
|
273
232
|
}
|
|
274
233
|
|
|
275
234
|
if (geolocateControl) {
|
|
@@ -280,6 +239,22 @@
|
|
|
280
239
|
}),
|
|
281
240
|
geolocateControlPosition
|
|
282
241
|
);
|
|
242
|
+
// Register with coordinator - GeolocateControl is ~29x29px
|
|
243
|
+
positioningCoordinator.register({
|
|
244
|
+
id: 'maplibre-geolocate-control',
|
|
245
|
+
type: 'custom',
|
|
246
|
+
isBuiltIn: true,
|
|
247
|
+
dimensions: { width: 29, height: 29 },
|
|
248
|
+
position: {
|
|
249
|
+
primary: geolocateControlPosition as
|
|
250
|
+
| 'top-left'
|
|
251
|
+
| 'top-right'
|
|
252
|
+
| 'bottom-left'
|
|
253
|
+
| 'bottom-right',
|
|
254
|
+
},
|
|
255
|
+
priority: 'critical',
|
|
256
|
+
zLayer: 'base',
|
|
257
|
+
});
|
|
283
258
|
}
|
|
284
259
|
|
|
285
260
|
// Map load handler
|
|
@@ -439,12 +414,41 @@
|
|
|
439
414
|
}
|
|
440
415
|
});
|
|
441
416
|
|
|
417
|
+
// Update positioning coordinator when container resizes
|
|
418
|
+
let containerResizeObserver: ResizeObserver | null = null;
|
|
419
|
+
|
|
420
|
+
$effect(() => {
|
|
421
|
+
if (!containerElement) return;
|
|
422
|
+
|
|
423
|
+
const updateDimensions = () => {
|
|
424
|
+
if (!containerElement) return;
|
|
425
|
+
positioningCoordinator.setMapDimensions({
|
|
426
|
+
width: containerElement.offsetWidth,
|
|
427
|
+
height: containerElement.offsetHeight,
|
|
428
|
+
});
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
// Initial update
|
|
432
|
+
updateDimensions();
|
|
433
|
+
|
|
434
|
+
// Observe container size changes
|
|
435
|
+
containerResizeObserver = new ResizeObserver(updateDimensions);
|
|
436
|
+
containerResizeObserver.observe(containerElement);
|
|
437
|
+
|
|
438
|
+
return () => {
|
|
439
|
+
containerResizeObserver?.disconnect();
|
|
440
|
+
containerResizeObserver = null;
|
|
441
|
+
};
|
|
442
|
+
});
|
|
443
|
+
|
|
442
444
|
onDestroy(() => {
|
|
443
445
|
if (map) {
|
|
444
446
|
map.remove();
|
|
445
447
|
map = null;
|
|
446
448
|
mapLoaded = false;
|
|
447
449
|
}
|
|
450
|
+
containerResizeObserver?.disconnect();
|
|
451
|
+
positioningCoordinator.destroy();
|
|
448
452
|
});
|
|
449
453
|
</script>
|
|
450
454
|
|
|
@@ -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, BasemapSwitcher, LayerManager, } from './controls/index.js';
|
|
5
|
+
export { Legend, LegendGroup, CoordinateDisplay, BasemapSwitcher, LayerManager, CustomControl, type CustomControlProps, } 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, BasemapSwitcher, LayerManager, } from './controls/index.js';
|
|
10
|
+
export { Legend, LegendGroup, CoordinateDisplay, BasemapSwitcher, LayerManager, CustomControl, } from './controls/index.js';
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { useMapTheme } from './useMapTheme.svelte.js';
|
|
2
2
|
export { useReducedMotion } from './useReducedMotion.svelte.js';
|
|
3
3
|
export { useMapContext, tryUseMapContext, type MapContextValue } from './useMapContext.svelte.js';
|
|
4
|
+
export { useControlPosition, type UseControlPositionOptions, type UseControlPositionResult, } from './useControlPosition.svelte.js';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composable for registering a control with the positioning coordinator
|
|
3
|
+
*
|
|
4
|
+
* This composable handles auto-registration on mount, dimension updates
|
|
5
|
+
* via ResizeObserver, and cleanup on unmount.
|
|
6
|
+
*/
|
|
7
|
+
import type { ControlOffset, ControlPriority, ControlType, ExtendedControlPosition, ZIndexLayer } from '@classic-homes/maps-core';
|
|
8
|
+
/**
|
|
9
|
+
* Options for useControlPosition composable
|
|
10
|
+
*/
|
|
11
|
+
export interface UseControlPositionOptions {
|
|
12
|
+
/** Unique identifier for the control instance */
|
|
13
|
+
id: string;
|
|
14
|
+
/** Type of control */
|
|
15
|
+
type: ControlType;
|
|
16
|
+
/** Primary position for the control */
|
|
17
|
+
position: ExtendedControlPosition;
|
|
18
|
+
/** Fallback positions to try if primary is occupied */
|
|
19
|
+
fallbacks?: ExtendedControlPosition[];
|
|
20
|
+
/** Priority level for collision resolution (default: 'normal') */
|
|
21
|
+
priority?: ControlPriority;
|
|
22
|
+
/** Z-index layer (default: 'interactive') */
|
|
23
|
+
zLayer?: ZIndexLayer;
|
|
24
|
+
/** Whether the control has an expanded state */
|
|
25
|
+
hasExpandedState?: boolean;
|
|
26
|
+
/** Whether the control is a built-in MapLibre control */
|
|
27
|
+
isBuiltIn?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Return value from useControlPosition composable
|
|
31
|
+
*/
|
|
32
|
+
export interface UseControlPositionResult {
|
|
33
|
+
/** Current computed offset and position (reactive) */
|
|
34
|
+
readonly offset: ControlOffset | null;
|
|
35
|
+
/** Update the expanded state */
|
|
36
|
+
setExpanded: (isExpanded: boolean) => void;
|
|
37
|
+
/** CSS style string to apply to the control */
|
|
38
|
+
readonly style: string;
|
|
39
|
+
/** Whether the control should be visible */
|
|
40
|
+
readonly visible: boolean;
|
|
41
|
+
/** Whether the coordinator is available */
|
|
42
|
+
readonly hasCoordinator: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a control position manager using the positioning coordinator
|
|
46
|
+
*
|
|
47
|
+
* @param getElement - Function that returns the control element (use a getter for reactivity)
|
|
48
|
+
* @param options - Control registration options
|
|
49
|
+
* @returns Position offset, visibility, and style to apply
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```svelte
|
|
53
|
+
* <script lang="ts">
|
|
54
|
+
* import { useControlPosition } from '../composables/useControlPosition.svelte.js';
|
|
55
|
+
*
|
|
56
|
+
* let legendElement: HTMLDivElement;
|
|
57
|
+
* const legendId = `legend-${Math.random().toString(36).slice(2, 9)}`;
|
|
58
|
+
*
|
|
59
|
+
* const controlPosition = useControlPosition(
|
|
60
|
+
* () => legendElement,
|
|
61
|
+
* {
|
|
62
|
+
* id: legendId,
|
|
63
|
+
* type: 'legend',
|
|
64
|
+
* position: 'bottom-left',
|
|
65
|
+
* priority: 'normal',
|
|
66
|
+
* }
|
|
67
|
+
* );
|
|
68
|
+
* </script>
|
|
69
|
+
*
|
|
70
|
+
* <div bind:this={legendElement} style={controlPosition.style} hidden={!controlPosition.visible}>
|
|
71
|
+
* ...
|
|
72
|
+
* </div>
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export declare function useControlPosition(getElement: () => HTMLElement | undefined, options: UseControlPositionOptions): UseControlPositionResult;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Composable for registering a control with the positioning coordinator
|
|
3
|
+
*
|
|
4
|
+
* This composable handles auto-registration on mount, dimension updates
|
|
5
|
+
* via ResizeObserver, and cleanup on unmount.
|
|
6
|
+
*/
|
|
7
|
+
import { onDestroy, untrack } from 'svelte';
|
|
8
|
+
import { tryUseMapContext } from './useMapContext.svelte.js';
|
|
9
|
+
/**
|
|
10
|
+
* Create a control position manager using the positioning coordinator
|
|
11
|
+
*
|
|
12
|
+
* @param getElement - Function that returns the control element (use a getter for reactivity)
|
|
13
|
+
* @param options - Control registration options
|
|
14
|
+
* @returns Position offset, visibility, and style to apply
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```svelte
|
|
18
|
+
* <script lang="ts">
|
|
19
|
+
* import { useControlPosition } from '../composables/useControlPosition.svelte.js';
|
|
20
|
+
*
|
|
21
|
+
* let legendElement: HTMLDivElement;
|
|
22
|
+
* const legendId = `legend-${Math.random().toString(36).slice(2, 9)}`;
|
|
23
|
+
*
|
|
24
|
+
* const controlPosition = useControlPosition(
|
|
25
|
+
* () => legendElement,
|
|
26
|
+
* {
|
|
27
|
+
* id: legendId,
|
|
28
|
+
* type: 'legend',
|
|
29
|
+
* position: 'bottom-left',
|
|
30
|
+
* priority: 'normal',
|
|
31
|
+
* }
|
|
32
|
+
* );
|
|
33
|
+
* </script>
|
|
34
|
+
*
|
|
35
|
+
* <div bind:this={legendElement} style={controlPosition.style} hidden={!controlPosition.visible}>
|
|
36
|
+
* ...
|
|
37
|
+
* </div>
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function useControlPosition(getElement, options) {
|
|
41
|
+
const { id, type, position, fallbacks, priority = 'normal', zLayer = 'interactive', hasExpandedState = false, isBuiltIn = false, } = options;
|
|
42
|
+
const mapContext = tryUseMapContext();
|
|
43
|
+
const coordinator = mapContext
|
|
44
|
+
?.positioningCoordinator;
|
|
45
|
+
// Reactive state
|
|
46
|
+
let offset = $state(null);
|
|
47
|
+
let registered = $state(false);
|
|
48
|
+
let resizeObserver = null;
|
|
49
|
+
let unsubscribe = null;
|
|
50
|
+
// Compute style from offset
|
|
51
|
+
const style = $derived(() => {
|
|
52
|
+
if (!offset) {
|
|
53
|
+
return 'position: absolute; visibility: hidden;';
|
|
54
|
+
}
|
|
55
|
+
const resolvedPosition = offset.position;
|
|
56
|
+
const isLeftPosition = resolvedPosition.includes('left') || resolvedPosition.includes('center');
|
|
57
|
+
const isCenterHorizontal = resolvedPosition.includes('center');
|
|
58
|
+
const styles = ['position: absolute', `z-index: ${offset.zIndex}`];
|
|
59
|
+
// Apply horizontal positioning
|
|
60
|
+
if (isCenterHorizontal) {
|
|
61
|
+
styles.push(`left: ${offset.x}px`);
|
|
62
|
+
}
|
|
63
|
+
else if (isLeftPosition) {
|
|
64
|
+
styles.push(`left: ${offset.x}px`);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
styles.push(`right: ${offset.x}px`);
|
|
68
|
+
}
|
|
69
|
+
// Apply vertical positioning
|
|
70
|
+
if (resolvedPosition.startsWith('bottom')) {
|
|
71
|
+
styles.push(`bottom: ${offset.y}px`);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
styles.push(`top: ${offset.y}px`);
|
|
75
|
+
}
|
|
76
|
+
return styles.join('; ');
|
|
77
|
+
});
|
|
78
|
+
const visible = $derived(offset?.visible ?? false);
|
|
79
|
+
const hasCoordinator = !!coordinator;
|
|
80
|
+
// Register with coordinator
|
|
81
|
+
$effect(() => {
|
|
82
|
+
if (!coordinator)
|
|
83
|
+
return;
|
|
84
|
+
const element = getElement();
|
|
85
|
+
if (!element)
|
|
86
|
+
return;
|
|
87
|
+
// Initial registration
|
|
88
|
+
if (!registered) {
|
|
89
|
+
untrack(() => {
|
|
90
|
+
const initialOffset = coordinator.register({
|
|
91
|
+
id,
|
|
92
|
+
type,
|
|
93
|
+
isBuiltIn,
|
|
94
|
+
dimensions: {
|
|
95
|
+
width: element.offsetWidth || 200,
|
|
96
|
+
height: element.offsetHeight || 100,
|
|
97
|
+
},
|
|
98
|
+
position: {
|
|
99
|
+
primary: position,
|
|
100
|
+
fallbacks,
|
|
101
|
+
},
|
|
102
|
+
priority,
|
|
103
|
+
zLayer,
|
|
104
|
+
hasExpandedState,
|
|
105
|
+
isExpanded: false,
|
|
106
|
+
});
|
|
107
|
+
offset = initialOffset;
|
|
108
|
+
registered = true;
|
|
109
|
+
});
|
|
110
|
+
// Subscribe to coordinator events
|
|
111
|
+
unsubscribe = coordinator.subscribe((event) => {
|
|
112
|
+
const newOffset = event.offsets.get(id);
|
|
113
|
+
if (newOffset) {
|
|
114
|
+
offset = newOffset;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
// Observe dimension changes
|
|
118
|
+
resizeObserver = new ResizeObserver(() => {
|
|
119
|
+
const el = getElement();
|
|
120
|
+
if (!el)
|
|
121
|
+
return;
|
|
122
|
+
coordinator.update(id, {
|
|
123
|
+
dimensions: {
|
|
124
|
+
width: el.offsetWidth,
|
|
125
|
+
height: el.offsetHeight,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
resizeObserver.observe(element);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
// Update position configuration when options change
|
|
133
|
+
$effect(() => {
|
|
134
|
+
if (!coordinator || !registered)
|
|
135
|
+
return;
|
|
136
|
+
// Track these values to trigger the effect when they change
|
|
137
|
+
const _position = position;
|
|
138
|
+
const _fallbacks = fallbacks;
|
|
139
|
+
const _priority = priority;
|
|
140
|
+
const _zLayer = zLayer;
|
|
141
|
+
untrack(() => {
|
|
142
|
+
coordinator.update(id, {
|
|
143
|
+
position: {
|
|
144
|
+
primary: _position,
|
|
145
|
+
fallbacks: _fallbacks,
|
|
146
|
+
},
|
|
147
|
+
priority: _priority,
|
|
148
|
+
zLayer: _zLayer,
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
// Setter for expanded state
|
|
153
|
+
function setExpanded(isExpanded) {
|
|
154
|
+
if (coordinator) {
|
|
155
|
+
coordinator.setExpanded(id, isExpanded);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Cleanup on destroy
|
|
159
|
+
onDestroy(() => {
|
|
160
|
+
resizeObserver?.disconnect();
|
|
161
|
+
unsubscribe?.();
|
|
162
|
+
if (coordinator && registered) {
|
|
163
|
+
coordinator.unregister(id);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
return {
|
|
167
|
+
get offset() {
|
|
168
|
+
return offset;
|
|
169
|
+
},
|
|
170
|
+
setExpanded,
|
|
171
|
+
get style() {
|
|
172
|
+
return style();
|
|
173
|
+
},
|
|
174
|
+
get visible() {
|
|
175
|
+
return visible;
|
|
176
|
+
},
|
|
177
|
+
hasCoordinator,
|
|
178
|
+
};
|
|
179
|
+
}
|