Sha256: a4dac4531ab1f48a602a1e65617034cdc3a264a560e89177ccd91060c31adbe8
Contents?: true
Size: 1.01 KB
Versions: 27
Compression:
Stored size: 1.01 KB
Contents
import { Controller } from "@hotwired/stimulus" // Connects to data-controller="resource-collapse" export default class extends Controller { static targets = ["trigger", "menu"] connect() { // Default to false if the data attribute isn't set if (!this.element.hasAttribute('data-visible')) { this.element.setAttribute('data-visible', 'false') } // Set initial state this.#updateState() } toggle() { const isVisible = this.element.getAttribute('data-visible') === 'true' this.element.setAttribute('data-visible', (!isVisible).toString()) this.#updateState() } #updateState() { const isVisible = this.element.getAttribute('data-visible') === 'true' if (isVisible) { this.menuTarget.classList.remove('hidden') this.triggerTarget.setAttribute('aria-expanded', 'true') this.dispatch('expand') } else { this.menuTarget.classList.add('hidden') this.triggerTarget.setAttribute('aria-expanded', 'false') this.dispatch('collapse') } } }
Version data entries
27 entries across 27 versions & 1 rubygems