Last active
April 29, 2020 22:05
-
-
Save minimul/967e4fd4df1f67b865abf5aeac10c32c to your computer and use it in GitHub Desktop.
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
// app/javascript/lib/utils.js | |
export default function animateCSS({ element, classes = [], callback }) { | |
const finalClasses = ['animated', ...classes] | |
element.classList.add(...finalClasses) | |
function handleAnimationEnd() { | |
element.classList.remove(...finalClasses) | |
element.removeEventListener('animationend', handleAnimationEnd) | |
if (typeof callback === 'function') callback() | |
} | |
element.addEventListener('animationend', handleAnimationEnd) | |
} | |
// Example -> Calling from animateCSS() from a Stimulus Controller | |
animateCSS({ element: this.menuTarget, classes: ['fadeIn', 'speed-200'], callback: () => { | |
if (this.invisibleValue) { | |
this.hide() | |
} | |
}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment