Created
May 10, 2020 05:58
-
-
Save jongacnik/2a67e0ede0d039623075652d29f2ad07 to your computer and use it in GitHub Desktop.
This file contains 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
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