Skip to content

Commit c72b0af

Browse files
authored
fix(Tooltip): Correctly set tooltip position on chart enter and exit (#655)
* Ignore content-collections (needed until #651 is merged) * fix(Tooltip): Correctly set tooltip position on chart enter and exit
1 parent 93cad59 commit c72b0af

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

.changeset/brown-terms-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
fix(Tooltip): Correctly set tooltip position on chart enter and exit

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ coverage/
99
.DS_Store
1010

1111
test-*
12+
13+
# Content Collections
14+
.content-collections

packages/layerchart/src/lib/components/tooltip/Tooltip.svelte

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,20 @@
192192
const ctx = getChartContext();
193193
const tooltipCtx = getTooltipContext();
194194
195-
let tooltipWidth = $state(0);
196-
let tooltipHeight = $state(0);
195+
let tooltipWidth = $state<number | null>(null);
196+
let tooltipHeight = $state<number | null>(null);
197197
198198
function alignValue(value: number, align: Align, additionalOffset: number, tooltipSize: number) {
199199
const alignOffset = align === 'center' ? tooltipSize / 2 : align === 'end' ? tooltipSize : 0;
200200
return value + (align === 'end' ? -additionalOffset : additionalOffset) - alignOffset;
201201
}
202202
203203
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 };
209207
}
208+
210209
const xBandOffset = isScaleBand(ctx.xScale)
211210
? ctx.xScale.step() / 2 - (ctx.xScale.padding() * ctx.xScale.step()) / 2
212211
: 0;
@@ -345,8 +344,8 @@
345344
};
346345
});
347346
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);
350349
351350
$effect(() => {
352351
if (!tooltipCtx.data) {

packages/layerchart/src/lib/utils/motion.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function setupTracking<T>(
162162
if (options.controlled) return;
163163

164164
$effect(() => {
165-
motion.set(getValue());
165+
motion.set(getValue(), { instant: motion.target == null });
166166
});
167167
}
168168

packages/layerchart/src/routes/docs/components/Tooltip/+page.svelte

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,37 @@
457457
</div>
458458
</Preview>
459459

460+
<h2>Disable motion</h2>
461+
462+
<Preview data={dateSeries}>
463+
<div class="h-[300px] p-4 border rounded-sm">
464+
<Chart
465+
data={dateSeries}
466+
x="date"
467+
y="value"
468+
yDomain={[0, null]}
469+
yNice
470+
padding={{ left: 16, bottom: 24 }}
471+
tooltip={{ mode: 'quadtree-x' }}
472+
>
473+
<Layer type={shared.renderContext}>
474+
<Axis placement="left" grid rule />
475+
<Axis placement="bottom" rule />
476+
<Area class="fill-primary/30" line={{ class: 'stroke-primary stroke-2' }} />
477+
<Highlight points lines />
478+
</Layer>
479+
<Tooltip.Root motion="none">
480+
{#snippet children({ data })}
481+
<Tooltip.Header value={data.date} format="day" />
482+
<Tooltip.List>
483+
<Tooltip.Item label="value" value={data.value} />
484+
</Tooltip.List>
485+
{/snippet}
486+
</Tooltip.Root>
487+
</Chart>
488+
</div>
489+
</Preview>
490+
460491
<h2>Anchor location</h2>
461492

462493
<div class="grid grid-cols-3 gap-2 mb-2">

0 commit comments

Comments
 (0)