2 * Handle common keyboard navigation events within a given container.
4 export class KeyboardNavigationHandler {
7 * @param {Element} container
8 * @param {Function|null} onEscape
9 * @param {Function|null} onEnter
11 constructor(container, onEscape = null, onEnter = null) {
12 this.containers = [container];
13 this.onEscape = onEscape;
14 this.onEnter = onEnter;
15 container.addEventListener('keydown', this.#keydownHandler.bind(this));
19 * Also share the keyboard event handling to the given element.
20 * Only elements within the original container are considered focusable though.
21 * @param {Element} element
23 shareHandlingToEl(element) {
24 this.containers.push(element);
25 element.addEventListener('keydown', this.#keydownHandler.bind(this));
29 * Focus on the next focusable element within the current containers.
32 const focusable = this.#getFocusable();
33 const currentIndex = focusable.indexOf(document.activeElement);
34 let newIndex = currentIndex + 1;
35 if (newIndex >= focusable.length) {
39 focusable[newIndex].focus();
43 * Focus on the previous existing focusable element within the current containers.
46 const focusable = this.#getFocusable();
47 const currentIndex = focusable.indexOf(document.activeElement);
48 let newIndex = currentIndex - 1;
50 newIndex = focusable.length - 1;
53 focusable[newIndex].focus();
57 * @param {KeyboardEvent} event
59 #keydownHandler(event) {
60 // Ignore certain key events in inputs to allow text editing.
61 if (event.target.matches('input') && (event.key === 'ArrowRight' || event.key === 'ArrowLeft')) {
65 if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
67 event.preventDefault();
68 } else if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {
70 event.preventDefault();
71 } else if (event.key === 'Escape') {
74 } else if (document.activeElement) {
75 document.activeElement.blur();
77 } else if (event.key === 'Enter' && this.onEnter) {
83 * Get an array of focusable elements within the current containers.
84 * @returns {Element[]}
88 const selector = '[tabindex]:not([tabindex="-1"]),[href],button:not([tabindex="-1"],[disabled]),input:not([type=hidden])';
89 for (const container of this.containers) {
90 focusable.push(...container.querySelectorAll(selector));