-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move focusgroup functionality to a separate polyfill (#51)
* Separate focusgroup attribute logic from roles into its own polyfill * Add hotkey compatibility with focusgroup polyfill * Adapt and separate test cases for focusgroup polyfill * Add TS types and export * Reduce size to 2.2 kB * Update documentation * Add additional compatibility tests * Remove the hotkeys search from the polyfill
- Loading branch information
1 parent
8f46cf8
commit 62e3ef3
Showing
10 changed files
with
1,082 additions
and
732 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
function focus(current, next) { | ||
if (next) { | ||
next.tabIndex = 0 | ||
next.focus() | ||
current.tabIndex = -1 | ||
} | ||
} | ||
|
||
function findGroupNodeByEventTarget(target) { | ||
let fg = target.closest('[focusgroup]:not([focusgroup="none"])') | ||
if (fg) return fg | ||
} | ||
|
||
function getItems(group) { | ||
if (group.hasAttribute('focusgroup')) { | ||
let items = [...group.querySelectorAll('*:not([focusgroup="none"])')] | ||
return items.filter(item => { | ||
return ( | ||
item.role === 'button' || | ||
item.type === 'button' || | ||
item.role === 'checkbox' || | ||
item.type === 'checkbox' | ||
) | ||
}) | ||
} | ||
} | ||
|
||
function isHorizontalOrientation(group) { | ||
let fg = group.getAttribute('focusgroup') | ||
if (fg !== null) return !fg.split(' ').includes('block') | ||
} | ||
|
||
export function focusGroupPolyfill() { | ||
return window => { | ||
let inGroup = false | ||
|
||
function keyDown(event) { | ||
let group = findGroupNodeByEventTarget(event.target) | ||
|
||
if (!group) { | ||
stop() | ||
return | ||
} | ||
|
||
let items = getItems(group) | ||
let index = Array.from(items).indexOf(event.target) | ||
|
||
let nextKey = 'ArrowDown' | ||
let prevKey = 'ArrowUp' | ||
if (isHorizontalOrientation(group)) { | ||
if (window.document.dir === 'rtl') { | ||
nextKey = 'ArrowLeft' | ||
prevKey = 'ArrowRight' | ||
} else { | ||
nextKey = 'ArrowRight' | ||
prevKey = 'ArrowLeft' | ||
} | ||
} | ||
|
||
if (event.key === nextKey) { | ||
event.preventDefault() | ||
if (items[index + 1]) { | ||
focus(event.target, items[index + 1]) | ||
} else if (group.getAttribute('focusgroup').includes('wrap')) { | ||
focus(event.target, items[0]) | ||
} | ||
} else if (event.key === prevKey) { | ||
event.preventDefault() | ||
if (items[index - 1]) { | ||
focus(event.target, items[index - 1]) | ||
} else if (group.getAttribute('focusgroup').includes('wrap')) { | ||
focus(event.target, items[items.length - 1]) | ||
} | ||
} else if (event.key === 'Home') { | ||
event.preventDefault() | ||
focus(event.target, items[0]) | ||
} else if (event.key === 'End') { | ||
event.preventDefault() | ||
focus(event.target, items[items.length - 1]) | ||
} | ||
} | ||
|
||
function stop() { | ||
inGroup = false | ||
window.removeEventListener('keydown', keyDown) | ||
} | ||
|
||
function focusIn(event) { | ||
let group = findGroupNodeByEventTarget(event.target) | ||
if (group) { | ||
if (!inGroup) { | ||
inGroup = true | ||
window.addEventListener('keydown', keyDown) | ||
} | ||
|
||
let items = getItems(group) | ||
if (!items.some(item => item.getAttribute('tabindex') === '0')) { | ||
items.forEach((item, index) => | ||
item.setAttribute('tabindex', index === 0 ? 0 : -1) | ||
) | ||
items[0]?.focus() | ||
} else { | ||
items.forEach(item => { | ||
if (item !== event.target) item.setAttribute('tabindex', -1) | ||
}) | ||
} | ||
} else if (inGroup) { | ||
stop() | ||
} | ||
} | ||
|
||
function focusOut(event) { | ||
let group = findGroupNodeByEventTarget(event.target) | ||
if (group?.getAttribute('focusgroup')?.includes('no-memory')) { | ||
let items = getItems(group) | ||
items.forEach((item, index) => { | ||
item.setAttribute('tabindex', index === 0 ? 0 : -1) | ||
}) | ||
} | ||
|
||
if (!event.relatedTarget || event.relatedTarget === window.document) { | ||
stop() | ||
} | ||
} | ||
|
||
function click(event) { | ||
let group = findGroupNodeByEventTarget(event.target) | ||
if (group) { | ||
let items = getItems(group) | ||
for (let item of items) { | ||
if (item !== event.target) { | ||
item.setAttribute('tabindex', -1) | ||
} | ||
} | ||
event.target.setAttribute('tabindex', 0) | ||
} | ||
} | ||
|
||
window.addEventListener('click', click) | ||
window.addEventListener('focusin', focusIn) | ||
window.addEventListener('focusout', focusOut) | ||
return () => { | ||
stop() | ||
window.removeEventListener('click', click) | ||
window.removeEventListener('focusin', focusIn) | ||
window.removeEventListener('focusout', focusOut) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.