-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create dropdown Stimulus controller * Create custom dropdown css ruleset mimicking geeksui styles * Apply new dropdown controller and custom styles to nav dropdown * Remove random comment * Add aria attributes to navbar button and menu * Add closing dropdown by clicking outside of it * Add comments for custom code * Refactor to erb comment
- Loading branch information
Showing
3 changed files
with
68 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
|
||
// Dropdown menu controller | ||
// Use only where theme dropdown menu defaults (hover-open) are undesirable. | ||
export default class extends Controller { | ||
static targets = ["button", "menu"]; | ||
|
||
initialize() { | ||
this.close(); | ||
this.closeMenuOnOutsideClick = this.closeMenuOnOutsideClick.bind(this); | ||
} | ||
|
||
connect() { | ||
window.addEventListener("click", this.closeMenuOnOutsideClick); | ||
} | ||
|
||
disconnect() { | ||
window.removeEventListener("click", this.closeMenuOnOutsideClick); | ||
} | ||
|
||
toggle() { | ||
this.menuTarget.hidden === true ? this.open() : this.close(); | ||
} | ||
|
||
open() { | ||
this.menuTarget.hidden = false; | ||
this.menuTarget.setAttribute("aria-hidden", "false"); | ||
this.buttonTarget.setAttribute("aria-expanded", "true"); | ||
} | ||
|
||
close() { | ||
this.menuTarget.hidden = true; | ||
this.menuTarget.setAttribute("aria-hidden", "true"); | ||
this.buttonTarget.setAttribute("aria-expanded", "false"); | ||
} | ||
|
||
closeMenuOnOutsideClick(event) { | ||
if (this.element === event.target || this.element.contains(event.target)) { | ||
return; | ||
} | ||
|
||
this.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters