I am trying to solve a tabbing issue inside my modal when a cross-origin <iframe> is present.
What I am noticing:
- If the iFrame is not the first or last focusable element in the modal, there is no need to set
.focus()with javascript. - If the iFrame is the first or last focusable element in the modal, I need to set
.focus()with javascript to maintain keyboard accessibility.
.focus() is restricted in this use. And as I can't set focus, the modal keyboard tabbing trap is broken.
Has anyone had success in this area?
HTML examples:
Works (I don't need to set focus with javascript on the iframe)
<input type="text" />
<iframe />
<input type="text" />
If iframe is the first or last focusable element, I will need to use javascript to set focus to maintain the modal keyboard trap
<input type="text" />
<iframe />
or
<iframe />
<input type="text" />
javascript:
var KEY = {
TAB: 9,
},
allowedFocusableElements =
'a[href], area[href], input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]';
function openDialog(e) {
// Get focusable elements.
dialogFocusableElements = dialog.querySelectorAll(allowedFocusableElements);
dialogFirstElement = dialogFocusableElements[0];
dialogLastElement = dialogFocusableElements[dialogFocusableElements.length - 1];
document.addEventListener("keydown", processKeys);
}
function processKeys(event) {
if (event.keyCode === KEY.TAB && !event.shiftKey) {
if (document.activeElement === dialogLastElement) {
event.preventDefault();
dialogFirstElement.focus();
}
} else if (event.keyCode === KEY.TAB && event.shiftKey) {
if (document.activeElement === dialogFirstElement) {
event.preventDefault();
dialogLastElement.focus();
}
}
}