@defra/interactive-map 0.0.59-alpha → 0.0.60-alpha

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 (87) hide show
  1. package/assets/templates/add-polygons.njk +2 -2
  2. package/assets/templates/add-symbols.njk +2 -2
  3. package/dist/css/index.css +1 -1
  4. package/dist/esm/im-core.js +1 -1
  5. package/dist/esm/im-shell.js +1 -1
  6. package/dist/umd/im-core.js +1 -1
  7. package/dist/umd/index.js +1 -1
  8. package/docs/api/panel-definition.md +30 -0
  9. package/docs/api/symbol-config.md +12 -0
  10. package/docs/api.md +21 -2
  11. package/docs/assets/images/slot-map.svg +3 -3
  12. package/docs/plugins/datasets.md +11 -1
  13. package/package.json +1 -1
  14. package/plugins/datasets/dist/esm/index.js +1 -1
  15. package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
  16. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  17. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  18. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  19. package/plugins/datasets/dist/umd/index.js +1 -1
  20. package/plugins/datasets/src/index.js +0 -1
  21. package/plugins/datasets/src/index.test.js +2 -8
  22. package/plugins/datasets/src/registry/dataset.test.js +9 -0
  23. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  24. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  25. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  26. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  27. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  28. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  29. package/plugins/search/dist/css/index.css +1 -1
  30. package/plugins/search/src/components/Form/Form.module.scss +1 -0
  31. package/providers/esri/dist/esm/im-esri-provider.js +1 -1
  32. package/providers/esri/src/esriProvider.js +10 -21
  33. package/providers/esri/src/utils/coords.js +37 -7
  34. package/providers/esri/src/utils/coords.test.js +22 -1
  35. package/providers/esri/src/utils/spatial.js +1 -47
  36. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  37. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  38. package/providers/maplibre/src/maplibreProvider.js +5 -14
  39. package/providers/maplibre/src/maplibreProvider.test.js +143 -146
  40. package/providers/maplibre/src/utils/spatial.js +0 -40
  41. package/providers/maplibre/src/utils/spatial.test.js +0 -35
  42. package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  43. package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  44. package/providers/openlayers/src/openlayersProvider.js +5 -14
  45. package/providers/openlayers/src/openlayersProvider.test.js +12 -10
  46. package/providers/openlayers/src/utils/spatial.js +0 -39
  47. package/providers/openlayers/src/utils/spatial.test.js +1 -26
  48. package/src/App/components/Hints/Hints.jsx +9 -21
  49. package/src/App/components/Hints/Hints.test.jsx +20 -33
  50. package/src/App/components/Panel/Panel.jsx +16 -1
  51. package/src/App/components/Panel/Panel.test.jsx +44 -0
  52. package/src/App/hooks/useCrossHairAPI.js +27 -28
  53. package/src/App/hooks/useCrossHairAPI.test.js +64 -7
  54. package/src/App/hooks/useLayoutMeasurements.js +175 -131
  55. package/src/App/hooks/useLayoutMeasurements.test.js +227 -122
  56. package/src/App/hooks/useMapProviderOverrides.js +114 -35
  57. package/src/App/hooks/useMapProviderOverrides.test.js +188 -5
  58. package/src/App/hooks/useModalPanelBehaviour.js +18 -32
  59. package/src/App/hooks/useModalPanelBehaviour.test.js +45 -33
  60. package/src/App/hooks/useVisibleGeometry.js +35 -45
  61. package/src/App/hooks/useVisibleGeometry.test.js +149 -121
  62. package/src/App/layout/Layout.jsx +4 -4
  63. package/src/App/layout/layout.module.scss +124 -61
  64. package/src/App/store/AppProvider.jsx +3 -0
  65. package/src/App/store/appDispatchMiddleware.js +18 -10
  66. package/src/App/store/appDispatchMiddleware.test.js +42 -4
  67. package/src/InteractiveMap/InteractiveMap.js +3 -2
  68. package/src/InteractiveMap/InteractiveMap.test.js +2 -2
  69. package/src/config/events.js +1 -1
  70. package/src/config/mapPanelSlots.js +8 -0
  71. package/src/config/symbolConfig.js +1 -1
  72. package/src/services/symbolRegistry.js +1 -1
  73. package/src/services/symbolRegistry.test.js +19 -0
  74. package/src/types.js +29 -8
  75. package/src/utils/getFitMove.js +73 -0
  76. package/src/utils/getFitMove.test.js +114 -0
  77. package/src/utils/getSafeZoneInset.js +122 -77
  78. package/src/utils/getSafeZoneInset.test.js +241 -54
  79. package/src/utils/getValueForStyle.js +10 -5
  80. package/src/utils/getValueForStyle.test.js +13 -2
  81. package/src/utils/renderedBoxes.js +25 -0
  82. package/src/utils/renderedBoxes.test.js +61 -0
  83. package/src/utils/screenBox.js +70 -0
  84. package/src/utils/screenBox.test.js +69 -0
  85. package/src/utils/symbolUtils.js +9 -9
  86. package/src/utils/symbolUtils.test.js +21 -0
  87. package/providers/esri/src/utils/spatial.test.js +0 -58
