Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/components/z-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,30 @@ export class ZModal {
const activeElement = this.host.ownerDocument.activeElement;
const firstFocusableElement = focusableElements[0];
const lastFocusableElement = focusableElements[focusableElements.length - 1];
if (e.shiftKey && (shadowActiveElement == firstFocusableElement || activeElement == firstFocusableElement)) {
// shift + tab was pressed and current active element is the first focusable element

const focusIsOnFirst = shadowActiveElement == firstFocusableElement || activeElement == firstFocusableElement;
const focusIsOnLast = shadowActiveElement == lastFocusableElement || activeElement == lastFocusableElement;
const focusIsInList =
focusIsOnFirst ||
focusIsOnLast ||
focusableElements.includes(shadowActiveElement as HTMLElement) ||
focusableElements.includes(activeElement as HTMLElement);

if (e.shiftKey && focusIsOnFirst) {
e.preventDefault();
lastFocusableElement.focus();
} else if (!e.shiftKey && (shadowActiveElement == lastFocusableElement || activeElement == lastFocusableElement)) {
// shift + tab was pressed and current active element is the first focusable element
} else if (!e.shiftKey && focusIsOnLast) {
e.preventDefault();
firstFocusableElement.focus();
} else if (!focusIsInList) {
// Focus has escaped to a non-focusable element (e.g., the host or dialog container).
// Redirect it back into the modal.
e.preventDefault();
if (e.shiftKey) {
lastFocusableElement.focus();
} else {
firstFocusableElement.focus();
}
}
}

Expand Down
Loading