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
@@ -1,50 +0,0 @@
|
||||
import { Controller } from '@hotwired/stimulus'
|
||||
|
||||
/*
|
||||
* Pins the hover menu of templates/components/dropdown.html.twig to the viewport while it is open.
|
||||
*
|
||||
* The menu sits in the last cell of data tables, whose wrapper scrolls horizontally when a table is
|
||||
* wider than its column (components/datatable.css). A scrolling box clips everything absolutely
|
||||
* positioned inside it, so on the last rows the menu would vanish into a scrollbar. Fixed
|
||||
* positioning takes it out of that box; it stays a descendant of the hover group, so the CSS
|
||||
* group-hover that shows and hides it keeps working unchanged.
|
||||
*
|
||||
* Right-aligned to the button like before, and flipped above it when there is no room below.
|
||||
*/
|
||||
export default class extends Controller {
|
||||
|
||||
static targets = [ 'button', 'menu' ]
|
||||
|
||||
connect () {
|
||||
this.boundPosition = this.position.bind(this)
|
||||
}
|
||||
|
||||
open () {
|
||||
this.position()
|
||||
window.addEventListener('scroll', this.boundPosition, true)
|
||||
window.addEventListener('resize', this.boundPosition)
|
||||
}
|
||||
|
||||
close () {
|
||||
window.removeEventListener('scroll', this.boundPosition, true)
|
||||
window.removeEventListener('resize', this.boundPosition)
|
||||
}
|
||||
|
||||
position () {
|
||||
const button = this.buttonTarget.getBoundingClientRect()
|
||||
const menu = this.menuTarget
|
||||
|
||||
menu.style.position = 'fixed'
|
||||
menu.style.left = `${Math.max(0, button.right - menu.offsetWidth)}px`
|
||||
|
||||
// the menu's own top padding bridges the gap to the button, so the hover is not lost
|
||||
// on the way down; when flipped, it covers the button from below instead
|
||||
const fitsBelow = button.top + menu.offsetHeight <= window.innerHeight
|
||||
menu.style.top = `${fitsBelow ? button.top : Math.max(0, button.bottom - menu.offsetHeight)}px`
|
||||
menu.style.right = 'auto'
|
||||
}
|
||||
|
||||
disconnect () {
|
||||
this.close()
|
||||
}
|
||||
}
|
||||
@@ -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