Skip to content

Commit 806ea0c

Browse files
authored
fix(tooltip): stop text blurring for ~150ms every time a tooltip appears (#6211)
* fix(tooltip): remove velocity skew/scale that blurred text on every appear The tooltip animated a fractional scale() + skew() over 150ms on the element containing its text. Chrome promotes the bubble to a compositor layer for the transition, rasterizes the text once at the pre-transition scale, then GPU-resamples that bitmap for the duration — so text rendered blurry until the transition settled and the layer re-rasterized at 1:1. It fired on every appear: a pointer entering a trigger is by definition moving, so the first pointermove after pointerenter always set a non-zero skew and a fractional scale. - drop the velocity-reactive skew/scale flourish and the pointer-velocity bookkeeping that existed only to feed it - round tooltip position to whole pixels; clientX/clientY are fractional on HiDPI/zoomed displays, leaving the bubble on a subpixel boundary - drop the dead `filter` from the transition list — nothing ever set a filter - skip the state update when the rounded position is unchanged, so pointer jitter no longer re-renders every Tooltip.Trigger/Content consumer The 150ms ease-out translate is kept, so the bubble still trails the cursor. * improvement(tooltip): keep the velocity flourish, drive it without a CSS transition Restores the velocity-reactive skew/scale removed in the previous commit. The flourish was never the problem on its own — handing it to a CSS transition was. An interpolated fractional scale makes the compositor rasterize the tooltip's text once and resample that bitmap for the duration, which is what read as blur. Applied as a static value per pointer event instead, so every frame is rasterized at its own scale: - split the transform across the individual `translate`, `scale`, and `transform: skew()` properties, and transition only `translate` — position still eases toward the cursor, the flourish no longer interpolates - smooth the pointer velocity in JS (low-pass filter) to replace the smoothing the CSS transition used to provide, so the squish still ramps rather than snapping between raw per-event velocities - quantize the flourish to 3 decimals so jitter below the visible threshold settles instead of re-rendering every consumer Whole-pixel position rounding and the redundant-update bail-out are unchanged. * fix(tooltip): don't seed pointer velocity from the trigger box on focus The previous commit routed `onFocus` through a shared reveal helper that seeds `lastPointerRef` from the coordinates it is given. For focus those are the trigger's box center, not the pointer — so if the pointer already happened to be over the trigger, the next `pointermove` measured the box-to-cursor delta as velocity and spiked the skew/scale flourish. Split the helper in two: reveal-from-pointer seeds velocity tracking, reveal-from-element leaves it cleared. Restores the pre-PR behavior, where focus explicitly nulled the pointer snapshot. Caught by Cursor Bugbot. * fix(tooltip): make the flourish smoothing frame-rate independent The velocity low-pass filter applied a fixed coefficient per pointer event, so how fast the squish settled depended on how fast the device emitted events — 233ms at 30Hz down to 29ms at 240Hz, an 8x spread for the same gesture. It was also far snappier than the 150ms CSS ease-out it replaced, so the flourish read as twitchier than before. Derive the coefficient from the real elapsed time instead (1 - exp(-dt / tau), tau = 50ms). Settling is now flat at ~150ms from 60Hz upward, matching the duration of the transition this stands in for. Also separates the smoothing delta from the velocity-normalization delta: the latter is still floored at one frame to keep a 1ms event from reporting an enormous velocity, but flooring the former was itself a source of frame-rate dependence below 16ms. Verified against Chrome's documented re-raster behavior: a layer is re-rastered at its new scale when the scale changes via script, but not when a declarative animation interpolates it, which is why the flourish must stay out of the transition list. https://developer.chrome.com/blog/re-rastering-composite
1 parent 6806fc2 commit 806ea0c

1 file changed

Lines changed: 109 additions & 42 deletions

File tree

packages/emcn/src/components/tooltip/tooltip.tsx

Lines changed: 109 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,23 @@ const EDGE_THRESHOLD = 360
1111
const MIN_FRAME_MS = 16
1212

1313
/**
14-
* Resolved position and motion of a floating tooltip. `x`/`y` are viewport
15-
* coordinates the tooltip anchors to; `alignX`/`alignY` flip the tooltip away
16-
* from the nearest viewport edge; `skew`/`scale*` add the velocity-reactive
14+
* Exponential time constant for smoothing the pointer velocity that drives the
15+
* flourish, in ms. The flourish is deliberately never handed to a CSS transition:
16+
* Chrome only re-rasters a layer at its new scale when the scale changes via
17+
* script, not when a declarative animation interpolates it, so a transitioned
18+
* fractional scale leaves the tooltip's text resampled from a stale bitmap until
19+
* the animation settles — which is what read as a blur on every appear.
20+
*
21+
* Smoothing here replaces the smoothing that transition used to provide. ~3x the
22+
* time constant is where the value has effectively settled, so 50ms reproduces
23+
* the feel of the 150ms ease-out it stands in for.
24+
*/
25+
const VELOCITY_TIME_CONSTANT_MS = 50
26+
27+
/**
28+
* Resolved position and motion of a floating tooltip. `x`/`y` are whole-pixel
29+
* viewport coordinates the tooltip anchors to; `alignX`/`alignY` flip the tooltip
30+
* away from the nearest viewport edge; `skew`/`scale*` add the velocity-reactive
1731
* flourish while the pointer is moving.
1832
*/
1933
export interface FloatingTooltipState {
@@ -27,6 +41,15 @@ export interface FloatingTooltipState {
2741
alignY: 'above' | 'below'
2842
}
2943

44+
/** Velocity-derived flourish applied to the tooltip on a given frame. */
45+
interface TooltipMotion {
46+
skew: number
47+
scaleX: number
48+
scaleY: number
49+
}
50+
51+
const NEUTRAL_MOTION: TooltipMotion = { skew: 0, scaleX: 1, scaleY: 1 }
52+
3053
interface PointerSnapshot {
3154
x: number
3255
y: number
@@ -50,9 +73,7 @@ const HIDDEN_STATE: FloatingTooltipState = {
5073
visible: false,
5174
x: 0,
5275
y: 0,
53-
skew: 0,
54-
scaleX: 1,
55-
scaleY: 1,
76+
...NEUTRAL_MOTION,
5677
alignX: 'left',
5778
alignY: 'below',
5879
}
@@ -72,46 +93,83 @@ export function useFloatingTooltip(canShow: (target: HTMLElement) => boolean): {
7293
canShowRef.current = canShow
7394

7495
const lastPointerRef = React.useRef<PointerSnapshot | null>(null)
96+
const velocityRef = React.useRef({ x: 0, magnitude: 0 })
7597
const [state, setState] = React.useState<FloatingTooltipState>(HIDDEN_STATE)
7698

7799
const handlers = React.useMemo<FloatingTooltipHandlers>(() => {
78-
const hide = () => {
100+
const reset = () => {
79101
lastPointerRef.current = null
102+
velocityRef.current.x = 0
103+
velocityRef.current.magnitude = 0
104+
}
105+
106+
const hide = () => {
107+
reset()
80108
setState((current) => (current.visible ? HIDDEN_STATE : current))
81109
}
82110

83-
const showStatic = (clientX: number, clientY: number) => {
111+
const apply = (clientX: number, clientY: number, motion: TooltipMotion) => {
112+
const next = { ...getTooltipPosition(clientX, clientY), ...motion }
113+
setState((current) =>
114+
current.visible &&
115+
current.x === next.x &&
116+
current.y === next.y &&
117+
current.alignX === next.alignX &&
118+
current.alignY === next.alignY &&
119+
current.skew === next.skew &&
120+
current.scaleX === next.scaleX &&
121+
current.scaleY === next.scaleY
122+
? current
123+
: { visible: true, ...next }
124+
)
125+
}
126+
127+
/** Reveals the tooltip at the pointer, seeding velocity tracking from it. */
128+
const showFromPointer = (clientX: number, clientY: number) => {
129+
reset()
84130
lastPointerRef.current = { x: clientX, y: clientY, time: performance.now() }
85-
setState({
86-
visible: true,
87-
...getTooltipPosition(clientX, clientY),
88-
skew: 0,
89-
scaleX: 1,
90-
scaleY: 1,
91-
})
131+
apply(clientX, clientY, NEUTRAL_MOTION)
132+
}
133+
134+
/**
135+
* Reveals the tooltip anchored to an element's box rather than the pointer.
136+
* Velocity tracking stays cleared: seeding it from the box would make the next
137+
* `pointermove` read the box-to-cursor delta as velocity and spike the flourish
138+
* when the pointer already happens to be over the trigger.
139+
*/
140+
const showFromElement = (clientX: number, clientY: number) => {
141+
reset()
142+
apply(clientX, clientY, NEUTRAL_MOTION)
92143
}
93144

94145
return {
95146
onPointerEnter: (event) => {
96147
if (!canShowRef.current(event.currentTarget)) return
97-
showStatic(event.clientX, event.clientY)
148+
showFromPointer(event.clientX, event.clientY)
98149
},
99150
onPointerMove: (event) => {
100151
if (!canShowRef.current(event.currentTarget)) return
101152
const now = performance.now()
102153
const previous = lastPointerRef.current
103-
const elapsed = previous ? Math.max(now - previous.time, MIN_FRAME_MS) : MIN_FRAME_MS
104-
const velocityX = previous ? ((event.clientX - previous.x) / elapsed) * MIN_FRAME_MS : 0
105-
const velocityY = previous ? ((event.clientY - previous.y) / elapsed) * MIN_FRAME_MS : 0
106-
const velocity = Math.hypot(velocityX, velocityY)
154+
const delta = previous ? Math.max(now - previous.time, 1) : MIN_FRAME_MS
155+
const perFrame = Math.max(delta, MIN_FRAME_MS)
156+
const instantX = previous ? ((event.clientX - previous.x) / perFrame) * MIN_FRAME_MS : 0
157+
const instantY = previous ? ((event.clientY - previous.y) / perFrame) * MIN_FRAME_MS : 0
158+
159+
/**
160+
* Derived from the real elapsed time rather than applied per event, so a
161+
* 120Hz pointer and a 60Hz one settle over the same wall-clock duration.
162+
*/
163+
const smoothing = 1 - Math.exp(-delta / VELOCITY_TIME_CONSTANT_MS)
164+
const velocity = velocityRef.current
165+
velocity.x += (instantX - velocity.x) * smoothing
166+
velocity.magnitude += (Math.hypot(instantX, instantY) - velocity.magnitude) * smoothing
107167

108168
lastPointerRef.current = { x: event.clientX, y: event.clientY, time: now }
109-
setState({
110-
visible: true,
111-
...getTooltipPosition(event.clientX, event.clientY),
112-
skew: clamp(velocityX * 0.11, -6, 6),
113-
scaleX: 1 + Math.min(0.035, velocity / 1100),
114-
scaleY: 1 - Math.min(0.02, velocity / 1500),
169+
apply(event.clientX, event.clientY, {
170+
skew: quantize(clamp(velocity.x * 0.11, -6, 6)),
171+
scaleX: quantize(1 + Math.min(0.035, velocity.magnitude / 1100)),
172+
scaleY: quantize(1 - Math.min(0.02, velocity.magnitude / 1500)),
115173
})
116174
},
117175
onPointerLeave: hide,
@@ -121,14 +179,7 @@ export function useFloatingTooltip(canShow: (target: HTMLElement) => boolean): {
121179
if (!canShowRef.current(target)) return
122180
if (!isFocusVisible(target)) return
123181
const rect = target.getBoundingClientRect()
124-
lastPointerRef.current = null
125-
setState({
126-
visible: true,
127-
...getTooltipPosition(rect.left + rect.width / 2, rect.bottom),
128-
skew: 0,
129-
scaleX: 1,
130-
scaleY: 1,
131-
})
182+
showFromElement(rect.left + rect.width / 2, rect.bottom)
132183
},
133184
onBlur: hide,
134185
}
@@ -196,6 +247,14 @@ export function clamp(value: number, min: number, max: number): number {
196247
return Math.max(min, Math.min(max, value))
197248
}
198249

250+
/**
251+
* Rounds a flourish value to 3 decimals so pointer jitter below the visible
252+
* threshold settles to a stable number instead of re-rendering every consumer.
253+
*/
254+
function quantize(value: number): number {
255+
return Math.round(value * 1000) / 1000
256+
}
257+
199258
/**
200259
* Whether an element currently matches `:focus-visible` (keyboard focus, not focus produced by a
201260
* mouse click). Used to keep the tooltip from re-appearing/repositioning when the trigger is
@@ -248,12 +307,14 @@ export const FloatingTooltip = React.memo(function FloatingTooltip({
248307
aria-hidden={role ? undefined : 'true'}
249308
data-native-surface-overlay=''
250309
className={cn(
251-
'pointer-events-none fixed top-0 left-0 z-[var(--z-tooltip)] w-fit max-w-[min(16rem,calc(100vw-2rem))] rounded-lg border border-[var(--border)] bg-[var(--bg)] px-2 py-1.5 text-[var(--text-body)] text-caption opacity-100 shadow-sm transition-[opacity,filter,transform] duration-150 ease-out',
310+
'pointer-events-none fixed top-0 left-0 z-[var(--z-tooltip)] w-fit max-w-[min(16rem,calc(100vw-2rem))] rounded-lg border border-[var(--border)] bg-[var(--bg)] px-2 py-1.5 text-[var(--text-body)] text-caption opacity-100 shadow-sm transition-[opacity,translate] duration-150 ease-out',
252311
'motion-reduce:transition-none',
253312
className
254313
)}
255314
style={{
256-
transform: `${getTooltipTranslate(state, offset)} skew(${state.skew}deg) scale(${state.scaleX}, ${state.scaleY})`,
315+
translate: getTooltipTranslate(state, offset),
316+
scale: `${state.scaleX} ${state.scaleY}`,
317+
transform: `skew(${state.skew}deg)`,
257318
transformOrigin: state.alignX === 'left' ? '12px 12px' : 'calc(100% - 12px) 12px',
258319
}}
259320
>
@@ -268,25 +329,31 @@ function getTooltipPosition(
268329
clientY: number
269330
): Pick<FloatingTooltipState, 'x' | 'y' | 'alignX' | 'alignY'> {
270331
if (typeof window === 'undefined') {
271-
return { x: clientX, y: clientY, alignX: 'left', alignY: 'below' }
332+
return { x: Math.round(clientX), y: Math.round(clientY), alignX: 'left', alignY: 'below' }
272333
}
273334

274335
const alignX = window.innerWidth - clientX < EDGE_THRESHOLD ? 'right' : 'left'
275336
const alignY = window.innerHeight - clientY < EDGE_THRESHOLD / 2 ? 'above' : 'below'
276337

277338
return {
278-
x: clamp(clientX, EDGE_GUTTER, window.innerWidth - EDGE_GUTTER),
279-
y: clamp(clientY, EDGE_GUTTER, window.innerHeight - EDGE_GUTTER),
339+
x: Math.round(clamp(clientX, EDGE_GUTTER, window.innerWidth - EDGE_GUTTER)),
340+
y: Math.round(clamp(clientY, EDGE_GUTTER, window.innerHeight - EDGE_GUTTER)),
280341
alignX,
281342
alignY,
282343
}
283344
}
284345

346+
/**
347+
* Value for the `translate` CSS property. Kept off the `transform` property so the
348+
* velocity flourish (`scale` + `transform: skew()`) can stay out of the transition
349+
* list while the tooltip's position still eases toward the cursor.
350+
*/
285351
function getTooltipTranslate(state: FloatingTooltipState, offset: number): string {
286-
const xOffset = state.alignX === 'left' ? `${offset}px` : `calc(-100% - ${offset}px)`
287-
const yOffset = state.alignY === 'below' ? `${offset}px` : `calc(-100% - ${offset}px)`
352+
const x = state.alignX === 'left' ? `${state.x + offset}px` : `calc(${state.x - offset}px - 100%)`
353+
const y =
354+
state.alignY === 'below' ? `${state.y + offset}px` : `calc(${state.y - offset}px - 100%)`
288355

289-
return `translate3d(${state.x}px, ${state.y}px, 0) translate(${xOffset}, ${yOffset})`
356+
return `${x} ${y}`
290357
}
291358

292359
/**

0 commit comments

Comments
 (0)