Skip to content
Source
Sign inSign up
TanStack/charts

[Bug] Tooltip exit: a repeated `hide()` restarts the exit, and finished animations stay attached #209

Closedtoomingos(GitHub profile, opens in a new tab) opened this issue 0 comments

Version: @tanstack/charts 1.1.0 (57774e1). Split from #187 (sections B and C) so it can be closed on its own.

Two bugs in the motion() tooltip controller.

B. A repeated hide() restarts the exit

  • hide() returns early only when hidden is set, which is not true during the exit (tooltip.ts:336-346).
  • The motion controller then cancels the running exit and starts a new one (motion.ts:614-616). A repeating caller means hidden is never set.
  • Real caller: during a data transition, every presentation frame calls updateFocus(..., true) for a resting pointer (renderer.ts:1232-1243), which ends in tooltipInstance?.hide() (:1258).

Expected: hide() during a running exit leaves it running. Proposed: return early in the controller's hide while the exit runs.

C. Finished tooltip animations stay attached

  • Movement and presence animations use fill: 'both' (motion.ts:471, :517). onfinish only removes inline styles (:475-478); nothing calls cancel().

Expected: nothing animated remains after finish. Proposed: call animation.cancel() in each onfinish after clearing styles.

Downstream measurement (bklit migration, Chrome only): with the animation attached, text paints about 0.25px off a static element; after cancelling, 0.00% pixel diff.

Repro

Save as packages/charts-core/src/repro.test.ts, run pnpm vitest run packages/charts-core/src/repro.test.ts. jsdom has no Element.animate, so the test installs a fake. Both tests assert the expected behaviour and fail on 57774e1 (exit.cancel called 1 time, expected 0; 1 animation remains, expected 0).

import { expect, it, vi } from 'vitest'
import { scaleLinear } from 'd3-scale'
import { lineY } from './line'
import { motion } from './motion'
import { mountChartRenderer } from './renderer'
import { defineChart } from './scene'
import { tooltip } from './tooltip'

const rows = [1, 2, 3, 4].map((x) => ({ x, y: x * 10 }))
function setup(extra = {}, onBody?: () => void) {
  const live: any[] = [] // fake Web Animations (jsdom has no Element.animate)
  ;(HTMLElement.prototype as any).animate = (f: any[], o: any) => {
    const a: any = { kind: 'translate' in f[0] ? 'move' : 'presence', onfinish: null }
    a.cancel = vi.fn(() => live.splice(live.indexOf(a), 1))
    return (live.push(a), a)
  }
  const q: FrameRequestCallback[] = []
  vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => q.push(cb))
  const make = (d: typeof rows) => defineChart({
    marks: [lineY(d, { x: 'x', y: 'y' })], maxFocusDistance: 20, tooltip: { use: tooltip, ...extra },
    scales: { x: { scale: scaleLinear().domain([1, 4]) }, y: { scale: scaleLinear().domain([0, 40]) } },
  })
  const opts = { renderer: motion({ initial: false, transition: { type: 'tween', duration: 300 } }), width: 300, height: 200, ariaLabel: 'r', onTooltipBodyChange: onBody }
  const el = document.createElement('div')
  const host = mountChartRenderer(el, { ...opts, definition: make(rows) })
  const svg = el.querySelector('svg.ts-chart')!
  vi.spyOn(svg, 'getBoundingClientRect').mockReturnValue(DOMRect.fromRect({ width: 300, height: 200 }))
  const move = (x: number, y: number) => svg.dispatchEvent(new MouseEvent('pointermove', { bubbles: true, clientX: x, clientY: y }))
  return { live, move, p: host.getScene().points[1]!, tip: () => el.querySelector<HTMLElement>('.ts-chart-tooltip')!,
    retarget() { host.update({ ...opts, definition: make(rows.map((r) => ({ ...r, y: r.y + 5 }))) }); q.splice(0).forEach((cb) => cb(16)) } }
}
it('B: a repeated hide() lets the running exit finish', () => {
  const { p, move, live, tip, retarget } = setup()
  move(p.x, p.y); move(p.x - 60, p.y + 60) // far from any point: hide() starts the exit
  const exit = live.filter((a) => a.kind === 'presence').at(-1)
  retarget() // data motion repaints focus under the resting pointer: hide() again
  expect(exit.cancel).not.toHaveBeenCalled(); exit.onfinish(); expect(tip().hidden).toBe(true)
})
it('C: no animation stays attached once they finish', () => {
  const { p, move, live } = setup(); move(p.x, p.y); move(p.x + 40, p.y)
  live.slice().forEach((a) => a.onfinish()); expect(live).toHaveLength(0) // fill: 'both' keeps them until cancel()
})

Related: #187 (tooltip move cost; its first-show and per-phase options moved to a separate feature issue that links here).

  1. tannerlinsley closed this

Sign in to join this conversation.