Created
September 29, 2021 00:21
-
-
Save leog/8b667715d41e8ada6a1005a9be266f5a to your computer and use it in GitHub Desktop.
Modal base (sin modularización ni función constructora)
This file contains hidden or 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
| 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