-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(esl-utils): focus order utility
- Loading branch information
Showing
1 changed file
with
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {TAB} from './keys'; | ||
|
||
/** | ||
* Chain focus order between two elements. | ||
* Passive (should be called inside keyboard event handler) | ||
*/ | ||
export const handleFocusChain = (e: KeyboardEvent, first: HTMLElement, last: HTMLElement) => { | ||
if (e.key !== TAB ) return; | ||
if (last && e.target === first && e.shiftKey) { | ||
last.focus(); | ||
e.preventDefault(); | ||
return true; | ||
} | ||
if (first && e.target === last && !e.shiftKey) { | ||
first.focus(); | ||
e.preventDefault(); | ||
return true; | ||
} | ||
}; |