Last active
September 19, 2021 01:42
-
-
Save davatron5000/863a32d8ce6144f5cf0063fbf75c0603 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="#" aria-haspopup="true" aria-controls="expandable" aria-expanded="false">Make something expand</a> | |
<div id="expandable" aria-hidden="true"> | |
<h3>Leave a commment</h3> | |
<form> | |
<label for="comment">Comment</label> | |
<textarea id="comment"></textarea> | |
</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('[aria-haspopup]'); | |
for(let i = 0; i<popupTriggers.length; i++) { | |
let popupTrigger = popupTriggers[i]; | |
let popupTarget = document.getElementById(popupTrigger.getAttribute('aria-controls')); | |
popupTrigger.addEventListener('click', function( event ){ | |
event.preventDefault(); | |
if(popupTrigger.getAttribute('aria-expanded') === "true") { | |
popupTrigger.setAttribute('aria-expanded', 'false'); | |
popupTarget.setAttribute('aria-hidden', 'true'); | |
// TODO: Set focus to previous focus | |
} else { | |
popupTrigger.setAttribute('aria-expanded', 'true'); | |
popupTarget.setAttribute('aria-hidden', 'false'); | |
popupTarget.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