Skip to content

Instantly share code, notes, and snippets.

@navdeepsingh
Last active April 19, 2022 11:50
Show Gist options
  • Save navdeepsingh/a702e561427fa9609dedfe81d3cde167 to your computer and use it in GitHub Desktop.
Save navdeepsingh/a702e561427fa9609dedfe81d3cde167 to your computer and use it in GitHub Desktop.
Simple Modal JS
// Will hold previously focused element
var focusedElementBeforeModal;
// Find the modal and its overlay
var modal = document.querySelector('.modal');
var modalOverlay = document.querySelector('.modal-overlay');
var modalToggle = document.querySelector('.modal-toggle');
modalToggle.addEventListener('click', openModal);
function openModal() {
// Save current focus
focusedElementBeforeModal = document.activeElement;
// Listen for and trap the keyboard
modal.addEventListener('keydown', trapTabKey);
// Listen for indicators to close the modal
modalOverlay.addEventListener('click', closeModal);
// Sign-Up button
var signUpBtn = modal.querySelector('#signup');
signUpBtn.addEventListener('click', closeModal);
// Find all focusable children
var focusableElementsString = 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]';
var focusableElements = modal.querySelectorAll(focusableElementsString);
// Convert NodeList to Array
focusableElements = Array.prototype.slice.call(focusableElements);
var firstTabStop = focusableElements[0];
var lastTabStop = focusableElements[focusableElements.length - 1];
// Show the modal and overlay
modal.style.display = 'block';
modalOverlay.style.display = 'block';
// Focus first child
firstTabStop.focus();
function trapTabKey(e) {
// Check for TAB key press
if (e.keyCode === 9) {
// SHIFT + TAB
if (e.shiftKey) {
if (document.activeElement === firstTabStop) {
e.preventDefault();
lastTabStop.focus();
}
// TAB
} else {
if (document.activeElement === lastTabStop) {
e.preventDefault();
firstTabStop.focus();
}
}
}
// ESCAPE
if (e.keyCode === 27) {
closeModal();
}
}
}
function closeModal() {
// Hide the modal and overlay
modal.style.display = 'none';
modalOverlay.style.display = 'none';
// Set focus back to element that had it before the modal was opened
focusedElementBeforeModal.focus();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment