It might be worthy of note, that animationend
/transitionend
events are emitted for every animated element in the element tree you attach the listeners to:
#parent:hover {
transition: ... 0.5s;
}
#child:hover {
transition: ... 0.2s;
}
<div id="parent">
<div id="child">Hover me</div>
<div>
...
parent.addEventListener('animationend', (event) => {
console.log('animated', event.target);
});
animated div#child
animated div#parent
As such, if you make an animated container that can have animated children, then you should account for event bubbling:
dialog.addEventListener('close', closeHandler);
dialog.addEventListener('transitionend', animationHandler);
let returnValue;
const closeHandler = () => {
returnValue = ...;
};
const animationHandler = ({ target }) => {
// make sure it's the dialog that ended animating,
// and not some button's 'onblur' animation
if (target === dialog) {
onClose(returnValue);
}
};