Thursday, June 04, 2026 3:46:06 AM
> collapse_toggle_controller.js
import ApplicationController from "./application_controller";
import * as bootstrap from "bootstrap";

// Connects to data-controller="collapse-toggle"
export default class extends ApplicationController {
  static values = {
    defaultText: String,
    activeText: String,
    show: Boolean,
    target: String,
  };

  connect() {
    this.element.textContent = this.defaultTextValue;
    this.isExpanded = false;

    if (this.showValue) {
      this.toggle();
    }
  }

  toggle() {
    this.isExpanded = !this.isExpanded;

    this.element.textContent = this.isExpanded
      ? this.activeTextValue
      : this.defaultTextValue;

    const collapse = bootstrap.Collapse.getOrCreateInstance(this.targetValue);
    collapse.toggle();
  }
}
All opinions represented herein are my own
- © 2024 - 2026 itsthedevman
- build 4294fb2