Skip to content
Source
Sign inSign up
TanStack/charts

[Bug] `motion()`: no enter animation for keyed rect/dot/text on first paint, or for marks in a newly joined group #193

Opentoomingos(GitHub profile, opens in a new tab) opened this issue 1 comment

Version: @tanstack/charts 1.1.0 (57774e1).

Description

Two enter paths skip marks that the update path animates:

  • A. Keyed rect, dot and text marks get no enter track on first paint.
  • E. A mark group that joins during an update only fades; its per-datum enter (bar grow) never plays.

A. First paint: no enter track for keyed rect, dot, text

  • First paint builds only bar, cartesian path, radial path and arc tracks (motion.ts:281-286).
  • The generic enter track is added only for elements that join in an update (motion.ts:2159).
  • Docs list only bars, paths and arcs for initial choreography (motion.md:54-56).

Expected: keyed rect, dot and text get the same enter track on first paint as when they join a group, or the docs say initial covers only bars, paths and arcs.

Fix: call addEnterMotionTrack (motion.ts:2475) in the first-paint path for keyed elements no other track covers.

Repro: save as packages/charts-core/src/repro.test.ts, run pnpm vitest run packages/charts-core/src/repro.test.ts (vitest + jsdom, rAF driven by hand). "Animated" means the element has data-ts-motion-role. The test asserts the expected behaviour; the control passes.

import { expect, it, vi } from 'vitest'
import { scaleLinear } from 'd3-scale'
import { barY, defineChart, dot, rect, text } from './index'
import { motion } from './motion'
import { createChartScene } from './scene'

const queue: FrameRequestCallback[] = []
const tick = (t: number) => queue.splice(0).forEach((f) => f(t))
function mount() {
  vi.spyOn(window, 'requestAnimationFrame').mockImplementation((f) => queue.push(f))
  vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(() => {})
  const el = document.createElement('div')
  const s = motion({ transition: { type: 'tween', duration: 1000, easing: 'linear' } }).mount(el, () => {})
  return { el, render: (d: ReturnType<typeof defineChart>) =>
    s.render(createChartScene(d, { width: 300, height: 200 }), { ariaLabel: 'x' }) }
}
const scales = { x: { scale: scaleLinear }, y: { scale: scaleLinear } }
const rows = [0, 1, 2].map((i) => ({ id: `r${i}`, x: i, x2: i + 0.5, y: 10 + i }))
const A = (data: typeof rows) => defineChart({ scales, guides: false, marks: [
  barY(data, { key: 'id', x: 'x', y: 'y' }),
  rect(data, { key: 'id', x1: 'x', x2: 'x2', y1: () => 0, y2: 'y' }),
  dot(data, { key: 'id', x: 'x', y: 'y' }),
  text(data, { key: 'id', x: 'x', y: 'y', text: 'y' }),
] })
const animated = (el: HTMLElement, sel: string) =>
  [...el.querySelectorAll(`${sel} [data-ts-key]`)].filter((n) => n.hasAttribute('data-ts-motion-role')).length
const kinds = (el: HTMLElement) => ['bar', 'rect', 'dot', 'text'].map((k) => `${k}=${animated(el, `.ts-chart__${k}`)}`)

it('A: first paint animates keyed rect, dot and text like bars', () => {
  const { el, render } = mount()
  render(A(rows)); tick(0); tick(100)
  expect(kinds(el)).toEqual(['bar=3', 'rect=3', 'dot=3', 'text=3'])
})
it('A control: the same marks join an empty chart with an enter track', () => {
  const { el, render } = mount()
  render(A([])); tick(0); tick(2000)
  render(A(rows)); tick(0); tick(100)
  expect(kinds(el)).toEqual(['bar=3', 'rect=3', 'dot=3', 'text=3'])
})

Output on 57774e14:

A: expected ['bar=3','rect=3','dot=3','text=3'], received ['bar=3','rect=0','dot=0','text=0']
A control: passes

Downstream measurement (bklit migration, Chrome, 800 ms window, keyed barY/lineY/rect/dot/text): first paint had 0 animating rects/dots/text; after an empty-then-data flip, 8/17 each.

E. A mark group that joins during an update only fades (from this comment)

In the update reconciler, an unmatched child is cloned and addEnterMotionTrack runs on that one element only (motion.ts:2140-2159). The function has no recursion into descendants. The bar grow branch requires the element itself to be a rect or path (motion.ts:1414-1420, motion.ts:2505-2510). So a bar joining an existing g.ts-chart__bar-y group grows, but a whole new group (a series that is added, or data that goes from [] to N rows so the mark group is new) falls to the opacity branch (motion.ts:2596-2635) and its bars never grow. createBarTracks runs only on first paint (motion.ts:273-286, motion.ts:1238).

Repro (source-level; not run): render a barY chart with [], then re-render with data. Expected a grow, observed a fade. Changing 0 to N on a chart whose group already exists does grow.

Proposal: when a group joins, run the first-paint enter tracks for its descendants (createBarTracks and friends scoped to the new group), so a joining group animates like a first paint.

B. Text content cannot be tweened

Moved to #215 (a new text option, with its repro).

C. Rounded polar arcs snap

Moved to #194, which covers arc motion, with its repro.

D. The only generic enter is an opacity fade (comment)

Moved to #214, together with the sunburst enter/exit seeds from #194 C.

  1. Two more first-paint and enter gaps next to A. Checked against 57774e14a1a86081eddb3a23523724a40da05640 (v1.1.0).

    D. The only generic enter is an opacity fade; there is no scale or transform enter

    When a keyed rect, dot or text does get an enter track (when it joins in an update), addEnterMotionTrack ends in a single opacity attribute tween from 0 to the target (motion.ts:2596-2635). ChartMotionTiming carries only delay, transition and path (types.ts:872-877), so a mark cannot ask for another shape of enter.

    Example: radial gauge notches (areaX/rect marks) that should grow from scale 0 about each notch's own centre, with a per-notch delay. With the built-in tracks the only options are a fade or nothing. The workaround is scoped CSS keyframes keyed on data-ts-key, which depend on renderer-owned DOM and have to be aligned with the motion clock by hand.

    Proposal: an enter option on motion() with from: { opacity?, scale? } and origin: 'center' | 'start', applied per element with the existing per-datum delay. Or a documented per-element enter transform.

    E. A mark group that joins during an update only fades; its per-datum enter never plays

    In the update reconciler, an unmatched child is cloned and addEnterMotionTrack runs on that one element only (motion.ts:2140-2159). The function has no recursion into descendants. The bar grow branch requires the element itself to be a rect or path (motion.ts:1414-1420, motion.ts:2505-2510). So a bar joining an existing g.ts-chart__bar-y group grows, but a whole new group (a series that is added, or data that goes from [] to N rows so the mark group is new) falls to the opacity branch (motion.ts:2596-2635) and its bars never grow. createBarTracks runs only on first paint (motion.ts:273-286, motion.ts:1238).

    Repro (source-level; not run): render a barY chart with [], then re-render with data. Expected a grow, observed a fade. Changing 0 to N on a chart whose group already exists does grow.

    Proposal: when a group joins, run the first-paint enter tracks for its descendants (createBarTracks and friends scoped to the new group), so a joining group animates like a first paint.

  2. toomingos(GitHub profile, opens in a new tab) changed the title

Sign in to join this conversation.