1
0
mirror of https://github.com/tabler/tabler.git synced 2026-08-07 03:42:27 +04:00
Files
tabler/shared/ui/Map.astro
2026-08-03 00:55:32 +02:00

75 lines
1.9 KiB
Plaintext

---
// Mapbox GL map. Access token from site.mapboxKey. Renders nothing unless
// maps[mapId] exists.
import maps from '@data/maps.json';
import site from '@data/site.json';
import CaptureScript from '../components/CaptureScript.astro';
interface Props {
/** Key into the maps data */
mapId: string;
ratio?: string;
}
const { mapId, ratio } = Astro.props;
type MapData = {
title?: string;
ratio?: string;
card?: boolean;
style?: string;
zoom?: number;
center?: [number, number];
markers?: { name?: string; center: [number, number] }[];
};
const data = (maps as unknown as Record<string, MapData>)[mapId];
const mapContainerId = `map-${mapId}`;
const style = data ? (data.style ?? 'streets-v11') : undefined;
const zoom = data?.zoom ?? 13;
const center = data?.center ? [data.center[1], data.center[0]] : [13.4049, 52.518827];
const markers = (data?.markers ?? []).map((marker) => [marker.center[1], marker.center[0]]);
const ratioClass = data?.ratio ?? ratio ?? '16x9';
---
{
data && (
<div class={`ratio ratio-${ratioClass}`}>
<div>
<div id={mapContainerId} class={`w-100 h-100${data.card ? ' rounded' : ''}`} />
</div>
</div>
)
}
{
data && (
<CaptureScript>
<!-- BEGIN MAP -->
<script define:vars={{ mapboxKey: site.mapboxKey, mapContainerId, style, zoom, center, markers }}>
window.tabler_map ??= {};
function initMap() {
mapboxgl.accessToken = mapboxKey;
const map = new mapboxgl.Map({
container: mapContainerId,
style: `mapbox://styles/mapbox/${style}`,
zoom,
center,
});
markers.forEach((coords) => {
new mapboxgl.Marker({ color: 'var(--tblr-primary)' }).setLngLat(coords).addTo(map);
});
window.tabler_map[mapContainerId] = map;
}
document.readyState !== 'loading' ? initMap() : document.addEventListener('DOMContentLoaded', initMap, { once: true });
</script>
<!-- END MAP -->
</CaptureScript>
)
}