Skip to content

Commit

Permalink
Define functions only once
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Mar 28, 2024
1 parent 69d1e4c commit 4a0a423
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
86 changes: 43 additions & 43 deletions focus-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"import": {
"./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, focusGroupKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint }"
},
"limit": "1960 B"
"limit": "1957 B"
}
],
"clean-publish": {
Expand Down

0 comments on commit 4a0a423

Please sign in to comment.