From 0e674eb678b40eb310de46448d1449bcf06fc65b Mon Sep 17 00:00:00 2001 From: William Laugesen Date: Wed, 19 Aug 2026 14:02:50 +1200 Subject: [PATCH] Delete the unused focus trap module `src/scripts/modules/focus.js` is dead code. Its only consumer was `nav-mobile.js`, which stopped importing it in 6dc9f4b1a (Feb 2024) when the mobile menu was rewritten. Nothing has imported it since. `query.js`, its own import, stays: fourteen other modules use it. Co-Authored-By: Claude Opus 5 (1M context) --- src/scripts/modules/focus.js | 79 ------------------------------------ 1 file changed, 79 deletions(-) delete mode 100644 src/scripts/modules/focus.js diff --git a/src/scripts/modules/focus.js b/src/scripts/modules/focus.js deleted file mode 100644 index a63529e022..0000000000 --- a/src/scripts/modules/focus.js +++ /dev/null @@ -1,79 +0,0 @@ -// @ts-check - -import { qsa } from './query.js'; - -/** - * Gets first, last, and all focusable elements in the target - * - * For the supplied element, finds all the elements that can receive keyboard focus. - * - * Examples: a, button, input, textarea, select, and other valid interactive items - * that haven't been disabled or hidden. - * - * @param {HTMLElement} target element - * @returns {{first: HTMLElement, last: HTMLElement, all: HTMLElement[]}} - */ -function getFocusableElement(target) { - const focusElements = Array.from( - qsa( - 'a[href], button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])', - target - ) - ).filter(function (el) { - return !el.hasAttribute('disabled') && !el.getAttribute('aria-hidden'); - }); - - return { - first: focusElements[0], - last: focusElements[focusElements.length - 1], - all: focusElements, - }; -} - -/** - * Mechanism to trap focus - * - * @param {KeyboardEvent} event - * @param {HTMLElement} focusItem - * @returns - */ -function trapFocus(event, focusItem) { - switch (event.code.toLowerCase()) { - case 'tab': - event.preventDefault(); - focusItem.focus(); - return false; - } -} - -/** - * Mechanism to trap focus (TAB) - * - * @param {KeyboardEvent} event - * @param {HTMLElement} focusItem - * @returns - */ -function trapFocusForward(event, focusItem) { - if (event.shiftKey) { - return; - } - - trapFocus(event, focusItem); -} - -/** - * Mechanism to trap tab (SHIFT + TAB) - * - * @param {KeyboardEvent} event - * @param {HTMLElement} focusItem - * @returns - */ -function trapReverseFocus(event, focusItem) { - if (!event.shiftKey) { - return; - } - - trapFocus(event, focusItem); -} - -export { getFocusableElement, trapFocusForward, trapReverseFocus };