Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Created May 10, 2020 05:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jongacnik/2a67e0ede0d039623075652d29f2ad07 to your computer and use it in GitHub Desktop.
import { define } from 'wicked-elements'
/**
* Toggles open class on target element on toggle click
*/
define('[data-component~="toggle"]', {
init () {
this.target = this.element.getAttribute('data-target')
this.toggle = this.toggle.bind(this)
this.targetElement = this.target === 'nextSibiling'
? this.element.nextElementSibling
: document.querySelector(`[data-ref="${this.target}"]`)
},
connected () {
this.element.addEventListener('click', this.toggle)
},
disconnected () {
this.element.removeEventListener('click', this.toggle)
},
toggle () {
this.element.classList.toggle('open')
this.targetElement.classList.toggle('open')
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment