@m3ui-vue/m3ui-vue 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/dist/API.md +666 -12
  2. package/dist/MMenuItem-BOkcVtEe.js +809 -0
  3. package/dist/MMenuItem-BOkcVtEe.js.map +1 -0
  4. package/dist/MSlider-BXsBv0ox.js +1402 -0
  5. package/dist/MSlider-BXsBv0ox.js.map +1 -0
  6. package/dist/MTextField-rML9jnXs.js +671 -0
  7. package/dist/MTextField-rML9jnXs.js.map +1 -0
  8. package/dist/api.json +1 -1
  9. package/dist/code-editor.js +1 -1
  10. package/dist/components/MBottomSheet.vue.d.ts +20 -1
  11. package/dist/components/MDialog.vue.d.ts +17 -8
  12. package/dist/components/MDragDropList.vue.d.ts +1 -1
  13. package/dist/components/MIconButton.vue.d.ts +1 -1
  14. package/dist/components/MMap.vue.d.ts +74 -0
  15. package/dist/components/MMapBaseLayers.vue.d.ts +15 -0
  16. package/dist/components/MMapControl.vue.d.ts +40 -0
  17. package/dist/components/MMapFeatureInfo.vue.d.ts +25 -0
  18. package/dist/components/MMapFilter.vue.d.ts +19 -0
  19. package/dist/components/MMapGeoJson.vue.d.ts +34 -0
  20. package/dist/components/MMapHover.vue.d.ts +17 -0
  21. package/dist/components/MMapIdentify.vue.d.ts +37 -0
  22. package/dist/components/MMapLayers.vue.d.ts +31 -0
  23. package/dist/components/MMapMarker.vue.d.ts +45 -0
  24. package/dist/components/MMapMeasure.vue.d.ts +43 -0
  25. package/dist/components/MMapPopup.vue.d.ts +45 -0
  26. package/dist/components/MMapSearch.vue.d.ts +28 -0
  27. package/dist/components/MMenuItem.vue.d.ts +2 -2
  28. package/dist/components/MSeekBar.vue.d.ts +122 -0
  29. package/dist/components/MSplitButton.vue.d.ts +2 -2
  30. package/dist/components/_MMapAttribution.vue.d.ts +7 -0
  31. package/dist/components/_MMapLayerItem.vue.d.ts +25 -0
  32. package/dist/composables/_mapGeometry.d.ts +13 -0
  33. package/dist/composables/_mapPin.d.ts +5 -0
  34. package/dist/composables/_popupPosition.d.ts +12 -0
  35. package/dist/composables/_ssrState.d.ts +30 -0
  36. package/dist/composables/_storageKeys.d.ts +3 -0
  37. package/dist/composables/_useCalendarMonth.d.ts +24 -0
  38. package/dist/composables/_useEscapeStack.d.ts +3 -0
  39. package/dist/composables/_useScrollLock.d.ts +8 -0
  40. package/dist/composables/_useSheetMotion.d.ts +16 -1
  41. package/dist/composables/_useThemeTokens.d.ts +7 -0
  42. package/dist/composables/mapAdapters.d.ts +111 -0
  43. package/dist/composables/mapSearch.d.ts +31 -0
  44. package/dist/composables/themeInitScript.d.ts +4 -3
  45. package/dist/composables/useColorPalette.d.ts +173 -7
  46. package/dist/composables/useLocale.d.ts +38 -0
  47. package/dist/composables/useMap.d.ts +47 -0
  48. package/dist/composables/useMapFilter.d.ts +10 -0
  49. package/dist/composables/useMapLayers.d.ts +30 -0
  50. package/dist/composables/useTheme.d.ts +2 -1
  51. package/dist/index.d.ts +4 -1
  52. package/dist/locales/index.js +273 -7
  53. package/dist/locales/index.js.map +1 -1
  54. package/dist/m3ui-vue.css +1 -1
  55. package/dist/m3ui.js +7913 -8625
  56. package/dist/m3ui.js.map +1 -1
  57. package/dist/map.d.ts +24 -0
  58. package/dist/map.js +2067 -0
  59. package/dist/map.js.map +1 -0
  60. package/dist/plugin.d.ts +18 -2
  61. package/dist/rich-text-editor.js +26 -25
  62. package/dist/rich-text-editor.js.map +1 -1
  63. package/dist/styles.css +1 -1
  64. package/dist/{useLocale-CuvWRZ1h.js → useLocale-CqQrhWFl.js} +40 -2
  65. package/dist/useLocale-CqQrhWFl.js.map +1 -0
  66. package/package.json +11 -2
  67. package/src/__tests__/MDialog.test.ts +30 -0
  68. package/src/__tests__/MFab.test.ts +52 -6
  69. package/src/__tests__/MMap.test.ts +422 -0
  70. package/src/__tests__/MMapLayers.test.ts +257 -0
  71. package/src/__tests__/MMapTools.test.ts +233 -0
  72. package/src/__tests__/MSeekBar.test.ts +170 -0
  73. package/src/__tests__/MTooltip.test.ts +110 -0
  74. package/src/__tests__/_fakeMaplibre.ts +161 -0
  75. package/src/__tests__/calendarMonth.test.ts +59 -0
  76. package/src/__tests__/hydration.test.ts +33 -0
  77. package/src/__tests__/pickerModals.test.ts +128 -0
  78. package/src/__tests__/popupPosition.test.ts +224 -0
  79. package/src/__tests__/scrollLock.test.ts +110 -0
  80. package/src/__tests__/sheets.test.ts +158 -0
  81. package/src/__tests__/ssr.test.ts +63 -1
  82. package/src/__tests__/ssrState.test.ts +155 -0
  83. package/src/__tests__/themeInitScript.test.ts +18 -0
  84. package/src/components/MAutocomplete.vue +2 -4
  85. package/src/components/MBottomSheet.vue +211 -13
  86. package/src/components/MCommandPalette.vue +4 -6
  87. package/src/components/MContextMenu.vue +19 -5
  88. package/src/components/MDatePicker.vue +29 -70
  89. package/src/components/MDatePickerModal.vue +116 -172
  90. package/src/components/MDateRangePicker.vue +29 -51
  91. package/src/components/MDateRangePickerModal.vue +84 -162
  92. package/src/components/MDialog.vue +87 -63
  93. package/src/components/MFab.vue +29 -7
  94. package/src/components/MIconButton.vue +1 -1
  95. package/src/components/MLightbox.vue +12 -5
  96. package/src/components/MMap.vue +568 -0
  97. package/src/components/MMapBaseLayers.vue +67 -0
  98. package/src/components/MMapControl.vue +175 -0
  99. package/src/components/MMapFeatureInfo.vue +41 -0
  100. package/src/components/MMapFilter.vue +80 -0
  101. package/src/components/MMapGeoJson.vue +136 -0
  102. package/src/components/MMapHover.vue +131 -0
  103. package/src/components/MMapIdentify.vue +104 -0
  104. package/src/components/MMapLayers.vue +111 -0
  105. package/src/components/MMapMarker.vue +109 -0
  106. package/src/components/MMapMeasure.vue +452 -0
  107. package/src/components/MMapPopup.vue +103 -0
  108. package/src/components/MMapSearch.vue +162 -0
  109. package/src/components/MMenu.vue +31 -12
  110. package/src/components/MMenuItem.vue +21 -4
  111. package/src/components/MMultiAutocomplete.vue +2 -4
  112. package/src/components/MMultiSelect.vue +2 -4
  113. package/src/components/MNavigationDrawer.vue +2 -8
  114. package/src/components/MNavigationRail.vue +2 -3
  115. package/src/components/MSearchBar.vue +2 -5
  116. package/src/components/MSeekBar.vue +593 -0
  117. package/src/components/MSelect.vue +2 -8
  118. package/src/components/MSplitButton.vue +23 -7
  119. package/src/components/MSpotlightSearch.vue +4 -6
  120. package/src/components/MTimePicker.vue +23 -6
  121. package/src/components/MTimePickerModal.vue +213 -226
  122. package/src/components/MTooltip.vue +99 -3
  123. package/src/components/_MMapAttribution.vue +81 -0
  124. package/src/components/_MMapLayerItem.vue +77 -0
  125. package/src/composables/_mapGeometry.ts +75 -0
  126. package/src/composables/_mapPin.ts +25 -0
  127. package/src/composables/_popupPosition.ts +39 -0
  128. package/src/composables/_ssrState.ts +80 -0
  129. package/src/composables/_storageKeys.ts +6 -0
  130. package/src/composables/_useCalendarMonth.ts +74 -0
  131. package/src/composables/_useEscapeStack.ts +33 -0
  132. package/src/composables/_useScrollLock.ts +41 -0
  133. package/src/composables/_useSheetMotion.ts +82 -20
  134. package/src/composables/_useThemeTokens.ts +42 -0
  135. package/src/composables/mapAdapters.ts +208 -0
  136. package/src/composables/mapSearch.ts +56 -0
  137. package/src/composables/themeInitScript.ts +6 -4
  138. package/src/composables/useColorPalette.ts +23 -9
  139. package/src/composables/useLocale.ts +78 -0
  140. package/src/composables/useMap.ts +98 -0
  141. package/src/composables/useMapFilter.ts +45 -0
  142. package/src/composables/useMapLayers.ts +124 -0
  143. package/src/composables/useTheme.ts +8 -3
  144. package/src/composables/useWindowSizeClass.ts +14 -5
  145. package/src/index.ts +4 -1
  146. package/src/locales/de.ts +39 -1
  147. package/src/locales/es.ts +39 -1
  148. package/src/locales/fr.ts +39 -1
  149. package/src/locales/ja.ts +39 -1
  150. package/src/locales/ko.ts +39 -1
  151. package/src/locales/pt.ts +39 -1
  152. package/src/locales/zh.ts +39 -1
  153. package/src/map.ts +32 -0
  154. package/src/plugin.ts +54 -3
  155. package/dist/MTextField-BPbFeI9M.js +0 -1368
  156. package/dist/MTextField-BPbFeI9M.js.map +0 -1
  157. package/dist/useLocale-CuvWRZ1h.js.map +0 -1
