|
192 | 192 | const ctx = getChartContext(); |
193 | 193 | const tooltipCtx = getTooltipContext(); |
194 | 194 |
|
195 | | - let tooltipWidth = $state(0); |
196 | | - let tooltipHeight = $state(0); |
| 195 | + let tooltipWidth = $state<number | null>(null); |
| 196 | + let tooltipHeight = $state<number | null>(null); |
197 | 197 |
|
198 | 198 | function alignValue(value: number, align: Align, additionalOffset: number, tooltipSize: number) { |
199 | 199 | const alignOffset = align === 'center' ? tooltipSize / 2 : align === 'end' ? tooltipSize : 0; |
200 | 200 | return value + (align === 'end' ? -additionalOffset : additionalOffset) - alignOffset; |
201 | 201 | } |
202 | 202 |
|
203 | 203 | const positions = $derived.by(() => { |
204 | | - if (!tooltipCtx.data) { |
205 | | - // if no data, fallback? |
206 | | - const tooltipX = untrack(() => tooltipCtx.x); |
207 | | - const tooltipY = untrack(() => tooltipCtx.y); |
208 | | - return { x: tooltipX, y: tooltipY }; |
| 204 | + // if no data or tooltip size is not known yet, return null |
| 205 | + if (!tooltipCtx.data || tooltipWidth === null || tooltipHeight === null) { |
| 206 | + return { x: null, y: null }; |
209 | 207 | } |
| 208 | +
|
210 | 209 | const xBandOffset = isScaleBand(ctx.xScale) |
211 | 210 | ? ctx.xScale.step() / 2 - (ctx.xScale.padding() * ctx.xScale.step()) / 2 |
212 | 211 | : 0; |
|
345 | 344 | }; |
346 | 345 | }); |
347 | 346 |
|
348 | | - const motionX = createMotion(tooltipCtx.x, () => positions.x, motion); |
349 | | - const motionY = createMotion(tooltipCtx.y, () => positions.y, motion); |
| 347 | + const motionX = createMotion(null, () => positions.x, motion); |
| 348 | + const motionY = createMotion(null, () => positions.y, motion); |
350 | 349 |
|
351 | 350 | $effect(() => { |
352 | 351 | if (!tooltipCtx.data) { |
|
0 commit comments