-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(trapFocus): added to keep focus when dialogs are open
fix(Modal): added trapFocus fix(Drawer): added trapFocus fix(Drawer): added example for multi-level drawers
- Loading branch information
Craig Howell
committed
Feb 6, 2023
1 parent
9178e9f
commit d15858f
Showing
6 changed files
with
225 additions
and
8 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,43 @@ | ||
let trapFocusList: HTMLElement[] = []; | ||
|
||
if (typeof window !== 'undefined') { | ||
const isNext = (event: KeyboardEvent) => event.keyCode === 9 && !event.shiftKey; | ||
const isPrevious = (event: KeyboardEvent) => event.keyCode === 9 && event.shiftKey; | ||
const trapFocusListener = (event: KeyboardEvent) => { | ||
if (event.target === window) { | ||
return; | ||
} | ||
|
||
const eventTarget = event.target as unknown as Element; | ||
|
||
const parentNode = trapFocusList.find((node) => node.contains(eventTarget)); | ||
if (!parentNode) { | ||
return; | ||
} | ||
|
||
const focusable: NodeListOf<HTMLElement> = parentNode.querySelectorAll( | ||
'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]' | ||
); | ||
const first = focusable[0]; | ||
const last = focusable[focusable.length - 1]; | ||
if (isNext(event) && event.target === last) { | ||
event.preventDefault(); | ||
first.focus(); | ||
} else if (isPrevious(event) && event.target === first) { | ||
event.preventDefault(); | ||
last.focus(); | ||
} | ||
}; | ||
|
||
document.addEventListener('keydown', trapFocusListener); | ||
} | ||
|
||
export const trapFocus = (node: HTMLElement) => { | ||
trapFocusList.push(node); | ||
node.getElementsByTagName('button')[0].focus(); | ||
return { | ||
destroy() { | ||
trapFocusList = trapFocusList.filter((element) => element !== node); | ||
} | ||
}; | ||
}; |
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
d15858f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
stwui – ./
stwui.vercel.app
stwui-git-main-n00nday.vercel.app
stwui-n00nday.vercel.app