Skip to content

Commit 87d479b

Browse files
committed
Merge branch 'next' of https://github.com/techniq/layerchart into next
2 parents 8154d67 + 0ec0f83 commit 87d479b

7 files changed

Lines changed: 53 additions & 11 deletions

File tree

.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

.changeset/pre.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"blue-doodles-chew",
1919
"brave-spies-give",
2020
"breezy-donuts-sniff",
21+
"brown-terms-tie",
2122
"bumpy-breads-rhyme",
2223
"calm-jars-mix",
2324
"chatty-flies-bet",

packages/layerchart/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# LayerChart
22

3+
## 2.0.0-next.41
4+
5+
### Patch Changes
6+
7+
- fix(Tooltip): Correctly set tooltip position on chart enter and exit ([#655](https://github.com/techniq/layerchart/pull/655))
8+
39
## 2.0.0-next.40
410

511
### Patch Changes

packages/layerchart/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": "MIT",
66
"repository": "techniq/layerchart",
77
"homepage": "https://layerchart.com",
8-
"version": "2.0.0-next.40",
8+
"version": "2.0.0-next.41",
99
"scripts": {
1010
"dev": "vite dev --port 3002",
1111
"build": "vite build",

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)