Skip to content

Instantly share code, notes, and snippets.

@svinkle
Forked from davatron5000/popup.css
Last active June 17, 2017 12:16
Show Gist options
  • Save svinkle/f020f7a188844999a40973d3c4a16d23 to your computer and use it in GitHub Desktop.
Save svinkle/f020f7a188844999a40973d3c4a16d23 to your computer and use it in GitHub Desktop.
Universal Popup Control
[aria-hidden="true"] {
display: none;
}
<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>
// 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