mirror of
https://github.com/fly-apps/live_beats.git
synced 2024-11-21 23:50:59 +00:00
Use keydown event to detect keyboard based clicks
Relying on e.detail is not supported on all OS/browsers
This commit is contained in:
parent
7d8e1f2ec6
commit
6622f791e0
1 changed files with 8 additions and 7 deletions
|
@ -19,7 +19,7 @@ Hooks.Menu = {
|
|||
return val
|
||||
},
|
||||
reset(){
|
||||
this.enabled = true
|
||||
this.enabled = false
|
||||
this.activeClass = this.getAttr("data-active-class")
|
||||
this.deactivate(this.menuItems())
|
||||
this.activeItem = null
|
||||
|
@ -30,17 +30,18 @@ Hooks.Menu = {
|
|||
this.menuItemsContainer = document.querySelector(`[aria-labelledby="${this.el.id}"]`)
|
||||
this.reset()
|
||||
this.handleKeyDown = (e) => this.onKeyDown(e)
|
||||
this.el.addEventListener("keydown", e => {
|
||||
if((e.key === "Enter" || e.key === " ") && e.currentTarget.isSameNode(this.el)){
|
||||
this.enabled = true
|
||||
}
|
||||
})
|
||||
this.el.addEventListener("click", e => {
|
||||
if(!e.currentTarget.isSameNode(this.el)){ return }
|
||||
|
||||
window.addEventListener("keydown", this.handleKeyDown)
|
||||
// disable if button clicked and click was not a keyboard event
|
||||
if(e.detail !== 0){
|
||||
this.enabled = false
|
||||
} else {
|
||||
if(this.enabled){
|
||||
window.requestAnimationFrame(() => this.activate(0))
|
||||
}
|
||||
if(this.enabled){
|
||||
window.requestAnimationFrame(() => this.activate(0))
|
||||
}
|
||||
})
|
||||
this.menuItemsContainer.addEventListener("phx:hide", () => this.reset())
|
||||
|
|
Loading…
Reference in a new issue