toggle
Generic show/hide functionality for accordions, dropdowns, and FAQs.
Live Demo
This content is toggled by the controller. It can contain any HTML.
Usage
<div data-controller="toggle">
<button data-action="click->toggle#toggle">
Toggle
</button>
<div data-toggle-target="content" class="hidden">
Hidden content
</div>
</div>
Targets
| Target | Purpose |
|---|---|
content |
The element to show/hide |
Actions
| Action | Purpose |
|---|---|
toggle |
Toggle visibility |
show |
Show the content |
hide |
Hide the content |
Source
import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static targets = ["content"];
static values = {
hiddenClass: { type: String, default: "hidden" }
};
toggle() {
this.contentTarget.classList.toggle(this.hiddenClassValue);
}
show() {
this.contentTarget.classList.remove(this.hiddenClassValue);
}
hide() {
this.contentTarget.classList.add(this.hiddenClassValue);
}
}