Skip to content

Commit f37cd91

Browse files
fix(react): render a custom tooltip body before the moved tooltip paints
Fixes #239
1 parent a2e82ab commit f37cd91

4 files changed

Lines changed: 94 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/react-charts': patch
3+
---
4+
5+
Render a custom tooltip body in the same task as the tooltip moves, so a pointer move no longer paints the previous point's body at the new position.

‎packages/react-charts/src/RendererChart.tsx‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ const serverSnapshot = () => true
2121
const useIsomorphicLayoutEffect =
2222
typeof document === 'undefined' ? React.useEffect : React.useLayoutEffect
2323

24+
let hostCommits = 0
25+
26+
function inHostCommit(run: () => void) {
27+
hostCommits += 1
28+
try {
29+
run()
30+
} finally {
31+
hostCommits -= 1
32+
}
33+
}
34+
35+
export function isInHostCommit() {
36+
return hostCommits > 0
37+
}
38+
2439
interface ChartSurfaceProps {
2540
markup: string
2641
}
@@ -179,16 +194,18 @@ export function RendererChartImplementation<
179194
useIsomorphicLayoutEffect(() => {
180195
const container = containerRef.current
181196
if (!container) return
182-
adapter.update(hostOptions)
183-
if (!mountedRef.current) {
184-
adapter.mount(container)
185-
mountedRef.current = true
186-
}
197+
inHostCommit(() => {
198+
adapter.update(hostOptions)
199+
if (!mountedRef.current) {
200+
adapter.mount(container)
201+
mountedRef.current = true
202+
}
203+
})
187204
}, [adapter, hostOptions])
188205

189206
useIsomorphicLayoutEffect(
190207
() => () => {
191-
adapter.destroy()
208+
inHostCommit(() => adapter.destroy())
192209
mountedRef.current = false
193210
},
194211
[adapter],

‎packages/react-charts/src/tooltip.test.tsx‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,66 @@ describe('tooltip state ownership', () => {
121121
}
122122
},
123123
)
124+
125+
it('commits the body before the browser paints an interaction', async () => {
126+
const definition = defineChart({
127+
marks: [
128+
dot(
129+
[
130+
{ x: 0, y: 1 },
131+
{ x: 1, y: 2 },
132+
],
133+
{ x: 'x', y: 'y' },
134+
),
135+
],
136+
scales: {
137+
x: { scale: scaleLinear().domain([0, 1]) },
138+
y: { scale: scaleLinear().domain([0, 3]) },
139+
},
140+
tooltip: { use: tooltip },
141+
})
142+
const container = document.createElement('div')
143+
document.body.append(container)
144+
const root = createRoot(container)
145+
const errors = vi.spyOn(console, 'error')
146+
const render = (width: number) =>
147+
act(async () =>
148+
root.render(
149+
<Chart
150+
definition={definition}
151+
width={width}
152+
height={260}
153+
ariaLabel="chart"
154+
renderTooltipBody={({ primaryPoint }) => (
155+
<span data-body>{primaryPoint?.datum.y}</span>
156+
)}
157+
/>,
158+
),
159+
)
160+
try {
161+
await render(480)
162+
const svg = container.querySelector('svg')
163+
if (!svg) throw new Error('Missing SVG')
164+
await act(async () =>
165+
svg.dispatchEvent(new FocusEvent('focusin', { bubbles: true })),
166+
)
167+
expect(container.querySelector('[data-body]')?.textContent).toBe('1')
168+
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: false })
169+
try {
170+
svg.dispatchEvent(
171+
new KeyboardEvent('keydown', { key: 'ArrowRight', bubbles: true }),
172+
)
173+
} finally {
174+
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true })
175+
}
176+
expect(container.querySelector('[data-body]')?.textContent).toBe('2')
177+
await render(520)
178+
expect(container.querySelector('[data-body]')?.textContent).toBe('2')
179+
expect(errors).not.toHaveBeenCalled()
180+
} finally {
181+
errors.mockRestore()
182+
await act(async () => root.unmount())
183+
container.remove()
184+
}
185+
})
124186
})

‎packages/react-charts/src/tooltip.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use client'
22

33
import * as React from 'react'
4-
import { createPortal } from 'react-dom'
4+
import { createPortal, flushSync } from 'react-dom'
55
import type {
66
ChartTooltipBodyContext,
77
ChartTooltipBodyTarget,
@@ -19,6 +19,7 @@ import {
1919
type CanvasChartProps as BaseCanvasChartProps,
2020
} from './CanvasChart'
2121
import {
22+
isInHostCommit,
2223
RendererChartImplementation,
2324
type RendererChartCommonProps as BaseRendererChartCommonProps,
2425
type RendererChartProps as BaseRendererChartProps,
@@ -163,7 +164,8 @@ function TooltipBody<
163164
> | null>(null)
164165
const onChange = React.useCallback(
165166
(nextTarget: ChartTooltipBodyTarget<TDatum, TXValue, TYValue> | null) => {
166-
setTarget(nextTarget)
167+
if (isInHostCommit()) setTarget(nextTarget)
168+
else flushSync(() => setTarget(nextTarget))
167169
},
168170
[],
169171
)

0 commit comments

Comments
 (0)