Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

fix(create): a help tooltip is readable instead of clipped by its own card

purple62fcee19dcd4
diff --git a/src/components/create/ui/Tooltip.tsx b/src/components/create/ui/Tooltip.tsx
index 32f72f60..f975d1e1 100644
--- a/src/components/create/ui/Tooltip.tsx
+++ b/src/components/create/ui/Tooltip.tsx
@@ -1,7 +1,9 @@
-import { useEffect, useRef, useState } from 'react'
+import { useEffect, useLayoutEffect, useRef, useState } from 'react'
+import { createPortal } from 'react-dom'
import { motion, AnimatePresence } from 'framer-motion'
import { cn } from './cn'
+import { tooltipPosition, type TooltipPlacement } from './tooltip-position'
interface Props {
content: React.ReactNode
@@ -13,7 +15,13 @@ interface Props {
export function Tooltip({ content, children, side = 'top', delay = 400, className }: Props) {
const [show, setShow] = useState(false)
+ // The bubble is portalled to the body, so its coordinates are computed
+ // rather than inherited. Until the first measurement it stays hidden — one
+ // frame of a bubble in the top-left corner would be worse than none.
+ const [place, setPlace] = useState<TooltipPlacement | null>(null)
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
+ const anchor = useRef<HTMLSpanElement>(null)
+ const bubble = useRef<HTMLSpanElement>(null)
const open = () => {
if (timer.current) {
@@ -33,6 +41,7 @@ const close = () => {
}
setShow(false)
+ setPlace(null)
}
const handleFocus = (
@@ -49,8 +58,33 @@ const handleFocus = (
useEffect(() => () => { if (timer.current) clearTimeout(timer.current) }, [])
+ // Measure before paint, then again whenever the page moves under the bubble.
+ useLayoutEffect(() => {
+ if (!show) return
+ const measure = () => {
+ const a = anchor.current?.getBoundingClientRect()
+ const b = bubble.current?.getBoundingClientRect()
+ if (!a || !b) return
+ setPlace(tooltipPosition(
+ { top: a.top, left: a.left, width: a.width, height: a.height },
+ { width: b.width, height: b.height },
+ { width: window.innerWidth, height: window.innerHeight },
+ side,
+ ))
+ }
+ measure()
+ // Capture phase, because the clipping ancestor is usually the scroller.
+ window.addEventListener('scroll', measure, true)
+ window.addEventListener('resize', measure)
+ return () => {
+ window.removeEventListener('scroll', measure, true)
+ window.removeEventListener('resize', measure)
+ }
+ }, [show, side, content])
+
return (
<span
+ ref={anchor}
className={cn('relative inline-flex', className)}
onMouseEnter={open}
onMouseLeave={close}
@@ -60,29 +94,32 @@ const handleFocus = (
onBlur={close}
>
{children}
- <AnimatePresence>
- {show && (
- <motion.span
- initial={{ opacity: 0, y: side === 'top' ? 3 : -3 }}
- animate={{ opacity: 1, y: 0 }}
- exit={{ opacity: 0, y: side === 'top' ? 3 : -3 }}
- transition={{ duration: 0.12 }}
- role="tooltip"
- className={cn(
- 'lu-elevated pointer-events-none absolute z-[60] left-1/2 -translate-x-1/2 w-max max-w-[220px] px-2.5 py-1.5 rounded-lg t-body text-gray-200',
- side === 'top' ? 'bottom-full mb-1.5' : 'top-full mt-1.5',
- )}
- >
- {content}
- <span
- className={cn(
- 'absolute left-1/2 -translate-x-1/2 w-2 h-2 rotate-45 bg-lu-overlay border-white/[0.08]',
- side === 'top' ? 'top-full -mt-1 border-b border-r' : 'bottom-full -mb-1 border-t border-l',
- )}
- />
- </motion.span>
- )}
- </AnimatePresence>
+ {createPortal(
+ <AnimatePresence>
+ {show && (
+ <motion.span
+ ref={bubble}
+ initial={{ opacity: 0, y: side === 'top' ? 3 : -3 }}
+ animate={{ opacity: place ? 1 : 0, y: 0 }}
+ exit={{ opacity: 0, y: side === 'top' ? 3 : -3 }}
+ transition={{ duration: 0.12 }}
+ role="tooltip"
+ style={{ top: place?.top ?? 0, left: place?.left ?? 0 }}
+ className="lu-elevated pointer-events-none fixed z-[60] w-max max-w-[220px] px-2.5 py-1.5 rounded-lg t-body text-gray-200"
+ >
+ {content}
+ <span
+ style={{ left: place?.arrowLeft ?? 0 }}
+ className={cn(
+ 'absolute -translate-x-1/2 w-2 h-2 rotate-45 bg-lu-overlay border-white/[0.08]',
+ (place?.side ?? side) === 'top' ? 'top-full -mt-1 border-b border-r' : 'bottom-full -mb-1 border-t border-l',
+ )}
+ />
+ </motion.span>
+ )}
+ </AnimatePresence>,
+ document.body,
+ )}
</span>
)
-}
\ No newline at end of file
+}
diff --git a/src/components/create/ui/__tests__/tooltip-position.test.ts b/src/components/create/ui/__tests__/tooltip-position.test.ts
new file mode 100644
index 00000000..05217aea
--- /dev/null
+++ b/src/components/create/ui/__tests__/tooltip-position.test.ts
@@ -0,0 +1,69 @@
+// Regression guard for #107: the latent-upscale help text was clipped by the
+// Native HiRes card, which carries overflow-hidden. The bubble is portalled to
+// the body now, so these coordinates are the whole placement logic.
+import { describe, it, expect } from 'vitest'
+import { tooltipPosition } from '../tooltip-position'
+
+const VIEW = { width: 1280, height: 800 }
+const BUBBLE = { width: 220, height: 60 }
+// A 12px help icon somewhere in the middle of the panel.
+const ICON = { top: 400, left: 300, width: 12, height: 12 }
+
+describe('tooltipPosition', () => {
+ it('sits under the trigger and centres on it for side bottom', () => {
+ const p = tooltipPosition(ICON, BUBBLE, VIEW, 'bottom')
+ expect(p.side).toBe('bottom')
+ expect(p.top).toBe(418)
+ expect(p.left).toBe(196)
+ expect(p.arrowLeft).toBe(110)
+ })
+
+ it('sits above the trigger for side top', () => {
+ const p = tooltipPosition(ICON, BUBBLE, VIEW, 'top')
+ expect(p.side).toBe('top')
+ expect(p.top).toBe(334)
+ })
+
+ it('flips up when the bottom edge is too close, which is ElBiggus case', () => {
+ const nearBottom = { ...ICON, top: 770 }
+ const p = tooltipPosition(nearBottom, BUBBLE, VIEW, 'bottom')
+ expect(p.side).toBe('top')
+ expect(p.top).toBe(704)
+ expect(p.top).toBeGreaterThanOrEqual(0)
+ })
+
+ it('does not flip into an edge that is just as tight', () => {
+ // Short window, no room on either side: keep the asked-for side rather
+ // than bouncing the bubble to the other cramped edge.
+ const p = tooltipPosition({ ...ICON, top: 40 }, BUBBLE, { width: 1280, height: 100 }, 'bottom')
+ expect(p.side).toBe('bottom')
+ })
+
+ it('pulls the bubble back inside the right edge and keeps the arrow on the trigger', () => {
+ const nearRight = { ...ICON, left: 1270 }
+ const p = tooltipPosition(nearRight, BUBBLE, VIEW, 'bottom')
+ expect(p.left).toBe(1052)
+ expect(p.left + BUBBLE.width).toBeLessThanOrEqual(VIEW.width)
+ expect(p.arrowLeft).toBe(210)
+ })
+
+ it('pulls the bubble back inside the left edge', () => {
+ const nearLeft = { ...ICON, left: 2 }
+ const p = tooltipPosition(nearLeft, BUBBLE, VIEW, 'bottom')
+ expect(p.left).toBe(8)
+ expect(p.arrowLeft).toBe(10)
+ })
+
+ it('keeps the left margin when the bubble is wider than the window', () => {
+ const p = tooltipPosition(ICON, { width: 400, height: 60 }, { width: 320, height: 800 }, 'bottom')
+ expect(p.left).toBe(8)
+ })
+
+ it('never puts the arrow outside the bubble', () => {
+ for (const left of [0, 100, 640, 1279]) {
+ const p = tooltipPosition({ ...ICON, left }, BUBBLE, VIEW, 'bottom')
+ expect(p.arrowLeft).toBeGreaterThanOrEqual(10)
+ expect(p.arrowLeft).toBeLessThanOrEqual(BUBBLE.width - 10)
+ }
+ })
+})
diff --git a/src/components/create/ui/tooltip-position.ts b/src/components/create/ui/tooltip-position.ts
new file mode 100644
index 00000000..14bff3f7
--- /dev/null
+++ b/src/components/create/ui/tooltip-position.ts
@@ -0,0 +1,61 @@
+// Where a tooltip bubble goes, as plain arithmetic.
+//
+// The bubble used to be an absolutely positioned child of its trigger, so any
+// ancestor with overflow-hidden cut it in half. The Native HiRes card is
+// exactly such an ancestor, which is how #107 came in: the latent-upscale help
+// text was readable for about two words. The bubble now lives in a portal on
+// the body with fixed coordinates, and this function computes them. Keeping
+// the maths out of the component is what makes it testable at all, since the
+// suite runs without a DOM.
+
+export interface Rect { top: number; left: number; width: number; height: number }
+export interface Viewport { width: number; height: number }
+
+export interface TooltipPlacement {
+ top: number
+ left: number
+ /** Which side the bubble ended up on, after flipping away from an edge. */
+ side: 'top' | 'bottom'
+ /** Arrow offset inside the bubble, so it keeps pointing at the trigger. */
+ arrowLeft: number
+}
+
+/** Breathing room between trigger and bubble, and between bubble and window. */
+const GAP = 6
+const MARGIN = 8
+/** The arrow is 8px wide and rotated; keep it off the rounded corners. */
+const ARROW_INSET = 10
+
+export function tooltipPosition(
+ trigger: Rect,
+ bubble: { width: number; height: number },
+ viewport: Viewport,
+ preferred: 'top' | 'bottom',
+): TooltipPlacement {
+ // Flip only when the preferred side has no room AND the other side does.
+ // Flipping into an even tighter edge would just move the problem.
+ const roomAbove = trigger.top - GAP - MARGIN
+ const roomBelow = viewport.height - (trigger.top + trigger.height) - GAP - MARGIN
+ let side = preferred
+ if (preferred === 'top' && bubble.height > roomAbove && roomBelow >= bubble.height) side = 'bottom'
+ if (preferred === 'bottom' && bubble.height > roomBelow && roomAbove >= bubble.height) side = 'top'
+
+ const top = side === 'top'
+ ? trigger.top - GAP - bubble.height
+ : trigger.top + trigger.height + GAP
+
+ // Centre on the trigger, then pull back inside the window. A tooltip wider
+ // than the window keeps the left margin rather than centring off-screen.
+ const centre = trigger.left + trigger.width / 2 - bubble.width / 2
+ const maxLeft = viewport.width - MARGIN - bubble.width
+ const left = maxLeft < MARGIN ? MARGIN : Math.min(Math.max(centre, MARGIN), maxLeft)
+
+ // The arrow follows the trigger even after the bubble was pushed sideways.
+ const triggerCentre = trigger.left + trigger.width / 2
+ const arrowLeft = Math.min(
+ Math.max(triggerCentre - left, ARROW_INSET),
+ Math.max(bubble.width - ARROW_INSET, ARROW_INSET),
+ )
+
+ return { top, left, side, arrowLeft }
+}