-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create get-entry-relative-to in utils
- Loading branch information
1 parent
005eb30
commit f3fcc11
Showing
4 changed files
with
50 additions
and
61 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,28 @@ | ||
/** | ||
* Helper method to find the element within the app menu at the given offset | ||
* (e.g. previous or next) relative to the specified element. | ||
* | ||
* @param {string} query - Argument that gets passed to document.querySelectorAll | ||
* @param {HTMLElement} element - Specified element (e.target) | ||
* @param {1 | -1} offset - Determines direction to move within array of focusable elements (previous or next) | ||
* @returns {HTMLElement} - element to be focused | ||
*/ | ||
|
||
export function getEntryRelativeTo( | ||
query: string, | ||
element: EventTarget, | ||
offset: 1 | -1 | ||
): HTMLElement { | ||
const entries = Array.from(document.querySelectorAll(query)) | ||
const firstElement = entries[0] | ||
const lastElement = entries[entries.length - 1] | ||
const elementIndex = entries.indexOf(element as HTMLButtonElement) | ||
|
||
if (element === firstElement && offset === -1) { | ||
return lastElement as HTMLElement | ||
} | ||
if (element === lastElement && offset === 1) { | ||
return firstElement as HTMLElement | ||
} | ||
return entries[elementIndex + offset] as HTMLElement | ||
} |