@@ -11,6 +11,7 @@
11
11
  height: 100%;
12
12
  min-height: 0;
13
13
  overflow: hidden;
14
+ overflow: clip; // Can't be scrolled, so focusing something wider than a narrow map can't shift it
14
15
 
15
16
  touch-action: none;
16
17
  }
@@ -119,19 +120,33 @@
119
120
  // Top: Includes a panel and buttons in colums
120
121
  // ---------------------------------------------------
121
122
 
122
- // Equal side columns keep the middle one centred; the middle is sized to its content first.
123
+ // The side columns share the row by their content, so one only wraps when the row is really full.
124
+ // A column is never narrower than its widest item, so items keep their size and wrap between them.
123
125
  .im-o-app__top {
124
126
  z-index: 10;
125
127
  display: grid;
126
- grid-template-columns: 1fr auto 1fr;
128
+ grid-template-columns: minmax(min-content, auto) auto minmax(min-content, auto);
127
129
  margin-bottom: auto;
128
130
  align-items: start;
129
131
  width: 100%;
130
132
  gap: var(--divider-gap);
131
133
  }
132
134
 
135
+ // With something in the middle, equal side columns keep it centred while there's room. It then moves
136
+ // off-centre as the gaps close, and nothing wraps until they have.
137
+ .im-o-app__top:has(> .im-o-app__top-col:nth-child(2) > :not([hidden])) {
138
+ grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr);
139
+ }
140
+
141
+ // Once the columns no longer fit on one line (set in useLayoutMeasurements), they can narrow and
142
+ // wrap, and the middle wraps past a third of the row.
143
+ .im-o-app__top--wrapped:has(> .im-o-app__top-col:nth-child(2) > :not([hidden])) {
144
+ grid-template-columns: minmax(min-content, 1fr) fit-content(34%) minmax(min-content, 1fr);
145
+ }
146
+
133
147
  .im-o-app__top-col {
134
148
  display: flex;
149
+ flex-wrap: wrap;
135
150
  gap: var(--divider-gap);
136
151
  }
137
152
 
@@ -164,41 +179,101 @@
164
179
  }
165
180
 
166
181
  // ---------------------------------------------------
167
- // Left: Buttons and panels
182
+ // Left and right: Buttons and panels
168
183
  // ---------------------------------------------------
169
184
 
