Created
April 5, 2022 09:48
-
-
Save wtho/bcc6e1edd883429b85d081e866607533 to your computer and use it in GitHub Desktop.
HTML <dialog> fade-in and fade-out
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
| dialog, | |
| dialog[open]::backdrop { | |
| visibility: hidden; | |
| opacity: 0; | |
| transition: visibility 0s linear 0.5s, opacity 0.5s linear; | |
| } | |
| dialog[open].fade-in, | |
| dialog[open].fade-in::backdrop { | |
| transition-delay: 0s; | |
| } | |
| dialog[open].fade-in, | |
| dialog[open].fade-in::backdrop { | |
| opacity: 1; | |
| visibility: visible; | |
| } | |
| dialog::backdrop { | |
| backdrop-filter: blur(3px); | |
| } |
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
| <dialog id="dialog"> | |
| <button type="button" id="close-dialog-button"> | |
| fade out | |
| </button> | |
| </dialog> | |
| <main> | |
| <button type="button" id="open-dialog-button"> | |
| fade in | |
| </button> | |
| </main> |
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 getDialog = () => document.getElementById('dialog') | |
| const getOpenDialogButton = () => document.getElementById('open-dialog-button') | |
| const getCloseDialogButton = () => document.getElementById('close-dialog-button') | |
| const closeModal = () => { | |
| const dialog = getDialog() | |
| dialog.close(); | |
| dialog.removeEventListener('transitionend', closeModal); | |
| } | |
| getCloseDialogButton().addEventListener('click', function() { | |
| const dialog = getDialog() | |
| dialog.classList.remove('fade-in'); | |
| dialog.addEventListener('transitionend', closeModal); | |
| }); | |
| getOpenDialogButton().addEventListener('click', function() { | |
| const dialog = getDialog() | |
| dialog.showModal(); | |
| dialog.classList.add('fade-in'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment