From 8f4bc9a8ef64a5fee58f9a31c6bd950436d471fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Wed, 11 Nov 2026 17:15:10 +0100 Subject: [PATCH] fix: keep wide data tables inside their column --- assets/controllers/dropdown_controller.js | 50 +++++++++++++++++++++++ assets/styles/components/datatable.css | 10 ++++- templates/components/dropdown.html.twig | 8 ++-- 3 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 assets/controllers/dropdown_controller.js diff --git a/assets/controllers/dropdown_controller.js b/assets/controllers/dropdown_controller.js new file mode 100644 index 0000000..27071b2 --- /dev/null +++ b/assets/controllers/dropdown_controller.js @@ -0,0 +1,50 @@ +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() + } +} diff --git a/assets/styles/components/datatable.css b/assets/styles/components/datatable.css index 8cf6528..2ccd0ef 100644 --- a/assets/styles/components/datatable.css +++ b/assets/styles/components/datatable.css @@ -1,9 +1,15 @@ +/* + * A table wider than its column scrolls inside this box, at every width: let overflow, it would + * run past the right edge of the page content - out of line with the buttons above it - and make + * the whole page scroll sideways. The row menus (components/dropdown.html.twig) are fixed to the + * viewport while open, so this box does not clip them. + */ .data-table-wrapper { - @apply overflow-x-auto lg:overflow-x-visible mb-8; + @apply overflow-x-auto mb-8; } .data-table-wrapper__inner { - @apply align-middle inline-block min-w-full overflow-hidden lg:overflow-visible; + @apply align-middle inline-block min-w-full; } table { diff --git a/templates/components/dropdown.html.twig b/templates/components/dropdown.html.twig index b7fa01e..2db447e 100644 --- a/templates/components/dropdown.html.twig +++ b/templates/components/dropdown.html.twig @@ -1,9 +1,11 @@ -
- -