Skip to content

Instantly share code, notes, and snippets.

@minimul
Created May 12, 2020 16:55
Show Gist options
  • Save minimul/c9a45dcd3d206001f095e75a3f0f0110 to your computer and use it in GitHub Desktop.
Save minimul/c9a45dcd3d206001f095e75a3f0f0110 to your computer and use it in GitHub Desktop.
import { Controller } from "stimulus"
import animateCSS from 'lib/utils'
export default class extends Controller {
static targets = [ "menu" ]
static values = { invisible: Boolean }
connect() {
this.hide()
}
toggle(event) {
event.preventDefault()
this.changeBit()
this.run()
}
hide() {
this.menuTarget.classList.toggle('opacity-0')
this.menuTarget.classList.toggle('hidden')
}
run() {
let state = []
if (this.invisibleValue) {
state.push('dropOut', 'speed-400')
//state.push('animated', 'fadeOut', 'speed-400')
} else {
this.hide()
state.push('dropIn', 'speed-200')
//state.push('animated', 'fadeIn', 'speed-200')
}
animateCSS({ element: this.menuTarget, classes: state, callback: () => {
if (this.invisibleValue) {
this.hide()
}
}})
}
changeBit() {
this.invisibleValue = !this.invisibleValue
}
clickedAway(event) {
if (this.element.contains(event.target) === false && this.invisibleValue === false) {
this.changeBit()
this.run()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment