-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keyboard): add
[Tab]
support (#767)
- Loading branch information
1 parent
8f203cc
commit 87470ff
Showing
6 changed files
with
151 additions
and
122 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
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,77 @@ | ||
import {isDisabled} from '../misc/isDisabled' | ||
import {isDocument} from '../misc/isDocument' | ||
import {isElementType} from '../misc/isElementType' | ||
import {isVisible} from '../misc/isVisible' | ||
import {FOCUSABLE_SELECTOR} from './selector' | ||
|
||
export function getTabDestination( | ||
activeElement: Element, | ||
shift: boolean, | ||
focusTrap: Document | Element, | ||
) { | ||
const focusableElements = focusTrap.querySelectorAll(FOCUSABLE_SELECTOR) | ||
|
||
const enabledElements = Array.from(focusableElements).filter( | ||
el => | ||
el === activeElement || | ||
(el.getAttribute('tabindex') !== '-1' && | ||
!isDisabled(el) && | ||
// Hidden elements are not tabable | ||
isVisible(el)), | ||
) | ||
|
||
if (activeElement.getAttribute('tabindex') !== '-1') { | ||
// tabindex has no effect if the active element has tabindex="-1" | ||
enabledElements.sort( | ||
(a, b) => | ||
Number(a.getAttribute('tabindex')) - Number(b.getAttribute('tabindex')), | ||
) | ||
} | ||
|
||
const checkedRadio: Record<string, HTMLInputElement> = {} | ||
let prunedElements: Element[] = isDocument(focusTrap) ? [focusTrap.body] : [] | ||
const activeRadioGroup = isElementType(activeElement, 'input', { | ||
type: 'radio', | ||
}) | ||
? activeElement.name | ||
: undefined | ||
enabledElements.forEach(currentElement => { | ||
const el = currentElement as HTMLInputElement | ||
|
||
// For radio groups keep only the active radio | ||
// If there is no active radio, keep only the checked radio | ||
// If there is no checked radio, treat like everything else | ||
if (isElementType(el, 'input', {type: 'radio'}) && el.name) { | ||
// If the active element is part of the group, add only that | ||
if (el === activeElement) { | ||
prunedElements.push(el) | ||
return | ||
} else if (el.name === activeRadioGroup) { | ||
return | ||
} | ||
|
||
// If we stumble upon a checked radio, remove the others | ||
if (el.checked) { | ||
prunedElements = prunedElements.filter( | ||
e => !isElementType(e, 'input', {type: 'radio', name: el.name}), | ||
) | ||
prunedElements.push(el) | ||
checkedRadio[el.name] = el | ||
return | ||
} | ||
|
||
// If we already found the checked one, skip | ||
if (typeof checkedRadio[el.name] !== 'undefined') { | ||
return | ||
} | ||
} | ||
|
||
prunedElements.push(el) | ||
}) | ||
|
||
const currentIndex = prunedElements.findIndex(el => el === activeElement) | ||
|
||
const nextIndex = shift ? currentIndex - 1 : currentIndex + 1 | ||
const defaultIndex = shift ? prunedElements.length - 1 : 0 | ||
return prunedElements[nextIndex] || prunedElements[defaultIndex] | ||
} |
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