170
- .im-o-app__left {
185
+ // The top sub-slot, a spacer, then the bottom sub-slot, which sits at the bottom of the column.
186
+ // Each sub-slot grows to its content until the two meet. Grid shares the room out equally, so a
187
+ // sub-slot that needs less than half keeps its full height and the other gets the rest, or each
188
+ // gets half. A panel squeezed this way scrolls its own content. The auto minimums keep buttons and
189
+ // controls at their full height, at this level and in the sub-slots: only panels give way.
190
+ .im-o-app__left,
191
+ .im-o-app__right {
171
192
  position: absolute;
172
- display: flex;
173
- flex-direction: column;
193
+ display: grid;
194
+ grid-template-columns: minmax(0, 1fr);
195
+ max-width: calc(100% - (var(--primary-gap) * 2));
196
+ }
197
+
198
+ // With panels open on both sides, each column is at most half the map, so they come no closer than
199
+ // a divider gap. Closed panels have the hidden attribute, or display: none for HTML panels
200
+ $open-panel: '.im-c-panel:not([hidden]):not([style*="display: none"])';
201
+
202
+ .im-o-app__main:has(> .im-o-app__left > * > #{$open-panel}):has(> .im-o-app__right > * > #{$open-panel}) {
203
+ > .im-o-app__left,
204
+ > .im-o-app__right {
205
+ max-width: calc(50% - var(--primary-gap) - (var(--divider-gap) / 2));
206
+ }
207
+ }
208
+
209
+ .im-o-app__left {
210
+ grid-template-rows: minmax(auto, max-content) minmax(var(--left-slot-gap, var(--divider-gap)), 1fr) minmax(auto, max-content);
174
211
  left: var(--primary-gap);
175
212
  top: var(--left-offset-top);
176
213
  bottom: var(--left-offset-bottom);
177
- gap: var(--divider-gap);
214
+ }
178
215
 
179
- & > *:empty {
180
- display: none;
181
- }
216
+ .im-o-app__right {
217
+ grid-template-rows: minmax(auto, max-content) minmax(var(--right-slot-gap, var(--divider-gap)), 1fr) minmax(auto, max-content);
218
+ right: var(--primary-gap);
219
+ top: var(--right-offset-top);
220
+ bottom: var(--right-offset-bottom);
182
221
  }
183
222
 
184
- .im-o-app__left-top {
185
- display: flex;
186
- flex-direction: column;
187
- align-items: flex-start;
188
- min-height: 0;
223
+ // Each panel or button in a sub-slot shares its room the same way, so two panels in one sub-slot
224
+ // split it rather than one squeezing the other below its own heading
225
+ .im-o-app__left-top,
226
+ .im-o-app__left-bottom,
227
+ .im-o-app__right-top,
228
+ .im-o-app__right-bottom {
189
229
  position: relative;
230
+ display: grid;
231
+ // A track no wider than the column, so a wide panel narrows to fit rather than widening the track
232
+ grid-template-columns: minmax(0, 1fr);
233
+ grid-auto-rows: minmax(auto, max-content);
190
234
  gap: var(--divider-gap);
191
235
  }
192
236
 
193
- .im-o-app__left-bottom {
194
- display: flex;
195
- flex-direction: column;
196
- align-items: flex-start;
197
- justify-content: flex-end;
237
+ // Only a sub-slot with an open panel can shrink, so the buttons and controls in the other keep their
238
+ // size and the mobile drawer cap can reserve room for them
239
+ :is(.im-o-app__left-top, .im-o-app__left-bottom, .im-o-app__right-top, .im-o-app__right-bottom):has(> #{$open-panel}) {
198
240
  min-height: 0;
199
- margin-top: auto;
200
- position: relative;
201
- gap: var(--divider-gap);
241
+ }
242
+
243
+ // A panel's set and minimum widths give way to its column's (a panel beside a button sits inside the button)
244
+ :is(.im-o-app__left-top, .im-o-app__left-bottom, .im-o-app__right-top, .im-o-app__right-bottom) > .im-c-panel {
245
+ min-width: min(var(--min-panel-width), 100%);
246
+ max-width: 100%;
247
+ }
248
+
249
+ .im-o-app__left-top,
250
+ .im-o-app__right-top {
251
+ grid-row: 1;
252
+ align-content: start;
253
+ }
254
+
255
+ .im-o-app__left-bottom,
256
+ .im-o-app__right-bottom {
257
+ grid-row: 3;
258
+ align-content: end;
259
+ }
260
+
261
+ .im-o-app__left-top,
262
+ .im-o-app__left-bottom {
263
+ justify-items: start;
264
+ }
265
+
266
+ .im-o-app__right-top,
267
+ .im-o-app__right-bottom {
268
+ justify-items: end;
269
+ }
270
+
271
+ .im-o-app__left-top {
272
+ max-height: var(--left-top-slot-max-height, none);
273
+ }
274
+
275
+ .im-o-app__right-top {
276
+ max-height: var(--right-top-slot-max-height, none);
202
277
  }
203
278
 
204
279
  // ---------------------------------------------------
@@ -217,39 +292,6 @@
217
292
  z-index: -1; // Support masking the viewport
218
293
  }
219
294
 
220
- // ---------------------------------------------------
221
- // Right: Buttons and panels
222
- // ---------------------------------------------------
223
-
224
- .im-o-app__right {
225
- position: absolute;
226
- display: flex;
227
- flex-direction: column;
228
- right: var(--primary-gap);
229
- top: var(--right-offset-top);
230
- bottom: var(--right-offset-bottom);
231
- gap: var(--divider-gap);
232
- }
233
-
234
- .im-o-app__right-top {
235
- display: flex;
236
- flex-direction: column;
237
- flex: 0 0 auto;
238
- align-items: flex-end;
239
- position: relative;
240
- gap: var(--divider-gap);
241
- }
242
-
243
- .im-o-app__right-bottom {
244
- display: flex;
245
- flex-direction: column;
246
- flex: 0 0 auto;
247
- margin-top: auto;
248
- align-items: flex-end;
249
- position: relative;
250
- gap: var(--divider-gap);
251
- }
252
-
253
295
  // ---------------------------------------------------
254
296
  // Bottom: Logo, scalebar, copyright etc
255
297
  // ---------------------------------------------------
@@ -334,11 +376,19 @@
334
376
  // Drawer:
335
377
  // ---------------------------------------------------
336
378
 
379
+ // Capped in useLayoutMeasurements so it never covers the side columns or runs off main
337
380
  .im-o-app__drawer {
381
+ display: flex;
382
+ flex-direction: column;
383
+ min-height: 0;
384
+ max-height: var(--drawer-max-height, none);
338
385
  z-index: 1;
339
386
  }
340
387
 
341
388
  .im-o-app__drawer .im-c-panel {
389
+ min-height: 0;
390
+ max-height: 100%;
391
+
342
392
  @include tools.border-focus-corner-override(
343
393
  $corners: 'top'
344
394
  );
@@ -468,6 +518,11 @@
468
518
 
469
519
  }
470
520
 
521
+ // Leaves a gap above and below, so a long banner scrolls instead of running off the map
522
+ .im-o-app .im-o-app__modal .im-c-panel--banner {
523
+ max-height: calc(100% - (var(--primary-gap) * 2));
524
+ }
525
+
471
526
  // Mobile and tablet
472
527
  .im-o-app--mobile, .im-o-app--tablet {
473
528
  .im-o-app__modal .im-c-panel--banner {
@@ -497,15 +552,15 @@
497
552
 
498
553
  // Actions inset when width is greater
499
554
  .im-o-app--tablet, .im-o-app--desktop {
555
+ // As wide as the bar, so its size changes when the bar's does and the layout is measured again
500
556
  .im-o-app__actions {
501
557
  position: absolute;
502
- width: 100%;
503
558
  left: 0;
559
+ right: 0;
504
560
  bottom: calc(var(--primary-gap) * 2);
505
-
506
- .im-c-panel {
507
- max-width: var(--action-bar-max-width);
508
- }
561
+ width: fit-content;
562
+ max-width: var(--action-bar-max-width);
563
+ margin-inline: auto;
509
564
 
510
565
  // Opacity-only so it fades in instead of visually sliding up.
511
566
  @media (prefers-reduced-motion: no-preference) {
@@ -527,6 +582,7 @@
527
582
 
528
583
  .im-c-panel {
529
584
  width: 100%;
585
+ min-width: 0;
530
586
 
531
587
  @include tools.border-focus-corner-override(
532
588
  $corners: 'none'
@@ -538,6 +594,13 @@
538
594
  clip-path: inset(-20px 0 0 0);
539
595
  }
540
596
 
597
+ // Only the top panel in the drawer casts a shadow over the map. Panels below it are separated by a
598
+ // border instead, as the actions bar is (see Actions.jsx)
599
+ .im-o-app--mobile .im-o-app__drawer .im-c-panel:not([hidden]):not([style*="display: none"]) ~ .im-c-panel {
600
+ clip-path: inset(0);
601
+ border-top: 1px solid var(--app-border-color);
602
+ }
603
+
541
604
  // Inset the focus ring so it isn't clipped by the parent's overflow:hidden.
542
605
  .im-o-app--mobile .im-o-app__drawer .im-c-panel::after,
543
606
  .im-o-app--mobile .im-o-app__actions .im-c-panel::after,
@@ -30,6 +30,7 @@ export const AppProvider = ({ options, children }) => {
30
30
  headerRef: useRef(null),
31
31
  topRef: useRef(null),
32
32
  topLeftColRef: useRef(null),
33
+ topMiddleColRef: useRef(null),
33
34
  topRightColRef: useRef(null),
34
35
  leftRef: useRef(null),
35
36
  leftTopRef: useRef(null),
@@ -40,6 +41,8 @@ export const AppProvider = ({ options, children }) => {
40
41
  rightBottomRef: useRef(null),
41
42
  drawerRef: useRef(null),
42
43
  bottomRef: useRef(null),
44
+ bottomLeftColRef: useRef(null),
45
+ bottomRightColRef: useRef(null),
43
46
  bottomRightRef: useRef(null),
44
47
  attributionsRef: useRef(null),
45
48
  actionsRef: useRef(null),
@@ -7,6 +7,16 @@ import { logger } from '../../services/logger.js'
7
7
 
8
8
  const BREAKPOINTS = ['mobile', 'tablet', 'desktop']
9
9
 
10
+ const VISIBLE_GEOMETRY_KEYS = ['visibleGeometry', 'preferPan', 'paddingPercent']
11
+
12
+ // The panel's visibleGeometry options that are set. Sent with the opened event for a panel that
13
+ // opens as it's added, as the panel registry may not have its config yet
14
+ const getVisibleGeometryOptions = (source) => Object.fromEntries(
15
+ VISIBLE_GEOMETRY_KEYS.filter(key => source[key] !== undefined).map(key => [key, source[key]])
16
+ )
17
+
18
+ const isOpen = (panelId, state) => Boolean(state.openPanels?.[panelId])
19
+
10
20
  /**
11
21
  * Determines which panels were implicitly closed when opening a new panel
12
22
  */
@@ -60,6 +70,7 @@ function emitOpenPanelEvents ({ panelId, props }, previousState, panelConfig, ev
60
70
  previousState.breakpoint,
61
71
  panelConfig
62
72
  )
73
+ const wasOpen = isOpen(panelId, previousState)
63
74
 
64
75
  queueMicrotask(() => {
65
76
  // Emit close events for implicitly closed panels
@@ -67,8 +78,10 @@ function emitOpenPanelEvents ({ panelId, props }, previousState, panelConfig, ev
67
78
  eventBus.emit(events.APP_PANEL_CLOSED, { panelId: closedPanelId })
68
79
  })
69
80
 
70
- // Emit open event for the new panel
71
- eventBus.emit(events.APP_PANEL_OPENED, { panelId, props })
81
+ // Emit open event for the new panel. Showing a panel that's already open doesn't open it
82
+ if (!wasOpen) {
83
+ eventBus.emit(events.APP_PANEL_OPENED, { panelId, props })
84
+ }
72
85
  })
73
86
  }
74
87
 
@@ -129,15 +142,10 @@ export function handleActionSideEffects (action, previousState, panelConfig, eve
129
142
  }
130
143
  })
131
144
  const bpConfig = mergedConfig[previousState.breakpoint]
132
- if (bpConfig?.open) {
145
+ // Adding a panel again while it's open doesn't open it
146
+ if (bpConfig?.open && !isOpen(id, previousState)) {
133
147
  queueMicrotask(() => {
134
- const slot = bpConfig.slot
135
- const { visibleGeometry } = mergedConfig
136
- const eventPayload = { panelId: id, slot }
137
- if (visibleGeometry) {
138
- eventPayload.visibleGeometry = visibleGeometry
139
- }
140
- eventBus.emit(events.APP_PANEL_OPENED, eventPayload)
148
+ eventBus.emit(events.APP_PANEL_OPENED, { panelId: id, slot: bpConfig.slot, ...getVisibleGeometryOptions(mergedConfig) })
141
149
  })
142
150
  }
143
151
  }
@@ -74,6 +74,33 @@ describe('OPEN_PANEL — regular panel', () => {
74
74
  })
75
75
  })
76
76
 
77
+ describe('OPEN_PANEL — panel already open', () => {
78
+ it('doesn\'t emit panel opened again', async () => {
79
+ run(
80
+ { type: 'OPEN_PANEL', payload: { panelId: 'openPanel', props: {} } },
81
+ { openPanels: { openPanel: {} }, breakpoint: 'md' },
82
+ { openPanel: { md: {} } }
83
+ )
84
+
85
+ await flushMicrotasks()
86
+
87
+ expect(eventBus.emit).not.toHaveBeenCalledWith(events.APP_PANEL_OPENED, expect.anything())
88
+ })
89
+
90
+ it('still emits panel closed for the panels it closes', async () => {
91
+ run(
92
+ { type: 'OPEN_PANEL', payload: { panelId: 'openPanel', props: {} } },
93
+ { openPanels: { openPanel: {}, exclusivePanel: {} }, breakpoint: 'md' },
94
+ { openPanel: { md: {} }, exclusivePanel: { md: { exclusive: true } } }
95
+ )
96
+
97
+ await flushMicrotasks()
98
+
99
+ expect(eventBus.emit).toHaveBeenCalledWith(events.APP_PANEL_CLOSED, { panelId: 'exclusivePanel' })
100
+ expect(eventBus.emit).not.toHaveBeenCalledWith(events.APP_PANEL_OPENED, expect.anything())
101
+ })
102
+ })
103
+
77
104
  const CLOSED_PLUS_OPENED = 3
78
105
 
79
106
  describe('OPEN_PANEL — opening exclusive closes non-exclusive', () => {
@@ -241,10 +268,10 @@ describe('ADD_PANEL', () => {
241
268
  )
242
269
  })
243
270
 
244
- it('emits APP_PANEL_OPENED with visibleGeometry when provided in config', async () => {
245
- const visibleGeometry = { type: 'Feature', geometry: { type: 'Point', coordinates: [1, 2] }, properties: {} }
271
+ it('emits APP_PANEL_OPENED with preferPan and paddingPercent when provided in config', async () => {
272
+ const visibleGeometry = { type: 'Point', coordinates: [1, 2] }
246
273
  run(
247
- { type: 'ADD_PANEL', payload: { id: 'geoPanel', config: { visibleGeometry } } },
274
+ { type: 'ADD_PANEL', payload: { id: 'geoPanel', config: { visibleGeometry, preferPan: false, paddingPercent: 10 } } },
248
275
  { breakpoint: 'desktop' }
249
276
  )
250
277
 
@@ -252,10 +279,21 @@ describe('ADD_PANEL', () => {
252
279
 
253
280
  expect(eventBus.emit).toHaveBeenCalledWith(
254
281
  events.APP_PANEL_OPENED,
255
- { panelId: 'geoPanel', slot: 'left-top', visibleGeometry }
282
+ { panelId: 'geoPanel', slot: 'left-top', visibleGeometry, preferPan: false, paddingPercent: 10 }
256
283
  )
257
284
  })
258
285
 
286
+ it('does not emit APP_PANEL_OPENED when the panel is added again while it\'s open', async () => {
287
+ run(
288
+ { type: 'ADD_PANEL', payload: { id: 'openPanel', config: {} } },
289
+ { breakpoint: 'desktop', openPanels: { openPanel: {} } }
290
+ )
291
+
292
+ await flushMicrotasks()
293
+
294
+ expect(eventBus.emit).not.toHaveBeenCalledWith(events.APP_PANEL_OPENED, expect.anything())
295
+ })
296
+
259
297
  it('does not emit APP_PANEL_OPENED when breakpoint config sets open: false', async () => {
260
298
  run(
261
299
  { type: 'ADD_PANEL', payload: { id: 'hiddenPanel', config: { desktop: { open: false } } } },
@@ -539,9 +539,10 @@ export default class InteractiveMap {
539
539
  * Fit the map view to a bounding box or GeoJSON geometry, respecting the safe zone padding.
540
540
  *
541
541
  * @param {[number, number, number, number] | object} target - Bounds as [west, south, east, north] or [minX, minY, maxX, maxY] depending on the crs, or a GeoJSON Feature, FeatureCollection, or geometry.
542
+ * @param {import('../types.js').FitToBoundsOptions} [options]
542
543
  */
543
- fitToBounds (target) {
544
- this.eventBus.emitWhenReady(events.MAP_FIT_TO_BOUNDS, target)
544
+ fitToBounds (target, options) {
545
+ this.eventBus.emitWhenReady(events.MAP_FIT_TO_BOUNDS, target, options)
545
546
  }
546
547
 
547
548
  /**
@@ -635,9 +635,9 @@ describe('InteractiveMap — Public API Methods', () => {
635
635
  it('fitToBounds and setView emit correct events', () => {
636
636
  const bbox = [-0.489, 51.28, 0.236, 51.686]
637
637
  const center = [-0.1276, 51.5074]
638
- map.fitToBounds(bbox)
638
+ map.fitToBounds(bbox, { preferPan: true })
639
639
  map.setView({ center, zoom: 12 })
640
- expect(map.eventBus.emitWhenReady).toHaveBeenCalledWith('map:fittobounds', bbox)
640
+ expect(map.eventBus.emitWhenReady).toHaveBeenCalledWith('map:fittobounds', bbox, { preferPan: true })
641
641
  expect(map.eventBus.emitWhenReady).toHaveBeenCalledWith('map:setview', { center, zoom: 12 })
642
642
  })
643
643
 
@@ -116,7 +116,7 @@ export const EVENTS = {
116
116
  APP_FULLSCREEN_CHANGE: 'app:fullscreenchange',
117
117
 
118
118
  /**
119
- * Emitted when a panel is opened.
119
+ * Emitted when a closed panel opens. Showing or adding a panel that's already open doesn't emit it again.
120
120
  * Payload: { panelId: string }
121
121
  *
122
122
  * @example
@@ -0,0 +1,8 @@
1
+ // Layout refs of the slots whose panels sit over the map. Panels in other slots are modal, beside
2
+ // the map, or hold buttons or little content.
3
+
4
+ // The side columns. The safe zone leaves out a panel here that's too small to narrow it.
5
+ export const SIDE_PANEL_SLOT_REFS = ['leftTopRef', 'leftBottomRef', 'rightTopRef', 'rightBottomRef']
6
+
7
+ // The side columns and the drawer, which is always within the safe zone's bottom inset
8
+ export const MAP_PANEL_SLOT_REFS = [...SIDE_PANEL_SLOT_REFS, 'drawerRef']
@@ -11,7 +11,7 @@ export const symbolDefaults = {
11
11
  foregroundColor: '#ffffff'
12
12
  }
13
13
 
14
- /** The symbol style keys a dataset or draw style can set (see docs/plugins/datasets.md) */
14
+ /** The named symbol style keys a dataset or draw style can set (see docs/plugins/datasets.md) */
15
15
  export const SYMBOL_STYLE_KEYS = [
16
16
  'symbol', 'symbolSvgContent', 'symbolViewBox', 'symbolAnchor', 'symbolSize',
17
17
  'symbolBackgroundColor', 'symbolForegroundColor', 'symbolHaloWidth', 'symbolGraphic'
@@ -105,7 +105,7 @@ function resolveValues (symbolDef, markerValues, mapStyle, constructorDefaults)
105
105
  merged.graphic = graphics[merged.graphic]
106
106
  }
107
107
  return Object.fromEntries(
108
- Object.entries(merged).map(([token, value]) => [token, getValueForStyle(value, mapStyleId) || ''])
108
+ Object.entries(merged).map(([token, value]) => [token, getValueForStyle(value, mapStyleId) ?? ''])
109
109
  )
110
110
  }
111
111
 
@@ -81,6 +81,17 @@ describe('getKeySymbol', () => {
81
81
  expect(svg).toBe(symbolRegistry.getKeySymbol(style, { ...mapStyle, mapColorScheme: 'light', appColorScheme: 'light' }).svg)
82
82
  })
83
83
 
84
+ it('fills number tokens, including zero', () => {
85
+ const style = { symbolSvgContent: '<path stroke-width="{{ringWidth}}" opacity="{{bandOpacity}}"/>', symbolRingWidth: 3, symbolBandOpacity: { [mapStyle.id]: 0 } }
86
+ expect(symbolRegistry.getKeySymbol(style, mapStyle).svg).toContain('stroke-width="3" opacity="0"')
87
+ })
88
+
89
+ it('fills custom tokens from the style', () => {
90
+ const style = { symbolSvgContent: '<path fill="{{accentColor}}"/>', symbolAccentColor: { outdoor: '#ffdd00', dark: '#f47738' } }
91
+ const { svg } = symbolRegistry.getKeySymbol(style, { ...mapStyle, id: 'dark' })
92
+ expect(svg).toContain('fill="#f47738"')
93
+ })
94
+
84
95
  it('is null for a style with no known symbol', () => {
85
96
  expect(symbolRegistry.getKeySymbol({ symbol: 'does-not-exist' }, mapStyle)).toBeNull()
86
97
  })
@@ -269,6 +280,14 @@ describe('symbolRegistry — image ids and rasterising', () => {
269
280
  expect(small).not.toBe(big)
270
281
  })
271
282
 
283
+ it('gives a different image id when a custom token changes, or differs by map style', () => {
284
+ const style = { symbolSvgContent: '<circle r="4" fill="{{accentColor}}"/>' }
285
+ const imageIdFor = (symbolAccentColor, id) => symbolRegistry.getSymbolImageId({ ...style, symbolAccentColor }, { ...mapStyle, id }, 'normal', 2)
286
+ const perStyle = { outdoor: '#ffdd00', dark: '#f47738' }
287
+ expect(imageIdFor('#ffdd00', 'outdoor')).not.toBe(imageIdFor('#f47738', 'outdoor'))
288
+ expect(imageIdFor(perStyle, 'outdoor')).not.toBe(imageIdFor(perStyle, 'dark'))
289
+ })
290
+
272
291
  it('rasterises at the sized viewBox × pixelRatio, under the same id getSymbolImageId gives', async () => {
273
292
  const style = { symbol: 'circle', symbolSize: 'large' }
274
293
  const { imageId, imageData } = await symbolRegistry.rasteriseSymbolImage(style, mapStyle, 'normal', 2)
package/src/types.js CHANGED
@@ -318,8 +318,9 @@
318
318
  * @property {(offset: [number, number]) => void} panBy
319
319
  * Pan map by pixel offset [x, y]. Positive x pans right, positive y pans down.
320
320
  *
321
- * @property {(bounds: [number, number, number, number] | object) => void} fitToBounds
321
+ * @property {(bounds: [number, number, number, number] | object, options?: { padding?: { top: number, right: number, bottom: number, left: number } }) => void} fitToBounds
322
322
  * Fit map view to the specified bounds [west, south, east, north] or [minX, minY, maxX, maxY] depending on the crs, or a GeoJSON Feature, FeatureCollection, or geometry.
323
+ * `padding` is extra space in pixels kept around the bounds, inside the map's padding.
323
324
  *
324
325
  * @property {(padding: { top?: number, bottom?: number, left?: number, right?: number }) => void} setPadding
325
326
  * Set map padding as pixel insets from the top, bottom, left and right edges of the map.
@@ -366,10 +367,21 @@
366
367
  *
367
368
  * @property {() => void} [clearHighlightedLabel]
368
369
  * @experimental Clear any highlighted label.
370
+ */
371
+
372
+ /**
373
+ * Options for `interactiveMap.fitToBounds()`.
374
+ *
375
+ * @typedef {Object} FitToBoundsOptions
369
376
  *
370
- * @property {(geojson: object, panelRect: DOMRect) => boolean} [isGeometryObscured]
371
- * Returns true if the geometry's screen bounding box overlaps the given panel element rectangle.
372
- * Used internally by useVisibleGeometry to decide whether to pan/zoom when a panel opens.
377
+ * @property {boolean} [preferPan]
378
+ * Leave the map where it is if the bounds are already inside the safe zone and not under an open
379
+ * panel. Otherwise pan to centre
380
+ * them in the safe zone, keeping the current zoom, and only zoom out if they don't fit.
381
+ *
382
+ * @property {number | { top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent]
383
+ * Space kept clear inside the safe zone, as a percentage of its size: left and right use its width,
384
+ * top and bottom its height. One value for every side, or set per side. Limited to 0–25. Defaults to 0.
373
385
  */
374
386
 
375
387
  /**
@@ -657,16 +669,25 @@
657
669
  * @property {PanelBreakpointConfig} mobile
658
670
  * Mobile breakpoint configuration.
659
671
  *
672
+ * @property {number | { top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent=0]
673
+ * Space to keep around `visibleGeometry`, as a percentage of the safe zone's size. See `FitToBoundsOptions`.
674
+ *
675
+ * @property {boolean} [preferPan=true]
676
+ * Whether to pan rather than zoom to show `visibleGeometry`. With `false`, the map fits the geometry when the panel opens over it.
677
+ *
660
678
  * @property {ComponentType} [render]
661
679
  * Render component.
662
680
  *
663
681
  * @property {PanelBreakpointConfig} tablet
664
682
  * Tablet breakpoint configuration.
665
683
  *
666
- * @property {object} [visibleGeometry]
667
- * GeoJSON Feature, FeatureCollection, or geometry to keep visible when this panel opens.
668
- * If any part of the geometry's bounding box is obscured by the safe zone after the panel opens,
669
- * the map automatically adjusts: Point or MultiPoint geometry routes to setView(), all other types to fitToBounds().
684
+ * @property {object | ((context: object) => object)} [visibleGeometry]
685
+ * GeoJSON Feature, FeatureCollection, or geometry to keep visible when this panel opens, or a function
686
+ * that receives the context and returns it, called each time the panel opens.
687
+ * If the panel opens over any part of the geometry's bounding box, the map pans to centre it in the safe zone, or zooms
688
+ * out to fit it if it doesn't fit at the current zoom. If it isn't under the panel, including when it's off screen, the
689
+ * map doesn't move.
690
+ * Applies to panels in the left, right and drawer slots. Panels in other slots, and modal panels, don't move the map.
670
691
  */
671
692
 
672
693
  /**
@@ -0,0 +1,73 @@
1
+ import { getBoxCenter, isInside, isOverlapping } from './screenBox.js'
2
+
3
+ const MAX_PADDING_PERCENT = 25
4
+ // Turns a percentage into a fraction
5
+ const PERCENT_DIVISOR = 100
6
+
7
+ const SIDES = ['top', 'right', 'bottom', 'left']
8
+
9
+ const clampPercent = (value) => Math.min(Math.max(Number(value) || 0, 0), MAX_PADDING_PERCENT)
10
+
11
+ /**
12
+ * Normalise `paddingPercent` into a percentage for each side, limited to 0–25.
13
+ *
14
+ * @param {number|{ top?: number, right?: number, bottom?: number, left?: number }} [paddingPercent]
15
+ * @returns {{ top: number, right: number, bottom: number, left: number }}
16
+ */
17
+ export const normalisePaddingPercent = (paddingPercent) => {
18
+ const isPerSide = paddingPercent !== null && typeof paddingPercent === 'object'
19
+ return Object.fromEntries(SIDES.map(side => [side, clampPercent(isPerSide ? paddingPercent[side] : paddingPercent)]))
20
+ }
21
+
22
+ /**
23
+ * How `fitToBounds` should move the map to show a box. Every value is in the same pixels as
24
+ * `box` and `safeZone`.
25
+ *
26
+ * Without `preferPan`, the box is always fitted. With it, a box already inside the safe zone and
27
+ * clear of every panel doesn't move, a box that fits inside the safe zone less its margin is
28
+ * centred there, and any other box is fitted. Panels are checked separately because the safe zone
29
+ * leaves out a panel too small to narrow it.
30
+ *
31
+ * @param {object} params
32
+ * @param {{ left: number, top: number, right: number, bottom: number }} [params.box] - The geometry's on-screen box. Needed with `preferPan`.
33
+ * @param {{ left: number, top: number, right: number, bottom: number }} params.safeZone - The safe zone's edges.
34
+ * @param {Array<{ left: number, top: number, right: number, bottom: number }>} [params.panels] - Open panels over the map.
35
+ * @param {boolean} [params.preferPan]
36
+ * @param {number|object} [params.paddingPercent] - Space kept clear inside the safe zone, as a percentage of its size.
37
+ * @returns {{ type: 'none' } | { type: 'pan', offset: [number, number] } | { type: 'fit', margin: { top: number, right: number, bottom: number, left: number } }}
38
+ */
39
+ export const getFitMove = ({ box, safeZone, panels = [], preferPan = false, paddingPercent }) => {
40
+ const percent = normalisePaddingPercent(paddingPercent)
41
+ const width = safeZone.right - safeZone.left
42
+ const height = safeZone.bottom - safeZone.top
43
+ const margin = {
44
+ top: height * percent.top / PERCENT_DIVISOR,
45
+ right: width * percent.right / PERCENT_DIVISOR,
46
+ bottom: height * percent.bottom / PERCENT_DIVISOR,
47
+ left: width * percent.left / PERCENT_DIVISOR
48
+ }
49
+
50
+ if (!preferPan) {
51
+ return { type: 'fit', margin }
52
+ }
53
+
54
+ if (isInside(box, safeZone) && !panels.some(panel => isOverlapping(box, panel))) {
55
+ return { type: 'none' }
56
+ }
57
+
58
+ const inner = {
59
+ left: safeZone.left + margin.left,
60
+ top: safeZone.top + margin.top,
61
+ right: safeZone.right - margin.right,
62
+ bottom: safeZone.bottom - margin.bottom
63
+ }
64
+ const isFitting = box.right - box.left <= inner.right - inner.left && box.bottom - box.top <= inner.bottom - inner.top
65
+
66
+ if (!isFitting) {
67
+ return { type: 'fit', margin }
68
+ }
69
+
70
+ const [boxX, boxY] = getBoxCenter(box)
71
+ const [innerX, innerY] = getBoxCenter(inner)
72
+ return { type: 'pan', offset: [boxX - innerX, boxY - innerY] }
73
+ }