@seta-labs/nuxt-foundation 0.11.0 → 0.12.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.
@@ -0,0 +1,315 @@
1
+ <script setup lang="ts">
2
+ import type { LngLatBoundsLike, Map as MapLibre } from 'maplibre-gl';
3
+
4
+ import workerUrl from 'maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url';
5
+ import 'maplibre-gl/dist/maplibre-gl.css';
6
+
7
+ import type { MapArea, MapPoint, MapView } from '../utils/map-view';
8
+
9
+ type Library = typeof import('maplibre-gl');
10
+
11
+ /** Where the map stopped, and whether the user took it there or the app framed its data. */
12
+ export interface MapMove {
13
+ view: MapView;
14
+ area: MapArea;
15
+ user: boolean;
16
+ }
17
+
18
+ const props = withDefaults(
19
+ defineProps<{
20
+ /** The view to open on; without one the map opens on Italy. */
21
+ start?: MapView | null;
22
+ /** The app is reading what the map shows. */
23
+ loading?: boolean;
24
+ maxZoom?: number;
25
+ /** Side of the zoom buttons: the attribution takes the other corner. */
26
+ controls?: 'top' | 'bottom';
27
+ scale?: boolean;
28
+ label?: string;
29
+ }>(),
30
+ { start: null, maxZoom: undefined, controls: 'top', label: 'Mappa' },
31
+ );
32
+ const emit = defineEmits<{
33
+ /** The map exists, its style still on the way: the area it opens on is already known. */
34
+ ready: [map: MapLibre, library: Library];
35
+ /** A style is loaded, the first one and each one of a theme change: none carries the app's layers. */
36
+ style: [map: MapLibre, library: Library];
37
+ /** The map stopped after a gesture, a zoom button, `go` or `fit`. */
38
+ moved: [move: MapMove];
39
+ }>();
40
+
41
+ /** The CARTO vector styles, one per theme of the shell. */
42
+ const STYLE = { notte: 'dark-matter-gl-style', giorno: 'positron-gl-style' };
43
+ const ITALY: LngLatBoundsLike = [
44
+ [6.6, 36.6],
45
+ [18.5, 47.1],
46
+ ];
47
+ const FIT_PADDING_PX = 48;
48
+ // Event data of the moves made through the component, to tell whose they are.
49
+ const BY_USER = { sfBy: 'user' };
50
+ const BY_APP = { sfBy: 'app' };
51
+
52
+ const { theme } = useFoundationTheme();
53
+ const cartoKey = useRuntimeConfig().public.cartoKey;
54
+ const root = ref<HTMLDivElement | null>(null);
55
+ const canvas = ref<HTMLDivElement | null>(null);
56
+ const drawn = ref(false);
57
+ let map: MapLibre | null = null;
58
+ let library: Library | null = null;
59
+
60
+ /** CARTO hands the key of the style on to its tiles, sprites and fonts. */
61
+ function styleUrl(): string {
62
+ const key = cartoKey ? `?key=${cartoKey}` : '';
63
+ return `https://basemaps.cartocdn.com/gl/${STYLE[theme.value]}/style.json${key}`;
64
+ }
65
+
66
+ function area(): MapArea | null {
67
+ const bounds = map?.getBounds();
68
+ return bounds
69
+ ? {
70
+ west: bounds.getWest(),
71
+ south: bounds.getSouth(),
72
+ east: bounds.getEast(),
73
+ north: bounds.getNorth(),
74
+ }
75
+ : null;
76
+ }
77
+
78
+ /** Frames these points, Italy when there is none: a move of the app, not a view the user chose. */
79
+ function fit(points: MapPoint[], options: { padding?: number; maxZoom?: number } = {}): void {
80
+ if (!map || !library) return;
81
+ if (!points.length) {
82
+ map.fitBounds(ITALY, { duration: 0 }, BY_APP);
83
+ return;
84
+ }
85
+ const bounds = new library.LngLatBounds();
86
+ for (const point of points) bounds.extend([point.lng, point.lat]);
87
+ map.fitBounds(bounds, { padding: FIT_PADDING_PX, duration: 0, ...options }, BY_APP);
88
+ }
89
+
90
+ /** Goes where the user asked through the app: a search, a click on a group. */
91
+ function go(view: MapPoint & { zoom?: number }): void {
92
+ map?.easeTo({ center: [view.lng, view.lat], zoom: view.zoom }, BY_USER);
93
+ }
94
+
95
+ /** A token of the design system as the browser resolves it on the map: paint takes no `var()`. */
96
+ function token(name: string): string {
97
+ return getComputedStyle(root.value ?? document.body)
98
+ .getPropertyValue(name)
99
+ .trim();
100
+ }
101
+
102
+ // A resize also ends a move: only gestures and the moves made through the component count.
103
+ function onMoveEnd(event: object): void {
104
+ const current = area();
105
+ if (!map || !current) return;
106
+ const gesture = 'originalEvent' in event && Boolean(event.originalEvent);
107
+ const by = 'sfBy' in event ? event.sfBy : null;
108
+ if (!gesture && !by) return;
109
+ const { lat, lng } = map.getCenter();
110
+ emit('moved', {
111
+ view: { lat, lng, zoom: map.getZoom() },
112
+ area: current,
113
+ user: gesture || by === BY_USER.sfBy,
114
+ });
115
+ }
116
+
117
+ onMounted(async () => {
118
+ const loaded = await import('maplibre-gl');
119
+ // The default worker URL is a sibling of the module, which the bundler moves away.
120
+ loaded.setWorkerUrl(workerUrl);
121
+ if (!canvas.value) return;
122
+ library = loaded;
123
+ const created = new loaded.Map({
124
+ container: canvas.value,
125
+ style: styleUrl(),
126
+ ...(props.start
127
+ ? { center: [props.start.lng, props.start.lat], zoom: props.start.zoom }
128
+ : { bounds: ITALY }),
129
+ ...(props.maxZoom ? { maxZoom: props.maxZoom } : {}),
130
+ attributionControl: false,
131
+ // A panel reads north up and flat: no gesture turns or tilts the map.
132
+ dragRotate: false,
133
+ pitchWithRotate: false,
134
+ touchPitch: false,
135
+ });
136
+ map = created;
137
+ created.touchZoomRotate.disableRotation();
138
+ created.keyboard.disableRotation();
139
+ created.addControl(
140
+ new loaded.AttributionControl({ compact: true }),
141
+ props.controls === 'bottom' ? 'bottom-left' : 'bottom-right',
142
+ );
143
+ if (props.scale) created.addControl(new loaded.ScaleControl({ unit: 'metric' }), 'bottom-left');
144
+ created.on('style.load', () => emit('style', created, loaded));
145
+ created.once('load', () => (drawn.value = true));
146
+ created.on('moveend', onMoveEnd);
147
+ emit('ready', created, loaded);
148
+ });
149
+
150
+ onBeforeUnmount(() => {
151
+ map?.remove();
152
+ map = null;
153
+ });
154
+
155
+ // A full load, not a diff: only that fires `style.load`, where the app adds its layers again.
156
+ watch(theme, () => map?.setStyle(styleUrl(), { diff: false }));
157
+
158
+ defineExpose({ area, fit, go, token });
159
+ </script>
160
+
161
+ <template>
162
+ <div
163
+ ref="root"
164
+ class="sf-mappa"
165
+ :class="`sf-mappa--${controls}`"
166
+ role="region"
167
+ :aria-label="label"
168
+ :aria-busy="!drawn || loading"
169
+ >
170
+ <div ref="canvas" class="sf-mappa__tela" />
171
+ <div v-if="!drawn || loading" class="sf-mappa__carico" role="status" aria-label="Carico" />
172
+ <div class="sf-mappa__zoom">
173
+ <button
174
+ type="button"
175
+ class="sf-btn sf-btn--icona sf-btn--compatto"
176
+ aria-label="Avvicina"
177
+ @click="map?.zoomIn({}, BY_USER)"
178
+ >
179
+ <SfIcona name="aggiungi" :size="16" />
180
+ </button>
181
+ <button
182
+ type="button"
183
+ class="sf-btn sf-btn--icona sf-btn--compatto"
184
+ aria-label="Allontana"
185
+ @click="map?.zoomOut({}, BY_USER)"
186
+ >
187
+ <SfIcona name="meno" :size="16" />
188
+ </button>
189
+ </div>
190
+ <slot />
191
+ </div>
192
+ </template>
193
+
194
+ <style scoped>
195
+ /* `:where` keeps the box at no specificity: the host sizes and rounds it with its own class. */
196
+ :where(.sf-mappa) {
197
+ position: relative;
198
+ height: 100%;
199
+ overflow: hidden;
200
+ background: var(--surface);
201
+ /* MapLibre stacks its own layers: kept inside the map, under the shell's overlays. */
202
+ isolation: isolate;
203
+ }
204
+
205
+ .sf-mappa__tela {
206
+ position: absolute;
207
+ inset: 0;
208
+ font: inherit;
209
+ }
210
+
211
+ .sf-mappa__carico {
212
+ position: absolute;
213
+ inset: 0 0 auto;
214
+ z-index: 3;
215
+ height: 2px;
216
+ overflow: hidden;
217
+ }
218
+
219
+ .sf-mappa__carico::after {
220
+ content: '';
221
+ position: absolute;
222
+ inset: 0;
223
+ width: 40%;
224
+ background: var(--accent);
225
+ animation: sf-mappa-carico 1.1s ease-in-out infinite;
226
+ }
227
+
228
+ @keyframes sf-mappa-carico {
229
+ from {
230
+ transform: translateX(-100%);
231
+ }
232
+
233
+ to {
234
+ transform: translateX(250%);
235
+ }
236
+ }
237
+
238
+ @media (prefers-reduced-motion: reduce) {
239
+ .sf-mappa__carico::after {
240
+ width: 100%;
241
+ opacity: 0.5;
242
+ animation: none;
243
+ }
244
+ }
245
+
246
+ .sf-mappa__zoom {
247
+ position: absolute;
248
+ right: var(--s-4);
249
+ z-index: 3;
250
+ display: flex;
251
+ flex-direction: column;
252
+ border: 1px solid var(--border-forte);
253
+ border-radius: var(--r-sm);
254
+ background: var(--surface);
255
+ }
256
+
257
+ .sf-mappa--top .sf-mappa__zoom {
258
+ top: var(--s-4);
259
+ }
260
+
261
+ .sf-mappa--bottom .sf-mappa__zoom {
262
+ bottom: var(--s-4);
263
+ }
264
+
265
+ .sf-mappa__zoom .sf-btn {
266
+ width: var(--controllo-compatto);
267
+ border: 0;
268
+ }
269
+
270
+ .sf-mappa__zoom .sf-btn + .sf-btn {
271
+ border-top: 1px solid var(--border);
272
+ }
273
+
274
+ /* MapLibre's own controls and popup, on the surfaces of the shell. */
275
+ .sf-mappa :deep(.maplibregl-ctrl-attrib) {
276
+ background: var(--surface) !important;
277
+ /* The compact control sets its own black, which wins on specificity. */
278
+ color: var(--muted) !important;
279
+ font-size: 11px;
280
+ }
281
+
282
+ .sf-mappa :deep(.maplibregl-ctrl-attrib a) {
283
+ color: var(--muted);
284
+ }
285
+
286
+ .sf-mappa :deep(.maplibregl-ctrl-scale) {
287
+ border-color: var(--muted);
288
+ background: color-mix(in oklab, var(--surface) 80%, transparent);
289
+ color: var(--muted);
290
+ font-size: 11px;
291
+ }
292
+
293
+ .sf-mappa :deep(.maplibregl-popup-content) {
294
+ padding: var(--s-3);
295
+ border: 1px solid var(--border);
296
+ border-radius: var(--r-sm);
297
+ background: var(--surface);
298
+ box-shadow: none;
299
+ color: var(--fg);
300
+ font: inherit;
301
+ font-size: 12.5px;
302
+ }
303
+
304
+ .sf-mappa :deep(.maplibregl-popup-tip) {
305
+ display: none;
306
+ }
307
+
308
+ .sf-mappa :deep(.maplibregl-popup-close-button) {
309
+ color: var(--muted);
310
+ }
311
+
312
+ .sf-mappa :deep(.maplibregl-popup-content a) {
313
+ color: var(--accent);
314
+ }
315
+ </style>
@@ -0,0 +1,52 @@
1
+ import type { LocationQuery, LocationQueryRaw } from 'vue-router';
2
+
3
+ export interface MapPoint {
4
+ lat: number;
5
+ lng: number;
6
+ }
7
+
8
+ /** Where a map looks: its centre and zoom, the three numbers kept in the URL. */
9
+ export interface MapView extends MapPoint {
10
+ zoom: number;
11
+ }
12
+
13
+ /** The box a map shows, in degrees. */
14
+ export interface MapArea {
15
+ west: number;
16
+ south: number;
17
+ east: number;
18
+ north: number;
19
+ }
20
+
21
+ const VIEW_KEYS = ['lat', 'lng', 'z'];
22
+ const MAX_ZOOM = 24;
23
+
24
+ function within(value: unknown, min: number, max: number): number | null {
25
+ const number = typeof value === 'string' && value !== '' ? Number(value) : Number.NaN;
26
+ return number >= min && number <= max ? number : null;
27
+ }
28
+
29
+ /** The view a URL carries in `?lat=&lng=&z=`, when all three are there and make sense. */
30
+ export function parseMapView(query: LocationQuery): MapView | null {
31
+ const lat = within(query.lat, -90, 90);
32
+ const lng = within(query.lng, -180, 180);
33
+ const zoom = within(query.z, 0, MAX_ZOOM);
34
+ return lat === null || lng === null || zoom === null ? null : { lat, lng, zoom };
35
+ }
36
+
37
+ /**
38
+ * The query with this view in place of the one it had; `null` drops it.
39
+ * Five decimals of a degree are about a metre.
40
+ */
41
+ export function withMapView(query: LocationQuery, view: MapView | null): LocationQueryRaw {
42
+ const rest = Object.fromEntries(
43
+ Object.entries(query).filter(([key]) => !VIEW_KEYS.includes(key)),
44
+ );
45
+ if (!view) return rest;
46
+ return {
47
+ ...rest,
48
+ lat: view.lat.toFixed(5),
49
+ lng: view.lng.toFixed(5),
50
+ z: view.zoom.toFixed(2),
51
+ };
52
+ }
package/nuxt.config.ts CHANGED
@@ -63,6 +63,9 @@ export default defineNuxtConfig({
63
63
  foundationApp: '',
64
64
  foundationAppVersion: '',
65
65
  foundationVersion: version,
66
+ // Key of the CARTO basemaps of `SfMappa`, from NUXT_PUBLIC_CARTO_KEY at
67
+ // runtime. Public by nature: it travels in every tile URL.
68
+ cartoKey: '',
66
69
  },
67
70
  },
68
71
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seta-labs/nuxt-foundation",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Seta Foundation: the Nuxt layer every internal Seta panel extends — design system mirror (tokens, sf- components, icons, brand) plus the shared shell (bar, app launcher, side menu, status bar)",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -36,10 +36,17 @@
36
36
  "h3": "^1.15.11"
37
37
  },
38
38
  "peerDependencies": {
39
+ "maplibre-gl": "^6.17.0",
39
40
  "nuxt": "^4.0.0"
40
41
  },
42
+ "peerDependenciesMeta": {
43
+ "maplibre-gl": {
44
+ "optional": true
45
+ }
46
+ },
41
47
  "devDependencies": {
42
48
  "@nuxt/eslint": "^1.17.0",
49
+ "maplibre-gl": "^6.17.0",
43
50
  "nuxt": "^4.6.3",
44
51
  "typescript": "^6.0.3",
45
52
  "vitest": "^5.0.5",