From 200c761648405362e4320ca12bc8f15c014bda49 Mon Sep 17 00:00:00 2001 From: zias Date: Tue, 16 Dec 2025 16:23:42 +0100 Subject: [PATCH] fix:OSM --- src/lib/components/map/Map.svelte | 49 ++++++++++++++++++------------ static/map-styles/dark-matter.json | 24 +++++++++++++++ static/map-styles/positron.json | 24 +++++++++++++++ 3 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 static/map-styles/dark-matter.json create mode 100644 static/map-styles/positron.json diff --git a/src/lib/components/map/Map.svelte b/src/lib/components/map/Map.svelte index a134126..28a3e16 100644 --- a/src/lib/components/map/Map.svelte +++ b/src/lib/components/map/Map.svelte @@ -36,7 +36,6 @@ } interface Props { - style?: StyleSpecification; center?: [number, number]; zoom?: number; class?: string; @@ -46,25 +45,20 @@ sidebarVisible?: boolean; } + // Map styles - Positron for light mode, Dark Matter for dark mode + const LIGHT_STYLE = '/map-styles/positron.json'; + const DARK_STYLE = '/map-styles/dark-matter.json'; + + // Detect dark mode preference + let isDarkMode = $state(false); + if (typeof window !== 'undefined') { + isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; + } + + // Compute map style based on dark mode preference + const mapStyle = $derived(isDarkMode ? DARK_STYLE : LIGHT_STYLE); + let { - style = { - version: 8, - sources: { - 'osm-raster': { - type: 'raster', - tiles: ['https://tile.openstreetmap.org/{z}/{x}/{y}.png'], - tileSize: 256, - attribution: '© OpenStreetMap contributors' - } - }, - layers: [ - { - id: 'osm-tiles', - type: 'raster', - source: 'osm-raster' - } - ] - }, center, zoom, class: className = '', @@ -85,6 +79,21 @@ // Use a plain variable (not reactive) to track programmatic moves let isProgrammaticMove = false; + // Listen for system theme changes + $effect(() => { + if (typeof window === 'undefined') return; + + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); + const handleThemeChange = (e: MediaQueryListEvent) => { + isDarkMode = e.matches; + }; + + mediaQuery.addEventListener('change', handleThemeChange); + return () => { + mediaQuery.removeEventListener('change', handleThemeChange); + }; + }); + // Calculate padding for map centering based on sidebar visibility const getMapPadding = $derived.by(() => { if (!sidebarVisible) { @@ -247,7 +256,7 @@
OpenStreetMap contributors © CARTO" + } + }, + "layers": [ + { + "id": "carto-dark-layer", + "type": "raster", + "source": "carto-dark" + } + ] +} diff --git a/static/map-styles/positron.json b/static/map-styles/positron.json new file mode 100644 index 0000000..12fb6e9 --- /dev/null +++ b/static/map-styles/positron.json @@ -0,0 +1,24 @@ +{ + "version": 8, + "name": "Positron (Light)", + "sources": { + "carto-light": { + "type": "raster", + "tiles": [ + "https://a.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png", + "https://b.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png", + "https://c.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png", + "https://d.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png" + ], + "tileSize": 256, + "attribution": "© OpenStreetMap contributors © CARTO" + } + }, + "layers": [ + { + "id": "carto-light-layer", + "type": "raster", + "source": "carto-light" + } + ] +}