Last active
July 3, 2022 14:25
-
-
Save mrjonesbot/4b1f2d345afbfb99e5256a2c46ec64dd to your computer and use it in GitHub Desktop.
Basic toggle controller to show/hide elements
This file contains hidden or 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 { Controller } from "@hotwired/stimulus" | |
| export default class extends Controller { | |
| static targets = [ "toggleable" ] | |
| connect() { | |
| this.toggleClass = this.data.get("class") || "hidden" | |
| } | |
| toggle(event) { | |
| event.preventDefault() | |
| this.toggleableTargets.forEach( target => { | |
| target.classList.toggle(this.toggleClass) | |
| }) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When implementing sidebar navigation with collapsable subsections, you can use the toggle controller to show/hide the subsections using whatever icon you'd like (in this case, an arrow).