Restore the class-based approach

aria-expanded should go on the toggle
This commit is contained in:
Jorge Manrubia
2025-09-28 13:02:58 +02:00
parent c4a23102e9
commit 531315f253
6 changed files with 20 additions and 22 deletions
@@ -1,26 +1,27 @@
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static classes = [ "collapsed" ]
static targets = [ "column" ]
toggle(event) {
const clickedColumn = event.target.closest("[data-collapsible-columns-target='column']")
const clickedColumn = event.target.closest('[data-collapsible-columns-target="column"]')
if (!clickedColumn) return
const isCurrentlyExpanded = clickedColumn.getAttribute("aria-expanded") === "true"
const isCurrentlyCollapsed = clickedColumn.classList.contains(this.collapsedClass)
this.columnTargets.forEach(column => {
column.setAttribute("aria-expanded", "false")
column.classList.add(this.collapsedClass)
})
if (!isCurrentlyExpanded) {
clickedColumn.setAttribute("aria-expanded", "true")
if (isCurrentlyCollapsed) {
clickedColumn.classList.remove(this.collapsedClass)
}
}
preventToggle(event) {
if (event.detail.attributeName === "aria-expanded") {
if (event.detail.attributeName === "class") {
event.preventDefault()
}
}