@@ -27,9 +27,11 @@
27
27
  * dampingRatio 0.9 / stiffness 700) stepped in one rAF loop that writes transforms directly.
28
28
  */
29
29
  import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
30
+ import { isHydrating } from '../composables/_hydration'
30
31
  import MIconButton from './MIconButton.vue'
31
32
  import MSpinner from './MSpinner.vue'
32
33
  import { useLocale } from '../composables/useLocale'
34
+ import { useScrollLock } from '../composables/_useScrollLock'
33
35
 
34
36
  export interface LightboxItem {
35
37
  src: string
@@ -284,6 +286,9 @@ function originTransform(): Origin | null {
284
286
  }
285
287
  }
286
288
 
289
+ // The page doesn't scroll from when it opens until its close animation ends
290
+ const scrollLock = useScrollLock()
291
+
287
292
  async function show() {
288
293
  restoreFocus = document.activeElement as HTMLElement | null
289
294
  rendered.value = true
@@ -295,7 +300,7 @@ async function show() {
295
300
  setNow(backdrop, 0)
296
301
  backdrop.k = 300
297
302
  document.addEventListener('keydown', onKeydown)
298
- document.body.style.overflow = 'hidden'
303
+ scrollLock.set(true)
299
304
  await nextTick()
300
305
  measure()
301
306
  backdrop.target = 1
@@ -329,19 +334,22 @@ function hide() {
329
334
  }
330
335
  onSettled = () => {
331
336
  rendered.value = false
332
- document.body.style.overflow = ''
337
+ scrollLock.set(false)
333
338
  restoreFocus?.focus?.()
334
339
  restoreFocus = null
335
340
  }
336
341
  animate()
337
342
  }
338
343
 
339
- // Not on the server (SSR): it renders closed there, and an open one opens on the client
344
+ // Not on the server (SSR): it renders closed there, and an open one opens on the client. Hydrating that HTML
345
+ // (createM3UI({ isHydrating })), the first render has to stay closed to match it: open once mounted instead
346
+ const hydrating = isHydrating()
340
347
  watch(() => props.modelValue, (v) => {
341
348
  if (typeof document === 'undefined') return
342
349
  if (v) !rendered.value && show()
343
350
  else rendered.value && hide()
344
- }, { immediate: true })
351
+ }, { immediate: !hydrating })
352
+ if (hydrating) onMounted(() => { if (props.modelValue && !rendered.value) show() })
345
353
  watch(() => props.index, (v) => {
346
354
  if (v === currentIndex.value || !galleryItems.value[v]) return
347
355
  // Closed, or still opening (a consumer usually sets the index and opens in the same tick): show
@@ -784,7 +792,6 @@ onBeforeUnmount(() => {
784
792
  if (raf) cancelAnimationFrame(raf)
785
793
  if (tapTimer) clearTimeout(tapTimer)
786
794
  document.removeEventListener('keydown', onKeydown)
787
- if (rendered.value) document.body.style.overflow = ''
788
795
  })
789
796
 
790
797
  defineExpose({ open, close, next, prev })
@@ -0,0 +1,568 @@
1
+ <script setup lang="ts">
2
+ /**
3
+ * A MapLibre GL map, for any style: a style URL (from an MVT server like mvt-rs, MapTiler, OpenFreeMap…) or a style
4
+ * object. Its controls take the M3 look and the app's locale, the view can be a v-model and remembered between visits,
5
+ * and markers, popups, GeoJSON layers and your own controls go inside it as components. For anything else, the
6
+ * MapLibre map itself is the `ready` event's argument and the `map` of `useMap()` in components placed inside.
7
+ * Available from the `/map` entry point; needs `maplibre-gl` installed and its CSS imported
8
+ * (`maplibre-gl/dist/maplibre-gl.css`). Give it a height. Server-side rendering renders the empty container: MapLibre
9
+ * loads in the browser.
10
+ *
11
+ * @slot default Map components (`MMapMarker`, `MMapPopup`, `MMapGeoJson`, `MMapControl`) and anything to show over
12
+ * the map, rendered once the map exists
13
+ * @event ready The map loaded (with its style), with the MapLibre `Map`
14
+ * @event click A click on the map (MapLibre's `MapMouseEvent`: `lngLat`, `point`)
15
+ * @event update:center The view moved (on `moveend`): `[lng, lat]`
16
+ * @event update:zoom The zoom changed (on `moveend`)
17
+ * @event update:bearing The rotation changed (on `moveend`)
18
+ * @event update:pitch The tilt changed (on `moveend`)
19
+ */
20
+ import { markRaw, onBeforeUnmount, onMounted, onUnmounted, provide, ref, shallowRef, watch } from 'vue'
21
+ import type * as Maplibre from 'maplibre-gl'
22
+ import MMapAttribution from './_MMapAttribution.vue'
23
+ import { useLocale } from '../composables/useLocale'
24
+ import { MAP_KEY, type MaplibreModule } from '../composables/useMap'
25
+ import { createLayerState, readLayerState } from '../composables/useMapLayers'
26
+ import type { MapAdapter, MapLayerModel } from '../composables/mapAdapters'
27
+
28
+ export type MapControlPosition = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
29
+
30
+ const props = withDefaults(
31
+ defineProps<{
32
+ /** The map's style: a style JSON URL or a style object (MapLibre's `StyleSpecification`). Changing it reloads the style */
33
+ mapStyle: string | Maplibre.StyleSpecification
34
+ /** `[lng, lat]` (v-model). When left out, the style's (or the remembered view's) */
35
+ center?: [number, number]
36
+ /** Zoom level (v-model) */
37
+ zoom?: number
38
+ /** Rotation in degrees (v-model) */
39
+ bearing?: number
40
+ /** Tilt in degrees (v-model) */
41
+ pitch?: number
42
+ /**
43
+ * Remember the view (center, zoom, bearing, pitch) in localStorage and start from it on the next visit: `true`
44
+ * under `m3-map-view`, or your own key (one per map)
45
+ */
46
+ persist?: boolean | string
47
+ /** Zoom and compass buttons: `false`, or a corner (`true` is top-right) */
48
+ navigation?: boolean | MapControlPosition
49
+ /** A button that centers the map on the user's location and follows it: `false`, or a corner (`true` is top-right) */
50
+ geolocate?: boolean | MapControlPosition
51
+ /** A fullscreen button: `false`, or a corner (`true` is top-right) */
52
+ fullscreen?: boolean | MapControlPosition
53
+ /** A metric scale bar: `false`, or a corner (`true` is bottom-left) */
54
+ scale?: boolean | MapControlPosition
55
+ /** The credits of the style's sources, an info button that opens into a pill: `false`, or a corner (`true` is bottom-right). Most tile providers require it */
56
+ attribution?: boolean | MapControlPosition
57
+ /** Anything else for MapLibre's `Map` constructor (`maxZoom`, `maxBounds`, `cooperativeGestures`…) */
58
+ options?: Partial<Omit<Maplibre.MapOptions, 'container' | 'style'>>
59
+ /**
60
+ * Reads the style's conventions (layer names, groups, base layers, what to identify or show on hover) for
61
+ * `MMapLayers`, `MMapBaseLayers`, `MMapIdentify` and `MMapHover`: `genericAdapter`, `orvianAdapter()`, or your own
62
+ */
63
+ adapter?: MapAdapter
64
+ }>(),
65
+ {
66
+ persist: false,
67
+ navigation: true,
68
+ geolocate: false,
69
+ fullscreen: false,
70
+ scale: true,
71
+ attribution: true,
72
+ },
73
+ )
74
+
75
+ const emit = defineEmits<{
76
+ ready: [map: Maplibre.Map]
77
+ click: [event: Maplibre.MapMouseEvent]
78
+ 'update:center': [center: [number, number]]
79
+ 'update:zoom': [zoom: number]
80
+ 'update:bearing': [bearing: number]
81
+ 'update:pitch': [pitch: number]
82
+ }>()
83
+
84
+ const root = ref<HTMLElement>()
85
+ const container = ref<HTMLElement>()
86
+ const map = shallowRef<Maplibre.Map>()
87
+ const maplibre = shallowRef<MaplibreModule>()
88
+ const styleVersion = ref(0)
89
+ const model = shallowRef<MapLayerModel>()
90
+ const layerState = createLayerState()
91
+ const activeTool = ref<string | null>(null)
92
+ provide(MAP_KEY, { map, maplibre, styleVersion, model, layerState, activeTool })
93
+
94
+ /** The adapter's model of the current style, and the layers' visibility and opacity in it. */
95
+ function readModel(m: Maplibre.Map) {
96
+ model.value = props.adapter
97
+ ? props.adapter(m.getStyle(), { styleUrl: typeof props.mapStyle === 'string' ? props.mapStyle : undefined })
98
+ : undefined
99
+ readLayerState(layerState, m, model.value)
100
+ }
101
+
102
+ const t = useLocale()
103
+
104
+ // ── Remembered view ────────────────────────────────────────────────────────────────────────────
105
+
106
+ interface View { center: [number, number]; zoom: number; bearing: number; pitch: number }
107
+ const persistKey = () => (typeof props.persist === 'string' ? props.persist : props.persist ? 'm3-map-view' : null)
108
+
109
+ function readView(): Partial<View> {
110
+ const key = persistKey()
111
+ if (!key) return {}
112
+ try {
113
+ const view = JSON.parse(localStorage.getItem(key) ?? 'null') as Partial<View> | null
114
+ return view && typeof view === 'object' ? view : {}
115
+ } catch {
116
+ return {}
117
+ }
118
+ }
119
+
120
+ function currentView(m: Maplibre.Map): View {
121
+ const c = m.getCenter()
122
+ return { center: [c.lng, c.lat], zoom: m.getZoom(), bearing: m.getBearing(), pitch: m.getPitch() }
123
+ }
124
+
125
+ // ── Controls ───────────────────────────────────────────────────────────────────────────────────
126
+
127
+ // The attribution is a component of ours (_MMapAttribution, in the template), not MapLibre's control
128
+ type ControlName = 'navigation' | 'geolocate' | 'fullscreen' | 'scale'
129
+ const CONTROLS: Record<ControlName, { corner: MapControlPosition; create: (ml: MaplibreModule) => Maplibre.IControl }> = {
130
+ navigation: { corner: 'top-right', create: (ml) => new ml.NavigationControl() },
131
+ geolocate: {
132
+ corner: 'top-right',
133
+ create: (ml) => new ml.GeolocateControl({ positionOptions: { enableHighAccuracy: true }, trackUserLocation: true }),
134
+ },
135
+ fullscreen: { corner: 'top-right', create: (ml) => new ml.FullscreenControl({ container: root.value }) },
136
+ scale: { corner: 'bottom-left', create: (ml) => new ml.ScaleControl({ maxWidth: 100, unit: 'metric' }) },
137
+ }
138
+ const added = new Map<ControlName, Maplibre.IControl>()
139
+
140
+ function syncControl(name: ControlName) {
141
+ const m = map.value
142
+ const ml = maplibre.value
143
+ if (!m || !ml) return
144
+ const old = added.get(name)
145
+ if (old) {
146
+ m.removeControl(old)
147
+ added.delete(name)
148
+ }
149
+ const setting = props[name]
150
+ if (!setting) return
151
+ const control = CONTROLS[name].create(ml)
152
+ m.addControl(control, setting === true ? CONTROLS[name].corner : setting)
153
+ added.set(name, control)
154
+ }
155
+
156
+ for (const name of Object.keys(CONTROLS) as ControlName[]) watch(() => props[name], () => syncControl(name))
157
+
158
+ // ── Map ────────────────────────────────────────────────────────────────────────────────────────
159
+
160
+ /** MapLibre's UI strings, from the app's M3 locale. */
161
+ function mapLocale() {
162
+ return {
163
+ 'AttributionControl.ToggleAttribution': t.mapToggleAttribution,
164
+ 'AttributionControl.MapFeedback': t.mapFeedback,
165
+ 'FullscreenControl.Enter': t.mapEnterFullscreen,
166
+ 'FullscreenControl.Exit': t.mapExitFullscreen,
167
+ 'GeolocateControl.FindMyLocation': t.mapFindMyLocation,
168
+ 'GeolocateControl.LocationNotAvailable': t.mapLocationNotAvailable,
169
+ 'Map.Title': t.mapTitle,
170
+ 'Marker.Title': t.mapMarker,
171
+ 'NavigationControl.ResetBearing': t.mapResetBearing,
172
+ 'NavigationControl.ZoomIn': t.mapZoomIn,
173
+ 'NavigationControl.ZoomOut': t.mapZoomOut,
174
+ 'Popup.Close': t.close,
175
+ 'CooperativeGesturesHandler.WindowsHelpText': t.mapCtrlScroll,
176
+ 'CooperativeGesturesHandler.MacHelpText': t.mapCmdScroll,
177
+ 'CooperativeGesturesHandler.MobileHelpText': t.mapTwoFingers,
178
+ }
179
+ }
180
+
181
+ // ── Control tooltips ───────────────────────────────────────────────────────────────────────────
182
+ // MapLibre's buttons (and yours, in MMapControl) carry a `title`, which the browser shows as its own tooltip: it's
183
+ // moved to data-m3-tip (and aria-label when there's none) and shown as an M3 plain tooltip, inside the map so it's
184
+ // there in fullscreen too
185
+
186
+ const TIP_DELAY = 600 // MTooltip's
187
+ const tip = ref<{ text: string; x: number; y: number; left: boolean } | null>(null)
188
+ let tipTimer: ReturnType<typeof setTimeout> | undefined
189
+ let tipObserver: MutationObserver | undefined
190
+
191
+ function adoptTitle(el: Element) {
192
+ const title = el.getAttribute('title')
193
+ if (!title || !el.closest('.maplibregl-ctrl')) return
194
+ el.setAttribute('data-m3-tip', title)
195
+ if (!el.hasAttribute('aria-label')) el.setAttribute('aria-label', title)
196
+ el.removeAttribute('title')
197
+ }
198
+
199
+ function showTip(el: HTMLElement) {
200
+ const text = el.getAttribute('data-m3-tip')
201
+ const box = root.value?.getBoundingClientRect()
202
+ if (!text || !box) return
203
+ const r = el.getBoundingClientRect()
204
+ // Toward the inside of the map: left of a control on the right half, right of one on the left
205
+ const left = r.left + r.width / 2 > box.left + box.width / 2
206
+ tip.value = { text, left, x: (left ? r.left - 8 : r.right + 8) - box.left, y: r.top + r.height / 2 - box.top }
207
+ }
208
+
209
+ function hideTip() {
210
+ clearTimeout(tipTimer)
211
+ tip.value = null
212
+ }
213
+
214
+ const tipTarget = (e: Event) => (e.target as Element | null)?.closest?.<HTMLElement>('.maplibregl-ctrl [data-m3-tip]') ?? null
215
+
216
+ function onTipOver(e: PointerEvent) {
217
+ const el = tipTarget(e)
218
+ if (!el || e.pointerType === 'touch') return
219
+ clearTimeout(tipTimer)
220
+ tipTimer = setTimeout(() => showTip(el), TIP_DELAY)
221
+ }
222
+ function onTipOut(e: PointerEvent) {
223
+ const el = tipTarget(e)
224
+ if (el && !el.contains(e.relatedTarget as Node | null)) hideTip()
225
+ }
226
+ function onTipFocus(e: FocusEvent) {
227
+ const el = tipTarget(e)
228
+ if (el?.matches(':focus-visible')) showTip(el)
229
+ }
230
+
231
+ onMounted(() => {
232
+ const el = root.value
233
+ if (!el) return
234
+ el.querySelectorAll('[title]').forEach(adoptTitle)
235
+ tipObserver = new MutationObserver((records) => {
236
+ for (const r of records) {
237
+ if (r.type === 'attributes') adoptTitle(r.target as Element)
238
+ else r.addedNodes.forEach((n) => {
239
+ if (!(n instanceof Element)) return
240
+ adoptTitle(n)
241
+ n.querySelectorAll('[title]').forEach(adoptTitle)
242
+ })
243
+ }
244
+ })
245
+ tipObserver.observe(el, { subtree: true, childList: true, attributes: true, attributeFilter: ['title'] })
246
+ })
247
+
248
+ onBeforeUnmount(() => {
249
+ tipObserver?.disconnect()
250
+ clearTimeout(tipTimer)
251
+ })
252
+
253
+ let destroyed = false
254
+
255
+ onMounted(async () => {
256
+ // maplibre-gl ships a UMD build: depending on the bundler its classes are on the namespace or on `default`
257
+ const mod = await import('maplibre-gl')
258
+ const ml = ('Map' in mod ? mod : (mod as { default: MaplibreModule }).default) as MaplibreModule
259
+ if (destroyed || !container.value) return
260
+ const saved = readView()
261
+ const view = {
262
+ center: saved.center ?? props.center,
263
+ zoom: saved.zoom ?? props.zoom,
264
+ bearing: saved.bearing ?? props.bearing,
265
+ pitch: saved.pitch ?? props.pitch,
266
+ }
267
+ const m = markRaw(new ml.Map({
268
+ container: container.value,
269
+ style: props.mapStyle,
270
+ attributionControl: false,
271
+ locale: mapLocale(),
272
+ // Only what's set, so a style's own center and zoom apply otherwise
273
+ ...Object.fromEntries(Object.entries(view).filter(([, v]) => v !== undefined)),
274
+ ...props.options,
275
+ }))
276
+ maplibre.value = ml
277
+
278
+ m.on('style.load', () => {
279
+ readModel(m)
280
+ styleVersion.value++
281
+ })
282
+ m.on('click', (e) => emit('click', e))
283
+ m.on('moveend', () => {
284
+ const v = currentView(m)
285
+ emit('update:center', v.center)
286
+ emit('update:zoom', v.zoom)
287
+ emit('update:bearing', v.bearing)
288
+ emit('update:pitch', v.pitch)
289
+ const key = persistKey()
290
+ if (key) {
291
+ try { localStorage.setItem(key, JSON.stringify(v)) } catch { /* storage blocked */ }
292
+ }
293
+ })
294
+ m.once('load', () => {
295
+ // The map is shared once its style is loaded: components can add sources and layers right away
296
+ map.value = m
297
+ for (const name of Object.keys(CONTROLS) as ControlName[]) syncControl(name)
298
+ emit('ready', m)
299
+ })
300
+ })
301
+
302
+ // After the children's unmount hooks (a parent's beforeUnmount runs before theirs): they remove their layers, markers
303
+ // and controls from a map that still exists
304
+ onUnmounted(() => {
305
+ destroyed = true
306
+ map.value?.remove()
307
+ map.value = undefined
308
+ })
309
+
310
+ // ── v-model ────────────────────────────────────────────────────────────────────────────────────
311
+ // Compared by value: an inline `:center="[lng, lat]"` is a new array on every parent render
312
+
313
+ const near = (a: number, b: number) => Math.abs(a - b) < 1e-9
314
+ watch(() => props.center?.join(','), () => {
315
+ const m = map.value
316
+ if (!m || !props.center) return
317
+ const c = m.getCenter()
318
+ if (!near(c.lng, props.center[0]) || !near(c.lat, props.center[1])) m.jumpTo({ center: props.center })
319
+ })
320
+ watch(() => props.zoom, (zoom) => {
321
+ if (map.value && zoom !== undefined && !near(map.value.getZoom(), zoom)) map.value.jumpTo({ zoom })
322
+ })
323
+ watch(() => props.bearing, (bearing) => {
324
+ if (map.value && bearing !== undefined && !near(map.value.getBearing(), bearing)) map.value.jumpTo({ bearing })
325
+ })
326
+ watch(() => props.pitch, (pitch) => {
327
+ if (map.value && pitch !== undefined && !near(map.value.getPitch(), pitch)) map.value.jumpTo({ pitch })
328
+ })
329
+ watch(() => props.adapter, () => { if (map.value) readModel(map.value) })
330
+ // A full reload (no diff): style.load fires, and components re-add their sources and layers
331
+ watch(() => props.mapStyle, (style) => map.value?.setStyle(style, { diff: false }))
332
+
333
+ defineExpose({
334
+ /** The MapLibre map, once loaded */
335
+ map,
336
+ })
337
+ </script>
338
+
339
+ <template>
340
+ <div
341
+ ref="root"
342
+ class="m3-map relative overflow-hidden"
343
+ @pointerover="onTipOver"
344
+ @pointerout="onTipOut"
345
+ @pointerdown="hideTip"
346
+ @focusin="onTipFocus"
347
+ @focusout="hideTip"
348
+ >
349
+ <!-- Sized, not positioned: MapLibre's CSS makes it position: relative (.maplibregl-map), which would undo absolute -->
350
+ <div ref="container" class="h-full w-full" />
351
+ <!-- Before the slot: added to its corner first, so it stays at the corner's edge -->
352
+ <MMapAttribution v-if="map && attribution" :position="attribution === true ? 'bottom-right' : attribution" />
353
+ <slot v-if="map" />
354
+ <Transition
355
+ enter-active-class="transition-opacity duration-150"
356
+ enter-from-class="opacity-0"
357
+ leave-active-class="transition-opacity duration-100"
358
+ leave-to-class="opacity-0"
359
+ >
360
+ <div
361
+ v-if="tip"
362
+ role="tooltip"
363
+ class="pointer-events-none absolute z-10 min-h-6 max-w-[200px] -translate-y-1/2 rounded bg-inverse-surface px-2 py-1 text-body-small whitespace-nowrap text-inverse-on-surface"
364
+ :class="tip.left ? '-translate-x-full' : ''"
365
+ :style="{ left: `${tip.x}px`, top: `${tip.y}px` }"
366
+ >
367
+ {{ tip.text }}
368
+ </div>
369
+ </Transition>
370
+ </div>
371
+ </template>
372
+
373
+ <style>
374
+ /* The app's typeface, not MapLibre's Helvetica (attribution, scale, popups) */
375
+ .m3-map .maplibregl-map {
376
+ font: inherit;
377
+ }
378
+
379
+ /* MapLibre's controls, popups and markers in M3: the theme's colors (they follow palette and dark mode), Material
380
+ Symbols icons instead of MapLibre's fixed-color SVGs, M3 shapes and elevation. Scoped to .m3-map. */
381
+ .m3-map .maplibregl-ctrl-group {
382
+ background: var(--color-surface-container-high);
383
+ border-radius: 12px;
384
+ box-shadow: var(--shadow-elevation-2);
385
+ overflow: hidden;
386
+ }
387
+ .m3-map .maplibregl-ctrl-group:not(:empty) {
388
+ box-shadow: var(--shadow-elevation-2);
389
+ }
390
+ /* MMapControl: switching group on or off, or expanding into its panel, fades the surface instead of snapping */
391
+ .m3-map .m3-map-control {
392
+ transition: background-color 200ms, box-shadow 200ms, border-radius 200ms;
393
+ }
394
+ .m3-map .m3-map-control-panel {
395
+ background: var(--color-surface-container-high);
396
+ color: var(--color-on-surface);
397
+ border-radius: 16px;
398
+ box-shadow: var(--shadow-elevation-2);
399
+ }
400
+ .m3-map .m3-map-control-panel-in {
401
+ animation: m3-map-control-in 200ms 60ms both;
402
+ }
403
+ @keyframes m3-map-control-in {
404
+ from { opacity: 0; }
405
+ }
406
+ @media (prefers-reduced-motion: reduce) {
407
+ .m3-map .m3-map-control-panel-in { animation: none; }
408
+ }
409
+ .m3-map .maplibregl-ctrl-group button {
410
+ width: 40px;
411
+ height: 40px;
412
+ color: var(--color-on-surface-variant);
413
+ }
414
+ .m3-map .maplibregl-ctrl-group button + button {
415
+ border-top: 1px solid var(--color-outline-variant);
416
+ }
417
+ .m3-map .maplibregl-ctrl:not(.m3-map-control-panel) button:not(:disabled):hover {
418
+ background-color: color-mix(in srgb, var(--color-on-surface) 8%, transparent);
419
+ }
420
+ .m3-map .maplibregl-ctrl:not(.m3-map-control-panel) button:not(:disabled):active {
421
+ background-color: color-mix(in srgb, var(--color-on-surface) 12%, transparent);
422
+ }
423
+ .m3-map .maplibregl-ctrl-group button[aria-pressed='true'] {
424
+ background-color: var(--color-primary-container);
425
+ color: var(--color-on-primary-container);
426
+ }
427
+ .m3-map .maplibregl-ctrl:not(.m3-map-control-panel) button:focus-visible {
428
+ box-shadow: inset 0 0 0 2px var(--color-secondary);
429
+ }
430
+ .m3-map .maplibregl-ctrl:not(.m3-map-control-panel) button:disabled {
431
+ color: color-mix(in srgb, var(--color-on-surface) 38%, transparent);
432
+ }
433
+ .m3-map .maplibregl-ctrl button .maplibregl-ctrl-icon {
434
+ display: flex;
435
+ align-items: center;
436
+ justify-content: center;
437
+ background-image: none !important;
438
+ }
439
+ .m3-map .maplibregl-ctrl button .maplibregl-ctrl-icon::before {
440
+ font-family: 'Material Symbols Outlined';
441
+ font-size: 20px;
442
+ line-height: 1;
443
+ font-feature-settings: 'liga';
444
+ -webkit-font-smoothing: antialiased;
445
+ }
446
+ .m3-map .maplibregl-ctrl-zoom-in .maplibregl-ctrl-icon::before { content: 'add'; }
447
+ .m3-map .maplibregl-ctrl-zoom-out .maplibregl-ctrl-icon::before { content: 'remove'; }
448
+ .m3-map .maplibregl-ctrl-compass .maplibregl-ctrl-icon::before { content: 'navigation'; }
449
+ .m3-map .maplibregl-ctrl-geolocate .maplibregl-ctrl-icon::before { content: 'my_location'; }
450
+ .m3-map .maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-active,
451
+ .m3-map .maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-background,
452
+ .m3-map .maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-waiting {
453
+ color: var(--color-primary);
454
+ }
455
+ .m3-map .maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-background .maplibregl-ctrl-icon::before { content: 'location_searching'; }
456
+ .m3-map .maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-active-error,
457
+ .m3-map .maplibregl-ctrl-geolocate.maplibregl-ctrl-geolocate-background-error {
458
+ color: var(--color-error);
459
+ }
460
+ .m3-map .maplibregl-ctrl-geolocate:disabled .maplibregl-ctrl-icon::before { content: 'location_disabled'; }
461
+ .m3-map .maplibregl-ctrl-fullscreen .maplibregl-ctrl-icon::before { content: 'fullscreen'; }
462
+ .m3-map .maplibregl-ctrl-shrink .maplibregl-ctrl-icon::before { content: 'fullscreen_exit'; }
463
+
464
+ /* Attribution (_MMapAttribution): a round icon button that opens into a pill with the credits. The surface is the
465
+ control itself, so MMapControl's size spring reveals the text; the row keeps its own width (max-content, capped at
466
+ the map's) so the text doesn't reflow while the control's size animates. */
467
+ .m3-map .m3-map-attrib {
468
+ border-radius: 16px;
469
+ background: var(--color-surface-container-high);
470
+ box-shadow: var(--shadow-elevation-2);
471
+ color: var(--color-on-surface-variant);
472
+ }
473
+ .m3-map .m3-map-attrib-row {
474
+ display: flex;
475
+ align-items: center;
476
+ width: max-content;
477
+ }
478
+ .m3-map .m3-map-attrib-button {
479
+ flex: none;
480
+ display: grid;
481
+ place-items: center;
482
+ width: 32px;
483
+ height: 32px;
484
+ border-radius: 16px;
485
+ color: var(--color-on-surface-variant);
486
+ cursor: pointer;
487
+ transition: background-color 150ms, color 150ms;
488
+ }
489
+ .m3-map .m3-map-attrib-button:hover {
490
+ background-color: color-mix(in srgb, var(--color-on-surface) 8%, transparent);
491
+ }
492
+ .m3-map .m3-map-attrib-button:focus-visible {
493
+ outline: none;
494
+ box-shadow: inset 0 0 0 2px var(--color-secondary);
495
+ }
496
+ .m3-map .m3-map-attrib-open .m3-map-attrib-button {
497
+ color: var(--color-primary);
498
+ }
499
+ .m3-map .m3-map-attrib-text {
500
+ min-width: 0;
501
+ padding-block: 8px;
502
+ font-size: 12px;
503
+ line-height: 16px;
504
+ letter-spacing: 0.4px;
505
+ animation: m3-map-control-in 200ms 60ms both;
506
+ }
507
+ .m3-map .m3-map-attrib-text a {
508
+ color: var(--color-on-surface);
509
+ text-decoration: none;
510
+ }
511
+ .m3-map .m3-map-attrib-text a:hover {
512
+ text-decoration: underline;
513
+ text-underline-offset: 2px;
514
+ }
515
+ .m3-map .maplibregl-ctrl-scale {
516
+ background: color-mix(in srgb, var(--color-surface-container-high) 85%, transparent);
517
+ border-color: var(--color-outline);
518
+ color: var(--color-on-surface-variant);
519
+ font-size: 11px;
520
+ }
521
+
522
+ /* Popups: an M3 surface (elevation 2, 16px corners) with a matching tip */
523
+ .m3-map .maplibregl-popup-content {
524
+ padding: 12px 16px;
525
+ border-radius: 16px;
526
+ background: var(--color-surface-container-high);
527
+ color: var(--color-on-surface);
528
+ box-shadow: var(--shadow-elevation-2);
529
+ overflow: hidden;
530
+ font: inherit;
531
+ font-size: var(--text-body-medium);
532
+ line-height: var(--text-body-medium--line-height);
533
+ }
534
+ .m3-map .maplibregl-popup-anchor-top .maplibregl-popup-tip,
535
+ .m3-map .maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
536
+ .m3-map .maplibregl-popup-anchor-top-right .maplibregl-popup-tip {
537
+ border-bottom-color: var(--color-surface-container-high);
538
+ }
539
+ .m3-map .maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
540
+ .m3-map .maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
541
+ .m3-map .maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip {
542
+ border-top-color: var(--color-surface-container-high);
543
+ }
544
+ .m3-map .maplibregl-popup-anchor-left .maplibregl-popup-tip {
545
+ border-right-color: var(--color-surface-container-high);
546
+ }
547
+ .m3-map .maplibregl-popup-anchor-right .maplibregl-popup-tip {
548
+ border-left-color: var(--color-surface-container-high);
549
+ }
550
+ .m3-map .maplibregl-popup-close-button {
551
+ top: 6px;
552
+ right: 6px;
553
+ width: 28px;
554
+ height: 28px;
555
+ border-radius: 14px;
556
+ color: var(--color-on-surface-variant);
557
+ font-size: 18px;
558
+ line-height: 1;
559
+ }
560
+ .m3-map .maplibregl-popup-close-button:hover {
561
+ background-color: color-mix(in srgb, var(--color-on-surface) 8%, transparent);
562
+ }
563
+
564
+ /* The default marker takes the theme's primary (its SVG is drawn in currentColor) */
565
+ .m3-map .m3-map-marker--default {
566
+ color: var(--color-primary);
567
+ }
568
+ </style>