From 4a0a423ba079062fb7d2353c63ebb69d6ae698f2 Mon Sep 17 00:00:00 2001 From: Andrey Sitnik Date: Thu, 28 Mar 2024 18:53:03 +0100 Subject: [PATCH] Define functions only once --- focus-group.js | 86 +++++++++++++++++++++++++------------------------- package.json | 2 +- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/focus-group.js b/focus-group.js index e28ad05..b455949 100644 --- a/focus-group.js +++ b/focus-group.js @@ -6,57 +6,57 @@ const ROLES = { tab: ['tablist'] } -export function focusGroupKeyUX(options) { - return window => { - let inGroup = false - let typingDelayMs = options?.searchDelayMs || 300 - let lastTyped = 0 - let searchPrefix = '' +function focus(current, next) { + next.tabIndex = 0 + next.focus() + current.tabIndex = -1 +} - function focus(current, next) { - next.tabIndex = 0 - next.focus() - current.tabIndex = -1 - } +function findGroupNodeByEventTarget(target) { + let itemRole = target.role || target.type || target.tagName + if (!itemRole) return null - function findGroupNodeByEventTarget(eventTarget) { - let itemRole = eventTarget.role || eventTarget.type || eventTarget.tagName - if (!itemRole) return null + let groupRoles = ROLES[itemRole.toLowerCase()] + if (!groupRoles) return null - let groupRoles = ROLES[itemRole.toLowerCase()] - if (!groupRoles) return null + for (let role of groupRoles) { + let node = target.closest(`[role=${role}]`) + if (node) return node + } +} - for (let role of groupRoles) { - let node = eventTarget.closest(`[role=${role}]`) - if (node) return node - } - } +function getItems(target, group) { + if (group.role === 'toolbar') return getToolbarItems(group) + return group.querySelectorAll(`[role=${target.role}]`) +} - function getItems(eventTarget, group) { - if (group.role === 'toolbar') return getToolbarItems(group) - return group.querySelectorAll(`[role=${eventTarget.role}]`) - } +function getToolbarItems(group) { + let items = [...group.querySelectorAll('*')] + return items.filter(item => { + return ( + item.role === 'button' || + item.type === 'button' || + item.role === 'checkbox' || + item.type === 'checkbox' + ) + }) +} - function getToolbarItems(group) { - let items = [...group.querySelectorAll('*')] - return items.filter(item => { - return ( - item.role === 'button' || - item.type === 'button' || - item.role === 'checkbox' || - item.type === 'checkbox' - ) - }) - } +function isHorizontalOrientation(group) { + let ariaOrientation = group.getAttribute('aria-orientation') + if (ariaOrientation === 'vertical') return false + if (ariaOrientation === 'horizontal') return true - function isHorizontalOrientation(group) { - let ariaOrientation = group.getAttribute('aria-orientation') - if (ariaOrientation === 'vertical') return false - if (ariaOrientation === 'horizontal') return true + let role = group.role + return role === 'menubar' || role === 'tablist' || role === 'toolbar' +} - let role = group.role - return role === 'menubar' || role === 'tablist' || role === 'toolbar' - } +export function focusGroupKeyUX(options) { + return window => { + let inGroup = false + let typingDelayMs = options?.searchDelayMs || 300 + let lastTyped = 0 + let searchPrefix = '' function keyDown(event) { let group = findGroupNodeByEventTarget(event.target) diff --git a/package.json b/package.json index 8620f42..05ab0ad 100644 --- a/package.json +++ b/package.json @@ -85,7 +85,7 @@ "import": { "./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, focusGroupKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint }" }, - "limit": "1960 B" + "limit": "1957 B" } ], "clean-publish": {