Skip to content
Source
Sign inSign up
TanStack/charts

Renderer treats a sub-pixel width difference as a resize, which cancels the `motion()` entrance #182

Closedluciensolteo(GitHub profile, opens in a new tab) opened this issue 2 comments

Version: @tanstack/charts 1.0.0 (unchanged on main).

The first render sizes the scene from getComputedStyle(container).width (contentBoxDimension → cssPixelValue). Chromium serializes that rounded, e.g. 569.594px. The ResizeObserver then reports the exact contentRect.width, 569.59375, and responsiveSizeChanged compares the two with !==:

next.width !== scene.width

So on any container with a fractional width (a 32% grid column, minmax(…)), the observer's first callback schedules a "resize" render. With motion()'s default resize: false, that render paints the final frame without animation, and the entrance is cancelled one frame after it starts. Containers that happen to be a whole number of pixels wide animate fine, so it looks random from the app side.

Repro: mount a Chart with renderer={motion()} inside <div style="width: 569.59375px">. data-ts-motion-state goes running, and the SVG is replaced on the first observer callback without ever reaching finished.

Suggested fix: treat a difference under half a pixel as no change (Math.abs(next.width - scene.width) >= 0.5, same for height), or take the first measurement from a source with the same precision as the observer. We're running the first as a pnpm patch, and it restores the entrance.

  1. Follow-up on 1.1.0 (57774e14a1a86081eddb3a23523724a40da05640): the fractional first-observation case from the report is fixed, but a real size change while the entrance is still running still cancels it.

    • The observer callback schedules a "resize" render whenever responsiveSizeChanged() is true (renderer.ts:349-402, the observer at renderer.ts:371-402; the 1.1.0 precision adoption only applies to the first callback).
    • With the default motion({ resize: false }), resolveAnimation returns undefined for a resize render (renderer.ts:1470-1486), and in motion().render the scene is marked as resized and animate is false (motion.ts:961-981). It then calls cancelAnimation() unconditionally (motion.ts:985), which tears down the entrance that is still running, and paints the final frame.

    Repro: mount a Chart with renderer={motion()} in a container whose width changes 100 ms after mount (a sidebar that opens, a scrollbar appearing, a font swap shifting layout). data-ts-motion-state goes running and is replaced without reaching finished.

    This is the usual case on a page where layout settles after first paint, and the entrance looks cut short.

    Read from source, not run in jsdom (no layout).

    Proposal, either:

    1. When a resize render arrives while an enter run is active, keep the run and apply the new size once it finishes (or retarget it), instead of cancelling.
    2. Document that a resize during an entrance cancels it under the default resize: false.
  2. Thanks @luciensolteo for the original report and @toomingos for separating out the real-resize follow-up. The fractional first-observation fix shipped in 1.1.0. PR #218 now documents the remaining policy explicitly: with resize: false, a real resize cancels an entrance and paints the resized final scene; resize: true animates from the currently painted geometry. The default is unchanged.

    The merged motion reference covers this behavior, and two regression cases verify both settings: https://github.com/TanStack/charts/blob/main/docs/reference/motion.md. Full validation passed before merge, including 2,220 main-suite tests. This addresses the documentation option in the follow-up, so I am closing this issue. The separate tooltip patch in #218 is still moving through the 1.1.1 release workflow.

  3. tannerlinsley closed this

Sign in to join this conversation.