Skip to content

Instantly share code, notes, and snippets.

@AgainPsychoX
Created May 3, 2023 11:40
Show Gist options
  • Save AgainPsychoX/0a191dfb3c86630fcaaa3ef9a6c22f78 to your computer and use it in GitHub Desktop.
Save AgainPsychoX/0a191dfb3c86630fcaaa3ef9a6c22f78 to your computer and use it in GitHub Desktop.
Utility code to fix transition animations when closing HTML5 dialog
dialog {
margin: auto;
width: 100%;
min-width: 30vw;
min-height: 20vw;
border: none;
box-shadow: 0 0 16px -4px rgba(0, 0, 0, 0.7);
opacity: 0;
}
dialog::backdrop {
background: rgba(0, 0, 0, 0.3);
opacity: 0;
}
dialog.open::backdrop,
dialog.open {
opacity: 1;
transition: opacity .2s;
}
dialog.closing::backdrop,
dialog.closing {
opacity: 0;
transition: opacity .2s;
}
@media (min-width: calc(960px - 32px)) {
dialog {
width: 480px;
}
}
document
.querySelectorAll('dialog')
.forEach(dialog => {
new MutationObserver((list, observer) => {
for (const mutation of list) {
if (typeof(mutation.oldValue) == 'object') {
dialog.classList.add('open');
}
}
}).observe(dialog, { attributeFilter: ['open'], attributeOldValue: true })
dialog.addEventListener('cancel', e => {
if (dialog.classList.contains('closing')) return;
e.preventDefault();
dialog.classList.add('closing');
});
dialog.addEventListener('transitionend', e => {
if (dialog.classList.contains('closing')) {
dialog.close();
dialog.classList.remove('open');
dialog.classList.remove('closing');
}
});
})
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment