| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.KeyboardHandler = exports.onToggleArrowKeydownDefault = exports.setTabIndex = exports.handleArrows = void 0; |
| 4 |
const react_1 = require("react"); |
| 5 |
const util_1 = require("./util"); |
| 6 |
/** |
| 7 |
* This function is a helper for handling basic arrow keyboard interactions. If a component already has its own key handler and event start up/tear down, this function may be easier to integrate in over the full component. |
| 8 |
* |
| 9 |
* @param {event} event Event triggered by the keyboard |
| 10 |
* @param {element[]} navigableElements Valid traversable elements of the container |
| 11 |
* @param {function} isActiveElement Callback to determine if a given element from the navigable elements array is the active element of the page |
| 12 |
* @param {function} getFocusableElement Callback returning the focusable element of a given element from the navigable elements array |
| 13 |
* @param {string[]} validSiblingTags Valid sibling tags that horizontal arrow handling will focus |
| 14 |
* @param {boolean} noVerticalArrowHandling Flag indicating that the included vertical arrow key handling should be ignored |
| 15 |
* @param {boolean} noHorizontalArrowHandling Flag indicating that the included horizontal arrow key handling should be ignored |
| 16 |
* @param {boolean} updateTabIndex Flag indicating that the tabIndex of the currently focused element and next focused element should be updated, in the case of using a roving tabIndex |
| 17 |
* @param {boolean} onlyTraverseSiblings Flag indicating that next focusable element of a horizontal movement will be this element's sibling |
| 18 |
*/ |
| 19 |
const handleArrows = (event, navigableElements, isActiveElement = (element) => document.activeElement.contains(element), getFocusableElement = (element) => element, validSiblingTags = ['A', 'BUTTON', 'INPUT'], noVerticalArrowHandling = false, noHorizontalArrowHandling = false, updateTabIndex = true, onlyTraverseSiblings = true) => { |
| 20 |
const activeElement = document.activeElement; |
| 21 |
const key = event.key; |
| 22 |
let moveTarget = null; |
| 23 |
// Handle vertical arrow keys. If noVerticalArrowHandling is passed, skip this block |
| 24 |
if (!noVerticalArrowHandling) { |
| 25 |
if (['ArrowUp', 'ArrowDown'].includes(key)) { |
| 26 |
event.preventDefault(); |
| 27 |
event.stopImmediatePropagation(); // For menus in menus |
| 28 |
// Traverse navigableElements to find the element which is currently active |
| 29 |
let currentIndex = -1; |
| 30 |
// while (currentIndex === -1) { |
| 31 |
navigableElements.forEach((element, index) => { |
| 32 |
if (isActiveElement(element)) { |
| 33 |
// Once found, move up or down the array by 1. Determined by the vertical arrow key direction |
| 34 |
let increment = 0; |
| 35 |
// keep increasing the increment until you've tried the whole navigableElement |
| 36 |
while (!moveTarget && increment < navigableElements.length && increment * -1 < navigableElements.length) { |
| 37 |
key === 'ArrowUp' ? increment-- : increment++; |
| 38 |
currentIndex = index + increment; |
| 39 |
if (currentIndex >= navigableElements.length) { |
| 40 |
currentIndex = 0; |
| 41 |
} |
| 42 |
if (currentIndex < 0) { |
| 43 |
currentIndex = navigableElements.length - 1; |
| 44 |
} |
| 45 |
// Set the next target element (undefined if none found) |
| 46 |
moveTarget = getFocusableElement(navigableElements[currentIndex]); |
| 47 |
} |
| 48 |
} |
| 49 |
}); |
| 50 |
// } |
| 51 |
} |
| 52 |
} |
| 53 |
// Handle horizontal arrow keys. If noHorizontalArrowHandling is passed, skip this block |
| 54 |
if (!noHorizontalArrowHandling) { |
| 55 |
if (['ArrowLeft', 'ArrowRight'].includes(key)) { |
| 56 |
event.preventDefault(); |
| 57 |
event.stopImmediatePropagation(); // For menus in menus |
| 58 |
let currentIndex = -1; |
| 59 |
navigableElements.forEach((element, index) => { |
| 60 |
var _a; |
| 61 |
if (isActiveElement(element)) { |
| 62 |
const activeRow = navigableElements[index].querySelectorAll(validSiblingTags.join(',')); // all focusable elements in my row |
| 63 |
if (!activeRow.length || onlyTraverseSiblings) { |
| 64 |
let nextSibling = activeElement; |
| 65 |
while (nextSibling) { |
| 66 |
const isDirectChildOfNavigableElement = nextSibling.parentElement === element; |
| 67 |
const nextSiblingMainElement = isDirectChildOfNavigableElement ? nextSibling : nextSibling.parentElement; |
| 68 |
nextSibling = |
| 69 |
key === 'ArrowLeft' |
| 70 |
? nextSiblingMainElement.previousElementSibling |
| 71 |
: nextSiblingMainElement.nextElementSibling; |
| 72 |
if (nextSibling) { |
| 73 |
if (validSiblingTags.includes(nextSibling === null || nextSibling === void 0 ? void 0 : nextSibling.tagName)) { |
| 74 |
moveTarget = nextSibling; |
| 75 |
break; |
| 76 |
} |
| 77 |
// For cases where the validSiblingTag is inside a div wrapper |
| 78 |
if (validSiblingTags.includes((_a = nextSibling.children[0]) === null || _a === void 0 ? void 0 : _a.tagName)) { |
| 79 |
moveTarget = nextSibling.children[0]; |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
else { |
| 86 |
activeRow.forEach((focusableElement, index) => { |
| 87 |
if (event.target === focusableElement) { |
| 88 |
const increment = key === 'ArrowLeft' ? -1 : 1; |
| 89 |
currentIndex = index + increment; |
| 90 |
if (currentIndex >= activeRow.length) { |
| 91 |
currentIndex = 0; |
| 92 |
} |
| 93 |
if (currentIndex < 0) { |
| 94 |
currentIndex = activeRow.length - 1; |
| 95 |
} |
| 96 |
moveTarget = activeRow[currentIndex]; |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
} |
| 101 |
}); |
| 102 |
} |
| 103 |
} |
| 104 |
if (moveTarget) { |
| 105 |
// If updateTabIndex is true, set the previously focussed element's tabIndex to -1 and the next focussed element's tabIndex to 0 |
| 106 |
// This updates the tabIndex for a roving tabIndex |
| 107 |
if (updateTabIndex) { |
| 108 |
activeElement.tabIndex = -1; |
| 109 |
moveTarget.tabIndex = 0; |
| 110 |
} |
| 111 |
// If a move target has been set by either arrow handler, focus that target |
| 112 |
moveTarget.focus(); |
| 113 |
} |
| 114 |
}; |
| 115 |
exports.handleArrows = handleArrows; |
| 116 |
/** |
| 117 |
* This function is a helper for setting the initial tabIndexes in a roving tabIndex |
| 118 |
* |
| 119 |
* @param {HTMLElement[]} options Array of elements which should have a tabIndex of -1, except for the first element which will have a tabIndex of 0 |
| 120 |
*/ |
| 121 |
const setTabIndex = (options) => { |
| 122 |
if (options && options.length > 0) { |
| 123 |
// Iterate the options and set the tabIndex to -1 on every option |
| 124 |
options.forEach((option) => { |
| 125 |
option.tabIndex = -1; |
| 126 |
}); |
| 127 |
// Manually set the tabIndex of the first option to 0 |
| 128 |
options[0].tabIndex = 0; |
| 129 |
} |
| 130 |
}; |
| 131 |
exports.setTabIndex = setTabIndex; |
| 132 |
/** |
| 133 |
* This function is used in Dropdown, Select and MenuContainer as a default toggle keydown behavior. When the toggle has focus and the menu is open, pressing the up/down arrow keys will focus a valid non-disabled menu item - the first item for the down arrow key and last item for the up arrow key. |
| 134 |
* |
| 135 |
* @param event Event triggered by the keyboard |
| 136 |
* @param menuRef Menu reference |
| 137 |
*/ |
| 138 |
const onToggleArrowKeydownDefault = (event, menuRef) => { |
| 139 |
var _a; |
| 140 |
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') { |
| 141 |
return; |
| 142 |
} |
| 143 |
event.preventDefault(); |
| 144 |
const listItems = Array.from((_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll('li')); |
| 145 |
const focusableElements = listItems |
| 146 |
.map((li) => li.querySelector('button:not(:disabled),input:not(:disabled),a:not([aria-disabled="true"])')) |
| 147 |
.filter((el) => el !== null); |
| 148 |
let focusableElement; |
| 149 |
if (event.key === 'ArrowDown') { |
| 150 |
focusableElement = focusableElements[0]; |
| 151 |
} |
| 152 |
else { |
| 153 |
focusableElement = focusableElements[focusableElements.length - 1]; |
| 154 |
} |
| 155 |
focusableElement && focusableElement.focus(); |
| 156 |
}; |
| 157 |
exports.onToggleArrowKeydownDefault = onToggleArrowKeydownDefault; |
| 158 |
class KeyboardHandler extends react_1.Component { |
| 159 |
constructor() { |
| 160 |
super(...arguments); |
| 161 |
this.keyHandler = (event) => { |
| 162 |
const { isEventFromContainer } = this.props; |
| 163 |
// If the passed keyboard event is not from the container, ignore the event by returning |
| 164 |
if (isEventFromContainer ? !isEventFromContainer(event) : !this._isEventFromContainer(event)) { |
| 165 |
return; |
| 166 |
} |
| 167 |
const { isActiveElement, getFocusableElement, noVerticalArrowHandling, noHorizontalArrowHandling, noEnterHandling, noSpaceHandling, updateTabIndex, validSiblingTags, additionalKeyHandler, createNavigableElements, onlyTraverseSiblings } = this.props; |
| 168 |
// Pass the event off to be handled by any custom handler |
| 169 |
additionalKeyHandler && additionalKeyHandler(event); |
| 170 |
// Initialize navigableElements from the createNavigableElements callback |
| 171 |
const navigableElements = createNavigableElements(); |
| 172 |
if (!navigableElements) { |
| 173 |
// eslint-disable-next-line no-console |
| 174 |
console.warn('No navigable elements have been passed to the KeyboardHandler. Keyboard navigation provided by this component will be ignored.'); |
| 175 |
return; |
| 176 |
} |
| 177 |
const key = event.key; |
| 178 |
// Handle enter key. If noEnterHandling is passed, skip this block |
| 179 |
if (!noEnterHandling) { |
| 180 |
if (key === 'Enter') { |
| 181 |
event.preventDefault(); |
| 182 |
event.stopImmediatePropagation(); // For menus in menus |
| 183 |
document.activeElement.click(); |
| 184 |
} |
| 185 |
} |
| 186 |
// Handle space key. If noSpaceHandling is passed, skip this block |
| 187 |
if (!noSpaceHandling) { |
| 188 |
if (key === ' ') { |
| 189 |
event.preventDefault(); |
| 190 |
event.stopImmediatePropagation(); // For menus in menus |
| 191 |
document.activeElement.click(); |
| 192 |
} |
| 193 |
} |
| 194 |
// Inject helper handler for arrow navigation |
| 195 |
(0, exports.handleArrows)(event, navigableElements, isActiveElement, getFocusableElement, validSiblingTags, noVerticalArrowHandling, noHorizontalArrowHandling, updateTabIndex, onlyTraverseSiblings); |
| 196 |
}; |
| 197 |
this._isEventFromContainer = (event) => { |
| 198 |
const { containerRef } = this.props; |
| 199 |
return containerRef.current && containerRef.current.contains(event.target); |
| 200 |
}; |
| 201 |
} |
| 202 |
componentDidMount() { |
| 203 |
if (util_1.canUseDOM) { |
| 204 |
window.addEventListener('keydown', this.keyHandler); |
| 205 |
} |
| 206 |
} |
| 207 |
componentWillUnmount() { |
| 208 |
if (util_1.canUseDOM) { |
| 209 |
window.removeEventListener('keydown', this.keyHandler); |
| 210 |
} |
| 211 |
} |
| 212 |
render() { |
| 213 |
return null; |
| 214 |
} |
| 215 |
} |
| 216 |
exports.KeyboardHandler = KeyboardHandler; |
| 217 |
KeyboardHandler.displayName = 'KeyboardHandler'; |
| 218 |
KeyboardHandler.defaultProps = { |
| 219 |
containerRef: null, |
| 220 |
createNavigableElements: () => null, |
| 221 |
isActiveElement: (navigableElement) => document.activeElement === navigableElement, |
| 222 |
getFocusableElement: (navigableElement) => navigableElement, |
| 223 |
validSiblingTags: ['BUTTON', 'A'], |
| 224 |
onlyTraverseSiblings: true, |
| 225 |
updateTabIndex: true, |
| 226 |
noHorizontalArrowHandling: false, |
| 227 |
noVerticalArrowHandling: false, |
| 228 |
noEnterHandling: false, |
| 229 |
noSpaceHandling: false |
| 230 |
}; |
| 231 |
//# sourceMappingURL=KeyboardHandler.js.map |