style: adopt ep-design 1.2.0 dashboard cards and replace the hover dropdown with a click menu
This commit is contained in:
1 parent
99e6119311
commit
6dbc6efa78
16 files changed
+381
-554
No files matched your search
@@ -0,0 +1,89 @@
|
||||
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])')]
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user