Skip to content

Instantly share code, notes, and snippets.

@joeybeninghove
Created February 5, 2021 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeybeninghove/3c6e0ada6eba58f1ed8df156ba73b7c6 to your computer and use it in GitHub Desktop.
Save joeybeninghove/3c6e0ada6eba58f1ed8df156ba73b7c6 to your computer and use it in GitHub Desktop.
Toggle Controller
import { Controller } from 'stimulus'
export default class extends Controller {
static targets = ['content', 'keyword', 'iconDown', 'iconUp']
toggle () {
if (this.contentTarget.classList.contains('hidden')) {
this.showContent()
this.changeKeyword('Hide')
} else {
this.hideContent()
this.changeKeyword('Show')
}
this.toggleIcon()
}
showContent () {
this.contentTarget.classList.remove('hidden')
}
hideContent () {
this.contentTarget.classList.add('hidden')
}
changeKeyword (keyword) {
if (this.hasKeywordTarget) this.keywordTarget.innerHTML = keyword
}
toggleIcon () {
if (this.hasIconDownTarget) this.iconDownTarget.classList.toggle('hidden')
if (this.hasIconUpTarget) this.iconUpTarget.classList.toggle('hidden')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment