Skip to content

The agent that grows with you

fix(desktop): keep tooltips compact and anchored with exit-only motion

brooklyn!b8b7d7531ce9
diff --git a/apps/desktop/src/components/ui/tooltip-placement.test.tsx b/apps/desktop/src/components/ui/tooltip-placement.test.tsx
new file mode 100644
index 00000000000..54fc95ea755
--- /dev/null
+++ b/apps/desktop/src/components/ui/tooltip-placement.test.tsx
@@ -0,0 +1,162 @@
+import { cleanup, render, screen } from '@testing-library/react'
+import { createRef, type ReactElement, type ReactNode, type Ref } from 'react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { Tip, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from './tooltip'
+
+const { contentProps } = vi.hoisted(() => ({ contentProps: vi.fn() }))
+
+vi.mock('radix-ui', async () => {
+ const { cloneElement, createElement, forwardRef } = await import('react')
+ const passthrough = ({ children }: { children: ReactNode }) => children
+
+ return {
+ Tooltip: {
+ Arrow: passthrough,
+ Content: ({ children, ...props }: { children: ReactNode }) => {
+ contentProps(props)
+
+ return createElement('div', { role: 'tooltip' }, children)
+ },
+ Portal: passthrough,
+ Provider: passthrough,
+ Root: passthrough,
+ Trigger: forwardRef<HTMLButtonElement, { asChild?: boolean; children: ReactNode }>(
+ ({ asChild, children, ...props }, ref) =>
+ asChild
+ ? cloneElement(children as ReactElement<{ ref?: Ref<HTMLButtonElement> }>, { ...props, ref })
+ : createElement('button', { ...props, ref }, children)
+ )
+ }
+ }
+})
+
+afterEach(() => {
+ cleanup()
+ contentProps.mockClear()
+})
+
+const latestContent = () => contentProps.mock.calls.at(-1)?.[0]
+
+describe('tooltip placement', () => {
+ it.each([
+ ['control', 'top'],
+ ['toolbar', 'bottom'],
+ ['row', 'right'],
+ ['left-rail', 'right'],
+ ['right-rail', 'left']
+ ] as const)('prefers %s tooltips on the %s', (placement, side) => {
+ render(
+ <Tip label="Details" placement={placement}>
+ <button>Trigger</button>
+ </Tip>
+ )
+
+ expect(latestContent()).toMatchObject({ side, align: 'center', hideWhenDetached: true, collisionPadding: 12 })
+ expect(latestContent().avoidCollisions).not.toBe(false)
+ })
+
+ it('keeps explicit overrides', () => {
+ render(
+ <Tip align="end" collisionPadding={20} label="Details" placement="row" side="bottom">
+ <button>Trigger</button>
+ </Tip>
+ )
+
+ expect(latestContent()).toMatchObject({ side: 'bottom', align: 'end', collisionPadding: 20 })
+ })
+
+ it('uses the owning pane for a control without changing its trigger', () => {
+ render(
+ <div data-testid="pane" data-tree-group="test-pane">
+ <Tip label="Details">
+ <button>Trigger</button>
+ </Tip>
+ </div>
+ )
+
+ expect(latestContent().collisionBoundary).toBe(screen.getByTestId('pane'))
+ expect(screen.getByRole('button').parentElement).toBe(screen.getByTestId('pane'))
+ })
+
+ it.each(['row', 'left-rail', 'right-rail'] as const)('lets %s escape the pane', placement => {
+ render(
+ <div data-tree-group="test-pane">
+ <Tip label="Details" placement={placement}>
+ <button>Trigger</button>
+ </Tip>
+ </div>
+ )
+
+ expect(latestContent().collisionBoundary).toBeUndefined()
+ })
+
+ it('allows a control to opt out of its pane boundary', () => {
+ render(
+ <div data-tree-group="test-pane">
+ <Tip boundary="viewport" label="Details">
+ <button>Trigger</button>
+ </Tip>
+ </div>
+ )
+
+ expect(latestContent().collisionBoundary).toBeUndefined()
+ })
+
+ it('preserves block content within one label surface', () => {
+ render(
+ <Tip
+ label={
+ <div>
+ Long description
+ <br />
+ Second line
+ </div>
+ }
+ >
+ <button>Trigger</button>
+ </Tip>
+ )
+
+ expect(screen.getByRole('tooltip').querySelector('[data-slot="tooltip-label"]')?.textContent).toBe(
+ 'Long descriptionSecond line'
+ )
+ expect(screen.getByRole('tooltip').querySelector('[data-slot="tooltip-arrow"]')).not.toBeNull()
+ })
+
+ it('forwards object refs and clears them on unmount', () => {
+ const ref = createRef<HTMLButtonElement>()
+
+ const { unmount } = render(
+ <TooltipProvider>
+ <Tooltip>
+ <TooltipTrigger ref={ref}>Trigger</TooltipTrigger>
+ <TooltipContent>Details</TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ )
+
+ expect(ref.current).toBe(screen.getByRole('button'))
+ unmount()
+ expect(ref.current).toBeNull()
+ })
+
+ it('honors callback-ref cleanup', () => {
+ const dispose = vi.fn()
+ const ref = vi.fn(() => dispose)
+
+ const { unmount } = render(
+ <TooltipProvider>
+ <Tooltip>
+ <TooltipTrigger ref={ref}>Trigger</TooltipTrigger>
+ <TooltipContent>Details</TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ )
+
+ expect(ref).toHaveBeenCalledWith(screen.getByRole('button'))
+ unmount()
+ expect(dispose).toHaveBeenCalledOnce()
+ expect(ref).not.toHaveBeenCalledWith(null)
+ })
+})
diff --git a/apps/desktop/src/components/ui/tooltip-placement.ts b/apps/desktop/src/components/ui/tooltip-placement.ts
new file mode 100644
index 00000000000..3d0564e39fb
--- /dev/null
+++ b/apps/desktop/src/components/ui/tooltip-placement.ts
@@ -0,0 +1,10 @@
+export type TooltipPlacement = 'control' | 'row' | 'toolbar' | 'left-rail' | 'right-rail'
+
+/** Preferred sides express intent; Radix still flips and shifts for available space. */
+export const TOOLTIP_PLACEMENTS = {
+ control: { side: 'top', align: 'center' },
+ row: { side: 'right', align: 'center' },
+ toolbar: { side: 'bottom', align: 'center' },
+ 'left-rail': { side: 'right', align: 'center' },
+ 'right-rail': { side: 'left', align: 'center' }
+} as const
diff --git a/apps/desktop/src/components/ui/tooltip.css b/apps/desktop/src/components/ui/tooltip.css
new file mode 100644
index 00000000000..0e9adf7a9cf
--- /dev/null
+++ b/apps/desktop/src/components/ui/tooltip.css
@@ -0,0 +1,63 @@
+.tooltip-bubble {
+ max-width: min(24rem, calc(100vw - 1.5rem), var(--radix-tooltip-content-available-width, 24rem));
+ border-radius: 0.25rem;
+ text-align: start;
+ overflow-wrap: anywhere;
+ white-space: normal;
+ transform-origin: var(--radix-tooltip-content-transform-origin);
+}
+
+.tooltip-bubble[data-state='closed'] {
+ animation: tooltip-exit 100ms ease-in both;
+}
+
+.tooltip-bubble[data-side='top'] {
+ --tooltip-exit-x: 0;
+ --tooltip-exit-y: 0.125rem;
+}
+
+.tooltip-bubble[data-side='bottom'] {
+ --tooltip-exit-x: 0;
+ --tooltip-exit-y: -0.125rem;
+}
+
+.tooltip-bubble[data-side='left'] {
+ --tooltip-exit-x: 0.125rem;
+ --tooltip-exit-y: 0;
+}
+
+.tooltip-bubble[data-side='right'] {
+ --tooltip-exit-x: -0.125rem;
+ --tooltip-exit-y: 0;
+}
+
+@keyframes tooltip-exit {
+ from {
+ opacity: 1;
+ transform: translate(0, 0);
+ }
+ to {
+ opacity: 0;
+ transform: translate(var(--tooltip-exit-x, 0), var(--tooltip-exit-y, 0));
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .tooltip-bubble[data-state='closed'] {
+ animation: none;
+ }
+}
+
+.tooltip-bubble-label {
+ min-width: 0;
+ text-wrap: pretty;
+}
+
+.tooltip-bubble [data-slot='tooltip-arrow'] {
+ fill: var(--foreground);
+}
+
+.tooltip-bubble-label pre {
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+}
diff --git a/apps/desktop/src/components/ui/tooltip.tsx b/apps/desktop/src/components/ui/tooltip.tsx
index 381e5f74082..50c7f128508 100644
--- a/apps/desktop/src/components/ui/tooltip.tsx
+++ b/apps/desktop/src/components/ui/tooltip.tsx
@@ -1,3 +1,5 @@
+import './tooltip.css'
+
import { Tooltip as TooltipPrimitive } from 'radix-ui'
import * as React from 'react'
@@ -6,6 +8,8 @@ import { type InputModality, lastInputModality } from '@/lib/input-modality'
import { useKeybindHint } from '@/lib/keybinds/use-keybind-hint'
import { cn } from '@/lib/utils'
+import { TOOLTIP_PLACEMENTS, type TooltipPlacement } from './tooltip-placement'
+
/** Default hover-open delay for `Tip`. Below 150ms a passing cursor still
* opens the tip; above 250ms an intentional hover feels broken. Call sites
* that need an instant tip pass `delayDuration={0}`. */
@@ -20,6 +24,7 @@ const TIP_SKIP_DELAY_MS = 300
/** True inside `RootTooltipProvider`. `Tip` uses this to decide whether it
* needs to supply its own provider — see the note on `Tip`. */
const HasTooltipProvider = React.createContext(false)
+const TooltipAnchor = React.createContext<React.RefObject<HTMLButtonElement | null> | null>(null)
function TooltipProvider({
delayDuration = 0,
@@ -47,7 +52,13 @@ function TooltipProvider({
}
function Tooltip({ ...props }: React.ComponentProps<typeof TooltipPrimitive.Root>) {
- return <TooltipPrimitive.Root data-slot="tooltip" {...props} />
+ const anchor = React.useRef<HTMLButtonElement | null>(null)
+
+ return (
+ <TooltipAnchor value={anchor}>
+ <TooltipPrimitive.Root data-slot="tooltip" {...props} />
+ </TooltipAnchor>
+ )
}
// Radix opens a tooltip on ANY trigger focus (its pointer-down guard only
@@ -81,6 +92,38 @@ export function suppressNonKeyboardFocusOpen(
}
function TooltipTrigger({ onFocus, ...props }: React.ComponentProps<typeof TooltipPrimitive.Trigger>) {
+ const anchor = React.useContext(TooltipAnchor)
+ const { ref, ...triggerProps } = props
+
+ const setRef = React.useCallback(
+ (node: HTMLButtonElement | null) => {
+ if (anchor) {
+ anchor.current = node
+ }
+
+ const cleanup = typeof ref === 'function' ? ref(node) : undefined
+
+ if (ref && typeof ref !== 'function') {
+ ref.current = node
+ }
+
+ return () => {
+ if (anchor) {
+ anchor.current = null
+ }
+
+ if (typeof cleanup === 'function') {
+ cleanup()
+ } else if (typeof ref === 'function') {
+ ref(null)
+ } else if (ref) {
+ ref.current = null
+ }
+ }
+ },
+ [anchor, ref]
+ )
+
return (
<TooltipPrimitive.Trigger
data-slot="tooltip-trigger"
@@ -88,57 +131,71 @@ function TooltipTrigger({ onFocus, ...props }: React.ComponentProps<typeof Toolt
onFocus?.(event)
suppressNonKeyboardFocusOpen(event)
}}
- {...props}
+ {...triggerProps}
+ ref={setRef}
/>
)
}
+interface TooltipContentProps extends React.ComponentProps<typeof TooltipPrimitive.Content> {
+ placement?: TooltipPlacement
+ /** Row descriptions may extend beyond a pane; local controls stay inside. */
+ boundary?: 'pane' | 'viewport'
+}
+
function TooltipContent({
+ align,
+ arrowPadding = 6,
+ children,
className,
+ collisionBoundary,
+ collisionPadding = 12,
+ hideWhenDetached = true,
+ placement = 'control',
+ boundary = placement === 'control' || placement === 'toolbar' ? 'pane' : 'viewport',
side,
- sideOffset = 6,
- children,
+ sideOffset = 5,
...props
-}: React.ComponentProps<typeof TooltipPrimitive.Content>) {
+}: TooltipContentProps) {
+ const preferred = TOOLTIP_PLACEMENTS[placement]
+ const anchor = React.useContext(TooltipAnchor)
+ const [pane, setPane] = React.useState<Element | null>(null)
+
+ React.useLayoutEffect(() => {
+ setPane(boundary === 'pane' ? (anchor?.current?.closest('[data-tree-group]') ?? null) : null)
+ }, [anchor, boundary])
+
return (
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
- // Transparent, width-capped wrapper. The visible chip is the inner inline
- // span so `box-decoration-break: clone` gives a marker-style background
- // that hugs EACH wrapped line (bg only on the text, ragged right — no
- // rectangular dead space). No fade transition — once the hover delay
- // elapses the chip appears at once.
- // pointer-events-none: the tip must never steal hover/clicks from the
- // chrome underneath (titlebar tools, adjacent tabs, etc.).
- // A tip beside its trigger rags toward it: wrapped lines hang off the
- // edge nearest the control, not away from it.
+ align={align ?? preferred.align}
+ arrowPadding={arrowPadding}
className={cn(
- 'pointer-events-none z-(--z-over-modal) w-fit max-w-64 select-none',
- side === 'left' && 'text-right',
+ 'tooltip-bubble pointer-events-none z-(--z-over-modal) w-fit select-none bg-foreground px-2 py-1 text-[0.6875rem] font-medium leading-[1.4] text-background',
className
)}
+ collisionBoundary={collisionBoundary ?? pane ?? undefined}
+ collisionPadding={collisionPadding}
data-slot="tooltip-content"
- side={side}
+ hideWhenDetached={hideWhenDetached}
+ side={side ?? preferred.side}
sideOffset={sideOffset}
{...props}
>
- {/* bg-foreground/text-background auto-inverts per theme. leading-normal
- keeps lines readable; py-1 makes the cloned line-boxes overlap just
- enough to read as one continuous fill (no gaps between lines). */}
- {/* [&>*]:!inline: this decoration only paints inline FLOW. A block child
- collapses it to zero and Radix parks an empty chip in the corner
- (#62022); an atomic inline child (`inline-flex`) sits on the baseline
- and hangs its extra lines below the background, dark-on-dark. Force
- direct children inline; break lines with `<br />`. */}
- <span className="box-decoration-clone inline bg-foreground px-1.5 py-1 text-[11px] font-bold leading-normal text-background [font-family:Arial,sans-serif] [&>*]:!inline">
+ <div className="tooltip-bubble-label" data-slot="tooltip-label">
{children}
- </span>
+ </div>
+ <TooltipPrimitive.Arrow asChild height={5} width={10}>
+ <svg aria-hidden data-slot="tooltip-arrow" viewBox="0 0 10 5">
+ <path d="M0 0h10L5.7 4.3a1 1 0 0 1-1.4 0Z" />
+ </svg>
+ </TooltipPrimitive.Arrow>
</TooltipPrimitive.Content>
</TooltipPrimitive.Portal>
)
}
-interface TipProps extends Omit<React.ComponentProps<typeof TooltipPrimitive.Content>, 'content'> {
+interface TipProps extends Omit<TooltipContentProps, 'content'> {
label: React.ReactNode
children: React.ReactNode
delayDuration?: number
@@ -274,9 +331,7 @@ interface TipHintLabelProps {
hint?: string
}
-/** Tooltip label with an optional trailing hotkey hint. Plain inline flow (no
- * flex box) so Tip's per-line background wraps it — prefer this over a bespoke
- * flex/gap span at the call site (see #62022). */
+/** Tooltip label with an optional trailing hotkey hint. */
function TipHintLabel({ text, hint }: TipHintLabelProps) {
if (!hint) {
return <>{text}</>
diff --git a/apps/desktop/vitest.setup.ts b/apps/desktop/vitest.setup.ts
index 67b12d23d26..5cd9db95745 100644
--- a/apps/desktop/vitest.setup.ts
+++ b/apps/desktop/vitest.setup.ts
@@ -1,5 +1,11 @@
import { configure } from '@testing-library/react'
+import { stubResizeObserver } from './src/test/jsdom'
+
+// Shared tooltips now measure their arrow through Radix's useSize hook.
+// Geometry assertions still belong in a real browser, not this inert observer.
+stubResizeObserver()
+
// Node 26 defines its own `localStorage` accessor on the global object, which
// returns `undefined` unless the process was started with --localstorage-file
// (it warns: "localStorage is not available because --localstorage-file was