-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 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,48 @@ | ||
import { getTabbableNodes } from './utils/DOMutils'; | ||
import { isNotAGuard } from './utils/is'; | ||
|
||
const getRelativeFocusable = (element, scope) => { | ||
if (!element || !scope || !scope.contains(element)) { | ||
return {}; | ||
} | ||
const focusables = getTabbableNodes([scope]); | ||
const current = focusables.indexOf(element); | ||
if (current === -1) { | ||
return {}; | ||
} | ||
|
||
return { | ||
prev: focusables[current - 1], | ||
next: focusables[current + 1], | ||
first: focusables[0], | ||
last: focusables[focusables.length - 1], | ||
}; | ||
}; | ||
|
||
/** | ||
* focuses next element in the tab-order | ||
* @param baseElement | ||
* @param scope | ||
* @param cycle | ||
*/ | ||
export const focusNextElement = (baseElement, { scope = document.body, cycle = true }) => { | ||
const { next, first } = getRelativeFocusable(baseElement, scope); | ||
const node = next || (cycle && first); | ||
if (node) { | ||
node.focus(); | ||
} | ||
}; | ||
|
||
/** | ||
* focuses prev element in the tab order | ||
* @param baseElement | ||
* @param parent | ||
* @param cycle | ||
*/ | ||
export const focusPrevElement = (baseElement, { parent = document.body, cycle = true }) => { | ||
const { prev, last } = getRelativeFocusable(baseElement, parent); | ||
const node = prev || (cycle && last); | ||
if (node) { | ||
node.focus(); | ||
} | ||
}; |