Skip to content

Commit 93a6cb9

Browse files
authored
feat(VectorLayer): Make filter reactive (#332)
1 parent 8561c72 commit 93a6cb9

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
import type { GeoJSON } from 'geojson';
1111
1212
import { SWRDataLabLight } from '../MapStyle';
13+
import type { FilterSpecification } from 'maplibre-gl';
14+
import { tokens } from '../../DesignTokens';
1315
1416
const { Story } = defineMeta({
1517
title: 'Maplibre/Layer/VectorLayer',
1618
component: VectorLayer
1719
});
1820
21+
const filters: FilterSpecification[] = [
22+
['<', 'coverage_2025', 1],
23+
['>', 'coverage_2025', 1]
24+
];
25+
26+
let selectedFilter: any = $state(0);
27+
1928
let hovered: any = $state();
2029
const handleMouseMove = (e) => {
2130
hovered = e.features?.[0];
@@ -104,6 +113,14 @@
104113

105114
<Story asChild name="Filter">
106115
<DesignTokens theme="light">
116+
<div class="controls">
117+
<label for="filter-select">Select filter</label>
118+
<select name="filter-select" id="filter-select" bind:value={selectedFilter}>
119+
{#each filters as f, i}
120+
<option value={i}>{JSON.stringify(f)}</option>
121+
{/each}
122+
</select>
123+
</div>
107124
<div class="container">
108125
<Map showDebug={true} style={SWRDataLabLight()}>
109126
<VectorTileSource
@@ -115,9 +132,9 @@
115132
sourceLayer="coverage"
116133
type="fill"
117134
id="coverage-fill"
118-
filter={['<', 'coverage_2025', 1]}
135+
filter={filters[selectedFilter]}
119136
paint={{
120-
'fill-color': '#ce541c'
137+
'fill-color': tokens.shades.forest.base
121138
}}
122139
/>
123140
<AttributionControl position="bottom-left" />
@@ -131,4 +148,8 @@
131148
width: 100%;
132149
height: 600px;
133150
}
151+
.controls {
152+
margin-bottom: 1em;
153+
font-family: monospace;
154+
}
134155
</style>

components/src/maplibre/VectorLayer/VectorLayer.svelte

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
type SymbolLayoutProps,
1111
type LinePaintProps,
1212
type MapGeoJSONFeature,
13-
type MapLayerMouseEvent
13+
type MapLayerMouseEvent,
14+
type FilterSpecification
1415
} from 'maplibre-gl';
1516
1617
import { getMapContext } from '../context.svelte.js';
@@ -24,7 +25,7 @@
2425
/**
2526
* Maplibre [filter expression](https://maplibre.org/maplibre-style-spec/layers/#filter)
2627
*/
27-
filter?: any[];
28+
filter?: FilterSpecification;
2829
type: 'line' | 'fill' | 'circle' | 'symbol';
2930
placeBelow?: string;
3031
visible?: boolean;
@@ -91,6 +92,13 @@
9192
}
9293
});
9394
95+
// Make filter reactive
96+
$effect(() => {
97+
if (styleLoaded && filter) {
98+
map?.setFilter(id, filter);
99+
}
100+
});
101+
94102
$effect(() => resetLayerEventListener(map, 'click', id, onclick));
95103
$effect(() => resetLayerEventListener(map, 'mousemove', id, onmousemove));
96104
$effect(() => resetLayerEventListener(map, 'mouseleave', id, onmouseleave));

0 commit comments

Comments
 (0)