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

adds focusgroup attribute basic support #46

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 9 additions & 2 deletions focus-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function focus(current, next) {
}

function findGroupNodeByEventTarget(target) {
let fg = target.closest('[focusgroup]');
if (fg) return fg;

let itemRole = target.role || target.type || target.tagName
if (!itemRole) return null

Expand All @@ -26,7 +29,7 @@ function findGroupNodeByEventTarget(target) {
}

function getItems(target, group) {
if (group.role === 'toolbar') return getToolbarItems(group)
if (group.role === 'toolbar' || group.hasAttribute('focusgroup')) return getToolbarItems(group)
return group.querySelectorAll(`[role=${target.role}]`)
}

Expand All @@ -37,12 +40,16 @@ function getToolbarItems(group) {
item.role === 'button' ||
item.type === 'button' ||
item.role === 'checkbox' ||
item.type === 'checkbox'
item.type === 'checkbox' ||
item.hasAttribute('tabindex')
ai marked this conversation as resolved.
Show resolved Hide resolved
)
})
}

function isHorizontalOrientation(group) {
let fg = group.getAttribute('focusgroup')
if (fg !== null) return !fg.split(' ').includes('block');

let ariaOrientation = group.getAttribute('aria-orientation')
if (ariaOrientation === 'vertical') return false
if (ariaOrientation === 'horizontal') return true
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, hotkeyOverrides, hotkeyMacCompat }"
},
"limit": "2194 B"
"limit": "2248 B"
}
],
"clean-publish": {
Expand Down
11 changes: 11 additions & 0 deletions test/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@
border: 1px solid #aaa;
padding: 1em;
}
.focusgroup {
margin-top: 1em;
gap: 16px;
align-items: center;
border: 1px solid #aaa;
padding: 0.5em 1em;
background: #eee;
}
.focusgroup-item {
padding: 0.5em 1em;
}
.toolbar {
margin-top: 1em;
display: flex;
Expand Down
54 changes: 54 additions & 0 deletions test/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,57 @@ const Toolbar: FC = () => {
)
}

const FocusGroup: FC = () => {
return (
<>
<div
className="focusgroup"
// @ts-expect-error
focusgroup={''}
tabIndex={0}
>
<span className="focusgroup-item" role="menuitem" tabIndex={-1}>Red</span>
<span className="focusgroup-item" role="menuitem" tabIndex={-1}>Yellow</span>
<span className="focusgroup-item" role="menuitem" tabIndex={-1}>Green</span>
</div>
</>
)
}

const FocusGroupInline: FC = () => {
return (
<>
<div
className="focusgroup"
// @ts-expect-error
focusgroup="inline"
tabIndex={0}
>
<span className="focusgroup-item" role="menuitem" tabIndex={-1}>Mac</span>
<span className="focusgroup-item" role="menuitem" tabIndex={-1}>Windows</span>
<span className="focusgroup-item" role="menuitem" tabIndex={-1}>Linux</span>
</div>
</>
)
}

const FocusGroupBlock: FC = () => {
return (
<>
<div
className="focusgroup"
// @ts-expect-error
focusgroup="block"
tabIndex={0}
>
<div className="focusgroup-item" role="menuitem" tabIndex={-1}>Dog</div>
<div className="focusgroup-item" role="menuitem" tabIndex={-1}>Cat</div>
<div className="focusgroup-item" role="menuitem" tabIndex={-1}>Turtle</div>
</div>
</>
)
}

const App: FC = () => {
let [, setUpdate] = useState({})
let [router, setRouter] = useState('home')
Expand All @@ -399,6 +450,9 @@ const App: FC = () => {
/>
<Tabs />
<Toolbar />
<FocusGroup />
<FocusGroupInline />
<FocusGroupBlock />
</>
)
}
Expand Down
100 changes: 100 additions & 0 deletions test/focus-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,103 @@ test('adds toolbar widget', () => {
press(window, 'ArrowRight')
equal(window.document.activeElement, buttons[0])
})

test('adds focusgroup widget', () => {
let window = new JSDOM().window
startKeyUX(window, [focusGroupKeyUX()])
window.document.body.innerHTML =
'<div focusgroup tabIndex="-1">' +
'<span tabindex="-1">Mac</span>' +
'<span tabindex="-1">Windows</span>' +
'<span tabindex="-1">Linux</span>' +
'</div>'
let inlineElements = window.document.querySelectorAll('span')
inlineElements[0].focus()

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

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

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

press(window, 'End')
equal(window.document.activeElement, inlineElements[2])

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

press(window, 'ArrowLeft')
equal(window.document.activeElement, inlineElements[2])

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

test('adds focusgroup inline widget', () => {
let window = new JSDOM().window
startKeyUX(window, [focusGroupKeyUX()])
window.document.body.innerHTML =
'<div focusgroup="inline" tabIndex="-1">' +
'<span tabindex="-1">Mac</span>' +
'<span tabindex="-1">Windows</span>' +
'<span tabindex="-1">Linux</span>' +
'</div>'
let inlineElements = window.document.querySelectorAll('span')
inlineElements[0].focus()

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

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

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

press(window, 'End')
equal(window.document.activeElement, inlineElements[2])

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

press(window, 'ArrowLeft')
equal(window.document.activeElement, inlineElements[2])

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

test('adds focusgroup block widget', () => {
let window = new JSDOM().window
startKeyUX(window, [focusGroupKeyUX()])
window.document.body.innerHTML =
'<div focusgroup="block" tabIndex="-1">' +
'<div tabindex="-1">Dog</div>' +
'<div tabindex="-1">Cat</div>' +
'<div tabindex="-1">Turtle</div>' +
'</div>'
let blockElements = window.document.querySelectorAll('div:not([focusgroup="block"])');
// @ts-ignore
blockElements[0].focus()

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

press(window, 'ArrowDown')
equal(window.document.activeElement, blockElements[1])

press(window, 'ArrowUp')
equal(window.document.activeElement, blockElements[0])

press(window, 'End')
equal(window.document.activeElement, blockElements[2])

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

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

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