Created
June 26, 2020 22:40
-
-
Save Juandresyn/619b7cc4ea1ee93b23cd2ddc7c334272 to your computer and use it in GitHub Desktop.
Trap focus modals
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
function handleVisibleElements(modal, hide = null) { | |
let parent = $(modal).parent(); | |
let grandparent = null; | |
const parentCount = $(modal).parents().length - 1; | |
const mainTag = $('body main').length ? 'main' : 'body'; | |
const tagToCheck = $(`${mainTag} > article`).length | |
? 'article' | |
: '.wrapper'; | |
const hideSiblings = element => { | |
$(element) | |
.siblings() | |
.attr('aria-hidden', hide); | |
}; | |
for (let index = 0; index < parentCount; index + 1) { | |
if ( | |
!$(parent) | |
.parent() | |
.is(tagToCheck) | |
) { | |
parent = $(parent).parent(); | |
} else { | |
grandparent = $(parent).parent(); | |
break; | |
} | |
} | |
const elements = [parent, grandparent]; | |
if (mainTag === 'main') { | |
elements.push($(mainTag)); | |
} | |
elements.map(hideSiblings); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment