Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: focus-group support toolbar #19

Merged
merged 10 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions focus-group.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const ROLES = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update package.jsonsize-limit

menuitem: ['menu', 'menubar'],
option: ['listbox'],
tab: ['tablist']
tab: ['tablist'],
button: ['toolbar'],
checkbox: ['toolbar']
}

export function focusGroupKeyUX(options) {
Expand All @@ -18,8 +20,10 @@ export function focusGroupKeyUX(options) {
}

function findGroupNodeByEventTarget(eventTarget) {
let itemRole = eventTarget.role
let groupRoles = ROLES[itemRole]
let itemRole = eventTarget.role || eventTarget.type || eventTarget.tagName
if (!itemRole) return null;

let groupRoles = ROLES[itemRole.toLowerCase()]
if (!groupRoles) return null

for (let role of groupRoles) {
Expand All @@ -28,13 +32,25 @@ export function focusGroupKeyUX(options) {
}
}

function getItems(eventTarget, group) {
if (group.role === "toolbar") return getToolbarItems(group)
return group.querySelectorAll(`[role=${eventTarget.role}]`)
}

function getToolbarItems(group) {
ai marked this conversation as resolved.
Show resolved Hide resolved
let items = [...group.querySelectorAll('*')]
return items.filter((item)=> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Format is not according to Prettier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously I used only Editor Config for the project should I install Prettier auto formatter ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Editor Config is for general settings (indent, new line format, etc).

Prettier is about formatting the code, where to add spaces, etc.

I recommend installing it since it is an industry standard nowadays.

But you can keep code formatting manually if you don’t want to install it.

Copy link
Contributor Author

@dmitry-kurmanov dmitry-kurmanov Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prettier is awesome I just clarified the info

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

let role = group.role
return role === 'menubar' || role === 'tablist'
return role === 'menubar' || role === 'tablist' || role === 'toolbar'
}

function keyDown(event) {
Expand All @@ -45,7 +61,7 @@ export function focusGroupKeyUX(options) {
return
}

let items = group.querySelectorAll(`[role=${event.target.role}]`)
let items = getItems(event.target, group);
let index = Array.from(items).indexOf(event.target)

let nextKey = 'ArrowDown'
Expand Down Expand Up @@ -106,7 +122,7 @@ export function focusGroupKeyUX(options) {
inGroup = true
window.addEventListener('keydown', keyDown)
}
let items = group.querySelectorAll(`[role=${event.target.role}]`)
let items = getItems(event.target, group);
for (let item of items) {
if (item !== event.target) {
item.setAttribute('tabindex', -1)
Expand All @@ -126,7 +142,7 @@ export function focusGroupKeyUX(options) {
function click(event) {
let group = findGroupNodeByEventTarget(event.target)
if (group) {
let items = group.querySelectorAll(`[role=${event.target.role}]`)
let items = getItems(event.target, group);
for (let item of items) {
if (item !== event.target) {
item.setAttribute('tabindex', -1)
Expand Down
39 changes: 39 additions & 0 deletions test/focus-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,42 @@ test('is ready to click after focus', () => {
'</nav>'
)
})

test('adds toolbar widget', () => {
let window = new JSDOM().window
startKeyUX(window, [focusGroupKeyUX()])
window.document.body.innerHTML =
'<div role="toolbar">' +
'<div>' +
'<button type="button">Copy</button>' +
'<button type="button">Paste</button>' +
'<button type="button">Cut</button>' +
'</div>' +
'<div>' +
'<input type="checkbox">' +
'</div>' +
'</div>'
let buttons = window.document.querySelectorAll('button')
let checkboxes = window.document.querySelectorAll('[type="checkbox"]')
buttons[0].focus()

equal(window.document.activeElement, buttons[0])

press(window, 'ArrowRight')
equal(window.document.activeElement, buttons[1])

press(window, 'ArrowLeft')
equal(window.document.activeElement, buttons[0])

press(window, 'End')
equal(window.document.activeElement, checkboxes[0])

press(window, 'Home')
equal(window.document.activeElement, buttons[0])

press(window, 'ArrowLeft')
equal(window.document.activeElement, checkboxes[0])

press(window, 'ArrowRight')
equal(window.document.activeElement, buttons[0])
})
Loading