Use aria-expanded instead of class

This commit is contained in:
Jorge Manrubia
2025-09-28 11:31:03 +02:00
parent 1b39e0b45e
commit 5a90de8bcb
10 changed files with 29 additions and 24 deletions
@@ -1,27 +1,26 @@
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 isCurrentlyCollapsed = clickedColumn.classList.contains(this.collapsedClass)
const isCurrentlyExpanded = clickedColumn.getAttribute("aria-expanded") === "true"
this.columnTargets.forEach(column => {
column.classList.add(this.collapsedClass)
column.setAttribute("aria-expanded", "false")
})
if (isCurrentlyCollapsed) {
clickedColumn.classList.remove(this.collapsedClass)
if (!isCurrentlyExpanded) {
clickedColumn.setAttribute("aria-expanded", "true")
}
}
preventToggle(event) {
if (event.detail.attributeName === "class") {
if (event.detail.attributeName === "aria-expanded") {
event.preventDefault()
}
}