Skip to content

Commit ee80654

Browse files
committed
[TooltipContext] Replace on:click event handler with onClick prop (easier to consume when using <Chart tooltip={{ ... }} />)
1 parent 9426f85 commit ee80654

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

.changeset/neat-rockets-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': minor
3+
---
4+
5+
[TooltipContext] Replace `on:click` event handler with `onClick` prop (easier to consume when using `<Chart tooltip={{ onClick(e) => { ... } }} />`)

packages/layerchart/src/lib/components/Chart.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
>
109109
<GeoContext {...geo} let:projection>
110110
{#if tooltip}
111-
<TooltipContext {...tooltip} let:tooltip>
111+
{@const tooltipProps = typeof tooltip === 'object' ? tooltip : {}}
112+
<TooltipContext {...tooltipProps} let:tooltip>
112113
<slot
113114
{aspectRatio}
114115
{containerHeight}

packages/layerchart/src/lib/components/TooltipContext.svelte

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
<script lang="ts">
3434
import { raise } from 'layercake';
35-
import { createEventDispatcher } from 'svelte';
3635
import { writable } from 'svelte/store';
3736
import { bisector, max, min } from 'd3-array';
3837
import { Delaunay } from 'd3-delaunay';
@@ -46,8 +45,6 @@
4645
import { isScaleBand, scaleInvert } from '$lib/utils/scales';
4746
import { quadtreeRects } from '$lib/utils/quadtree';
4847
49-
const dispatch = createEventDispatcher<{ click: { data: any } }>();
50-
5148
const { flatData, x, xScale, xGet, xRange, y, yScale, yGet, yRange, width, height, padding } =
5249
getContext('LayerCake');
5350
@@ -89,6 +86,8 @@
8986
/** Enable debug view (show hit targets, etc) */
9087
export let debug = false;
9188
89+
export let onClick: ({ data }: { data: any }) => any = () => {};
90+
9291
const tooltip = writable({ y: 0, x: 0, data: null, show: showTooltip, hide: hideTooltip });
9392
setTooltipContext(tooltip);
9493
@@ -319,13 +318,13 @@
319318
width: Array.isArray(xValue)
320319
? xValue[1] - xValue[0]
321320
: isScaleBand($xScale)
322-
? $xScale.step()
323-
: min($xRange) + x,
321+
? $xScale.step()
322+
: min($xRange) + x,
324323
height: Array.isArray(yValue)
325324
? yValue[1] - yValue[0]
326325
: isScaleBand($yScale)
327-
? $yScale.step()
328-
: max($yRange) - y,
326+
? $yScale.step()
327+
: max($yRange) - y,
329328
data: d,
330329
};
331330
}
@@ -349,7 +348,7 @@
349348
on:mousemove={showTooltip}
350349
on:mouseleave={hideTooltip}
351350
on:click={(e) => {
352-
dispatch('click', { data: $tooltip?.data });
351+
onClick({ data: $tooltip?.data });
353352
}}
354353
/>
355354
</Html>
@@ -365,7 +364,7 @@
365364
on:mousemove={(e) => showTooltip(e, point.data)}
366365
on:mouseleave={hideTooltip}
367366
on:click={(e) => {
368-
dispatch('click', { data: point.data });
367+
onClick({ data: point.data });
369368
}}
370369
/>
371370
</g>
@@ -386,7 +385,7 @@
386385
on:mousemove={(e) => showTooltip(e, rect.data)}
387386
on:mouseleave={hideTooltip}
388387
on:click={(e) => {
389-
dispatch('click', { data: rect.data });
388+
onClick({ data: rect.data });
390389
}}
391390
/>
392391
{/each}

0 commit comments

Comments
 (0)