Skip to content

Commit

Permalink
Close menu target before turbo cache. Fixes #224
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Mar 14, 2024
1 parent 4c204ad commit dbb681a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ export default class extends Controller {
static values = { open: Boolean, default: false }

connect() {
document.addEventListener("turbo:before-cache", this.beforeCache.bind(this))

if (this.hasButtonTarget) {
this.buttonTarget.addEventListener("keydown", this._onMenuButtonKeydown)
this.buttonTarget.setAttribute("aria-haspopup", "true")
}
}

disconnect() {
document.removeEventListener("turbo:before-cache", this.beforeCache.bind(this))

if (this.hasButtonTarget) {
this.buttonTarget.removeEventListener("keydown", this._onMenuButtonKeydown)
this.buttonTarget.removeAttribute("aria-haspopup")
Expand All @@ -28,7 +32,11 @@ export default class extends Controller {
}

show() {
this.openValue = true;
this.openValue = true
}

close() {
this.openValue = false
}

hide(event) {
Expand All @@ -54,4 +62,10 @@ export default class extends Controller {
get currentItemIndex() {
return this.menuItemTargets.indexOf(document.activeElement)
}

// Ensures the menu is hidden before Turbo caches the page
beforeCache() {
this.openValue = false
this.menuTarget.classList.add("hidden")
}
}

0 comments on commit dbb681a

Please sign in to comment.