90 lines
3.3 KiB
JavaScript
90 lines
3.3 KiB
JavaScript
import { Controller } from '@hotwired/stimulus'
|
|
|
|
/*
|
|
* A small menu behind a "⋯" button: the button (menu#toggle) opens the panel (target "panel") below
|
|
* itself. The panel is positioned fixed, so a scrolling container can't cut it off. Escape, a click
|
|
* outside or a click on one of its items closes it; arrow keys move between the items. Scrolling or
|
|
* resizing closes it too: a fixed panel would stay put while its row moves away (a table swiped
|
|
* sideways on a phone).
|
|
*/
|
|
export default class extends Controller {
|
|
static targets = ['button', 'panel']
|
|
|
|
connect () {
|
|
this.onOutside = (event) => {
|
|
if (!this.element.contains(event.target)) {
|
|
this.close()
|
|
}
|
|
}
|
|
this.onKey = (event) => this.key(event)
|
|
this.onScroll = (event) => {
|
|
if (!this.panelTarget.contains(event.target)) {
|
|
this.close()
|
|
}
|
|
}
|
|
this.onResize = () => this.close()
|
|
}
|
|
|
|
disconnect () {
|
|
this.close()
|
|
}
|
|
|
|
toggle (event) {
|
|
event.stopPropagation()
|
|
this.panelTarget.hidden ? this.open() : this.close()
|
|
}
|
|
|
|
open () {
|
|
const box = this.buttonTarget.getBoundingClientRect()
|
|
const panel = this.panelTarget
|
|
panel.hidden = false
|
|
// below the button, aligned to its right edge, and inside the window
|
|
const left = Math.max(8, Math.min(box.right - panel.offsetWidth, window.innerWidth - panel.offsetWidth - 8))
|
|
const below = box.bottom + 4 + panel.offsetHeight <= window.innerHeight
|
|
panel.style.left = `${left}px`
|
|
panel.style.top = `${below ? box.bottom + 4 : box.top - panel.offsetHeight - 4}px`
|
|
this.buttonTarget.setAttribute('aria-expanded', 'true')
|
|
document.addEventListener('click', this.onOutside, true)
|
|
document.addEventListener('keydown', this.onKey)
|
|
// capture: scroll events don't bubble, and the scrolling box may be any ancestor
|
|
document.addEventListener('scroll', this.onScroll, true)
|
|
window.addEventListener('resize', this.onResize)
|
|
this.items()[0]?.focus({ preventScroll: true })
|
|
}
|
|
|
|
close () {
|
|
if (this.panelTarget.hidden) {
|
|
return
|
|
}
|
|
this.panelTarget.hidden = true
|
|
this.buttonTarget.setAttribute('aria-expanded', 'false')
|
|
document.removeEventListener('click', this.onOutside, true)
|
|
document.removeEventListener('keydown', this.onKey)
|
|
document.removeEventListener('scroll', this.onScroll, true)
|
|
window.removeEventListener('resize', this.onResize)
|
|
}
|
|
|
|
// an item was chosen: it does its own work (htmx, link), the menu only closes
|
|
choose () {
|
|
this.close()
|
|
}
|
|
|
|
key (event) {
|
|
const items = this.items()
|
|
const index = items.indexOf(document.activeElement)
|
|
if (event.key === 'Escape') {
|
|
event.stopPropagation()
|
|
this.close()
|
|
this.buttonTarget.focus()
|
|
} else if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
event.preventDefault()
|
|
const next = (index + (event.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length
|
|
items[next]?.focus()
|
|
}
|
|
}
|
|
|
|
items () {
|
|
return [...this.panelTarget.querySelectorAll('a, button:not([disabled])')]
|
|
}
|
|
}
|