From 23af4f3930b20e99b07bd6456b9c3098497e15ec Mon Sep 17 00:00:00 2001 From: Jose Lago Date: Mon, 19 Oct 2026 21:19:30 +0200 Subject: [PATCH] fix(admin): clamp tooltips into the viewport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The portal kept cards out of table overflow but nothing kept them on screen: centred on a fixed-width card, any trigger near an edge lost text -- reported on Sessions' right-aligned Generate button. The shared Tooltip now measures the card, clamps it to the viewport, flips below when there is no room above, and keeps the arrow aimed at the trigger. No more CSS centreing transform, JS owns the coordinates. New tooltip-containment.spec.ts (4 tests) asserts the measured rectangle, no document widening and arrow alignment at wide/narrow/short viewports; verified it catches the bug by reverting the fix (3 of 4 fail). Full serial e2e: 211 passed, 0 failed. Co-authored-by: Stephan Charles Co-authored-by: Amelia Hernandez Co-authored-by: Markus Franz Co-authored-by: Sabrina Hota Co-authored-by: Nilüfer Doğan --- apps/admin/src/components/ui.tsx | 62 ++++++++++- apps/admin/src/styles.css | 17 ++- docs/frontend/admin-app.md | 11 ++ docs/releases/2026-10-19.md | 23 ++++ e2e/tests/tooltip-containment.spec.ts | 145 ++++++++++++++++++++++++++ 5 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 e2e/tests/tooltip-containment.spec.ts diff --git a/apps/admin/src/components/ui.tsx b/apps/admin/src/components/ui.tsx index 24f5fc1..e378012 100644 --- a/apps/admin/src/components/ui.tsx +++ b/apps/admin/src/components/ui.tsx @@ -124,13 +124,62 @@ export function Tooltip({ content, title, children }: { children: ReactNode; }) { const anchor = useRef(null); + const cardRef = useRef(null); const id = useId(); - const [pos, setPos] = useState<{ x: number; y: number } | null>(null); + // left/top are the card's real viewport coordinates (the card carries no CSS + // transform), so the clamping below and the rendered position cannot disagree. + const [pos, setPos] = useState<{ left: number; top: number; arrow: number; below: boolean } | null>(null); + const hide = () => setPos(null); + + /** + * Compute the card's viewport position so it stays entirely on screen. + * + * It is portal-rendered so no table's `overflow` can clip it, but nothing used + * to keep it inside the *screen*: centred on its trigger, the tooltip on a + * right-aligned button (Sessions' "Generate AI Feedback Summary") ran past the + * right edge and lost half its text. A trigger near the top also has nowhere to + * put the card, so it flips below. + * + * `below` is derived from the card's height rather than passed in, so the + * caller does not have to guess it — which matters because the height is only + * knowable once the card is in the DOM. + */ + const compute = ( + w: number, h: number, + trigger: { x: number; top: number; bottom: number }, + ) => { + const gap = 10; + // Keep this in sync with the `inset` in @keyframes tip-in: the card starts + // 6px lower and slides up, so reserving 6px more keeps a flipped card from + // briefly overlapping the control it describes. + const slide = 6; + const vw = window.innerWidth; + const edge = Math.min(8, Math.max(0, (vw - w) / 2)); + let left = trigger.x - w / 2; + if (left + w > vw - edge) left = vw - edge - w; + if (left < edge) left = edge; + // Flip below when there is not enough room above, including the arrow gap. + const below = trigger.top - h - gap - slide < edge; + const top = below ? trigger.bottom + gap : trigger.top - h - gap; + // The arrow tracks the trigger even after the card slides sideways, but + // stays inside the card so the two never look disconnected. + const arrow = Math.max(18, Math.min(trigger.x - left, w - 18)); + return { left, top, arrow, below }; + }; + const show = () => { const r = anchor.current?.getBoundingClientRect(); - if (r) setPos({ x: r.left + r.width / 2, y: r.top }); + if (!r) return; + const trigger = { x: r.left + r.width / 2, top: r.top, bottom: r.bottom }; + // The card is unmounted between hovers, so its size is unknown until it is + // in the DOM. Mount it at a provisional spot, then measure and correct: two + // passes total, no recursion, nothing that can loop. + setPos(compute(cardRef.current?.offsetWidth || 250, cardRef.current?.offsetHeight || 96, trigger)); + requestAnimationFrame(() => { + const card = cardRef.current; + if (card) setPos(compute(card.offsetWidth, card.offsetHeight, trigger)); + }); }; - const hide = () => setPos(null); // Point the control at the card so screen readers announce the explanation // when it is focused (focus also reveals it visually). const child = isValidElement(children) @@ -140,7 +189,10 @@ export function Tooltip({ content, title, children }: { {child} {pos && createPortal( -