Skip to content

Commit 4a180a4

Browse files
authored
feat(VectorTileSource): Make tiles and url props reactive (#338)
* Add test case * Build out feature
1 parent e80cae7 commit 4a180a4

File tree

6 files changed

+233
-189
lines changed

6 files changed

+233
-189
lines changed

components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"dependencies": {
3636
"@maplibre/maplibre-gl-geocoder": "1.9.1",
3737
"@maplibre/maplibre-gl-inspect": "1.7.1",
38-
"maplibre-gl": "5.7.0",
38+
"maplibre-gl": "5.20.0",
3939
"svelte-select": "5.8.3"
4040
},
4141
"devDependencies": {

components/src/maplibre/MapStyle/SWRDataLabLight.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const tokens = {
2424
sans_bold: ['swr_sans_bold'],
2525
background: {
2626
stops: [
27-
[6, 'hsl(24, 20%, 97%)'],
27+
[6, 'hsl(24, 20%, 96%)'],
2828
[6.5, 'hsl(24, 10%, 99%)']
2929
]
3030
},
@@ -37,7 +37,7 @@ const tokens = {
3737
sand: 'hsl(60, 0%, 95%)',
3838
rock: 'hsl(192, 0%, 90%)',
3939
street_primary: 'hsl(0, 4%, 95%)',
40-
street_primary_faded: 'hsl(0, 4%, 93%)',
40+
street_primary_faded: 'hsl(0, 4%, 90%)',
4141
street_primary_case: 'hsl(240, 1%, 84%)',
4242
street_secondary: 'hsl(0, 0%, 95%)',
4343
street_secondary_case: 'hsl(0, 0%, 75%)',

components/src/maplibre/Source/MapSource.svelte

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<script lang="ts">
22
import { onDestroy, type Snippet } from 'svelte';
3-
import { type Map, type SourceSpecification } from 'maplibre-gl';
4-
import { getMapContext, createSourceContext } from '../context.svelte.js';
3+
import {
4+
VectorTileSource,
5+
type VectorSourceSpecification,
6+
type Map,
7+
type SourceSpecification
8+
} from 'maplibre-gl';
9+
import { getMapContext, createSourceContext, SourceContext } from '../context.svelte.js';
510
611
type Source = maplibregl.VectorTileSource | maplibregl.GeoJSONSource;
712
@@ -14,7 +19,8 @@
1419
}
1520
1621
let { id, sourceSpec, source = $bindable(), children }: MapSourceProps = $props();
17-
let firstRun = true;
22+
23+
let firstRun = $state(true);
1824
1925
// Get map context
2026
const { map, styleLoaded } = $derived(getMapContext());
@@ -26,13 +32,12 @@
2632
// actual source object back from the map instance
2733
$effect(() => {
2834
if (map && styleLoaded && firstRun) {
29-
map.addSource(id, $state.snapshot(sourceSpec) as SourceSpecification);
35+
map.addSource(id, $state.snapshot(sourceSpec));
3036
source = map.getSource(id);
3137
firstRun = false;
3238
}
3339
});
3440
35-
// 2. Do extra stuff with the source object
3641
$effect(() => {
3742
if (source && sourceSpec.type === 'geojson') {
3843
if (firstRun === false) {
@@ -41,6 +46,18 @@
4146
}
4247
});
4348
49+
$effect(() => {
50+
if (!firstRun && source instanceof VectorTileSource) {
51+
source.setTiles(sourceSpec.tiles);
52+
}
53+
});
54+
55+
$effect(() => {
56+
if (!firstRun && source instanceof VectorTileSource) {
57+
source.setUrl(sourceSpec.url);
58+
}
59+
});
60+
4461
onDestroy(() => {
4562
if (map && styleLoaded) {
4663
const layers = map?.getStyle().layers;

components/src/maplibre/VectorTileSource/VectorTileSource.stories.svelte

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
title: 'Maplibre/Source/VectorTileSource',
1414
component: VectorTileSource
1515
});
16+
17+
const demoUrls = [
18+
'https://demotiles.maplibre.org/tiles/tiles.json',
19+
'https://tiles.datenhub.net/tiles/osm/tiles.json'
20+
];
21+
let demoUrl = $state(demoUrls[0]);
1622
</script>
1723

1824
<Story asChild name="Default">
@@ -57,7 +63,7 @@
5763
</DesignTokens>
5864
</Story>
5965

60-
<Story asChild name="Usign bare tile URL">
66+
<Story asChild name="Using a bare tile URL">
6167
<DesignTokens theme="light">
6268
<div class="container">
6369
<Map showDebug={true} style={SWRDataLabLight()}>
@@ -84,6 +90,46 @@
8490
</DesignTokens>
8591
</Story>
8692

93+
<Story asChild name="Reactive url parameter">
94+
<DesignTokens theme="light">
95+
<label for="select-url">URL</label>
96+
<select name="select-url" id="select-url" bind:value={demoUrl}>
97+
{#each demoUrls as url}
98+
<option>{url}</option>
99+
{/each}
100+
</select>
101+
<div class="container">
102+
<Map showDebug={true} style={SWRDataLabLight()}>
103+
<VectorTileSource id="test-source" url={demoUrl} />
104+
105+
<VectorLayer
106+
sourceId="test-source"
107+
sourceLayer="countries"
108+
id="test-layer"
109+
type="line"
110+
paint={{
111+
'line-color': shades.red.base,
112+
'line-width': 2
113+
}}
114+
/>
115+
116+
<VectorLayer
117+
sourceId="test-source"
118+
sourceLayer="boundaries"
119+
id="test-layer-2"
120+
type="line"
121+
paint={{
122+
'line-color': shades.forest.base,
123+
'line-width': 2
124+
}}
125+
/>
126+
127+
<AttributionControl position="bottom-left" />
128+
</Map>
129+
</div>
130+
</DesignTokens>
131+
</Story>
132+
87133
<style>
88134
.container {
89135
width: 100%;

components/src/maplibre/VectorTileSource/VectorTileSource.svelte

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,8 @@
1818
promoteId?: PromoteIdSpecification;
1919
}
2020
21-
const {
22-
minZoom,
23-
maxZoom,
24-
id,
25-
url,
26-
tiles,
27-
attribution,
28-
promoteId
29-
}: VectorTileSourceProps = $props();
21+
const { minZoom, maxZoom, id, url, tiles, attribution, promoteId }: VectorTileSourceProps =
22+
$props();
3023
3124
let tileJsonData = $derived(url ? await fetchTileJSON(url) : {});
3225

0 commit comments

Comments
 (0)