@defra/interactive-map 0.0.58-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 (140) 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/docs/plugins/draw.md +95 -13
  14. package/package.json +1 -1
  15. package/plugins/datasets/dist/esm/index.js +1 -1
  16. package/plugins/datasets/dist/esm/layerAdapter.js +1 -1
  17. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  18. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  19. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  20. package/plugins/datasets/dist/umd/index.js +1 -1
  21. package/plugins/datasets/src/index.js +0 -1
  22. package/plugins/datasets/src/index.test.js +2 -8
  23. package/plugins/datasets/src/registry/dataset.test.js +9 -0
  24. package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
  25. package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
  26. package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
  27. package/plugins/draw/dist/esm/index.js +1 -1
  28. package/plugins/draw/dist/esm/liveDrawChecks.js +1 -1
  29. package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
  30. package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
  31. package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
  32. package/plugins/draw/dist/umd/index.js +1 -1
  33. package/plugins/draw/src/adapterEvents.js +2 -0
  34. package/plugins/draw/src/adapters/adapterContract.test.js +1 -0
  35. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +15 -3
  36. package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
  37. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.js +8 -19
  38. package/plugins/draw/src/adapters/maplibre/modes/editPointMode/pointerHandlers.test.js +20 -0
  39. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.js +6 -17
  40. package/plugins/draw/src/adapters/maplibre/modes/editVertexMode/pointerHandlers.test.js +19 -0
  41. package/plugins/draw/src/adapters/maplibre/utils/editModeEvents.js +29 -1
  42. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +13 -3
  43. package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +28 -0
  44. package/plugins/draw/src/api/addFeature.js +9 -6
  45. package/plugins/draw/src/api/addFeature.test.js +37 -10
  46. package/plugins/draw/src/api/editFeature.js +1 -1
  47. package/plugins/draw/src/api/recheckGeometry.js +1 -1
  48. package/plugins/draw/src/api/recheckGeometry.test.js +2 -2
  49. package/plugins/draw/src/api/setProperties.js +39 -0
  50. package/plugins/draw/src/api/setProperties.test.js +74 -0
  51. package/plugins/draw/src/api/setStyle.js +16 -19
  52. package/plugins/draw/src/api/setStyle.test.js +17 -2
  53. package/plugins/draw/src/api/split.js +7 -4
  54. package/plugins/draw/src/api/split.test.js +25 -2
  55. package/plugins/draw/src/events.js +95 -53
  56. package/plugins/draw/src/events.test.js +425 -6
  57. package/plugins/draw/src/manifest.js +6 -4
  58. package/plugins/draw/src/manifest.test.js +20 -26
  59. package/plugins/draw/src/reducer.js +7 -0
  60. package/plugins/draw/src/reducer.test.js +11 -0
  61. package/plugins/draw/src/utils/flattenStyleProperties.js +54 -2
  62. package/plugins/draw/src/utils/flattenStyleProperties.test.js +74 -1
  63. package/plugins/draw/src/utils/isPlainObject.js +14 -0
  64. package/plugins/draw/src/utils/isPlainObject.test.js +24 -0
  65. package/plugins/draw/src/utils/mergeFeatureProperties.js +45 -0
  66. package/plugins/draw/src/utils/mergeFeatureProperties.test.js +48 -0
  67. package/plugins/draw/src/utils/stripInternalProperties.js +3 -2
  68. package/plugins/draw/src/validation/commitChecks.js +29 -11
  69. package/plugins/draw/src/validation/commitChecks.test.js +67 -11
  70. package/plugins/draw/src/validation/validateGeometry.js +12 -5
  71. package/plugins/draw/src/validation/validateGeometry.test.js +18 -0
  72. package/plugins/interact/dist/esm/im-interact-plugin.js +1 -1
  73. package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
  74. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  75. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  76. package/plugins/search/dist/css/index.css +1 -1
  77. package/plugins/search/src/components/Form/Form.module.scss +1 -0
  78. package/providers/esri/dist/esm/im-esri-provider.js +1 -1
  79. package/providers/esri/src/esriProvider.js +10 -21
  80. package/providers/esri/src/utils/coords.js +37 -7
  81. package/providers/esri/src/utils/coords.test.js +22 -1
  82. package/providers/esri/src/utils/spatial.js +1 -47
  83. package/providers/maplibre/dist/esm/im-maplibre-provider.js +1 -1
  84. package/providers/maplibre/dist/umd/im-maplibre-provider.js +1 -1
  85. package/providers/maplibre/src/maplibreProvider.js +5 -14
  86. package/providers/maplibre/src/maplibreProvider.test.js +143 -146
  87. package/providers/maplibre/src/utils/spatial.js +0 -40
  88. package/providers/maplibre/src/utils/spatial.test.js +0 -35
  89. package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
  90. package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
  91. package/providers/openlayers/src/openlayersProvider.js +5 -14
  92. package/providers/openlayers/src/openlayersProvider.test.js +12 -10
  93. package/providers/openlayers/src/utils/spatial.js +0 -39
  94. package/providers/openlayers/src/utils/spatial.test.js +1 -26
  95. package/src/App/components/Hints/Hints.jsx +9 -21
  96. package/src/App/components/Hints/Hints.test.jsx +20 -33
  97. package/src/App/components/Panel/Panel.jsx +16 -1
  98. package/src/App/components/Panel/Panel.test.jsx +44 -0
  99. package/src/App/hooks/useCrossHairAPI.js +27 -28
  100. package/src/App/hooks/useCrossHairAPI.test.js +64 -7
  101. package/src/App/hooks/useLayoutMeasurements.js +175 -131
  102. package/src/App/hooks/useLayoutMeasurements.test.js +227 -122
  103. package/src/App/hooks/useMapProviderOverrides.js +114 -35
  104. package/src/App/hooks/useMapProviderOverrides.test.js +188 -5
  105. package/src/App/hooks/useModalPanelBehaviour.js +18 -32
  106. package/src/App/hooks/useModalPanelBehaviour.test.js +45 -33
  107. package/src/App/hooks/useVisibleGeometry.js +35 -45
  108. package/src/App/hooks/useVisibleGeometry.test.js +149 -121
  109. package/src/App/layout/Layout.jsx +4 -4
  110. package/src/App/layout/layout.module.scss +124 -61
  111. package/src/App/store/AppProvider.jsx +3 -0
  112. package/src/App/store/MapProvider.jsx +1 -0
  113. package/src/App/store/MapProvider.test.jsx +25 -0
  114. package/src/App/store/appDispatchMiddleware.js +18 -10
  115. package/src/App/store/appDispatchMiddleware.test.js +42 -4
  116. package/src/App/store/mapActionsMap.js +6 -0
  117. package/src/App/store/mapActionsMap.test.js +7 -0
  118. package/src/App/store/mapReducer.js +2 -0
  119. package/src/App/store/mapReducer.test.js +2 -0
  120. package/src/InteractiveMap/InteractiveMap.js +3 -2
  121. package/src/InteractiveMap/InteractiveMap.test.js +2 -2
  122. package/src/config/events.js +1 -1
  123. package/src/config/mapPanelSlots.js +8 -0
  124. package/src/config/symbolConfig.js +1 -1
  125. package/src/services/symbolRegistry.js +1 -1
  126. package/src/services/symbolRegistry.test.js +19 -0
  127. package/src/types.js +29 -8
  128. package/src/utils/getFitMove.js +73 -0
  129. package/src/utils/getFitMove.test.js +114 -0
  130. package/src/utils/getSafeZoneInset.js +122 -77
  131. package/src/utils/getSafeZoneInset.test.js +241 -54
  132. package/src/utils/getValueForStyle.js +10 -5
  133. package/src/utils/getValueForStyle.test.js +13 -2
  134. package/src/utils/renderedBoxes.js +25 -0
  135. package/src/utils/renderedBoxes.test.js +61 -0
  136. package/src/utils/screenBox.js +70 -0
  137. package/src/utils/screenBox.test.js +69 -0
  138. package/src/utils/symbolUtils.js +9 -9
  139. package/src/utils/symbolUtils.test.js +21 -0
  140. package/providers/esri/src/utils/spatial.test.js +0 -58
@@ -3,10 +3,84 @@ import { useConfig } from '../store/configContext.js'
3
3
  import { useApp } from '../store/appContext.js'
4
4
  import { useMap } from '../store/mapContext.js'
5
5
  import { EVENTS as events } from '../../config/events.js'
6
- import { getSafeZoneInset } from '../../utils/getSafeZoneInset.js'
6
+ import { getSafeZoneInset, getOpenPanelBoxes } from '../../utils/getSafeZoneInset.js'
7
7
  import { calculateLayout } from './useLayoutMeasurements.js'
8
8
  import { scalePoints } from '../../utils/scalePoints.js'
9
- import { scaleFactor } from '../../config/appConfig.js'
9
+ import { getMapSizeScale } from '../../config/mapSizeScales.js'
10
+ import { SIDE_PANEL_SLOT_REFS } from '../../config/mapPanelSlots.js'
11
+ import { getFitMove } from '../../utils/getFitMove.js'
12
+ import { getScreenBox, getBoxCenter } from '../../utils/screenBox.js'
13
+
14
+ const FIT_WITHOUT_MARGIN = { type: 'fit' }
15
+
16
+ // fitToBounds' second argument was the boolean skipPaddingCalc, still accepted
17
+ const toFitOptions = (options) => typeof options === 'boolean' ? { skipPaddingCalc: options } : (options ?? {})
18
+
19
+ const hasMargin = (margin) => Object.values(margin).some(value => value > 0)
20
+
21
+ // The open side panels, relative to the viewport. The safe zone leaves out small ones, so they
22
+ // are checked separately. Drawer panels are always within the safe zone.
23
+ const getPanelBoxes = (layoutRefs, viewport) => {
24
+ const origin = viewport.getBoundingClientRect()
25
+ return SIDE_PANEL_SLOT_REFS.flatMap(refName => getOpenPanelBoxes(layoutRefs[refName], origin))
26
+ }
27
+
28
+ // Turns a move worked out in CSS pixels into one the map can make. A pan becomes the coordinate to
29
+ // centre in the safe zone: whatever is that far from its centre now. A fit's margin goes into map
30
+ // pixels, which differ from CSS pixels at the larger map sizes.
31
+ const toMapMove = (move, { safeZone, mapProvider, scale }) => {
32
+ if (move.type === 'pan') {
33
+ const [offsetX, offsetY] = move.offset
34
+ const [safeZoneX, safeZoneY] = getBoxCenter(safeZone)
35
+ const center = mapProvider.screenToMap({ x: (safeZoneX + offsetX) / scale, y: (safeZoneY + offsetY) / scale })
36
+ return { type: 'pan', center }
37
+ }
38
+ if (move.type === 'fit') {
39
+ return hasMargin(move.margin) ? { type: 'fit', margin: scalePoints(move.margin, scale) } : FIT_WITHOUT_MARGIN
40
+ }
41
+ return move
42
+ }
43
+
44
+ // Works in CSS pixels relative to the viewport, where the safe zone is measured
45
+ const getMove = ({ bounds, safeZoneInset, mapProvider, layoutRefs, mapSize, preferPan, paddingPercent }) => {
46
+ const viewport = layoutRefs.viewportRef?.current
47
+ if (!safeZoneInset || !viewport || (!preferPan && !paddingPercent)) {
48
+ return FIT_WITHOUT_MARGIN
49
+ }
50
+
51
+ const scale = getMapSizeScale(mapSize)
52
+ const safeZone = {
53
+ left: safeZoneInset.left,
54
+ top: safeZoneInset.top,
55
+ right: viewport.offsetWidth - safeZoneInset.right,
56
+ bottom: viewport.offsetHeight - safeZoneInset.bottom
57
+ }
58
+ const box = preferPan ? getScreenBox(bounds, (coords) => mapProvider.mapToScreen(coords), scale) : undefined
59
+ const panels = preferPan ? getPanelBoxes(layoutRefs, viewport) : []
60
+ return toMapMove(getFitMove({ box, safeZone, panels, preferPan, paddingPercent }), { safeZone, mapProvider, scale })
61
+ }
62
+
63
+ // Forward public API events to the (overridden) mapProvider methods so that
64
+ // interactiveMap.fitToBounds() and interactiveMap.setView() respect safe zone padding.
65
+ // fitToBounds' options are passed through.
66
+ const useForwardMapEvents = (mapProvider, eventBus) => {
67
+ useEffect(() => {
68
+ if (!mapProvider || !eventBus) {
69
+ return undefined
70
+ }
71
+
72
+ const handleFitToBounds = (bounds, options) => mapProvider.fitToBounds(bounds, options)
73
+ const handleSetView = (opts) => mapProvider.setView(opts)
74
+
75
+ eventBus.on(events.MAP_FIT_TO_BOUNDS, handleFitToBounds)
76
+ eventBus.on(events.MAP_SET_VIEW, handleSetView)
77
+
78
+ return () => {
79
+ eventBus.off(events.MAP_FIT_TO_BOUNDS, handleFitToBounds)
80
+ eventBus.off(events.MAP_SET_VIEW, handleSetView)
81
+ }
82
+ }, [mapProvider, eventBus])
83
+ }
10
84
 
11
85
  export const useMapProviderOverrides = () => {
12
86
  const { mapProvider, eventBus } = useConfig()
@@ -18,24 +92,29 @@ export const useMapProviderOverrides = () => {
18
92
  const latestBreakpoint = useRef(breakpoint)
19
93
  latestBreakpoint.current = breakpoint
20
94
 
21
- const updatePadding = () => {
22
- // Refresh the derived layout CSS vars (e.g. --left-offset-top) before reading them —
23
- // callers often trigger this right after a state change that resizes the header/columns
24
- // (e.g. collapsing mobile search), and those vars are only otherwise refreshed by a
25
- // separate ResizeObserver callback that isn't guaranteed to have run yet.
95
+ // Refresh the derived layout CSS vars (e.g. --left-offset-top) before reading them —
96
+ // callers often trigger this right after a state change that resizes the header/columns
97
+ // (e.g. collapsing mobile search), and those vars are only otherwise refreshed by a
98
+ // separate ResizeObserver callback that isn't guaranteed to have run yet.
99
+ const measureSafeZone = () => {
26
100
  calculateLayout(layoutRefs, latestBreakpoint.current)
27
- const safeZoneInset = getSafeZoneInset(layoutRefs)
28
- const padding = scalePoints(safeZoneInset, scaleFactor[latestMapSize.current])
101
+ return getSafeZoneInset(layoutRefs)
102
+ }
29
103
 
104
+ // Stores the safe zone, which positions the crosshair, and sets it as the map's padding, which
105
+ // sets the map's centre, together so they always match. Only called just before the map moves.
106
+ const applySafeZone = (safeZoneInset) => {
107
+ if (!safeZoneInset) {
108
+ return
109
+ }
30
110
  if (typeof appDispatch === 'function') {
31
111
  appDispatch({
32
112
  type: 'SET_SAFE_ZONE_INSET',
33
113
  payload: { safeZoneInset, syncMapPadding: false }
34
114
  })
35
115
  }
36
-
37
116
  if (typeof mapProvider.setPadding === 'function') {
38
- mapProvider.setPadding(padding)
117
+ mapProvider.setPadding(scalePoints(safeZoneInset, getMapSizeScale(latestMapSize.current)))
39
118
  }
40
119
  }
41
120
 
@@ -45,12 +124,15 @@ export const useMapProviderOverrides = () => {
45
124
  }
46
125
 
47
126
  const originalFitToBounds = mapProvider.fitToBounds
127
+ const originalSetView = mapProvider.setView
48
128
 
49
- mapProvider.fitToBounds = (bounds, skipPaddingCalc = false) => {
129
+ mapProvider.fitToBounds = (bounds, options) => {
50
130
  if (!bounds) {
51
131
  return undefined
52
132
  }
53
133
 
134
+ const { skipPaddingCalc = false, preferPan = false, paddingPercent } = toFitOptions(options)
135
+
54
136
  if (skipPaddingCalc) {
55
137
  return originalFitToBounds.call(mapProvider, bounds)
56
138
  }
@@ -59,21 +141,35 @@ export const useMapProviderOverrides = () => {
59
141
  // form) immediately before calling this, so measuring on the same tick can capture
60
142
  // the DOM mid-transition. Wait a frame so React has painted first.
61
143
  requestAnimationFrame(() => {
62
- updatePadding()
63
- originalFitToBounds.call(mapProvider, bounds)
144
+ const safeZoneInset = measureSafeZone()
145
+ const move = getMove({ bounds, safeZoneInset, mapProvider, layoutRefs, mapSize: latestMapSize.current, preferPan, paddingPercent })
146
+
147
+ // Nothing moves, so the stored safe zone and the padding are left as they are, together.
148
+ // Changing the padding moves the map's centre, which would be announced as a move.
149
+ if (move.type === 'none') {
150
+ return
151
+ }
152
+
153
+ // Set the padding first, as the move uses it. The map is only redrawn once the move starts.
154
+ applySafeZone(safeZoneInset)
155
+ if (move.type === 'pan') {
156
+ originalSetView.call(mapProvider, { center: move.center })
157
+ } else if (move.margin) {
158
+ originalFitToBounds.call(mapProvider, bounds, { padding: move.margin })
159
+ } else {
160
+ originalFitToBounds.call(mapProvider, bounds)
161
+ }
64
162
  })
65
163
  return undefined
66
164
  }
67
165
 
68
- const originalSetView = mapProvider.setView
69
-
70
166
  mapProvider.setView = ({ center, zoom }) => {
71
167
  if (!center) {
72
168
  return undefined
73
169
  }
74
170
 
75
171
  requestAnimationFrame(() => {
76
- updatePadding()
172
+ applySafeZone(measureSafeZone())
77
173
  originalSetView.call(mapProvider, { center, zoom })
78
174
  })
79
175
  return undefined
@@ -85,22 +181,5 @@ export const useMapProviderOverrides = () => {
85
181
  }
86
182
  }, [mapProvider, appDispatch, layoutRefs, mapSize])
87
183
 
88
- // Forward public API events to the (overridden) mapProvider methods so that
89
- // interactiveMap.fitToBounds() and interactiveMap.setView() respect safe zone padding.
90
- useEffect(() => {
91
- if (!mapProvider || !eventBus) {
92
- return undefined
93
- }
94
-
95
- const handleFitToBounds = (bbox) => mapProvider.fitToBounds(bbox)
96
- const handleSetView = (opts) => mapProvider.setView(opts)
97
-
98
- eventBus.on(events.MAP_FIT_TO_BOUNDS, handleFitToBounds)
99
- eventBus.on(events.MAP_SET_VIEW, handleSetView)
100
-
101
- return () => {
102
- eventBus.off(events.MAP_FIT_TO_BOUNDS, handleFitToBounds)
103
- eventBus.off(events.MAP_SET_VIEW, handleSetView)
104
- }
105
- }, [mapProvider, eventBus])
184
+ useForwardMapEvents(mapProvider, eventBus)
106
185
  }
@@ -6,12 +6,14 @@ import { useMap } from '../store/mapContext.js'
6
6
  import { getSafeZoneInset } from '../../utils/getSafeZoneInset.js'
7
7
  import { calculateLayout } from './useLayoutMeasurements.js'
8
8
  import { scalePoints } from '../../utils/scalePoints.js'
9
- import { scaleFactor } from '../../config/appConfig.js'
10
9
 
11
10
  jest.mock('../store/configContext.js')
12
11
  jest.mock('../store/appContext.js')
13
12
  jest.mock('../store/mapContext.js')
14
- jest.mock('../../utils/getSafeZoneInset.js')
13
+ jest.mock('../../utils/getSafeZoneInset.js', () => ({
14
+ ...jest.requireActual('../../utils/getSafeZoneInset.js'),
15
+ getSafeZoneInset: jest.fn()
16
+ }))
15
17
  jest.mock('./useLayoutMeasurements.js')
16
18
  jest.mock('../../utils/scalePoints.js')
17
19
 
@@ -56,7 +58,7 @@ describe('useMapProviderOverrides', () => {
56
58
  expect(getSafeZoneInset).not.toHaveBeenCalled()
57
59
  })
58
60
 
59
- test('fitToBounds and setView call updatePadding with correct flow', () => {
61
+ test('fitToBounds and setView store the safe zone and set it as the padding, keeping the view still', () => {
60
62
  const { mapProvider, dispatch, layoutRefs } = setup({ map: { mapSize: 'lg' } })
61
63
  renderHook(() => useMapProviderOverrides())
62
64
 
@@ -67,7 +69,8 @@ describe('useMapProviderOverrides', () => {
67
69
 
68
70
  expect(calculateLayout).toHaveBeenCalledWith(layoutRefs, 'mobile')
69
71
  expect(getSafeZoneInset).toHaveBeenCalledWith(layoutRefs)
70
- expect(scalePoints).toHaveBeenCalledWith(safeZone, scaleFactor.lg)
72
+ // An unknown map size scales by 1
73
+ expect(scalePoints).toHaveBeenCalledWith(safeZone, 1)
71
74
  expect(dispatch).toHaveBeenCalledWith({
72
75
  type: 'SET_SAFE_ZONE_INSET',
73
76
  payload: { safeZoneInset: safeZone, syncMapPadding: false }
@@ -79,7 +82,7 @@ describe('useMapProviderOverrides', () => {
79
82
 
80
83
  expect(getSafeZoneInset).toHaveBeenCalledWith(layoutRefs)
81
84
  expect(dispatch).toHaveBeenCalled()
82
- expect(mapProvider.setPadding).toHaveBeenCalled()
85
+ expect(mapProvider.setPadding).toHaveBeenCalledWith(scaled)
83
86
  })
84
87
 
85
88
  test('fitToBounds skips padding calculation when skipPaddingCalc is true', () => {
@@ -149,6 +152,186 @@ describe('useMapProviderOverrides', () => {
149
152
  expect(mapProvider.fitToBounds).not.toBe(firstOverride)
150
153
  })
151
154
 
155
+ test('fitToBounds skips padding calculation when the skipPaddingCalc option is set', () => {
156
+ const { mapProvider, dispatch } = setup()
157
+ const originalFitBounds = mapProvider.fitToBounds
158
+ renderHook(() => useMapProviderOverrides())
159
+
160
+ mapProvider.fitToBounds([0, 0, 1, 1], { skipPaddingCalc: true })
161
+
162
+ expect(getSafeZoneInset).not.toHaveBeenCalled()
163
+ expect(dispatch).not.toHaveBeenCalled()
164
+ expect(originalFitBounds).toHaveBeenCalledWith([0, 0, 1, 1])
165
+ })
166
+
167
+ describe('preferPan and paddingPercent', () => {
168
+ // A 1000 × 800 viewport with a 400px panel on the left: the safe zone runs from
169
+ // (400, 0) to (1000, 800), so its centre is (700, 400)
170
+ const safeZoneInset = { top: 0, right: 0, bottom: 0, left: 400 }
171
+
172
+ // The viewport sits 20px from the top of the page, so panel rects are offset by it
173
+ const viewport = { offsetWidth: 1000, offsetHeight: 800, getBoundingClientRect: () => ({ left: 0, top: 20 }) }
174
+
175
+ const makePanel = (rect, { hidden = false } = {}) => {
176
+ const panel = document.createElement('div')
177
+ panel.className = 'im-c-panel'
178
+ panel.hidden = hidden
179
+ panel.getBoundingClientRect = () => rect
180
+ return panel
181
+ }
182
+
183
+ const makeSlot = (...children) => {
184
+ const slot = document.createElement('div')
185
+ children.forEach(child => slot.appendChild(child))
186
+ return { current: slot }
187
+ }
188
+
189
+ const setupWithViewport = (overrides = {}) => {
190
+ const result = setup({
191
+ app: { layoutRefs: { viewportRef: { current: viewport }, ...overrides.slots } },
192
+ map: { mapSize: 'small' },
193
+ ...overrides,
194
+ mapProvider: {
195
+ screenToMap: jest.fn(({ x, y }) => [x, y]),
196
+ mapToScreen: jest.fn(([x, y]) => ({ x, y })),
197
+ ...overrides.mapProvider
198
+ }
199
+ })
200
+ getSafeZoneInset.mockReturnValue(safeZoneInset)
201
+ scalePoints.mockImplementation(jest.requireActual('../../utils/scalePoints.js').scalePoints)
202
+ return { ...result, originalFitToBounds: result.mapProvider.fitToBounds, originalSetView: result.mapProvider.setView }
203
+ }
204
+
205
+ test('leaves the map alone when the bounds are already inside the safe zone', () => {
206
+ const { mapProvider, originalFitToBounds, dispatch, originalSetView } = setupWithViewport()
207
+ renderHook(() => useMapProviderOverrides())
208
+
209
+ mapProvider.fitToBounds([500, 100, 600, 200], { preferPan: true })
210
+
211
+ // The stored safe zone and the padding both stay as they were, so they still match
212
+ expect(dispatch).not.toHaveBeenCalled()
213
+ expect(originalSetView).not.toHaveBeenCalled()
214
+ expect(mapProvider.setPadding).not.toHaveBeenCalled()
215
+ expect(originalFitToBounds).not.toHaveBeenCalled()
216
+ })
217
+
218
+ test('pans bounds that fit to the centre of the safe zone, after setting the padding without moving the view', () => {
219
+ const { mapProvider, originalFitToBounds, dispatch, originalSetView } = setupWithViewport()
220
+ renderHook(() => useMapProviderOverrides())
221
+
222
+ // Centre (150, 400), behind the panel
223
+ mapProvider.fitToBounds([100, 300, 200, 500], { preferPan: true })
224
+
225
+ expect(dispatch).toHaveBeenCalledWith({ type: 'SET_SAFE_ZONE_INSET', payload: { safeZoneInset, syncMapPadding: false } })
226
+ expect(mapProvider.setPadding).toHaveBeenCalledWith(safeZoneInset)
227
+ expect(mapProvider.setPadding.mock.invocationCallOrder[0]).toBeLessThan(originalSetView.mock.invocationCallOrder[0])
228
+ expect(originalSetView).toHaveBeenCalledWith({ center: [150, 400] })
229
+ expect(originalFitToBounds).not.toHaveBeenCalled()
230
+ })
231
+
232
+ test('pans in map pixels at larger map sizes', () => {
233
+ const { mapProvider, originalSetView } = setupWithViewport({ map: { mapSize: 'large' } })
234
+ renderHook(() => useMapProviderOverrides())
235
+
236
+ // Map pixels (50, 200) are CSS pixels (100, 400), 600 left of the safe zone centre
237
+ mapProvider.fitToBounds([50, 200, 50, 200], { preferPan: true })
238
+
239
+ expect(originalSetView).toHaveBeenCalledWith({ center: [50, 200] })
240
+ })
241
+
242
+ test('pans bounds inside the safe zone but under a panel too small to narrow it', () => {
243
+ // A 300 × 100 panel at the top right, which the safe zone leaves out
244
+ const smallPanel = makePanel({ left: 700, top: 20, right: 1000, bottom: 120, width: 300, height: 100 })
245
+ const { mapProvider, originalSetView } = setupWithViewport({ slots: { rightTopRef: makeSlot(smallPanel) } })
246
+ renderHook(() => useMapProviderOverrides())
247
+
248
+ mapProvider.fitToBounds([800, 50, 800, 50], { preferPan: true })
249
+
250
+ expect(originalSetView).toHaveBeenCalledWith({ center: [800, 50] })
251
+ })
252
+
253
+ test('checks every open panel in a slot, skipping closed ones', () => {
254
+ const closedPanel = makePanel({ left: 0, top: 0, right: 0, bottom: 0, width: 0, height: 0 }, { hidden: true })
255
+ const openPanel = makePanel({ left: 700, top: 20, right: 1000, bottom: 120, width: 300, height: 100 })
256
+ const { mapProvider, originalSetView } = setupWithViewport({ slots: { rightTopRef: makeSlot(closedPanel, openPanel) } })
257
+ renderHook(() => useMapProviderOverrides())
258
+
259
+ mapProvider.fitToBounds([800, 50, 800, 50], { preferPan: true })
260
+
261
+ expect(originalSetView).toHaveBeenCalled()
262
+ })
263
+
264
+ test('ignores elements in a slot that aren\'t panels', () => {
265
+ const buttons = document.createElement('div')
266
+ buttons.getBoundingClientRect = () => ({ left: 700, top: 20, right: 1000, bottom: 120, width: 300, height: 100 })
267
+ const { mapProvider, originalSetView } = setupWithViewport({ slots: { rightTopRef: makeSlot(buttons) } })
268
+ renderHook(() => useMapProviderOverrides())
269
+
270
+ mapProvider.fitToBounds([800, 50, 800, 50], { preferPan: true })
271
+
272
+ expect(originalSetView).not.toHaveBeenCalled()
273
+ })
274
+
275
+ test('fits bounds too big for the safe zone, setting the padding first', () => {
276
+ const { mapProvider, originalFitToBounds, originalSetView } = setupWithViewport()
277
+ renderHook(() => useMapProviderOverrides())
278
+
279
+ mapProvider.fitToBounds([0, 0, 900, 100], { preferPan: true })
280
+
281
+ expect(originalSetView).not.toHaveBeenCalled()
282
+ expect(mapProvider.setPadding).toHaveBeenCalledWith(safeZoneInset)
283
+ expect(originalFitToBounds).toHaveBeenCalledWith([0, 0, 900, 100])
284
+ })
285
+
286
+ test('passes paddingPercent to the fit as extra padding, worked out from the safe zone', () => {
287
+ const { mapProvider, originalFitToBounds } = setupWithViewport()
288
+ renderHook(() => useMapProviderOverrides())
289
+
290
+ mapProvider.fitToBounds([0, 0, 900, 100], { paddingPercent: 10 })
291
+
292
+ expect(originalFitToBounds).toHaveBeenCalledWith([0, 0, 900, 100], { padding: { top: 80, right: 60, bottom: 80, left: 60 } })
293
+ })
294
+
295
+ test('passes the extra padding in map pixels at larger map sizes', () => {
296
+ const { mapProvider, originalFitToBounds } = setupWithViewport({ map: { mapSize: 'large' } })
297
+ renderHook(() => useMapProviderOverrides())
298
+
299
+ mapProvider.fitToBounds([0, 0, 900, 100], { paddingPercent: 10 })
300
+
301
+ expect(originalFitToBounds).toHaveBeenCalledWith([0, 0, 900, 100], { padding: { top: 40, right: 30, bottom: 40, left: 30 } })
302
+ })
303
+
304
+ test('passes no extra padding when paddingPercent is 0', () => {
305
+ const { mapProvider, originalFitToBounds } = setupWithViewport()
306
+ renderHook(() => useMapProviderOverrides())
307
+
308
+ mapProvider.fitToBounds([0, 0, 900, 100], { preferPan: true, paddingPercent: 0 })
309
+
310
+ expect(originalFitToBounds).toHaveBeenCalledWith([0, 0, 900, 100])
311
+ })
312
+
313
+ test('fits as before when the safe zone can\'t be measured', () => {
314
+ const { mapProvider, originalFitToBounds, originalSetView } = setupWithViewport()
315
+ renderHook(() => useMapProviderOverrides())
316
+ getSafeZoneInset.mockReturnValue(undefined)
317
+
318
+ mapProvider.fitToBounds([500, 100, 600, 200], { preferPan: true })
319
+
320
+ expect(originalSetView).not.toHaveBeenCalled()
321
+ expect(mapProvider.setPadding).not.toHaveBeenCalled()
322
+ expect(originalFitToBounds).toHaveBeenCalledWith([500, 100, 600, 200])
323
+ })
324
+
325
+ test('MAP_FIT_TO_BOUNDS event forwards the options', () => {
326
+ const { capturedHandlers, originalSetView } = setupWithViewport()
327
+ renderHook(() => useMapProviderOverrides())
328
+
329
+ capturedHandlers['map:fittobounds']([100, 300, 200, 500], { preferPan: true })
330
+
331
+ expect(originalSetView).toHaveBeenCalledWith({ center: [150, 400] })
332
+ })
333
+ })
334
+
152
335
  test('subscribes to MAP_FIT_TO_BOUNDS and MAP_SET_VIEW on eventBus', () => {
153
336
  const { eventBus } = setup()
154
337
  renderHook(() => useMapProviderOverrides())
@@ -4,21 +4,17 @@ import { constrainKeyboardFocus } from '../../utils/constrainKeyboardFocus.js'
4
4
  import { toggleInertElements } from '../../utils/toggleInertElements.js'
5
5
  import { useApp } from '../store/appContext.js'
6
6
 
7
- // Left/right slots reuse the layout CSS vars set by useLayoutMeasurements — no DOM measurement needed.
8
- // CSS var references resolve correctly at the panel element (inside .im-o-app) even though
9
- // --modal-inset is set on :root.
10
- const SLOT_MODAL_VARS = {
11
- 'left-top': { inset: 'var(--left-offset-top) auto auto var(--primary-gap)', maxHeight: 'var(--left-top-max-height)' },
12
- 'left-bottom': { inset: 'auto auto var(--left-offset-bottom) var(--primary-gap)', maxHeight: 'var(--left-top-max-height)' },
13
- 'right-top': { inset: 'var(--right-offset-top) var(--primary-gap) auto auto', maxHeight: 'var(--right-top-max-height)' },
14
- 'right-bottom': { inset: 'auto var(--primary-gap) var(--right-offset-bottom) auto', maxHeight: 'var(--right-top-max-height)' }
15
- }
7
+ // --modal-inset and --modal-max-height position modal panels next to a button or in a measured slot
8
+ // (see layout.module.scss). Left and right slot modals are placed by their own CSS rules instead,
9
+ // using the full height of the map, as only one modal shows at a time. The vars are set on the app
10
+ // container, so each map on a page keeps its own values.
16
11
 
17
12
  const MODAL_INSET = '--modal-inset'
18
13
  const MODAL_MAX_HEIGHT = '--modal-max-height'
19
14
 
20
- const setButtonCSSVar = (effectiveContainer, mainRef, dividerGap) => {
21
- const root = document.documentElement
15
+ // A divider gap separates the panel from its button, and it stops a primary gap short of the far
16
+ // edge of the map, as other modals do
17
+ const setButtonCSSVar = (appContainer, effectiveContainer, mainRef, dividerGap, primaryMargin) => {
22
18
  const mainRect = mainRef.current.getBoundingClientRect()
23
19
  const buttonRect = effectiveContainer.getBoundingClientRect()
24
20
  const isBottomSlot = !!effectiveContainer.closest('.im-o-app__left-bottom, .im-o-app__right-bottom')
@@ -30,22 +26,12 @@ const setButtonCSSVar = (effectiveContainer, mainRef, dividerGap) => {
30
26
  const insetLeft = isLeftSlot ? `${Math.round(buttonRect.right - mainRect.left + dividerGap)}px` : 'auto'
31
27
  const anchor = isBottomSlot ? Math.round(mainRect.bottom - buttonRect.bottom) : Math.round(buttonRect.top - mainRect.top)
32
28
 
33
- root.style.setProperty(MODAL_INSET, `${insetTop} ${insetRight} ${insetBottom} ${insetLeft}`)
34
- root.style.setProperty(MODAL_MAX_HEIGHT, `${mainRect.height - anchor - dividerGap}px`)
29
+ appContainer.style.setProperty(MODAL_INSET, `${insetTop} ${insetRight} ${insetBottom} ${insetLeft}`)
30
+ appContainer.style.setProperty(MODAL_MAX_HEIGHT, `${mainRect.height - anchor - primaryMargin}px`)
35
31
  }
36
32
 
37
- const setSlotCSSVar = (slot, layoutRefs, primaryMargin) => {
38
- const root = document.documentElement
39
-
40
- // Left/right slots: delegate entirely to existing layout CSS vars
41
- const mapped = SLOT_MODAL_VARS[slot]
42
- if (mapped) {
43
- root.style.setProperty(MODAL_INSET, mapped.inset)
44
- root.style.setProperty(MODAL_MAX_HEIGHT, mapped.maxHeight)
45
- return
46
- }
47
-
48
- // Other slots (e.g. inset): measure position from DOM
33
+ const setSlotCSSVar = (appContainer, slot, layoutRefs, primaryMargin) => {
34
+ // Single-part slots (e.g. inset): measure position from DOM. Left and right slots have no ref here
49
35
  const refKey = `${slot[0].toLowerCase() + slot.slice(1)}Ref` // single-part slots only
50
36
  const slotRef = layoutRefs[refKey]?.current
51
37
  const mainContainer = layoutRefs.mainRef?.current
@@ -58,8 +44,8 @@ const setSlotCSSVar = (slot, layoutRefs, primaryMargin) => {
58
44
  const relLeft = slotRect.left - mainRect.left
59
45
  const relTop = slotRect.top - mainRect.top
60
46
 
61
- root.style.setProperty(MODAL_INSET, `${relTop}px auto auto ${relLeft}px`)
62
- root.style.setProperty(MODAL_MAX_HEIGHT, `${mainRect.height - relTop - primaryMargin}px`)
47
+ appContainer.style.setProperty(MODAL_INSET, `${relTop}px auto auto ${relLeft}px`)
48
+ appContainer.style.setProperty(MODAL_MAX_HEIGHT, `${mainRect.height - relTop - primaryMargin}px`)
63
49
  }
64
50
 
65
51
  // Computes and applies --modal-inset/--modal-max-height for the panel's current slot. Shared by
@@ -67,12 +53,12 @@ const setSlotCSSVar = (slot, layoutRefs, primaryMargin) => {
67
53
  // open-triggered layout effect below (panels stay permanently mounted — see mapPanels.js — so
68
54
  // opening one doesn't itself resize mainRef and wouldn't otherwise trigger a recalc).
69
55
  const recalcModalPosition = ({ isModal, mainRef, panelRef, buttonContainerEl, layoutRefs }) => {
70
- if (!isModal || !mainRef.current) {
56
+ const appContainer = layoutRefs.appContainerRef?.current
57
+ if (!isModal || !mainRef.current || !appContainer) {
71
58
  return
72
59
  }
73
60
 
74
- const root = document.documentElement
75
- const styles = getComputedStyle(root)
61
+ const styles = getComputedStyle(document.documentElement)
76
62
  const dividerGap = Number.parseInt(styles.getPropertyValue('--divider-gap'), 10)
77
63
  const primaryMargin = Number.parseInt(styles.getPropertyValue('--primary-gap'), 10)
78
64
  const slot = panelRef.current.dataset.slot
@@ -92,12 +78,12 @@ const recalcModalPosition = ({ isModal, mainRef, panelRef, buttonContainerEl, la
92
78
  return
93
79
  }
94
80
 
95
- setButtonCSSVar(effectiveContainer, mainRef, dividerGap)
81
+ setButtonCSSVar(appContainer, effectiveContainer, mainRef, dividerGap, primaryMargin)
96
82
  return
97
83
  }
98
84
 
99
85
  // Slot-based panels: derive position from the slot container element
100
- setSlotCSSVar(slot, layoutRefs, primaryMargin)
86
+ setSlotCSSVar(appContainer, slot, layoutRefs, primaryMargin)
101
87
  }
102
88
 
103
89
  const useModalKeyHandler = (panelRef, isModal, handleClose) => {