Skip to content

Commit

Permalink
feat(esl-utils): focus order utility
Browse files Browse the repository at this point in the history
  • Loading branch information
ala-n committed May 20, 2021
1 parent 0915fd6 commit bd61f99
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/modules/esl-utils/dom/focus.ts
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;
}
};

0 comments on commit bd61f99

Please sign in to comment.