Skip to content

Commit 0ce47fe

Browse files
authored
feat(Map): Add cursor prop (#158)
1 parent d1e88e5 commit 0ce47fe

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

components/src/maplibre/Map/Map.svelte

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@
2525
projection?: ProjectionSpecification;
2626
showDebug?: boolean;
2727
options?: any;
28+
/**
29+
* Set the mouse cursor. `""` (empty string) restores Maplibre's default behaviour. See VectorLayer/Default for a common usage example
30+
*/
31+
cursor?: string;
2832
mapContext?: MapContext;
2933
/**
30-
* "Use Ctrl + scroll to zoom"
34+
* Show "Use Ctrl + scroll to zoom" overlay
3135
*/
3236
cooperativeGestures?: boolean;
3337
onmovestart?: (e: MapLibreEvent) => null;
@@ -50,6 +54,7 @@
5054
allowRotation = false,
5155
allowZoom = true,
5256
showDebug = false,
57+
cursor,
5358
initialLocation: receivedInitialLocation,
5459
// Future: This should become bindable.readonly when that becomes
5560
// available, see: https://github.com/sveltejs/svelte/issues/7712
@@ -122,6 +127,12 @@
122127
if (mapContext.map) mapContext.map.setStyle(style);
123128
});
124129
130+
$effect(() => {
131+
if (mapContext.map) {
132+
mapContext.map.getCanvas().style.cursor = cursor ?? '';
133+
}
134+
});
135+
125136
$effect(() => {
126137
if (mapContext.styleLoaded) {
127138
mapContext.map?.setProjection(projection);

components/src/maplibre/VectorLayer/VectorLayer.stories.svelte

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script context="module" lang="ts">
1+
<script module lang="ts">
22
import { defineMeta } from '@storybook/addon-svelte-csf';
33
import Map from '../Map/Map.svelte';
44
import VectorLayer from './VectorLayer.svelte';
@@ -15,17 +15,27 @@
1515
title: 'Maplibre/Layer/VectorLayer',
1616
component: VectorLayer
1717
});
18+
19+
let hovered: any = $state();
20+
const handleMouseMove = (e) => {
21+
hovered = e.features?.[0];
22+
};
23+
const handleMouseLeave = () => {
24+
hovered = null;
25+
};
1826
</script>
1927

2028
<Story asChild name="Default">
2129
<DesignTokens>
2230
<div class="container">
23-
<Map showDebug={true} style={SWRDataLabLight()}>
31+
<Map showDebug={true} style={SWRDataLabLight()} cursor={hovered ? 'pointer' : ''}>
2432
<VectorTileSource
2533
id="ev-infra-source"
2634
url={`https://static.datenhub.net/data/p108_e_auto_check/ev_infra_merged.versatiles?tiles/{z}/{x}/{y}`}
2735
/>
2836
<VectorLayer
37+
onmousemove={handleMouseMove}
38+
onmouseleave={handleMouseLeave}
2939
sourceId="ev-infra-source"
3040
sourceLayer="coverage"
3141
type="fill"
@@ -44,12 +54,22 @@
4454
}}
4555
/>
4656
<VectorLayer
57+
bind:hovered
4758
sourceId="ev-infra-source"
4859
sourceLayer="coverage"
4960
id="ev-infra-outline"
5061
type="line"
5162
paint={{
52-
'line-width': 0.5,
63+
'line-width': [
64+
'case',
65+
[
66+
'any',
67+
['boolean', ['feature-state', 'hovered'], false],
68+
['boolean', ['feature-state', 'selected'], false]
69+
],
70+
2,
71+
0.5
72+
],
5373
'line-color': 'purple',
5474
'line-opacity': 1
5575
}}

0 commit comments

Comments
 (0)