-
-
Save svinkle/f020f7a188844999a40973d3c4a16d23 to your computer and use it in GitHub Desktop.
Universal Popup Control
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
[aria-hidden="true"] { | |
display: none; | |
} |
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
<a href="#comment-form" data-popup="comment-form" aria-expanded="false">Leave a comment!</a> | |
<div id="comment-form" aria-hidden="true"> | |
<h3 tabindex="0">Leave a comment</h3> | |
<form> | |
<label for="comment">Comment</label> | |
<textarea id="comment"></textarea> | |
<button>Submit</button> | |
</form> | |
</div> |
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
// For use with little popup menus or subnavigation dropdowns. | |
const popupTriggers = document.querySelectorAll('[data-popup]'); | |
for (let popupTrigger of popupTriggers) { | |
let popupContainer = document.getElementById(popupTrigger.getAttribute('data-popup')); | |
popupTrigger.addEventListener('click', (event) => { | |
event.preventDefault(); | |
if (popupTrigger.getAttribute('aria-expanded') === 'true') { | |
popupTrigger.setAttribute('aria-expanded', 'false'); | |
popupContainer.setAttribute('aria-hidden', 'true'); | |
popupTrigger.focus(); | |
} else { | |
popupTrigger.setAttribute('aria-expanded', 'true'); | |
popupContainer.setAttribute('aria-hidden', 'false'); | |
popupContainer.querySelector('[tabindex="0"], input, textarea, select, button, a').focus(); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment