|
| 1 | +<!-- |
| 2 | + @component |
| 3 | + Implement a static map using Leaflet, showing a marker at the specified coordinates. |
| 4 | + @see https://leafletjs.com/ |
| 5 | +--> |
| 6 | +<script> |
| 7 | + import { onMount } from 'svelte'; |
| 8 | + import { _ } from 'svelte-i18n'; |
| 9 | + import { getUnpkgURL, loadModule } from '$lib/services/app/dependencies'; |
| 10 | +
|
| 11 | + /** |
| 12 | + * @import { GeoCoordinates } from '$lib/types/private'; |
| 13 | + */ |
| 14 | +
|
| 15 | + /** |
| 16 | + * @typedef {object} Props |
| 17 | + * @property {GeoCoordinates} coordinates GeoCoordinates of the location to show on the map. |
| 18 | + */ |
| 19 | +
|
| 20 | + /** @type {Props} */ |
| 21 | + let { |
| 22 | + /* eslint-disable prefer-const */ |
| 23 | + coordinates, |
| 24 | + /* eslint-enable prefer-const */ |
| 25 | + } = $props(); |
| 26 | +
|
| 27 | + /** @type {HTMLElement | undefined} */ |
| 28 | + let mapElement = $state(); |
| 29 | +
|
| 30 | + /** @type {import('leaflet').Map | undefined} */ |
| 31 | + let map = undefined; |
| 32 | +
|
| 33 | + /** |
| 34 | + * Load the Leaflet library and initialize the map. We don’t bundle the library because of the |
| 35 | + * bundle size. The component may not be used often, and multiple map services, including Google |
| 36 | + * Maps and Here Maps, may be supported in the future. |
| 37 | + */ |
| 38 | + const init = async () => { |
| 39 | + if (!mapElement) { |
| 40 | + return; |
| 41 | + } |
| 42 | +
|
| 43 | + /** @type {import('leaflet')} */ |
| 44 | + const leaflet = await loadModule('leaflet', 'dist/leaflet-src.esm.js'); |
| 45 | + const iconUrl = `${getUnpkgURL('leaflet')}/dist/images/marker-icon-2x.png`; |
| 46 | +
|
| 47 | + map = leaflet.map(mapElement, { center: [0, 0], zoom: 2 }); |
| 48 | +
|
| 49 | + leaflet |
| 50 | + .tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { |
| 51 | + attribution: |
| 52 | + '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', |
| 53 | + }) |
| 54 | + .addTo(map); |
| 55 | +
|
| 56 | + const { latitude, longitude } = coordinates; |
| 57 | + const icon = leaflet.icon({ iconUrl, iconSize: [25, 41] }); |
| 58 | +
|
| 59 | + leaflet.marker([latitude, longitude], { icon }).addTo(map); |
| 60 | + map.setView([latitude, longitude], 12); |
| 61 | +
|
| 62 | + new ResizeObserver(() => { |
| 63 | + map?.invalidateSize(); |
| 64 | + }).observe(mapElement); |
| 65 | + }; |
| 66 | +
|
| 67 | + onMount(() => { |
| 68 | + init(); |
| 69 | + }); |
| 70 | +</script> |
| 71 | +
|
| 72 | +<div |
| 73 | + role="application" |
| 74 | + class="map" |
| 75 | + bind:this={mapElement} |
| 76 | + aria-label={$_('map_lat_lng', { values: coordinates })} |
| 77 | +></div> |
| 78 | +
|
| 79 | +<style lang="scss"> |
| 80 | + // @todo Copy minimal styles from Leaflet to avoid loading the whole CSS file |
| 81 | + @import 'node_modules/leaflet/dist/leaflet.css'; |
| 82 | +
|
| 83 | + .map { |
| 84 | + margin: var(--sui-focus-ring-width); |
| 85 | + border: 1px solid var(--sui-textbox-border-color); |
| 86 | + border-radius: var(--sui-textbox-border-radius); |
| 87 | + overflow: hidden; |
| 88 | + aspect-ratio: 1 / 1; |
| 89 | + background-clip: text; |
| 90 | + } |
| 91 | +
|
| 92 | + :global(.leaflet-container) { |
| 93 | + font-family: inherit !important; |
| 94 | + font-size: var(--sui-font-size-small) !important; |
| 95 | + } |
| 96 | +
|
| 97 | + :global(.leaflet-container a) { |
| 98 | + color: var(--sui-primary-accent-color-text) !important; |
| 99 | + text-decoration: none !important; |
| 100 | + } |
| 101 | +
|
| 102 | + :global(.leaflet-bar a) { |
| 103 | + border-color: var(--sui-button-border-color) !important; |
| 104 | + color: var(--sui-secondary-foreground-color) !important; |
| 105 | + background-color: var(--sui-button-background-color) !important; |
| 106 | + } |
| 107 | +
|
| 108 | + :global(.leaflet-control) { |
| 109 | + color: var(--sui-secondary-foreground-color) !important; |
| 110 | + background-color: var(--sui-secondary-background-color-translucent) !important; |
| 111 | + } |
| 112 | +
|
| 113 | + :global(.leaflet-control-attribution) { |
| 114 | + padding: 4px 8px; |
| 115 | + } |
| 116 | +
|
| 117 | + // Dark theme: https://stackoverflow.com/q/59819792 |
| 118 | + :global(:root[data-theme='dark'] .leaflet-layer) { |
| 119 | + filter: invert(100%) hue-rotate(180deg); |
| 120 | + } |
| 121 | +</style> |
0 commit comments