Skip to content

Instantly share code, notes, and snippets.

@minimul
Last active April 29, 2020 22:05
Show Gist options
  • Save minimul/967e4fd4df1f67b865abf5aeac10c32c to your computer and use it in GitHub Desktop.
Save minimul/967e4fd4df1f67b865abf5aeac10c32c to your computer and use it in GitHub Desktop.
// 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