@defra/interactive-map 0.0.60-alpha → 0.0.62-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.
- package/dist/css/index.css +1 -1
- package/dist/esm/im-core.js +1 -1
- package/dist/esm/im-shell.js +1 -1
- package/dist/umd/im-core.js +1 -1
- package/dist/umd/index.js +1 -1
- package/docs/api/button-definition.md +11 -0
- package/docs/api/context.md +2 -0
- package/docs/api.md +20 -0
- package/docs/plugins/draw.md +26 -0
- package/package.json +1 -1
- package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
- package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
- package/plugins/datasets/src/reducers/datasetsToMenu.js +12 -9
- package/plugins/datasets/src/reducers/datasetsToMenu.test.js +42 -0
- package/plugins/datasets/src/reducers/pluginState.test.js +12 -0
- package/plugins/draw/dist/esm/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/esm/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ml-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-ol-adapter.js +1 -1
- package/plugins/draw/dist/umd/im-draw-plugin.js +1 -1
- package/plugins/draw/dist/umd/index.js +1 -1
- package/plugins/draw/src/DrawInit.jsx +3 -0
- package/plugins/draw/src/DrawInit.test.jsx +16 -0
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.js +19 -3
- package/plugins/draw/src/adapters/maplibre/MaplibreDrawAdapter.test.js +27 -0
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.js +10 -2
- package/plugins/draw/src/adapters/openlayers/OLDrawAdapter.test.js +14 -0
- package/plugins/draw/src/defaults.js +4 -0
- package/plugins/draw/src/hooks/useSnapMinZoom.js +29 -0
- package/plugins/draw/src/hooks/useSnapMinZoom.test.js +82 -0
- package/plugins/draw/src/manifest.js +4 -1
- package/plugins/draw/src/manifest.test.js +15 -2
- package/plugins/draw/src/utils/snapMinZoom.js +8 -0
- package/plugins/draw/src/utils/snapMinZoom.test.js +22 -0
- package/plugins/interact/dist/umd/im-interact-plugin.js +1 -1
- package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
- package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
- package/plugins/map-key/src/manifest.js +1 -1
- package/providers/openlayers/dist/esm/im-openlayers-provider.js +1 -1
- package/providers/openlayers/dist/umd/im-openlayers-provider.js +1 -1
- package/providers/openlayers/src/openlayersProvider.js +2 -1
- package/providers/openlayers/src/openlayersProvider.test.js +1 -1
- package/src/App/components/MapButton/MapButton.jsx +20 -2
- package/src/App/components/MapButton/MapButton.module.scss +15 -1
- package/src/App/components/MapButton/MapButton.test.jsx +21 -3
- package/src/App/components/MapControls/MapControls.jsx +2 -1
- package/src/App/components/MapControls/MapControls.test.jsx +1 -1
- package/src/App/components/PopupMenu/PopupMenu.test.jsx +22 -0
- package/src/App/components/PopupMenu/usePopupMenu.js +9 -6
- package/src/App/components/Tooltip/Tooltip.jsx +29 -11
- package/src/App/components/Tooltip/Tooltip.test.jsx +45 -3
- package/src/App/components/Viewport/MapController.jsx +4 -0
- package/src/App/components/Viewport/MapController.test.jsx +6 -0
- package/src/App/hooks/useMapProviderOverrides.js +10 -5
- package/src/App/hooks/useMapProviderOverrides.test.js +11 -0
- package/src/App/hooks/useResetViewTarget.js +135 -0
- package/src/App/hooks/useResetViewTarget.test.js +211 -0
- package/src/App/hooks/useUserMoveTracking.js +72 -0
- package/src/App/hooks/useUserMoveTracking.test.js +117 -0
- package/src/App/renderer/mapButtons.js +3 -1
- package/src/App/renderer/mapButtons.test.js +20 -0
- package/src/App/store/ServiceProvider.jsx +8 -2
- package/src/App/store/ServiceProvider.test.jsx +13 -0
- package/src/App/store/mapActionsMap.js +16 -1
- package/src/App/store/mapActionsMap.test.js +17 -0
- package/src/App/store/mapReducer.js +6 -0
- package/src/App/store/mapReducer.test.js +3 -0
- package/src/InteractiveMap/InteractiveMap.js +8 -0
- package/src/InteractiveMap/InteractiveMap.test.js +5 -0
- package/src/config/appConfig.js +17 -0
- package/src/config/appConfig.test.js +28 -0
- package/src/config/defaults.js +1 -0
- package/src/config/events.js +2 -0
- package/src/scss/settings/_colors.scss +4 -0
- package/src/services/mapFocus.js +3 -2
- package/src/services/mapFocus.test.js +10 -0
- package/src/types.js +9 -0
- package/src/utils/focusAfterPanelClose.js +3 -1
- package/src/utils/getFocusOptions.js +3 -0
- package/src/utils/getFocusOptions.test.js +11 -0
- package/src/utils/mapStateSync.js +1 -0
- package/src/utils/resetView.js +61 -0
- package/src/utils/resetView.test.js +62 -0
|
@@ -110,7 +110,8 @@ export default class OpenLayersProvider extends MapProvider {
|
|
|
110
110
|
|
|
111
111
|
if (bounds) {
|
|
112
112
|
map.once('rendercomplete', () => {
|
|
113
|
-
|
|
113
|
+
// Fits inside the view's padding, as fitToBounds does
|
|
114
|
+
view.fit(bounds, { duration: 0 })
|
|
114
115
|
})
|
|
115
116
|
}
|
|
116
117
|
|
|
@@ -164,7 +164,7 @@ describe('OpenLayersProvider', () => {
|
|
|
164
164
|
await provider.initMap({ ...defaultInitConfig, bounds })
|
|
165
165
|
expect(mockMapOnce).toHaveBeenCalledWith('rendercomplete', expect.any(Function))
|
|
166
166
|
renderCallback()
|
|
167
|
-
expect(mockViewFit).toHaveBeenCalledWith(bounds, {
|
|
167
|
+
expect(mockViewFit).toHaveBeenCalledWith(bounds, { duration: 0 })
|
|
168
168
|
})
|
|
169
169
|
|
|
170
170
|
it('does not call map.once when no bounds', async () => {
|
|
@@ -114,6 +114,21 @@ const getControlledElement = ({ idPrefix, panelId, buttonId, hasMenu, panelRole
|
|
|
114
114
|
return null
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* The button's tooltip: its label when the label isn't shown, and its description when it has one.
|
|
119
|
+
* @param {Object} options - Configuration options
|
|
120
|
+
* @param {string} options.label - The button's label
|
|
121
|
+
* @param {string} [options.description] - The button's description
|
|
122
|
+
* @param {boolean} [options.showLabel] - Whether the label is shown
|
|
123
|
+
* @returns {{ label?: string, description?: string }|null} The tooltip, or null when it would be empty
|
|
124
|
+
*/
|
|
125
|
+
const getTooltip = ({ label, description, showLabel }) => {
|
|
126
|
+
if (showLabel && !description) {
|
|
127
|
+
return null
|
|
128
|
+
}
|
|
129
|
+
return { label: showLabel ? undefined : label, description }
|
|
130
|
+
}
|
|
131
|
+
|
|
117
132
|
/**
|
|
118
133
|
* Builds the complete props object for a button or anchor element.
|
|
119
134
|
* Handles ARIA attributes, event handlers, refs, and conditional styling.
|
|
@@ -211,6 +226,7 @@ const buildButtonProps = ({
|
|
|
211
226
|
* @param {string} [props.href] - URL for anchor element; if provided, renders as <a> instead of <button>
|
|
212
227
|
* @param {string} [props.ariaControls] - Explicit aria-controls target id for buttons that toggle
|
|
213
228
|
* inline UI which is neither a panel nor a popup (a panelId/menuItems always takes precedence).
|
|
229
|
+
* @param {string} [props.description] - Extra information shown as the tooltip and read by screen readers after the label
|
|
214
230
|
* @returns {JSX.Element} The rendered button component
|
|
215
231
|
*/
|
|
216
232
|
export const MapButton = ({
|
|
@@ -232,7 +248,8 @@ export const MapButton = ({
|
|
|
232
248
|
menuItems,
|
|
233
249
|
idPrefix,
|
|
234
250
|
href,
|
|
235
|
-
ariaControls
|
|
251
|
+
ariaControls,
|
|
252
|
+
description
|
|
236
253
|
}) => {
|
|
237
254
|
const { id: appId } = useConfig()
|
|
238
255
|
const { buttonRefs } = useApp()
|
|
@@ -254,6 +271,7 @@ export const MapButton = ({
|
|
|
254
271
|
const showIcon = iconId || iconSvgContent || hasMenu
|
|
255
272
|
const buttonSlot = getButtonSlot(panelId, buttonId)
|
|
256
273
|
const controlledElement = getControlledElement({ idPrefix, panelId, buttonId, hasMenu, panelRole })
|
|
274
|
+
const tooltip = getTooltip({ label, description, showLabel })
|
|
257
275
|
|
|
258
276
|
/**
|
|
259
277
|
* Handles button click events.
|
|
@@ -314,7 +332,7 @@ export const MapButton = ({
|
|
|
314
332
|
data-button-slot={buttonSlot}
|
|
315
333
|
hidden={isHidden}
|
|
316
334
|
>
|
|
317
|
-
{
|
|
335
|
+
{tooltip ? <Tooltip label={tooltip.label} description={tooltip.description}>{buttonEl}</Tooltip> : buttonEl}
|
|
318
336
|
{buttonSlot && <SlotRenderer slot={buttonSlot} />}
|
|
319
337
|
{/* Mounted permanently, not just while open, so its aria-controls id stays a stable node. */}
|
|
320
338
|
{controlledElement?.type === 'popup' && (
|
|
@@ -184,7 +184,7 @@
|
|
|
184
184
|
transform: rotate(180deg);
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
.im-c-map-button[aria-disabled="true"]:not(.im-c-map-button--primary):not(.im-c-map-button--tertiary) {
|
|
187
|
+
.im-c-map-button[aria-disabled="true"]:not(.im-c-map-button--primary):not(.im-c-map-button--tertiary):not([aria-pressed="true"]) {
|
|
188
188
|
svg, span {
|
|
189
189
|
opacity: var(--disabled-button-opacity);
|
|
190
190
|
}
|
|
@@ -197,6 +197,20 @@
|
|
|
197
197
|
background-color: var(--pressed-button-background-color);
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
+
// Pressed and disabled fades the fill rather than the icon and label, which would leave dim
|
|
201
|
+
// content on a full-strength pressed fill. Doubled class to outrank the slot-specific pressed
|
|
202
|
+
// rules and hover.
|
|
203
|
+
.im-c-map-button.im-c-map-button[aria-pressed="true"][aria-disabled="true"] {
|
|
204
|
+
color: var(--disabled-pressed-button-foreground-color);
|
|
205
|
+
border-color: var(--disabled-pressed-button-background-color);
|
|
206
|
+
background-color: var(--disabled-pressed-button-background-color);
|
|
207
|
+
|
|
208
|
+
@media (-ms-high-contrast:active), screen and (forced-colors: active) {
|
|
209
|
+
color: GrayText;
|
|
210
|
+
border-color: GrayText;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
200
214
|
// 5. Responsive tweaks
|
|
201
215
|
|
|
202
216
|
.im-o-app--tablet, .im-o-app--desktop {
|
|
@@ -7,8 +7,8 @@ jest.mock('../../../utils/stringToKebab', () => ({
|
|
|
7
7
|
}))
|
|
8
8
|
|
|
9
9
|
jest.mock('../Tooltip/Tooltip', () => ({
|
|
10
|
-
Tooltip: ({
|
|
11
|
-
<div data-testid='tooltip' data-
|
|
10
|
+
Tooltip: ({ label, description, children }) => (
|
|
11
|
+
<div data-testid='tooltip' data-label={label} data-description={description}>{children}</div>
|
|
12
12
|
)
|
|
13
13
|
}))
|
|
14
14
|
|
|
@@ -68,7 +68,13 @@ describe('MapButton', () => {
|
|
|
68
68
|
|
|
69
69
|
it('wraps in Tooltip when showLabel is false', () => {
|
|
70
70
|
renderButton({ showLabel: false })
|
|
71
|
-
expect(screen.getByTestId('tooltip')).toHaveAttribute('data-
|
|
71
|
+
expect(screen.getByTestId('tooltip')).toHaveAttribute('data-label', 'Label')
|
|
72
|
+
expect(screen.getByTestId('tooltip')).not.toHaveAttribute('data-description')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('has no Tooltip when the label is shown and there\'s no description', () => {
|
|
76
|
+
renderButton({ showLabel: true })
|
|
77
|
+
expect(screen.queryByTestId('tooltip')).toBeNull()
|
|
72
78
|
})
|
|
73
79
|
|
|
74
80
|
it('hides wrapper when isHidden', () => {
|
|
@@ -309,4 +315,16 @@ describe('MapButton', () => {
|
|
|
309
315
|
expect(screen.queryByRole('img', { hidden: true })).toBeNull()
|
|
310
316
|
expect(screen.queryByTestId('icon')).toBeNull()
|
|
311
317
|
})
|
|
318
|
+
|
|
319
|
+
it('shows the label and description as the tooltip when the label isn\'t shown', () => {
|
|
320
|
+
renderButton({ showLabel: false, description: 'Zoom in to use snap' })
|
|
321
|
+
expect(screen.getByTestId('tooltip')).toHaveAttribute('data-label', 'Label')
|
|
322
|
+
expect(screen.getByTestId('tooltip')).toHaveAttribute('data-description', 'Zoom in to use snap')
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('shows only the description as the tooltip when the label is shown', () => {
|
|
326
|
+
renderButton({ showLabel: true, description: 'Zoom in to use snap' })
|
|
327
|
+
expect(screen.getByTestId('tooltip')).not.toHaveAttribute('data-label')
|
|
328
|
+
expect(screen.getByTestId('tooltip')).toHaveAttribute('data-description', 'Zoom in to use snap')
|
|
329
|
+
})
|
|
312
330
|
})
|
|
@@ -5,6 +5,7 @@ import { useConfig } from '../../store/configContext.js'
|
|
|
5
5
|
import { useMap } from '../../store/mapContext.js'
|
|
6
6
|
import { useService } from '../../store/serviceContext.js'
|
|
7
7
|
import { resolveStepAmount } from '../../../utils/resolveNudgeStep.js'
|
|
8
|
+
import { getFocusOptions } from '../../../utils/getFocusOptions.js'
|
|
8
9
|
|
|
9
10
|
const DIRECTIONS = [
|
|
10
11
|
{ id: 'panUp', verb: 'up', dx: 0, dy: -1, key: 'ArrowUp' },
|
|
@@ -52,7 +53,7 @@ export const MapControls = () => {
|
|
|
52
53
|
// arrow keys right after without an extra click into the map first.
|
|
53
54
|
const returnFocusToViewport = () => {
|
|
54
55
|
if (interfaceType !== 'keyboard') {
|
|
55
|
-
requestAnimationFrame(() => layoutRefs.viewportRef.current?.focus())
|
|
56
|
+
requestAnimationFrame(() => layoutRefs.viewportRef.current?.focus(getFocusOptions(interfaceType)))
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
59
|
|
|
@@ -180,7 +180,7 @@ describe('MapControls', () => {
|
|
|
180
180
|
render(<MapControls />)
|
|
181
181
|
|
|
182
182
|
fireEvent.click(screen.getByRole('button', { name: 'Move right' }))
|
|
183
|
-
expect(appState.layoutRefs.viewportRef.current.focus).
|
|
183
|
+
expect(appState.layoutRefs.viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll: true })
|
|
184
184
|
})
|
|
185
185
|
|
|
186
186
|
it('keeps focus on the button when driven by keyboard, so repeated Enter/Space presses do not require re-tabbing', () => {
|
|
@@ -251,6 +251,28 @@ describe('PopupMenu', () => {
|
|
|
251
251
|
expect(focusSpy).toHaveBeenCalled()
|
|
252
252
|
})
|
|
253
253
|
|
|
254
|
+
it.each([
|
|
255
|
+
['pointer', true],
|
|
256
|
+
['keyboard', false]
|
|
257
|
+
])('click on item focuses viewport with preventScroll for %s users', (interfaceType, preventScroll) => {
|
|
258
|
+
mockUseApp.interfaceType = interfaceType
|
|
259
|
+
renderMenu()
|
|
260
|
+
fireEvent.click(screen.getByText('Item 1'))
|
|
261
|
+
expect(mockUseApp.layoutRefs.viewportRef.current.focus).toHaveBeenCalledWith({ preventScroll })
|
|
262
|
+
delete mockUseApp.interfaceType
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it('click on item with keepFocus passes preventScroll to the instigator for pointer users', () => {
|
|
266
|
+
const focusSpy = jest.fn()
|
|
267
|
+
mockUseApp.buttonRefs.current.instigator.focus = focusSpy
|
|
268
|
+
mockUseApp.buttonConfig = { item1: { keepFocus: true } }
|
|
269
|
+
mockUseApp.interfaceType = 'pointer'
|
|
270
|
+
renderMenu()
|
|
271
|
+
fireEvent.click(screen.getByText('Item 1'))
|
|
272
|
+
expect(focusSpy).toHaveBeenCalledWith({ preventScroll: true })
|
|
273
|
+
delete mockUseApp.interfaceType
|
|
274
|
+
})
|
|
275
|
+
|
|
254
276
|
it('calls buttonConfig.onClick with evaluateProp if defined', () => {
|
|
255
277
|
const mockOnClick = jest.fn()
|
|
256
278
|
mockUseApp.buttonConfig = { item2: { onClick: mockOnClick } }
|
|
@@ -2,6 +2,7 @@ import { useState, useMemo, useEffect } from 'react'
|
|
|
2
2
|
import { stringToKebab } from '../../../utils/stringToKebab.js'
|
|
3
3
|
import { findTabStop } from '../../../utils/findNextTabStop.js'
|
|
4
4
|
import { useApp } from '../../store/appContext.js'
|
|
5
|
+
import { getFocusOptions } from '../../../utils/getFocusOptions.js'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Computes the position and alignment style for the popup menu based on the
|
|
@@ -95,12 +96,13 @@ const resolveItemFocus = (item, buttonConfig) => ({
|
|
|
95
96
|
*
|
|
96
97
|
* @param {HTMLElement|null} instigator - DOM node of the trigger button, may no longer exist.
|
|
97
98
|
* @param {object} viewportRef - Ref to the map viewport element, used as fallback.
|
|
99
|
+
* @param {object} [focusOptions] - Options passed to focus(), e.g. preventScroll.
|
|
98
100
|
*/
|
|
99
|
-
const focusInstigatorOrViewport = (instigator, viewportRef) => {
|
|
101
|
+
const focusInstigatorOrViewport = (instigator, viewportRef, focusOptions) => {
|
|
100
102
|
if (instigator) {
|
|
101
|
-
instigator.focus()
|
|
103
|
+
instigator.focus(focusOptions)
|
|
102
104
|
} else {
|
|
103
|
-
viewportRef.current?.focus()
|
|
105
|
+
viewportRef.current?.focus(focusOptions)
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
108
|
|
|
@@ -284,7 +286,7 @@ export const usePopupMenu = ({
|
|
|
284
286
|
items, hiddenButtons, startIndex, startPos, instigator, instigatorKey,
|
|
285
287
|
buttonRefs, buttonConfig, disabledButtons, pluginId, evaluateProp, id, menuRef, setIsOpen, buttonRect, isOpen
|
|
286
288
|
}) => {
|
|
287
|
-
const { dispatch, layoutRefs } = useApp()
|
|
289
|
+
const { dispatch, layoutRefs, interfaceType } = useApp()
|
|
288
290
|
const viewportRef = layoutRefs.viewportRef
|
|
289
291
|
|
|
290
292
|
const visibleIndices = useMemo(() => {
|
|
@@ -324,10 +326,11 @@ export const usePopupMenu = ({
|
|
|
324
326
|
}
|
|
325
327
|
setIsOpen(false)
|
|
326
328
|
activateItem(event, item, activateCtx)
|
|
329
|
+
const focusOptions = getFocusOptions(interfaceType)
|
|
327
330
|
if (keepFocus) {
|
|
328
|
-
focusInstigatorOrViewport(instigator, viewportRef)
|
|
331
|
+
focusInstigatorOrViewport(instigator, viewportRef, focusOptions)
|
|
329
332
|
} else {
|
|
330
|
-
viewportRef.current?.focus()
|
|
333
|
+
viewportRef.current?.focus(focusOptions)
|
|
331
334
|
}
|
|
332
335
|
}
|
|
333
336
|
|
|
@@ -6,9 +6,19 @@ let tooltipIdCounter = 0
|
|
|
6
6
|
const showDelay = 500
|
|
7
7
|
const hideDelay = 0
|
|
8
8
|
|
|
9
|
+
// The label names the trigger (aria-labelledby) and the description describes it (aria-describedby).
|
|
10
|
+
// With both, they're shown together, each in its own element so neither is read twice.
|
|
11
|
+
const TooltipText = ({ tooltipId, label, description }) => (
|
|
12
|
+
<>
|
|
13
|
+
{label && <span id={`${tooltipId}-label`}>{label}</span>}
|
|
14
|
+
{label && description && ': '}
|
|
15
|
+
{description && <span id={`${tooltipId}-description`}>{description}</span>}
|
|
16
|
+
</>
|
|
17
|
+
)
|
|
18
|
+
|
|
9
19
|
// eslint-disable-next-line camelcase, react/jsx-pascal-case
|
|
10
20
|
// sonarjs/disable-next-line function-name
|
|
11
|
-
export const Tooltip = ({ children,
|
|
21
|
+
export const Tooltip = ({ children, label, description }) => {
|
|
12
22
|
const { interfaceType } = useApp()
|
|
13
23
|
const triggerRef = useRef(null)
|
|
14
24
|
const tooltipRef = useRef(null)
|
|
@@ -76,23 +86,31 @@ export const Tooltip = ({ children, content }) => {
|
|
|
76
86
|
}
|
|
77
87
|
}
|
|
78
88
|
|
|
89
|
+
// Runs the trigger's own handler after the tooltip's, so the trigger keeps its own behaviour
|
|
90
|
+
// (such as a menu button's keyboard handling)
|
|
91
|
+
const withChildHandler = (name, handler) => (event) => {
|
|
92
|
+
handler()
|
|
93
|
+
children.props?.[name]?.(event)
|
|
94
|
+
}
|
|
95
|
+
|
|
79
96
|
const childWithProps = cloneElement(children, {
|
|
80
97
|
ref: mergeRefs,
|
|
81
|
-
onMouseEnter: show,
|
|
82
|
-
onMouseLeave: hide,
|
|
83
|
-
onMouseDown: cancel,
|
|
84
|
-
onKeyDown: cancel,
|
|
85
|
-
onFocus: () => {
|
|
98
|
+
onMouseEnter: withChildHandler('onMouseEnter', show),
|
|
99
|
+
onMouseLeave: withChildHandler('onMouseLeave', hide),
|
|
100
|
+
onMouseDown: withChildHandler('onMouseDown', cancel),
|
|
101
|
+
onKeyDown: withChildHandler('onKeyDown', cancel),
|
|
102
|
+
onFocus: withChildHandler('onFocus', () => {
|
|
86
103
|
if (interfaceType === 'keyboard') {
|
|
87
104
|
show()
|
|
88
105
|
}
|
|
89
106
|
setFocused(true)
|
|
90
|
-
},
|
|
91
|
-
onBlur: () => {
|
|
107
|
+
}),
|
|
108
|
+
onBlur: withChildHandler('onBlur', () => {
|
|
92
109
|
hide()
|
|
93
110
|
setFocused(false)
|
|
94
|
-
},
|
|
95
|
-
'aria-labelledby': tooltipId
|
|
111
|
+
}),
|
|
112
|
+
'aria-labelledby': label ? `${tooltipId}-label` : undefined,
|
|
113
|
+
'aria-describedby': description ? `${tooltipId}-description` : undefined
|
|
96
114
|
})
|
|
97
115
|
|
|
98
116
|
return (
|
|
@@ -105,7 +123,7 @@ export const Tooltip = ({ children, content }) => {
|
|
|
105
123
|
role='tooltip'
|
|
106
124
|
aria-hidden={!visible}
|
|
107
125
|
>
|
|
108
|
-
{
|
|
126
|
+
<TooltipText tooltipId={tooltipId} label={label} description={description} />
|
|
109
127
|
</div>
|
|
110
128
|
</div>
|
|
111
129
|
)
|
|
@@ -18,7 +18,7 @@ describe('Tooltip', () => {
|
|
|
18
18
|
getTooltipPosition.mockReturnValue('bottom')
|
|
19
19
|
|
|
20
20
|
const { container, unmount } = render(
|
|
21
|
-
<Tooltip
|
|
21
|
+
<Tooltip label='Test tooltip' {...props}>
|
|
22
22
|
<Child />
|
|
23
23
|
</Tooltip>
|
|
24
24
|
)
|
|
@@ -36,6 +36,48 @@ describe('Tooltip', () => {
|
|
|
36
36
|
afterEach(cleanup)
|
|
37
37
|
|
|
38
38
|
// --- Standard Tooltip tests ---
|
|
39
|
+
it('names the trigger with the label', () => {
|
|
40
|
+
const { button, tooltip } = renderTooltip()
|
|
41
|
+
expect(tooltip).toHaveTextContent('Test tooltip')
|
|
42
|
+
expect(button).toHaveAccessibleName('Test tooltip')
|
|
43
|
+
expect(button).not.toHaveAttribute('aria-describedby')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('describes the trigger with the description, without naming it', () => {
|
|
47
|
+
const { button, tooltip } = renderTooltip({ label: undefined, description: 'More detail' })
|
|
48
|
+
expect(tooltip).toHaveTextContent('More detail')
|
|
49
|
+
expect(button).toHaveAccessibleName('Test button')
|
|
50
|
+
expect(button).toHaveAccessibleDescription('More detail')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('shows the label and description together, reading each only once', () => {
|
|
54
|
+
const { button, tooltip } = renderTooltip({ description: 'More detail' })
|
|
55
|
+
expect(tooltip).toHaveTextContent('Test tooltip: More detail')
|
|
56
|
+
expect(button).toHaveAccessibleName('Test tooltip')
|
|
57
|
+
expect(button).toHaveAccessibleDescription('More detail')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('keeps the trigger\'s own handlers', () => {
|
|
61
|
+
const handlers = {
|
|
62
|
+
onMouseEnter: jest.fn(),
|
|
63
|
+
onMouseLeave: jest.fn(),
|
|
64
|
+
onMouseDown: jest.fn(),
|
|
65
|
+
onKeyDown: jest.fn(),
|
|
66
|
+
onFocus: jest.fn(),
|
|
67
|
+
onBlur: jest.fn()
|
|
68
|
+
}
|
|
69
|
+
useApp.mockReturnValue({ interfaceType: 'keyboard' })
|
|
70
|
+
const { container } = render(<Tooltip label='Test tooltip'><Button {...handlers} /></Tooltip>)
|
|
71
|
+
const button = container.querySelector('button')
|
|
72
|
+
fireEvent.mouseEnter(button)
|
|
73
|
+
fireEvent.mouseLeave(button)
|
|
74
|
+
fireEvent.mouseDown(button)
|
|
75
|
+
fireEvent.keyDown(button, { key: 'ArrowDown' })
|
|
76
|
+
fireEvent.focus(button)
|
|
77
|
+
fireEvent.blur(button)
|
|
78
|
+
Object.values(handlers).forEach(handler => expect(handler).toHaveBeenCalledTimes(1))
|
|
79
|
+
})
|
|
80
|
+
|
|
39
81
|
it('renders with correct initial state', () => {
|
|
40
82
|
const { tooltip } = renderTooltip()
|
|
41
83
|
expect(tooltip).toHaveClass('im-c-tooltip--hidden')
|
|
@@ -111,7 +153,7 @@ describe('Tooltip', () => {
|
|
|
111
153
|
const fnRef = jest.fn()
|
|
112
154
|
|
|
113
155
|
render(
|
|
114
|
-
<Tooltip
|
|
156
|
+
<Tooltip label='Test tooltip'>
|
|
115
157
|
<ChildWithFunctionRef ref={fnRef} />
|
|
116
158
|
</Tooltip>
|
|
117
159
|
)
|
|
@@ -131,7 +173,7 @@ describe('Tooltip', () => {
|
|
|
131
173
|
const objRef = { current: null }
|
|
132
174
|
|
|
133
175
|
render(
|
|
134
|
-
<Tooltip
|
|
176
|
+
<Tooltip label='Test tooltip'>
|
|
135
177
|
<ChildWithObjectRef ref={objRef} />
|
|
136
178
|
</Tooltip>
|
|
137
179
|
)
|
|
@@ -3,6 +3,8 @@ import { useConfig } from '../../store/configContext'
|
|
|
3
3
|
import { useApp } from '../../store/appContext'
|
|
4
4
|
import { useMap } from '../../store/mapContext'
|
|
5
5
|
import { useMapStateSync } from '../../hooks/useMapStateSync'
|
|
6
|
+
import { useUserMoveTracking } from '../../hooks/useUserMoveTracking'
|
|
7
|
+
import { useResetViewTarget } from '../../hooks/useResetViewTarget'
|
|
6
8
|
import { useMapURLSync } from '../../hooks/useMapURLSync'
|
|
7
9
|
import { useMapAnnouncements } from '../../hooks/useMapAnnouncements'
|
|
8
10
|
import { useMapProviderOverrides } from '../../hooks/useMapProviderOverrides'
|
|
@@ -53,6 +55,8 @@ export const MapController = ({ mapContainerRef }) => {
|
|
|
53
55
|
|
|
54
56
|
// Sync React state, handle URL persistence and announce to screen reader
|
|
55
57
|
useMapStateSync()
|
|
58
|
+
useUserMoveTracking()
|
|
59
|
+
useResetViewTarget()
|
|
56
60
|
useMapURLSync()
|
|
57
61
|
useMapAnnouncements()
|
|
58
62
|
|
|
@@ -5,6 +5,8 @@ import { useConfig } from '../../store/configContext'
|
|
|
5
5
|
import { useApp } from '../../store/appContext'
|
|
6
6
|
import { useMap } from '../../store/mapContext'
|
|
7
7
|
import { useMapStateSync } from '../../hooks/useMapStateSync'
|
|
8
|
+
import { useUserMoveTracking } from '../../hooks/useUserMoveTracking'
|
|
9
|
+
import { useResetViewTarget } from '../../hooks/useResetViewTarget'
|
|
8
10
|
import { useMapURLSync } from '../../hooks/useMapURLSync'
|
|
9
11
|
import { useMapAnnouncements } from '../../hooks/useMapAnnouncements'
|
|
10
12
|
import { useMapProviderOverrides } from '../../hooks/useMapProviderOverrides'
|
|
@@ -15,6 +17,8 @@ jest.mock('../../store/configContext', () => ({ useConfig: jest.fn() }))
|
|
|
15
17
|
jest.mock('../../store/appContext', () => ({ useApp: jest.fn() }))
|
|
16
18
|
jest.mock('../../store/mapContext', () => ({ useMap: jest.fn() }))
|
|
17
19
|
jest.mock('../../hooks/useMapStateSync', () => ({ useMapStateSync: jest.fn() }))
|
|
20
|
+
jest.mock('../../hooks/useUserMoveTracking', () => ({ useUserMoveTracking: jest.fn() }))
|
|
21
|
+
jest.mock('../../hooks/useResetViewTarget', () => ({ useResetViewTarget: jest.fn() }))
|
|
18
22
|
jest.mock('../../hooks/useMapURLSync', () => ({ useMapURLSync: jest.fn() }))
|
|
19
23
|
jest.mock('../../hooks/useMapAnnouncements', () => ({ useMapAnnouncements: jest.fn() }))
|
|
20
24
|
jest.mock('../../hooks/useMapProviderOverrides', () => ({ useMapProviderOverrides: jest.fn() }))
|
|
@@ -91,6 +95,8 @@ describe('MapController', () => {
|
|
|
91
95
|
it('calls all map-related hooks', () => {
|
|
92
96
|
render(<MapController mapContainerRef={containerRef} />)
|
|
93
97
|
expect(useMapStateSync).toHaveBeenCalled()
|
|
98
|
+
expect(useUserMoveTracking).toHaveBeenCalled()
|
|
99
|
+
expect(useResetViewTarget).toHaveBeenCalled()
|
|
94
100
|
expect(useMapURLSync).toHaveBeenCalled()
|
|
95
101
|
expect(useMapAnnouncements).toHaveBeenCalled()
|
|
96
102
|
expect(useMapProviderOverrides).toHaveBeenCalled()
|
|
@@ -10,6 +10,7 @@ import { getMapSizeScale } from '../../config/mapSizeScales.js'
|
|
|
10
10
|
import { SIDE_PANEL_SLOT_REFS } from '../../config/mapPanelSlots.js'
|
|
11
11
|
import { getFitMove } from '../../utils/getFitMove.js'
|
|
12
12
|
import { getScreenBox, getBoxCenter } from '../../utils/screenBox.js'
|
|
13
|
+
import { resetView } from '../../utils/resetView.js'
|
|
13
14
|
|
|
14
15
|
const FIT_WITHOUT_MARGIN = { type: 'fit' }
|
|
15
16
|
|
|
@@ -61,9 +62,9 @@ const getMove = ({ bounds, safeZoneInset, mapProvider, layoutRefs, mapSize, pref
|
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
// Forward public API events to the (overridden) mapProvider methods so that
|
|
64
|
-
// interactiveMap.fitToBounds() and
|
|
65
|
+
// interactiveMap.fitToBounds(), setView() and resetView() respect safe zone padding.
|
|
65
66
|
// fitToBounds' options are passed through.
|
|
66
|
-
const useForwardMapEvents = (mapProvider, eventBus) => {
|
|
67
|
+
const useForwardMapEvents = (mapProvider, eventBus, appConfig) => {
|
|
67
68
|
useEffect(() => {
|
|
68
69
|
if (!mapProvider || !eventBus) {
|
|
69
70
|
return undefined
|
|
@@ -71,19 +72,23 @@ const useForwardMapEvents = (mapProvider, eventBus) => {
|
|
|
71
72
|
|
|
72
73
|
const handleFitToBounds = (bounds, options) => mapProvider.fitToBounds(bounds, options)
|
|
73
74
|
const handleSetView = (opts) => mapProvider.setView(opts)
|
|
75
|
+
const handleResetView = () => resetView(mapProvider, appConfig)
|
|
74
76
|
|
|
75
77
|
eventBus.on(events.MAP_FIT_TO_BOUNDS, handleFitToBounds)
|
|
76
78
|
eventBus.on(events.MAP_SET_VIEW, handleSetView)
|
|
79
|
+
eventBus.on(events.MAP_RESET_VIEW, handleResetView)
|
|
77
80
|
|
|
78
81
|
return () => {
|
|
79
82
|
eventBus.off(events.MAP_FIT_TO_BOUNDS, handleFitToBounds)
|
|
80
83
|
eventBus.off(events.MAP_SET_VIEW, handleSetView)
|
|
84
|
+
eventBus.off(events.MAP_RESET_VIEW, handleResetView)
|
|
81
85
|
}
|
|
82
|
-
}, [mapProvider, eventBus])
|
|
86
|
+
}, [mapProvider, eventBus, appConfig])
|
|
83
87
|
}
|
|
84
88
|
|
|
85
89
|
export const useMapProviderOverrides = () => {
|
|
86
|
-
const
|
|
90
|
+
const appConfig = useConfig()
|
|
91
|
+
const { mapProvider, eventBus } = appConfig
|
|
87
92
|
const { dispatch: appDispatch, layoutRefs, breakpoint } = useApp()
|
|
88
93
|
const { mapSize } = useMap()
|
|
89
94
|
|
|
@@ -181,5 +186,5 @@ export const useMapProviderOverrides = () => {
|
|
|
181
186
|
}
|
|
182
187
|
}, [mapProvider, appDispatch, layoutRefs, mapSize])
|
|
183
188
|
|
|
184
|
-
useForwardMapEvents(mapProvider, eventBus)
|
|
189
|
+
useForwardMapEvents(mapProvider, eventBus, appConfig)
|
|
185
190
|
}
|
|
@@ -360,6 +360,16 @@ describe('useMapProviderOverrides', () => {
|
|
|
360
360
|
expect(originalSetView).toHaveBeenCalledWith({ center: [1, 2], zoom: 10 })
|
|
361
361
|
})
|
|
362
362
|
|
|
363
|
+
test('MAP_RESET_VIEW event moves the map to the configured view', () => {
|
|
364
|
+
const { mapProvider, capturedHandlers } = setup({ config: { center: [1, 2], zoom: 10 } })
|
|
365
|
+
const originalSetView = mapProvider.setView
|
|
366
|
+
renderHook(() => useMapProviderOverrides())
|
|
367
|
+
|
|
368
|
+
capturedHandlers['map:resetview']()
|
|
369
|
+
|
|
370
|
+
expect(originalSetView).toHaveBeenCalledWith({ center: [1, 2], zoom: 10 })
|
|
371
|
+
})
|
|
372
|
+
|
|
363
373
|
test('unsubscribes from MAP_FIT_TO_BOUNDS and MAP_SET_VIEW on unmount', () => {
|
|
364
374
|
const { eventBus } = setup()
|
|
365
375
|
const { unmount } = renderHook(() => useMapProviderOverrides())
|
|
@@ -368,6 +378,7 @@ describe('useMapProviderOverrides', () => {
|
|
|
368
378
|
|
|
369
379
|
expect(eventBus.off).toHaveBeenCalledWith('map:fittobounds', expect.any(Function))
|
|
370
380
|
expect(eventBus.off).toHaveBeenCalledWith('map:setview', expect.any(Function))
|
|
381
|
+
expect(eventBus.off).toHaveBeenCalledWith('map:resetview', expect.any(Function))
|
|
371
382
|
})
|
|
372
383
|
|
|
373
384
|
test('skips event subscriptions when eventBus is null', () => {
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { useEffect } from 'react'
|
|
2
|
+
import { useConfig } from '../store/configContext.js'
|
|
3
|
+
import { useApp } from '../store/appContext.js'
|
|
4
|
+
import { useMap } from '../store/mapContext.js'
|
|
5
|
+
import { useService } from '../store/serviceContext.js'
|
|
6
|
+
import { EVENTS as events } from '../../config/events.js'
|
|
7
|
+
import { getMapStateFromURL } from '../../utils/mapStateSync.js'
|
|
8
|
+
import { isAtResetViewTarget } from '../../utils/resetView.js'
|
|
9
|
+
|
|
10
|
+
// How long after a reset the map has to start moving. If it doesn't, it was already at the reset view.
|
|
11
|
+
export const RESET_MOVE_WINDOW_MS = 1000
|
|
12
|
+
|
|
13
|
+
// Input on the map that can move it, so a reset move the user takes over isn't recorded
|
|
14
|
+
const MAP_INPUT_EVENTS = ['pointerdown', 'wheel', 'keydown']
|
|
15
|
+
|
|
16
|
+
const getStorageKey = (id) => `${id}:resetViewTarget`
|
|
17
|
+
|
|
18
|
+
// Session storage can be blocked, so a target that can't be saved or read is just not remembered
|
|
19
|
+
const saveTarget = (id, target) => {
|
|
20
|
+
try {
|
|
21
|
+
globalThis.sessionStorage.setItem(getStorageKey(id), JSON.stringify(target))
|
|
22
|
+
} catch {
|
|
23
|
+
// Not remembered
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const readTarget = (id) => {
|
|
28
|
+
try {
|
|
29
|
+
const target = JSON.parse(globalThis.sessionStorage.getItem(getStorageKey(id)))
|
|
30
|
+
return Array.isArray(target?.center) && typeof target.zoom === 'number' ? target : null
|
|
31
|
+
} catch {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sets mapState.resetViewTarget to the center and zoom the map settles at when showing its
|
|
38
|
+
* configured view: on load, and at the end of each reset. The reset view button is disabled
|
|
39
|
+
* while the map is still there, and while the target isn't known: before the map first settles
|
|
40
|
+
* and during a reset. It's null when the map loads somewhere else, or the user takes over a reset.
|
|
41
|
+
*
|
|
42
|
+
* The target is what the map reports, not the configured values, as fitting bounds gives a center
|
|
43
|
+
* and zoom that depend on the map's size and safe zone. It's kept in session storage, so a page
|
|
44
|
+
* reloaded at the reset view, with the view in the URL, starts with the button disabled.
|
|
45
|
+
*/
|
|
46
|
+
export function useResetViewTarget () {
|
|
47
|
+
const { id, mapProvider, urlPosition = 'sync' } = useConfig()
|
|
48
|
+
const { layoutRefs } = useApp()
|
|
49
|
+
const { dispatch } = useMap()
|
|
50
|
+
const { eventBus } = useService()
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
const viewport = layoutRefs.viewportRef?.current
|
|
54
|
+
if (!mapProvider || !viewport) {
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const isInitialViewConfigured = urlPosition === 'none' || !getMapStateFromURL(id, globalThis.location.search)
|
|
59
|
+
let pendingReset = null
|
|
60
|
+
|
|
61
|
+
const dispatchTarget = (payload) => dispatch({ type: 'SET_RESET_VIEW_TARGET', payload })
|
|
62
|
+
|
|
63
|
+
const setTarget = ({ center, zoom }) => {
|
|
64
|
+
saveTarget(id, { center, zoom })
|
|
65
|
+
dispatchTarget({ center, zoom })
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const cancelPendingReset = () => {
|
|
69
|
+
clearTimeout(pendingReset?.timer)
|
|
70
|
+
pendingReset = null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// A view from the URL is only the reset view if it's where the last reset in this session went
|
|
74
|
+
const handleFirstIdle = (state) => {
|
|
75
|
+
if (!state) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
if (isInitialViewConfigured) {
|
|
79
|
+
setTarget(state)
|
|
80
|
+
return
|
|
81
|
+
}
|
|
82
|
+
const savedTarget = readTarget(id)
|
|
83
|
+
dispatchTarget(isAtResetViewTarget({ ...state, resetViewTarget: savedTarget }, mapProvider) ? savedTarget : null)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const handleResetView = () => {
|
|
87
|
+
cancelPendingReset()
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
if (!pendingReset.hasMoved) {
|
|
90
|
+
cancelPendingReset()
|
|
91
|
+
setTarget({ center: mapProvider.getCenter(), zoom: mapProvider.getZoom() })
|
|
92
|
+
}
|
|
93
|
+
}, RESET_MOVE_WINDOW_MS)
|
|
94
|
+
pendingReset = { timer, hasMoved: false }
|
|
95
|
+
dispatch({ type: 'CLEAR_RESET_VIEW_TARGET' })
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const handleMoveStart = () => {
|
|
99
|
+
if (pendingReset) {
|
|
100
|
+
pendingReset.hasMoved = true
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Move end is debounced by the providers, so it fires once after the safe zone padding and the reset move
|
|
105
|
+
const handleMoveEnd = (state) => {
|
|
106
|
+
if (pendingReset?.hasMoved && state) {
|
|
107
|
+
cancelPendingReset()
|
|
108
|
+
setTarget(state)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// The press of the reset button itself comes before the reset, so doesn't cancel it
|
|
113
|
+
const handleMapInput = () => {
|
|
114
|
+
if (pendingReset) {
|
|
115
|
+
cancelPendingReset()
|
|
116
|
+
dispatchTarget(null)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
eventBus.on(events.MAP_FIRST_IDLE, handleFirstIdle)
|
|
121
|
+
eventBus.on(events.MAP_RESET_VIEW, handleResetView)
|
|
122
|
+
eventBus.on(events.MAP_MOVE_START, handleMoveStart)
|
|
123
|
+
eventBus.on(events.MAP_MOVE_END, handleMoveEnd)
|
|
124
|
+
MAP_INPUT_EVENTS.forEach(type => viewport.addEventListener(type, handleMapInput, true))
|
|
125
|
+
|
|
126
|
+
return () => {
|
|
127
|
+
cancelPendingReset()
|
|
128
|
+
eventBus.off(events.MAP_FIRST_IDLE, handleFirstIdle)
|
|
129
|
+
eventBus.off(events.MAP_RESET_VIEW, handleResetView)
|
|
130
|
+
eventBus.off(events.MAP_MOVE_START, handleMoveStart)
|
|
131
|
+
eventBus.off(events.MAP_MOVE_END, handleMoveEnd)
|
|
132
|
+
MAP_INPUT_EVENTS.forEach(type => viewport.removeEventListener(type, handleMapInput, true))
|
|
133
|
+
}
|
|
134
|
+
}, [mapProvider, eventBus, dispatch])
|
|
135
|
+
}
|