Skip to content

Commit

Permalink
Allow user menu to be close after a click inside it (#1005)
Browse files Browse the repository at this point in the history
Previously if you clicked somewhere inside the user menu, without
clicking on an actual link it would effectively lock the menu into an
open state with no way to close it.

This makes it so that a click outside the menu will close it.

Fixes #987
  • Loading branch information
jagthedrummer authored Jan 23, 2025
1 parent b30349d commit 4c16bc4
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bullet_train/app/javascript/controllers/desktop_menu_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { Controller } from "@hotwired/stimulus";
export default class extends Controller {
static targets = [ "menuItemHeader", "menuItemGroup", "menuItemLink" ];

connect() {
document.addEventListener('click', this.handleClickOutside.bind(this))
}

disconnect() {
document.removeEventListener('click', this.handleClickOutside.bind(this))
}

showSubmenu() {
this.menuItemGroupTarget.classList.remove('invisible');
}
Expand All @@ -23,4 +31,10 @@ export default class extends Controller {

if(hideMenu) { this.menuItemGroupTarget.classList.add('invisible'); }
}

handleClickOutside(event) {
if (!this.element.contains(event.target)) {
this.menuItemGroupTarget.classList.add('invisible')
}
}
}

0 comments on commit 4c16bc4

Please sign in to comment.