@defra/interactive-map 0.0.53-alpha → 0.0.54-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 (80) hide show
  1. package/dist/css/index.css +1 -1
  2. package/dist/esm/im-core.js +1 -1
  3. package/dist/esm/im-shell.js +1 -1
  4. package/dist/umd/im-core.js +1 -1
  5. package/dist/umd/index.js +1 -1
  6. package/docs/api/context.md +12 -0
  7. package/docs/api.md +11 -3
  8. package/docs/plugins/datasets.md +2 -0
  9. package/docs/plugins/map-key.md +41 -0
  10. package/package.json +1 -1
  11. package/plugins/datasets/dist/esm/im-datasets-ml-adapter.js +1 -1
  12. package/plugins/datasets/dist/esm/im-datasets-ol-adapter.js +1 -1
  13. package/plugins/datasets/dist/esm/im-datasets-plugin.js +1 -1
  14. package/plugins/datasets/dist/umd/im-datasets-ml-adapter.js +1 -1
  15. package/plugins/datasets/dist/umd/im-datasets-ol-adapter.js +1 -1
  16. package/plugins/datasets/dist/umd/im-datasets-plugin.js +1 -1
  17. package/plugins/datasets/src/adapters/esri/esriLayerAdapter.js +6 -6
  18. package/plugins/datasets/src/adapters/esri/registry/esriDataset.test.js +1 -1
  19. package/plugins/datasets/src/adapters/maplibre/layerBuilders.js +1 -1
  20. package/plugins/datasets/src/adapters/maplibre/layerBuilders.test.js +3 -3
  21. package/plugins/datasets/src/adapters/maplibre/registry/mapLibreDataset.test.js +1 -1
  22. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.js +1 -3
  23. package/plugins/datasets/src/adapters/openlayers/canvasPatternStyle.test.js +0 -12
  24. package/plugins/datasets/src/adapters/openlayers/openlayersLayerAdapter.test.js +2 -2
  25. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.js +1 -2
  26. package/plugins/datasets/src/adapters/openlayers/registry/openLayersDataset.test.js +3 -3
  27. package/plugins/datasets/src/fetch/createDynamicSource.js +2 -2
  28. package/plugins/datasets/src/initialise/DatasetsInit.jsx +2 -2
  29. package/plugins/datasets/src/initialise/defaults.js +12 -24
  30. package/plugins/datasets/src/initialise/defaults.test.js +25 -57
  31. package/plugins/datasets/src/initialise/initialiseDatasets.js +1 -4
  32. package/plugins/datasets/src/reducers/mappedDatasetsReducer.test.js +1 -1
  33. package/plugins/datasets/src/registry/dataset.js +21 -16
  34. package/plugins/datasets/src/registry/dataset.test.js +101 -11
  35. package/plugins/map-key/dist/css/index.css +1 -1
  36. package/plugins/map-key/dist/esm/im-map-key-plugin.js +1 -1
  37. package/plugins/map-key/dist/umd/im-map-key-plugin.js +1 -1
  38. package/plugins/map-key/dist/umd/index.js +1 -1
  39. package/plugins/map-key/src/components/Key/Key.module.scss +144 -53
  40. package/plugins/map-key/src/components/Key/KeyGroupItem.jsx +34 -3
  41. package/plugins/map-key/src/components/Key/KeyGroupItem.test.jsx +44 -2
  42. package/plugins/map-key/src/components/Key/KeySvg.jsx +5 -5
  43. package/plugins/map-key/src/components/Key/KeySvg.test.jsx +4 -4
  44. package/plugins/map-key/src/components/Key/KeySvgRamp.jsx +35 -6
  45. package/plugins/map-key/src/components/Key/KeySvgRamp.test.jsx +56 -16
  46. package/plugins/map-key/src/components/Key/useRampOrientation.js +61 -0
  47. package/plugins/map-key/src/components/Key/useRampOrientation.test.jsx +155 -0
  48. package/plugins/map-key/src/index.js +34 -0
  49. package/plugins/map-key/src/utils/groupStyles.js +5 -2
  50. package/plugins/search/dist/esm/im-search-plugin.js +1 -1
  51. package/plugins/search/dist/umd/im-search-plugin.js +1 -1
  52. package/plugins/search/dist/umd/index.js +1 -1
  53. package/plugins/search/src/Search.jsx +0 -1
  54. package/plugins/search/src/components/Form/Form.jsx +3 -3
  55. package/plugins/search/src/components/Suggestions/Suggestions.jsx +2 -0
  56. package/plugins/search/src/components/Suggestions/Suggestions.test.jsx +6 -0
  57. package/plugins/search/src/defaults.js +3 -0
  58. package/plugins/search/src/events/formHandlers.js +3 -9
  59. package/plugins/search/src/events/formHandlers.test.js +6 -9
  60. package/plugins/search/src/events/inputHandlers.js +8 -2
  61. package/plugins/search/src/events/inputHandlers.test.js +20 -0
  62. package/plugins/search/src/events/suggestionHandlers.js +3 -5
  63. package/plugins/search/src/events/suggestionHandlers.test.js +5 -8
  64. package/plugins/search/src/utils/showResult.js +9 -0
  65. package/plugins/search/src/utils/showResult.test.js +16 -0
  66. package/src/App/components/Hints/Hints.module.scss +1 -1
  67. package/src/App/components/Viewport/Viewport.jsx +6 -4
  68. package/src/App/components/Viewport/Viewport.test.jsx +18 -1
  69. package/src/App/hooks/useMapAnnouncements.js +7 -2
  70. package/src/App/hooks/useMapAnnouncements.test.js +13 -2
  71. package/src/App/hooks/useModalPanelBehaviour.js +3 -1
  72. package/src/App/initialiseApp.js +1 -1
  73. package/src/App/store/ServiceProvider.jsx +7 -1
  74. package/src/App/store/ServiceProvider.test.jsx +15 -0
  75. package/src/InteractiveMap/InteractiveMap.js +3 -3
  76. package/src/services/mapFocus.js +64 -0
  77. package/src/services/mapFocus.test.js +76 -0
  78. package/src/types.js +2 -2
  79. package/src/utils/detectInterfaceType.js +16 -5
  80. package/src/utils/detectInterfaceType.test.js +32 -0
@@ -8,7 +8,7 @@
8
8
  left: 0;
9
9
  right: 0;
10
10
  margin-inline: auto;
11
- z-index: 1001;
11
+ z-index: 98; // Above non-modal slots, below .im-o-app__modal (99)
12
12
  width: fit-content;
13
13
  max-width: min(var(--hint-max-width), calc(100% - (2 * var(--primary-gap))));
14
14
  }
@@ -22,7 +22,7 @@ export const Viewport = () => {
22
22
  const { id, mapProvider, mapLabel, keyboardHintText, focusOnMount } = useConfig()
23
23
  const { layoutRefs, safeZoneInset, dispatch } = useApp()
24
24
  const { mapSize, isMapReady } = useMap()
25
- const { eventBus, hints } = useService()
25
+ const { eventBus, hints, mapFocus } = useService()
26
26
 
27
27
  const mapContainerRef = useRef(null)
28
28
  const spatialListRef = useRef(null)
@@ -75,7 +75,7 @@ export const Viewport = () => {
75
75
  return (
76
76
  <>
77
77
  <MapController mapContainerRef={mapContainerRef} />
78
- <div
78
+ <div // NOSONAR - role="application" is interactive: the map handles its own keyboard and pointer input
79
79
  id={`${id}-viewport`}
80
80
  className={`im-c-viewport im-c-viewport--${mapSize}`}
81
81
  aria-label={mapLabel}
@@ -83,10 +83,12 @@ export const Viewport = () => {
83
83
  tabIndex='0' // nosonar
84
84
  data-map-keyboard-scope
85
85
  onFocus={handleFocus}
86
- onBlur={handleBlur}
86
+ onBlur={(event) => { handleBlur(event); mapFocus.clear() }}
87
+ onKeyDown={() => mapFocus.releaseAnnouncements()}
88
+ onPointerDown={() => mapFocus.releaseAnnouncements()}
87
89
  ref={layoutRefs.viewportRef}
88
90
  aria-describedby={`${id}-keyboard-desc`}
89
- aria-controls={`${id}-features`}
91
+ aria-controls={`${id}-spatial-list`}
90
92
  >
91
93
  <div className='im-c-viewport__map-container' ref={mapContainerRef} aria-hidden='true' />
92
94
  <MapStatus />
@@ -46,7 +46,8 @@ function setupHookMocks (mainEl, viewportEl) {
46
46
  useService.mockReturnValue({
47
47
  announce: jest.fn(),
48
48
  hints: { show: jest.fn(), dismiss: jest.fn(), subscribe: jest.fn(() => jest.fn()) },
49
- eventBus: { on: jest.fn(), off: jest.fn(), emit: jest.fn() }
49
+ eventBus: { on: jest.fn(), off: jest.fn(), emit: jest.fn() },
50
+ mapFocus: { clear: jest.fn(), releaseAnnouncements: jest.fn() }
50
51
  })
51
52
  useKeyboardHint.mockImplementation(({ onViewportFocusChange }) => ({
52
53
  handleFocus: () => onViewportFocusChange(true),
@@ -114,6 +115,22 @@ describe('Viewport rendering', () => {
114
115
  expect(viewport).toHaveAttribute('aria-describedby', 'test-map-keyboard-desc')
115
116
  })
116
117
 
118
+ it('restores the map label via mapFocus when the viewport loses focus', () => {
119
+ const { mapFocus } = useService()
120
+ const { viewport } = renderViewport()
121
+ fireEvent.blur(viewport)
122
+ expect(mapFocus.clear).toHaveBeenCalled()
123
+ })
124
+
125
+ it('releases held map announcements on a key or pointer press in the viewport', () => {
126
+ const { mapFocus } = useService()
127
+ const { viewport } = renderViewport()
128
+ fireEvent.keyDown(viewport, { key: 'ArrowUp' })
129
+ fireEvent.pointerDown(viewport)
130
+ expect(mapFocus.releaseAnnouncements).toHaveBeenCalledTimes(2)
131
+ expect(mapFocus.clear).not.toHaveBeenCalled()
132
+ })
133
+
117
134
  it('calls hints.show() with keyboardHintText when viewport gains keyboard focus', () => {
118
135
  const { hints } = useService()
119
136
  renderViewport()
@@ -40,7 +40,7 @@ const resolveMessage = (previous, current, mapProvider) => {
40
40
 
41
41
  export function useMapAnnouncements () {
42
42
  const { mapProvider } = useConfig()
43
- const { eventBus, announce } = useService()
43
+ const { eventBus, announce, mapFocus } = useService()
44
44
 
45
45
  useEffect(() => {
46
46
  const handleAnnounceStateUpdate = ({ previous, current }) => {
@@ -48,6 +48,11 @@ export function useMapAnnouncements () {
48
48
  return
49
49
  }
50
50
 
51
+ // A map focus message already describes this move
52
+ if (mapFocus.isHoldingAnnouncements()) {
53
+ return
54
+ }
55
+
51
56
  const message = resolveMessage(previous, current, mapProvider)
52
57
  if (message) {
53
58
  announce(message, 'action')
@@ -59,5 +64,5 @@ export function useMapAnnouncements () {
59
64
  return () => {
60
65
  eventBus.off(events.MAP_STATE_UPDATED, handleAnnounceStateUpdate)
61
66
  }
62
- }, [mapProvider, announce])
67
+ }, [mapProvider, announce, mapFocus])
63
68
  }
@@ -22,14 +22,15 @@ const setup = (overrides = {}) => {
22
22
  }
23
23
 
24
24
  useConfig.mockReturnValue({ mapProvider, ...overrides.config })
25
- useService.mockReturnValue({ announce, eventBus, ...overrides.service })
25
+ const mapFocus = { isHoldingAnnouncements: jest.fn(() => false) }
26
+ useService.mockReturnValue({ announce, eventBus, mapFocus, ...overrides.service })
26
27
 
27
28
  getMapStatusMessage.moved = jest.fn(() => 'Moved north')
28
29
  getMapStatusMessage.zoomed = jest.fn(() => 'Zoomed in')
29
30
  getMapStatusMessage.noChange = jest.fn(() => 'No change')
30
31
  getMapStatusMessage.newArea = jest.fn(() => 'New area')
31
32
 
32
- return { announce, mapProvider, eventBus }
33
+ return { announce, mapProvider, eventBus, mapFocus }
33
34
  }
34
35
 
35
36
  describe('useMapAnnouncements', () => {
@@ -50,6 +51,16 @@ describe('useMapAnnouncements', () => {
50
51
  expect(eventBus.off).toHaveBeenCalledWith('map:stateupdated', expect.any(Function))
51
52
  })
52
53
 
54
+ test('does not announce while a map focus message is holding announcements', () => {
55
+ const { announce, eventBus, mapFocus } = setup()
56
+ mapFocus.isHoldingAnnouncements.mockReturnValue(true)
57
+ renderHook(() => useMapAnnouncements())
58
+ const handler = eventBus.on.mock.calls[0][1]
59
+
60
+ handler({ previous: { center: [0, 0], zoom: 1 }, current: { center: [1, 1], zoom: 2 } })
61
+ expect(announce).not.toHaveBeenCalled()
62
+ })
63
+
53
64
  test('early returns when previous or current center is missing', () => {
54
65
  const { announce, eventBus } = setup()
55
66
  renderHook(() => useMapAnnouncements())
@@ -200,7 +200,9 @@ export function useModalPanelBehaviour ({
200
200
  }, [isModal, rootEl, handleClose])
201
201
 
202
202
  // === Inert everything outside the panel but within the app === //
203
- useEffect(() => {
203
+ // Layout effect so the cleanup restores aria-hidden at commit, before handleClose's
204
+ // requestAnimationFrame returns focus to the trigger (which sits inside an inerted branch).
205
+ useLayoutEffect(() => {
204
206
  if (!isModal || !panelRef.current || !rootEl) {
205
207
  return undefined
206
208
  }
@@ -38,7 +38,7 @@ const getOrCreateRegistries = (rootElement, eventBus) => {
38
38
  const loadPlugins = async (plugins, registerPlugin) => {
39
39
  for (const plugin of plugins) {
40
40
  if (typeof plugin.load === 'function') {
41
- const module = await plugin.load()
41
+ const module = await plugin.load() // NOSONAR: Await inside a for loop is fine, parent function is async
42
42
  const { id: pluginId, load, manifest: overrideManifest, ...config } = plugin
43
43
  const { InitComponent, api, reducer, ...baseManifest } = module
44
44
 
@@ -2,8 +2,10 @@
2
2
  import React, { createContext, useMemo, useRef } from 'react'
3
3
  import { createAnnouncer } from '../../services/announcer.js'
4
4
  import { createHints } from '../../services/hints.js'
5
+ import { createMapFocus } from '../../services/mapFocus.js'
5
6
  import { reverseGeocode } from '../../services/reverseGeocode.js'
6
7
  import { useConfig } from '../store/configContext.js'
8
+ import { useApp } from '../store/appContext.js'
7
9
  import { closeApp } from '../../services/closeApp.js'
8
10
  import { createSymbolRegistry } from '../../services/symbolRegistry.js'
9
11
  import { createPatternRegistry } from '../../services/patternRegistry.js'
@@ -15,6 +17,8 @@ export const ServiceProvider = ({ eventBus, pluginRegistry, children }) => {
15
17
  const mapStatusRef = useRef(null)
16
18
  const announce = useMemo(() => createAnnouncer(mapStatusRef), [])
17
19
  const hints = useMemo(() => createHints(announce), [announce])
20
+ const { layoutRefs } = useApp()
21
+ const mapFocus = useMemo(() => createMapFocus({ viewportRef: layoutRefs.viewportRef }), [])
18
22
  // One of each per map, so maps on the same page keep their own symbols, symbolDefaults and patterns
19
23
  const symbolRegistry = useMemo(() => createSymbolRegistry(), [])
20
24
  const patternRegistry = useMemo(() => createPatternRegistry(), [])
@@ -27,12 +31,14 @@ export const ServiceProvider = ({ eventBus, pluginRegistry, children }) => {
27
31
  reverseGeocode: (zoom, center) => reverseGeocode(zoom, center),
28
32
  eventBus,
29
33
  mapStatusRef,
34
+ focusMap: mapFocus.focus,
35
+ mapFocus,
30
36
  closeApp: () => closeApp(id, handleExitClick, eventBus),
31
37
  symbolRegistry,
32
38
  patternRegistry,
33
39
  // See pluginRegistry.js's getPlugin() for what this does and doesn't guarantee.
34
40
  getPlugin: (pluginId) => pluginRegistry?.getPlugin(pluginId)
35
- }), [announce, hints, pluginRegistry, symbolRegistry, patternRegistry])
41
+ }), [announce, hints, mapFocus, pluginRegistry, symbolRegistry, patternRegistry])
36
42
 
37
43
  return (
38
44
  <ServiceContext.Provider value={services}>
@@ -24,6 +24,11 @@ jest.mock('../../services/closeApp.js', () => ({
24
24
  closeApp: jest.fn()
25
25
  }))
26
26
 
27
+ const mockViewportRef = { current: { focus: jest.fn(), getAttribute: jest.fn(), setAttribute: jest.fn() } }
28
+ jest.mock('../store/appContext.js', () => ({
29
+ useApp: jest.fn(() => ({ layoutRefs: { viewportRef: mockViewportRef } }))
30
+ }))
31
+
27
32
  // Mock config values including id + handleExitClick
28
33
  jest.mock('../store/configContext.js', () => ({
29
34
  useConfig: jest.fn(() => ({
@@ -56,6 +61,16 @@ describe('ServiceProvider', () => {
56
61
  expect(result.current.mapStatusRef.current).toBeNull()
57
62
  })
58
63
 
64
+ test('focusMap names and focuses the viewport from layoutRefs', () => {
65
+ const { result } = renderHook(() => React.useContext(ServiceContext), { wrapper })
66
+
67
+ result.current.focusMap({ message: 'Map moved to Carlisle' })
68
+
69
+ expect(mockViewportRef.current.setAttribute).toHaveBeenCalledWith('aria-label', 'Map moved to Carlisle')
70
+ expect(mockViewportRef.current.focus).toHaveBeenCalled()
71
+ expect(result.current.mapFocus.isHoldingAnnouncements()).toBe(true)
72
+ })
73
+
59
74
  test('gives each map its own symbol registry, kept across renders, with that map\'s symbolDefaults', () => {
60
75
  const { useConfig } = jest.requireMock('../store/configContext.js')
61
76
  useConfig.mockReturnValue({ id: 'map-a', symbolDefaults: { backgroundColor: '#111111' } })
@@ -99,7 +99,7 @@ export default class InteractiveMap {
99
99
  this._hybridBehaviourCleanup = setupBehavior(this)
100
100
 
101
101
  if (shouldLoadComponent(this.config)) {
102
- this.loadApp()
102
+ this.loadApp() // NOSONAR: Not changing for now as used in production
103
103
  } else {
104
104
  removeLoadingState()
105
105
  }
@@ -120,7 +120,7 @@ export default class InteractiveMap {
120
120
  history.pushState({ isBack: true }, '', e.currentTarget.getAttribute('href'))
121
121
  }
122
122
  // Only a genuine launcher click moves focus into the map — never a resize/breakpoint transition.
123
- this.loadApp({ focusOnMount: true })
123
+ this.loadApp({ focusOnMount: true }) // NOSONAR: Not changing for now as used in production
124
124
  }
125
125
  }
126
126
 
@@ -564,7 +564,7 @@ export default class InteractiveMap {
564
564
  } else if (this._root) {
565
565
  // App is already open — no-op
566
566
  } else {
567
- this.loadApp()
567
+ this.loadApp() // NOSONAR: Not changing for now as used in production
568
568
  }
569
569
  }
570
570
 
@@ -0,0 +1,64 @@
1
+ // services/mapFocus.js
2
+
3
+ /**
4
+ * Moves focus to the map viewport, optionally with a message that stands in
5
+ * for the viewport's accessible name. Being the name, the message is spoken
6
+ * reliably and straight away as part of the focus announcement, where a live
7
+ * region update would race the focus change. The original name returns when
8
+ * focus leaves the viewport, so the name never changes while it has focus.
9
+ *
10
+ * A message also holds back map state announcements ("New area
11
+ * approximately...") until the user next presses a key or pointer on the map,
12
+ * since it already describes the move that follows.
13
+ */
14
+ const ARIA_LABEL = 'aria-label'
15
+
16
+ export function createMapFocus ({ viewportRef }) {
17
+ let originalLabel = null
18
+ let hasMessage = false
19
+ let isHoldingAnnouncements = false
20
+
21
+ const focus = ({ message } = {}) => {
22
+ const viewport = viewportRef.current
23
+ if (!viewport) {
24
+ return
25
+ }
26
+ if (message) {
27
+ if (!hasMessage) {
28
+ originalLabel = viewport.getAttribute(ARIA_LABEL)
29
+ }
30
+ viewport.setAttribute(ARIA_LABEL, message)
31
+ hasMessage = true
32
+ isHoldingAnnouncements = true
33
+ }
34
+ viewport.focus()
35
+ }
36
+
37
+ const clear = () => {
38
+ isHoldingAnnouncements = false
39
+ if (!hasMessage) {
40
+ return
41
+ }
42
+ hasMessage = false
43
+ const viewport = viewportRef.current
44
+ if (!viewport) {
45
+ return
46
+ }
47
+ if (originalLabel === null) {
48
+ viewport.removeAttribute(ARIA_LABEL)
49
+ } else {
50
+ viewport.setAttribute(ARIA_LABEL, originalLabel)
51
+ }
52
+ }
53
+
54
+ const releaseAnnouncements = () => {
55
+ isHoldingAnnouncements = false
56
+ }
57
+
58
+ return {
59
+ focus,
60
+ clear,
61
+ releaseAnnouncements,
62
+ isHoldingAnnouncements: () => isHoldingAnnouncements
63
+ }
64
+ }
@@ -0,0 +1,76 @@
1
+ import { createMapFocus } from './mapFocus.js'
2
+
3
+ describe('createMapFocus', () => {
4
+ let viewport
5
+ let mapFocus
6
+
7
+ beforeEach(() => {
8
+ viewport = document.createElement('div')
9
+ viewport.tabIndex = 0
10
+ viewport.setAttribute('aria-label', 'Interactive map')
11
+ document.body.appendChild(viewport)
12
+ mapFocus = createMapFocus({ viewportRef: { current: viewport } })
13
+ })
14
+
15
+ afterEach(() => {
16
+ viewport.remove()
17
+ })
18
+
19
+ it('focuses the viewport without changing its name or holding announcements', () => {
20
+ mapFocus.focus()
21
+ expect(document.activeElement).toBe(viewport)
22
+ expect(viewport.getAttribute('aria-label')).toBe('Interactive map')
23
+ expect(mapFocus.isHoldingAnnouncements()).toBe(false)
24
+ })
25
+
26
+ it('replaces the name with the message before focusing and holds announcements', () => {
27
+ viewport.addEventListener('focus', () => {
28
+ expect(viewport.getAttribute('aria-label')).toBe('Map moved to Carlisle')
29
+ })
30
+ mapFocus.focus({ message: 'Map moved to Carlisle' })
31
+ expect(document.activeElement).toBe(viewport)
32
+ expect(mapFocus.isHoldingAnnouncements()).toBe(true)
33
+ })
34
+
35
+ it('releaseAnnouncements ends the hold but keeps the message', () => {
36
+ mapFocus.focus({ message: 'Map moved to Carlisle' })
37
+ mapFocus.releaseAnnouncements()
38
+ expect(mapFocus.isHoldingAnnouncements()).toBe(false)
39
+ expect(viewport.getAttribute('aria-label')).toBe('Map moved to Carlisle')
40
+ })
41
+
42
+ it('clear restores the original name and ends the hold', () => {
43
+ mapFocus.focus({ message: 'Map moved to Carlisle' })
44
+ mapFocus.focus({ message: 'Map moved to Leeds' })
45
+ mapFocus.clear()
46
+ expect(viewport.getAttribute('aria-label')).toBe('Interactive map')
47
+ expect(mapFocus.isHoldingAnnouncements()).toBe(false)
48
+ })
49
+
50
+ it('clear leaves the name alone when no message was set', () => {
51
+ viewport.setAttribute('aria-label', 'Changed by the app')
52
+ mapFocus.clear()
53
+ expect(viewport.getAttribute('aria-label')).toBe('Changed by the app')
54
+ })
55
+
56
+ it('clear removes the name again when the viewport had none', () => {
57
+ viewport.removeAttribute('aria-label')
58
+ mapFocus.focus({ message: 'Map moved to Carlisle' })
59
+ mapFocus.clear()
60
+ expect(viewport.hasAttribute('aria-label')).toBe(false)
61
+ })
62
+
63
+ it('copes with the viewport not being mounted', () => {
64
+ const viewportRef = { current: null }
65
+ mapFocus = createMapFocus({ viewportRef })
66
+ expect(() => mapFocus.focus({ message: 'Map moved to Carlisle' })).not.toThrow()
67
+ expect(mapFocus.isHoldingAnnouncements()).toBe(false)
68
+ expect(() => mapFocus.clear()).not.toThrow()
69
+
70
+ // Unmounted between focus and clear
71
+ viewportRef.current = viewport
72
+ mapFocus.focus({ message: 'Map moved to Carlisle' })
73
+ viewportRef.current = null
74
+ expect(() => mapFocus.clear()).not.toThrow()
75
+ })
76
+ })
package/src/types.js CHANGED
@@ -112,7 +112,7 @@
112
112
  * Plugin-specific state.
113
113
  *
114
114
  * @property {Object} [services]
115
- * Core services (announce, reverseGeocode, closeApp, etc.).
115
+ * Core services (announce, focusMap, reverseGeocode, closeApp, etc.).
116
116
  *
117
117
  * @property {(id: string, options?: ApplicationModeOptions) => void} [setApplicationMode]
118
118
  * Plugin components only. Enters an application mode, putting it on top of the stack (or moving it to
@@ -879,7 +879,7 @@
879
879
  * @property {string} [mapHintText]
880
880
  * Visually hidden text, rendered immediately before the map for screen reader users, explaining that it must be focused before keyboard commands work. Prefixed with mapLabel, so multiple maps on one page can be told apart.
881
881
  *
882
- * @property {string} [mapLabel='Interactive map']
882
+ * @property {string} [mapLabel='Interactive map application']
883
883
  * Accessible label for the map, announced by screen readers. Also prefixed onto mapHintText, so give each map on a page a distinct label.
884
884
  *
885
885
  * @property {string} [mapControlsHintText]
@@ -18,6 +18,19 @@ function normalizePointerType (pointerType) {
18
18
  return 'unknown'
19
19
  }
20
20
 
21
+ const KEYBOARD_MODE_KEYS = new Set(['Enter', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Escape'])
22
+ const NON_TEXT_INPUT_TYPES = new Set(['button', 'checkbox', 'color', 'file', 'image', 'radio', 'range', 'reset', 'submit'])
23
+
24
+ function isTextField (el) {
25
+ if (!el) {
26
+ return false
27
+ }
28
+ if (el.tagName === 'TEXTAREA' || el.isContentEditable) {
29
+ return true
30
+ }
31
+ return el.tagName === 'INPUT' && !NON_TEXT_INPUT_TYPES.has(el.type)
32
+ }
33
+
21
34
  function notifyListeners (newType) {
22
35
  if (lastInterfaceType !== newType) {
23
36
  lastInterfaceType = newType
@@ -59,11 +72,9 @@ function createInterfaceDetector () {
59
72
  }
60
73
 
61
74
  const handleKeyDown = e => {
62
- // Recognize keyboard mode from Tab (explicit focus), arrow keys (navigation),
63
- // Enter (confirmation), or other significant keys. This allows the interface type
64
- // to update even when keyboard input happens during drawing (where focus is on map).
65
- const keyboardModeKeys = new Set(['Tab', 'Enter', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Escape'])
66
- if (keyboardModeKeys.has(e.key)) {
75
+ // Tab, and arrows/Enter/Escape outside text fields, switch to keyboard mode. The
76
+ // latter cover a pointer user who clicks the map then pans or draws by keyboard.
77
+ if (e.key === 'Tab' || (KEYBOARD_MODE_KEYS.has(e.key) && !isTextField(e.target))) {
67
78
  notifyListeners('keyboard')
68
79
  }
69
80
  }
@@ -178,6 +178,38 @@ describe('Interface Detector Utility Module', () => {
178
178
  expect(handler).toHaveBeenCalledTimes(2) // no longer called after unsubscribe
179
179
  })
180
180
 
181
+ it('ignores Enter, arrows and Escape typed in a text field but still honours Tab there', () => {
182
+ cleanup = createInterfaceDetector()
183
+ triggerDomEvent('pointerdown', { pointerType: 'mouse' })
184
+
185
+ const input = document.createElement('input')
186
+ const textarea = document.createElement('textarea')
187
+ const editable = { tagName: 'DIV', isContentEditable: true }
188
+ const textFields = [input, textarea, editable]
189
+ textFields.forEach(target => {
190
+ ['Enter', 'ArrowDown', 'ArrowUp', 'Escape'].forEach(key => {
191
+ triggerDomEvent('keydown', { key, target })
192
+ expect(getInterfaceType()).toBe('mouse')
193
+ })
194
+ })
195
+
196
+ triggerDomEvent('keydown', { key: 'Tab', target: input })
197
+ expect(getInterfaceType()).toBe('keyboard')
198
+ })
199
+
200
+ it('switches to keyboard on Enter from non-text controls', () => {
201
+ cleanup = createInterfaceDetector()
202
+ const checkbox = document.createElement('input')
203
+ checkbox.type = 'checkbox'
204
+ const button = document.createElement('button')
205
+ const targets = [checkbox, button, undefined]
206
+ targets.forEach(target => {
207
+ triggerDomEvent('pointerdown', { pointerType: 'mouse' })
208
+ triggerDomEvent('keydown', { key: 'Enter', target })
209
+ expect(getInterfaceType()).toBe('keyboard')
210
+ })
211
+ })
212
+
181
213
  it('should return "touch" when matchMedia initially matches coarse pointer', async () => {
182
214
  jest.resetModules()
183
215