@classic-homes/maps-svelte 0.1.1
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/README.md +432 -0
- package/dist/lib/components/base/MapContainer.svelte +68 -0
- package/dist/lib/components/base/MapContainer.svelte.d.ts +18 -0
- package/dist/lib/components/base/MapEmpty.svelte +42 -0
- package/dist/lib/components/base/MapEmpty.svelte.d.ts +8 -0
- package/dist/lib/components/base/MapError.svelte +54 -0
- package/dist/lib/components/base/MapError.svelte.d.ts +9 -0
- package/dist/lib/components/base/MapSkeleton.svelte +45 -0
- package/dist/lib/components/base/MapSkeleton.svelte.d.ts +7 -0
- package/dist/lib/components/base/index.d.ts +4 -0
- package/dist/lib/components/base/index.js +4 -0
- package/dist/lib/components/controls/CoordinateDisplay.svelte +163 -0
- package/dist/lib/components/controls/CoordinateDisplay.svelte.d.ts +6 -0
- package/dist/lib/components/controls/Legend.svelte +179 -0
- package/dist/lib/components/controls/Legend.svelte.d.ts +6 -0
- package/dist/lib/components/controls/LegendGroup.svelte +216 -0
- package/dist/lib/components/controls/LegendGroup.svelte.d.ts +6 -0
- package/dist/lib/components/controls/index.d.ts +3 -0
- package/dist/lib/components/controls/index.js +3 -0
- package/dist/lib/components/core/Map.svelte +434 -0
- package/dist/lib/components/core/Map.svelte.d.ts +11 -0
- package/dist/lib/components/core/Marker.svelte +185 -0
- package/dist/lib/components/core/Marker.svelte.d.ts +9 -0
- package/dist/lib/components/core/Popup.svelte +205 -0
- package/dist/lib/components/core/Popup.svelte.d.ts +11 -0
- package/dist/lib/components/core/PopupCard.svelte +17 -0
- package/dist/lib/components/core/PopupCard.svelte.d.ts +10 -0
- package/dist/lib/components/core/PopupContent.svelte +17 -0
- package/dist/lib/components/core/PopupContent.svelte.d.ts +10 -0
- package/dist/lib/components/core/PopupFooter.svelte +22 -0
- package/dist/lib/components/core/PopupFooter.svelte.d.ts +10 -0
- package/dist/lib/components/core/PopupHeader.svelte +45 -0
- package/dist/lib/components/core/PopupHeader.svelte.d.ts +14 -0
- package/dist/lib/components/core/Source.svelte +326 -0
- package/dist/lib/components/core/Source.svelte.d.ts +59 -0
- package/dist/lib/components/core/index.d.ts +8 -0
- package/dist/lib/components/core/index.js +8 -0
- package/dist/lib/components/deck/DeckOverlay.svelte +135 -0
- package/dist/lib/components/deck/DeckOverlay.svelte.d.ts +25 -0
- package/dist/lib/components/deck/index.d.ts +1 -0
- package/dist/lib/components/deck/index.js +1 -0
- package/dist/lib/components/index.d.ts +5 -0
- package/dist/lib/components/index.js +10 -0
- package/dist/lib/components/layers/CircleLayer.svelte +78 -0
- package/dist/lib/components/layers/CircleLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/FillExtrusionLayer.svelte +75 -0
- package/dist/lib/components/layers/FillExtrusionLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/FillLayer.svelte +70 -0
- package/dist/lib/components/layers/FillLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/HeatmapLayer.svelte +68 -0
- package/dist/lib/components/layers/HeatmapLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/LineLayer.svelte +81 -0
- package/dist/lib/components/layers/LineLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/SymbolLayer.svelte +108 -0
- package/dist/lib/components/layers/SymbolLayer.svelte.d.ts +11 -0
- package/dist/lib/components/layers/index.d.ts +7 -0
- package/dist/lib/components/layers/index.js +6 -0
- package/dist/lib/components/layers/useLayer.svelte.d.ts +38 -0
- package/dist/lib/components/layers/useLayer.svelte.js +286 -0
- package/dist/lib/composables/index.d.ts +3 -0
- package/dist/lib/composables/index.js +3 -0
- package/dist/lib/composables/useMapContext.svelte.d.ts +62 -0
- package/dist/lib/composables/useMapContext.svelte.js +58 -0
- package/dist/lib/composables/useMapTheme.svelte.d.ts +11 -0
- package/dist/lib/composables/useMapTheme.svelte.js +66 -0
- package/dist/lib/composables/useReducedMotion.svelte.d.ts +6 -0
- package/dist/lib/composables/useReducedMotion.svelte.js +26 -0
- package/dist/lib/index.d.ts +12 -0
- package/dist/lib/index.js +29 -0
- package/dist/lib/utils.d.ts +2 -0
- package/dist/lib/utils.js +5 -0
- package/package.json +75 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy } from 'svelte';
|
|
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';
|
|
10
|
+
import { cn } from '../../utils.js';
|
|
11
|
+
|
|
12
|
+
interface MapContext {
|
|
13
|
+
readonly map: maplibregl.Map | null;
|
|
14
|
+
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
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Props extends CoordinateDisplayProps {}
|
|
22
|
+
|
|
23
|
+
let {
|
|
24
|
+
position = 'bottom-right',
|
|
25
|
+
format = 'decimal',
|
|
26
|
+
precision = 5,
|
|
27
|
+
showZoom = true,
|
|
28
|
+
showBearing = false,
|
|
29
|
+
class: className,
|
|
30
|
+
}: Props = $props();
|
|
31
|
+
|
|
32
|
+
const mapContext = getContext<MapContext>('maplibre-map');
|
|
33
|
+
|
|
34
|
+
// Generate unique ID for this control instance
|
|
35
|
+
const controlId = `coord-display-${Math.random().toString(36).slice(2, 9)}`;
|
|
36
|
+
|
|
37
|
+
let cursorLngLat: LngLat | null = $state(null);
|
|
38
|
+
let currentZoom = $state(0);
|
|
39
|
+
let currentBearing = $state(0);
|
|
40
|
+
let mouseMoveHandler: ((e: maplibregl.MapMouseEvent) => void) | null = null;
|
|
41
|
+
let zoomHandler: (() => void) | null = null;
|
|
42
|
+
let rotateHandler: (() => void) | null = null;
|
|
43
|
+
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
|
+
|
|
52
|
+
// Get combined position classes using shared utility
|
|
53
|
+
const getPositionClasses = $derived(() => {
|
|
54
|
+
return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Register this control with the map context and get vertical offset
|
|
58
|
+
$effect(() => {
|
|
59
|
+
if (!controlElement || !mapContext?.registerControl) return;
|
|
60
|
+
|
|
61
|
+
// Get the element's height
|
|
62
|
+
const height = controlElement.offsetHeight;
|
|
63
|
+
|
|
64
|
+
// Register and get offset
|
|
65
|
+
customControlOffset = mapContext.registerControl(controlId, position, height);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Cleanup on destroy
|
|
69
|
+
onDestroy(() => {
|
|
70
|
+
mapContext?.unregisterControl?.(controlId, position);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Calculate vertical offset style for stacking with other custom controls
|
|
74
|
+
// Uses top/bottom properties instead of margins for correct absolute positioning
|
|
75
|
+
const verticalOffsetStyle = $derived(() => {
|
|
76
|
+
const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
|
|
77
|
+
return Object.entries(styleObj)
|
|
78
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
79
|
+
.join('; ');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Format the coordinate based on settings
|
|
83
|
+
function formatCoord(lngLat: LngLat): string {
|
|
84
|
+
return formatCoordinates(lngLat, precision, format);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Format zoom with 1 decimal
|
|
88
|
+
function formatZoom(zoom: number): string {
|
|
89
|
+
return zoom.toFixed(1);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Format bearing
|
|
93
|
+
function formatBearingValue(bearing: number): string {
|
|
94
|
+
// Normalize to 0-360
|
|
95
|
+
const normalized = ((bearing % 360) + 360) % 360;
|
|
96
|
+
return `${normalized.toFixed(0)}°`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Setup event listeners
|
|
100
|
+
$effect(() => {
|
|
101
|
+
const { map, loaded } = mapContext;
|
|
102
|
+
|
|
103
|
+
if (!map || !loaded) return;
|
|
104
|
+
|
|
105
|
+
// Get initial zoom and bearing
|
|
106
|
+
currentZoom = map.getZoom();
|
|
107
|
+
currentBearing = map.getBearing();
|
|
108
|
+
|
|
109
|
+
// Mouse move handler for coordinates
|
|
110
|
+
mouseMoveHandler = (e: maplibregl.MapMouseEvent) => {
|
|
111
|
+
cursorLngLat = [e.lngLat.lng, e.lngLat.lat];
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Zoom handler
|
|
115
|
+
zoomHandler = () => {
|
|
116
|
+
currentZoom = map.getZoom();
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Rotate handler for bearing
|
|
120
|
+
rotateHandler = () => {
|
|
121
|
+
currentBearing = map.getBearing();
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
map.on('mousemove', mouseMoveHandler);
|
|
125
|
+
map.on('zoom', zoomHandler);
|
|
126
|
+
map.on('rotate', rotateHandler);
|
|
127
|
+
|
|
128
|
+
return () => {
|
|
129
|
+
if (mouseMoveHandler) map.off('mousemove', mouseMoveHandler);
|
|
130
|
+
if (zoomHandler) map.off('zoom', zoomHandler);
|
|
131
|
+
if (rotateHandler) map.off('rotate', rotateHandler);
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
</script>
|
|
135
|
+
|
|
136
|
+
<div
|
|
137
|
+
bind:this={controlElement}
|
|
138
|
+
class={cn(
|
|
139
|
+
'absolute z-10 bg-card rounded px-2 py-1',
|
|
140
|
+
'text-xs font-mono text-muted-foreground',
|
|
141
|
+
'border border-black dark:border-gray-400 shadow-sm',
|
|
142
|
+
'flex items-center gap-3',
|
|
143
|
+
getPositionClasses(),
|
|
144
|
+
className
|
|
145
|
+
)}
|
|
146
|
+
style={verticalOffsetStyle()}
|
|
147
|
+
>
|
|
148
|
+
{#if cursorLngLat}
|
|
149
|
+
<span title="Cursor coordinates">{formatCoord(cursorLngLat)}</span>
|
|
150
|
+
{:else}
|
|
151
|
+
<span class="text-muted-foreground/60">Move cursor over map</span>
|
|
152
|
+
{/if}
|
|
153
|
+
|
|
154
|
+
{#if showZoom}
|
|
155
|
+
<span class="text-border">|</span>
|
|
156
|
+
<span title="Zoom level">z{formatZoom(currentZoom)}</span>
|
|
157
|
+
{/if}
|
|
158
|
+
|
|
159
|
+
{#if showBearing}
|
|
160
|
+
<span class="text-border">|</span>
|
|
161
|
+
<span title="Bearing">{formatBearingValue(currentBearing)}</span>
|
|
162
|
+
{/if}
|
|
163
|
+
</div>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CoordinateDisplayProps } from '@classic-homes/maps-core';
|
|
2
|
+
interface Props extends CoordinateDisplayProps {
|
|
3
|
+
}
|
|
4
|
+
declare const CoordinateDisplay: import("svelte").Component<Props, {}, "">;
|
|
5
|
+
type CoordinateDisplay = ReturnType<typeof CoordinateDisplay>;
|
|
6
|
+
export default CoordinateDisplay;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy } from 'svelte';
|
|
3
|
+
import type { LegendProps, LegendItem, ControlPosition } from '@classic-homes/maps-core';
|
|
4
|
+
import { computePositionClasses, computeVerticalOffsetStyle } from '@classic-homes/maps-core';
|
|
5
|
+
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
|
+
}
|
|
13
|
+
|
|
14
|
+
interface Props extends LegendProps {}
|
|
15
|
+
|
|
16
|
+
let {
|
|
17
|
+
title,
|
|
18
|
+
position = 'bottom-left',
|
|
19
|
+
type = 'categorical',
|
|
20
|
+
items = [],
|
|
21
|
+
gradient = [],
|
|
22
|
+
minLabel,
|
|
23
|
+
maxLabel,
|
|
24
|
+
collapsible = false,
|
|
25
|
+
collapsed = false,
|
|
26
|
+
class: className,
|
|
27
|
+
onItemClick,
|
|
28
|
+
}: Props = $props();
|
|
29
|
+
|
|
30
|
+
const mapContext = getContext<MapContext>('maplibre-map');
|
|
31
|
+
|
|
32
|
+
// Generate unique ID for this legend instance
|
|
33
|
+
const legendId = `legend-${Math.random().toString(36).slice(2, 9)}`;
|
|
34
|
+
|
|
35
|
+
// Use a separate variable to track whether user has toggled locally
|
|
36
|
+
let userToggled = $state(false);
|
|
37
|
+
let localCollapsed = $state(false);
|
|
38
|
+
let legendElement: HTMLDivElement;
|
|
39
|
+
let customControlOffset = $state(0);
|
|
40
|
+
|
|
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
|
+
if (!legendElement || !mapContext?.registerControl) return;
|
|
47
|
+
|
|
48
|
+
// Get the element's height
|
|
49
|
+
const height = legendElement.offsetHeight;
|
|
50
|
+
|
|
51
|
+
// Register and get offset
|
|
52
|
+
customControlOffset = mapContext.registerControl(legendId, position, height);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Cleanup on destroy
|
|
56
|
+
onDestroy(() => {
|
|
57
|
+
mapContext?.unregisterControl?.(legendId, position);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Check if controls exist at this position - if so, offset horizontally toward center
|
|
61
|
+
const hasControlsAtPosition = $derived(() => {
|
|
62
|
+
const offset = mapContext?.controlOffsets?.[position] ?? 0;
|
|
63
|
+
return offset > 0;
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Get combined position classes using shared utility
|
|
67
|
+
const getPositionClasses = $derived(() => {
|
|
68
|
+
return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Calculate vertical offset style for stacking with other custom controls
|
|
72
|
+
// Uses top/bottom properties instead of margins for correct absolute positioning
|
|
73
|
+
const verticalOffsetStyle = $derived(() => {
|
|
74
|
+
const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
|
|
75
|
+
return Object.entries(styleObj)
|
|
76
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
77
|
+
.join('; ');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
function handleItemClick(item: LegendItem, index: number) {
|
|
81
|
+
if (onItemClick) {
|
|
82
|
+
onItemClick(item, index);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function toggleCollapse() {
|
|
87
|
+
if (collapsible) {
|
|
88
|
+
userToggled = true;
|
|
89
|
+
localCollapsed = !isCollapsed;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Generate gradient CSS
|
|
94
|
+
const gradientStyle = $derived(() => {
|
|
95
|
+
if (type !== 'gradient' || gradient.length === 0) return '';
|
|
96
|
+
const stops = gradient.map((stop) => `${stop.color} ${stop.position * 100}%`).join(', ');
|
|
97
|
+
return `linear-gradient(to right, ${stops})`;
|
|
98
|
+
});
|
|
99
|
+
</script>
|
|
100
|
+
|
|
101
|
+
<div
|
|
102
|
+
bind:this={legendElement}
|
|
103
|
+
class={cn(
|
|
104
|
+
'absolute z-10 bg-card rounded-lg shadow-lg border border-black dark:border-gray-400',
|
|
105
|
+
'text-sm min-w-[140px] max-w-[280px]',
|
|
106
|
+
getPositionClasses(),
|
|
107
|
+
className
|
|
108
|
+
)}
|
|
109
|
+
style={verticalOffsetStyle()}
|
|
110
|
+
>
|
|
111
|
+
{#if title || collapsible}
|
|
112
|
+
<button
|
|
113
|
+
type="button"
|
|
114
|
+
class={cn(
|
|
115
|
+
'w-full flex items-center justify-between px-3 py-2 font-medium text-card-foreground',
|
|
116
|
+
collapsible && 'cursor-pointer hover:bg-muted rounded-t-lg',
|
|
117
|
+
!collapsible && 'cursor-default'
|
|
118
|
+
)}
|
|
119
|
+
onclick={toggleCollapse}
|
|
120
|
+
disabled={!collapsible}
|
|
121
|
+
>
|
|
122
|
+
<span>{title || 'Legend'}</span>
|
|
123
|
+
{#if collapsible}
|
|
124
|
+
<svg
|
|
125
|
+
class={cn('w-4 h-4 transition-transform', isCollapsed && '-rotate-90')}
|
|
126
|
+
fill="none"
|
|
127
|
+
viewBox="0 0 24 24"
|
|
128
|
+
stroke="currentColor"
|
|
129
|
+
>
|
|
130
|
+
<path
|
|
131
|
+
stroke-linecap="round"
|
|
132
|
+
stroke-linejoin="round"
|
|
133
|
+
stroke-width="2"
|
|
134
|
+
d="M19 9l-7 7-7-7"
|
|
135
|
+
/>
|
|
136
|
+
</svg>
|
|
137
|
+
{/if}
|
|
138
|
+
</button>
|
|
139
|
+
{/if}
|
|
140
|
+
|
|
141
|
+
{#if !isCollapsed}
|
|
142
|
+
<div class={cn('px-3 pb-3', (title || collapsible) && 'pt-1')}>
|
|
143
|
+
{#if type === 'categorical' && items.length > 0}
|
|
144
|
+
<ul class="space-y-1.5">
|
|
145
|
+
{#each items as item, index}
|
|
146
|
+
<li>
|
|
147
|
+
<button
|
|
148
|
+
type="button"
|
|
149
|
+
class={cn(
|
|
150
|
+
'flex items-center gap-2 w-full text-left rounded px-1 -mx-1',
|
|
151
|
+
onItemClick && 'hover:bg-muted cursor-pointer',
|
|
152
|
+
!onItemClick && 'cursor-default',
|
|
153
|
+
item.active === false && 'opacity-50'
|
|
154
|
+
)}
|
|
155
|
+
onclick={() => handleItemClick(item, index)}
|
|
156
|
+
disabled={!onItemClick}
|
|
157
|
+
>
|
|
158
|
+
<span class="w-4 h-4 rounded flex-shrink-0" style="background-color: {item.color};"
|
|
159
|
+
></span>
|
|
160
|
+
<span class="text-foreground truncate flex-1">{item.label}</span>
|
|
161
|
+
{#if item.value !== undefined}
|
|
162
|
+
<span class="text-muted-foreground text-xs">{item.value}</span>
|
|
163
|
+
{/if}
|
|
164
|
+
</button>
|
|
165
|
+
</li>
|
|
166
|
+
{/each}
|
|
167
|
+
</ul>
|
|
168
|
+
{:else if type === 'gradient' && gradient.length > 0}
|
|
169
|
+
<div class="space-y-2">
|
|
170
|
+
<div class="h-3 rounded-sm" style="background: {gradientStyle()};"></div>
|
|
171
|
+
<div class="flex justify-between text-xs text-muted-foreground">
|
|
172
|
+
<span>{minLabel || gradient[0]?.label || ''}</span>
|
|
173
|
+
<span>{maxLabel || gradient[gradient.length - 1]?.label || ''}</span>
|
|
174
|
+
</div>
|
|
175
|
+
</div>
|
|
176
|
+
{/if}
|
|
177
|
+
</div>
|
|
178
|
+
{/if}
|
|
179
|
+
</div>
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getContext, onDestroy } from 'svelte';
|
|
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';
|
|
10
|
+
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
|
+
}
|
|
18
|
+
|
|
19
|
+
interface Props extends LegendGroupProps {}
|
|
20
|
+
|
|
21
|
+
let {
|
|
22
|
+
legends,
|
|
23
|
+
position = 'bottom-left',
|
|
24
|
+
defaultExpanded,
|
|
25
|
+
expanded: controlledExpanded,
|
|
26
|
+
onExpandedChange,
|
|
27
|
+
allowCollapseAll = false,
|
|
28
|
+
class: className,
|
|
29
|
+
}: Props = $props();
|
|
30
|
+
|
|
31
|
+
const mapContext = getContext<MapContext>('maplibre-map');
|
|
32
|
+
|
|
33
|
+
// Generate unique ID for this legend group instance
|
|
34
|
+
const groupId = `legend-group-${Math.random().toString(36).slice(2, 9)}`;
|
|
35
|
+
|
|
36
|
+
// Determine initial expanded state
|
|
37
|
+
function getInitialExpanded(): string | null {
|
|
38
|
+
if (controlledExpanded !== undefined) return controlledExpanded;
|
|
39
|
+
if (defaultExpanded) return defaultExpanded;
|
|
40
|
+
return legends[0]?.id || null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let internalExpanded = $state<string | null>(getInitialExpanded());
|
|
44
|
+
let containerElement: HTMLDivElement;
|
|
45
|
+
let customControlOffset = $state(0);
|
|
46
|
+
|
|
47
|
+
// Use controlled or internal state
|
|
48
|
+
const expandedId = $derived(
|
|
49
|
+
controlledExpanded !== undefined ? controlledExpanded : internalExpanded
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Handle expansion toggle
|
|
53
|
+
function handleToggle(id: string) {
|
|
54
|
+
const newExpanded = expandedId === id ? (allowCollapseAll ? null : id) : id;
|
|
55
|
+
|
|
56
|
+
if (controlledExpanded === undefined) {
|
|
57
|
+
internalExpanded = newExpanded;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
onExpandedChange?.(newExpanded);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Sync with external controlled prop changes
|
|
64
|
+
$effect(() => {
|
|
65
|
+
if (controlledExpanded !== undefined) {
|
|
66
|
+
internalExpanded = controlledExpanded;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Register this control with the map context and get vertical offset
|
|
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);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Cleanup on destroy
|
|
87
|
+
onDestroy(() => {
|
|
88
|
+
mapContext?.unregisterControl?.(groupId, position);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// Check if controls exist at this position
|
|
92
|
+
const hasControlsAtPosition = $derived(() => {
|
|
93
|
+
const offset = mapContext?.controlOffsets?.[position] ?? 0;
|
|
94
|
+
return offset > 0;
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Get combined position classes using shared utility
|
|
98
|
+
const getPositionClasses = $derived(() => {
|
|
99
|
+
return computePositionClasses(position as ControlPosition, hasControlsAtPosition());
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// Calculate vertical offset style for stacking with other custom controls
|
|
103
|
+
const verticalOffsetStyle = $derived(() => {
|
|
104
|
+
const styleObj = computeVerticalOffsetStyle(position as ControlPosition, customControlOffset);
|
|
105
|
+
return Object.entries(styleObj)
|
|
106
|
+
.map(([key, value]) => `${key}: ${value}`)
|
|
107
|
+
.join('; ');
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Generate gradient CSS
|
|
111
|
+
function getGradientStyle(legend: LegendConfig): string {
|
|
112
|
+
if (legend.type !== 'gradient' || !legend.gradient?.length) return '';
|
|
113
|
+
const stops = legend.gradient.map((stop) => `${stop.color} ${stop.position * 100}%`).join(', ');
|
|
114
|
+
return `linear-gradient(to right, ${stops})`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function handleItemClick(legend: LegendConfig, item: LegendItem, index: number) {
|
|
118
|
+
legend.onItemClick?.(item, index);
|
|
119
|
+
}
|
|
120
|
+
</script>
|
|
121
|
+
|
|
122
|
+
<div
|
|
123
|
+
bind:this={containerElement}
|
|
124
|
+
class={cn(
|
|
125
|
+
'absolute z-10 bg-card rounded-lg shadow-lg border border-black dark:border-gray-400',
|
|
126
|
+
'text-sm min-w-[160px] max-w-[280px]',
|
|
127
|
+
getPositionClasses(),
|
|
128
|
+
className
|
|
129
|
+
)}
|
|
130
|
+
style={verticalOffsetStyle()}
|
|
131
|
+
>
|
|
132
|
+
{#each legends as legend, legendIndex}
|
|
133
|
+
{@const isExpanded = expandedId === legend.id}
|
|
134
|
+
{@const isFirst = legendIndex === 0}
|
|
135
|
+
{@const isLast = legendIndex === legends.length - 1}
|
|
136
|
+
|
|
137
|
+
<!-- Divider between sections -->
|
|
138
|
+
{#if !isFirst}
|
|
139
|
+
<div class="border-t border-border"></div>
|
|
140
|
+
{/if}
|
|
141
|
+
|
|
142
|
+
<!-- Accordion header -->
|
|
143
|
+
<button
|
|
144
|
+
type="button"
|
|
145
|
+
class={cn(
|
|
146
|
+
'w-full flex items-center justify-between px-3 py-2 font-medium text-card-foreground',
|
|
147
|
+
'cursor-pointer hover:bg-muted transition-colors',
|
|
148
|
+
isFirst && 'rounded-t-lg',
|
|
149
|
+
isLast && !isExpanded && 'rounded-b-lg'
|
|
150
|
+
)}
|
|
151
|
+
onclick={() => handleToggle(legend.id)}
|
|
152
|
+
aria-expanded={isExpanded}
|
|
153
|
+
>
|
|
154
|
+
<span class="truncate">{legend.title}</span>
|
|
155
|
+
<svg
|
|
156
|
+
class={cn('w-4 h-4 transition-transform flex-shrink-0 ml-2', !isExpanded && '-rotate-90')}
|
|
157
|
+
fill="none"
|
|
158
|
+
viewBox="0 0 24 24"
|
|
159
|
+
stroke="currentColor"
|
|
160
|
+
>
|
|
161
|
+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" />
|
|
162
|
+
</svg>
|
|
163
|
+
</button>
|
|
164
|
+
|
|
165
|
+
<!-- Accordion content -->
|
|
166
|
+
<div
|
|
167
|
+
class={cn(
|
|
168
|
+
'overflow-hidden transition-all duration-200',
|
|
169
|
+
isExpanded ? 'max-h-96 opacity-100' : 'max-h-0 opacity-0'
|
|
170
|
+
)}
|
|
171
|
+
>
|
|
172
|
+
<div class={cn('px-3 pb-3', isLast && 'rounded-b-lg')}>
|
|
173
|
+
<!-- Categorical items -->
|
|
174
|
+
{#if legend.type !== 'gradient' && legend.items && legend.items.length > 0}
|
|
175
|
+
<ul class="space-y-1.5">
|
|
176
|
+
{#each legend.items as item, index}
|
|
177
|
+
<li>
|
|
178
|
+
<button
|
|
179
|
+
type="button"
|
|
180
|
+
class={cn(
|
|
181
|
+
'flex items-center gap-2 w-full text-left rounded px-1 -mx-1',
|
|
182
|
+
legend.onItemClick && 'hover:bg-muted cursor-pointer',
|
|
183
|
+
!legend.onItemClick && 'cursor-default',
|
|
184
|
+
item.active === false && 'opacity-50'
|
|
185
|
+
)}
|
|
186
|
+
onclick={() => handleItemClick(legend, item, index)}
|
|
187
|
+
disabled={!legend.onItemClick}
|
|
188
|
+
>
|
|
189
|
+
<span
|
|
190
|
+
class="w-4 h-4 rounded flex-shrink-0"
|
|
191
|
+
style="background-color: {item.color};"
|
|
192
|
+
></span>
|
|
193
|
+
<span class="text-foreground truncate flex-1">{item.label}</span>
|
|
194
|
+
{#if item.value !== undefined}
|
|
195
|
+
<span class="text-muted-foreground text-xs">{item.value}</span>
|
|
196
|
+
{/if}
|
|
197
|
+
</button>
|
|
198
|
+
</li>
|
|
199
|
+
{/each}
|
|
200
|
+
</ul>
|
|
201
|
+
{:else if legend.type === 'gradient' && legend.gradient && legend.gradient.length > 0}
|
|
202
|
+
<!-- Gradient -->
|
|
203
|
+
<div class="space-y-2">
|
|
204
|
+
<div class="h-3 rounded-sm" style="background: {getGradientStyle(legend)};"></div>
|
|
205
|
+
<div class="flex justify-between text-xs text-muted-foreground">
|
|
206
|
+
<span>{legend.minLabel || legend.gradient[0]?.label || ''}</span>
|
|
207
|
+
<span
|
|
208
|
+
>{legend.maxLabel || legend.gradient[legend.gradient.length - 1]?.label || ''}</span
|
|
209
|
+
>
|
|
210
|
+
</div>
|
|
211
|
+
</div>
|
|
212
|
+
{/if}
|
|
213
|
+
</div>
|
|
214
|
+
</div>
|
|
215
|
+
{/each}
|
|
216
|
+
</div>
|