Skip to content

Instantly share code, notes, and snippets.

@leog
Created September 29, 2021 00:21
Show Gist options
  • Select an option

  • Save leog/8b667715d41e8ada6a1005a9be266f5a to your computer and use it in GitHub Desktop.

Select an option

Save leog/8b667715d41e8ada6a1005a9be266f5a to your computer and use it in GitHub Desktop.
Modal base (sin modularización ni función constructora)
const button = document.querySelector('button');
let modal;
let backdrop;
button.addEventListener('click', showModalHandler);
function showModalHandler() {
if (modal) {
return;
}
modal = document.createElement('div');
modal.className = 'modal';
const modalText = document.createElement('p');
modalText.textContent = 'Are you sure?';
const modalCancelAction = document.createElement('button');
modalCancelAction.textContent = 'Cancel';
modalCancelAction.className = 'btn btn--alt';
modalCancelAction.addEventListener('click', closeModalHandler);
const modalConfirmAction = document.createElement('button');
modalConfirmAction.textContent = 'Confirm';
modalConfirmAction.className = 'btn';
modalConfirmAction.addEventListener('click', closeModalHandler);
modal.append(modalText);
modal.append(modalCancelAction);
modal.append(modalConfirmAction);
document.body.append(modal);
backdrop = document.createElement('div');
backdrop.className = 'backdrop';
backdrop.addEventListener('click', closeModalHandler);
document.body.append(backdrop);
}
function closeModalHandler() {
modal.remove();
modal = null;
backdrop.remove();
backdrop = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment