svelteplot 0.8.1-pr-298.6 → 0.8.1-pr-301.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +1 -1
  2. package/dist/core/Plot.svelte +4 -4
  3. package/dist/hooks/usePlot.svelte.d.ts +50 -0
  4. package/dist/hooks/usePlot.svelte.js +87 -0
  5. package/dist/index.d.ts +1 -0
  6. package/dist/index.js +1 -0
  7. package/dist/marks/Area.svelte +3 -5
  8. package/dist/marks/Arrow.svelte +3 -5
  9. package/dist/marks/AxisX.svelte +2 -3
  10. package/dist/marks/AxisY.svelte +2 -3
  11. package/dist/marks/BarX.svelte +2 -4
  12. package/dist/marks/BarY.svelte +2 -4
  13. package/dist/marks/Brush.svelte +3 -3
  14. package/dist/marks/Cell.svelte +2 -4
  15. package/dist/marks/ColorLegend.svelte +2 -4
  16. package/dist/marks/CustomMarkHTML.svelte +5 -10
  17. package/dist/marks/DifferenceY.svelte +3 -5
  18. package/dist/marks/Dot.svelte +4 -5
  19. package/dist/marks/Frame.svelte +3 -9
  20. package/dist/marks/Geo.svelte +3 -5
  21. package/dist/marks/GridX.svelte +3 -10
  22. package/dist/marks/GridY.svelte +3 -4
  23. package/dist/marks/HTMLTooltip.svelte +5 -5
  24. package/dist/marks/Line.svelte +2 -4
  25. package/dist/marks/Link.svelte +2 -4
  26. package/dist/marks/Pointer.svelte +4 -4
  27. package/dist/marks/Rect.svelte +2 -4
  28. package/dist/marks/RectX.svelte +4 -4
  29. package/dist/marks/RectY.svelte +4 -4
  30. package/dist/marks/RuleX.svelte +2 -4
  31. package/dist/marks/RuleY.svelte +2 -4
  32. package/dist/marks/SymbolLegend.svelte +2 -4
  33. package/dist/marks/SymbolLegend.svelte.d.ts +17 -2
  34. package/dist/marks/TickX.svelte +2 -3
  35. package/dist/marks/TickY.svelte +2 -3
  36. package/dist/marks/Trail.svelte +3 -5
  37. package/dist/marks/Vector.svelte +3 -4
  38. package/dist/marks/WaffleX.svelte +2 -3
  39. package/dist/marks/WaffleY.svelte +2 -4
  40. package/dist/marks/helpers/AreaCanvas.svelte +2 -4
  41. package/dist/marks/helpers/Box.svelte +0 -1
  42. package/dist/marks/helpers/CanvasLayer.svelte +2 -4
  43. package/dist/marks/helpers/DotCanvas.svelte +3 -5
  44. package/dist/marks/helpers/GeoCanvas.svelte +2 -4
  45. package/dist/marks/helpers/LineCanvas.svelte +2 -4
  46. package/dist/marks/helpers/LinearGradientX.svelte +3 -4
  47. package/dist/marks/helpers/LinearGradientY.svelte +3 -4
  48. package/dist/marks/helpers/MarkerPath.svelte +4 -5
  49. package/dist/marks/helpers/MultilineText.svelte +4 -4
  50. package/dist/marks/helpers/RectPath.svelte +5 -6
  51. package/dist/marks/helpers/Regression.svelte +4 -8
  52. package/dist/marks/helpers/TrailCanvas.svelte +2 -4
  53. package/dist/marks/helpers/events.d.ts +2 -2
  54. package/dist/marks/helpers/events.js +4 -4
  55. package/dist/types/plot.d.ts +1 -0
  56. package/dist/ui/Spiral.svelte +4 -0
  57. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
- import { getContext } from 'svelte';
3
- import type { PlotContext, RawValue } from '../../index.js';
2
+ import type { RawValue } from '../../index.js';
3
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
4
4
 
5
5
  let {
6
6
  id,
@@ -10,8 +10,7 @@
10
10
  stops: { y: RawValue; color: string }[];
11
11
  } = $props();
12
12
 
13
- const { getPlotState } = getContext<PlotContext>('svelteplot');
14
- const plot = $derived(getPlotState());
13
+ const plot = usePlot();
15
14
 
16
15
  const projectedStops = $derived(
17
16
  stops
@@ -11,11 +11,10 @@
11
11
  ConstantAccessor,
12
12
  DataRecord,
13
13
  Mark,
14
- PlotContext,
15
14
  PlotScales
16
15
  } from '../../types/index.js';
17
16
  import { addEventHandlers } from './events.js';
18
- import { getContext } from 'svelte';
17
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
19
18
 
20
19
  type MarkerPathProps = BaseMarkProps<Datum> & {
21
20
  /**
@@ -78,7 +77,7 @@
78
77
 
79
78
  const id = randomId();
80
79
 
81
- const { getPlotState } = getContext<PlotContext>('svelteplot');
80
+ const plot = usePlot();
82
81
 
83
82
  const points = $derived(text && d != null ? d.split(/[LMC]/).slice(1) : []);
84
83
  const hasPath = $derived(points.length > 0);
@@ -96,7 +95,7 @@
96
95
  class={className}
97
96
  stroke-width={strokeWidth_}
98
97
  {@attach addEventHandlers({
99
- getPlotState,
98
+ plot,
100
99
  options: mark.options,
101
100
  datum: datum
102
101
  })}>
@@ -127,7 +126,7 @@
127
126
  {d}
128
127
  {style}
129
128
  {@attach addEventHandlers({
130
- getPlotState,
129
+ plot,
131
130
  options: mark.options,
132
131
  datum: datum
133
132
  })} />
@@ -1,10 +1,11 @@
1
1
  <script lang="ts">
2
2
  import { resolveProp, resolveStyles } from '../../helpers/resolve';
3
- import { getContext, type ComponentProps } from 'svelte';
4
- import type { PlotContext, ScaledDataRecord, UsedScales } from '../../index.js';
3
+ import { type ComponentProps } from 'svelte';
4
+ import type { ScaledDataRecord, UsedScales } from '../../index.js';
5
5
  import type Text from '../Text.svelte';
6
6
  import { CSS_VAR } from '../../constants';
7
7
  import { maybeFromPixel, maybeFromRem } from '../../helpers/getBaseStyles';
8
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
8
9
 
9
10
  const LINE_ANCHOR = {
10
11
  bottom: 'auto',
@@ -12,8 +13,7 @@
12
13
  top: 'hanging'
13
14
  } as const;
14
15
 
15
- const { getPlotState } = getContext<PlotContext>('svelteplot');
16
- const plot = $derived(getPlotState());
16
+ const plot = usePlot();
17
17
 
18
18
  let {
19
19
  textLines,
@@ -29,11 +29,11 @@ Helper component for rendering rectangular marks in SVG
29
29
  import { resolveProp, resolveStyles } from '../../helpers/resolve';
30
30
  import { roundedRect } from '../../helpers/roundedRect';
31
31
  import { addEventHandlers } from './events.js';
32
- import { getContext } from 'svelte';
33
32
  import Anchor from './Anchor.svelte';
34
33
  import type { BaseMarkProps, BaseRectMarkProps, BorderRadius } from '../../types/mark.js';
35
34
  import type { DataRecord, ScaledDataRecord } from '../../types/data.js';
36
- import type { PlotContext, UsedScales } from '../../types/index.js';
35
+ import type { UsedScales } from '../../types/index.js';
36
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
37
37
 
38
38
  let {
39
39
  datum,
@@ -49,8 +49,7 @@ Helper component for rendering rectangular marks in SVG
49
49
  fallbackStyle
50
50
  }: RectPathProps = $props();
51
51
 
52
- const { getPlotState } = getContext<PlotContext>('svelteplot');
53
- const plot = $derived(getPlotState());
52
+ const plot = usePlot();
54
53
 
55
54
  const inset = $derived(+(resolveProp(options.inset, datum?.datum, 0) as number));
56
55
  const insetLeft = $derived(
@@ -119,7 +118,7 @@ Helper component for rendering rectangular marks in SVG
119
118
  class={[styleClass, className]}
120
119
  {style}
121
120
  {@attach addEventHandlers({
122
- getPlotState,
121
+ plot,
123
122
  options,
124
123
  datum: datum?.datum
125
124
  })} />
@@ -131,7 +130,7 @@ Helper component for rendering rectangular marks in SVG
131
130
  class={[styleClass, className]}
132
131
  {style}
133
132
  {@attach addEventHandlers({
134
- getPlotState,
133
+ plot,
135
134
  options,
136
135
  datum: datum?.datum
137
136
  })} />
@@ -1,10 +1,5 @@
1
1
  <script module lang="ts">
2
- import type {
3
- BaseMarkProps,
4
- ChannelAccessor,
5
- FacetContext,
6
- PlotContext
7
- } from '../../types/index.js';
2
+ import type { BaseMarkProps, ChannelAccessor, FacetContext } from '../../types/index.js';
8
3
 
9
4
  type RegressionType = 'linear' | 'quad' | 'poly' | 'exp' | 'log' | 'pow' | 'loess';
10
5
 
@@ -65,8 +60,9 @@
65
60
  throw new Error('unknown regression ' + name);
66
61
  }
67
62
 
68
- const { getPlotState } = getContext<PlotContext>('svelteplot');
69
- let plot = $derived(getPlotState());
63
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
64
+
65
+ const plot = usePlot();
70
66
 
71
67
  let {
72
68
  data,
@@ -2,7 +2,6 @@
2
2
  import type {
3
3
  Mark,
4
4
  BaseMarkProps,
5
- PlotContext,
6
5
  ScaledDataRecord,
7
6
  UsedScales,
8
7
  CurveName,
@@ -12,10 +11,10 @@
12
11
  import type { Attachment } from 'svelte/attachments';
13
12
  import { devicePixelRatio } from 'svelte/reactivity/window';
14
13
  import { resolveColor } from './canvas.js';
15
- import { getContext } from 'svelte';
16
14
  import type { CurveFactory } from 'd3-shape';
17
15
  import { trailPath, type TrailSample } from './trail';
18
16
  import { resolveProp, resolveScaledStyleProps } from '../../helpers/resolve';
17
+ import { usePlot } from '../../hooks/usePlot.svelte.js';
19
18
 
20
19
  interface TrailCanvasProps<Datum> {
21
20
  curve?: CurveName | CurveFactory;
@@ -46,8 +45,7 @@
46
45
  return value == null ? 1 : +value;
47
46
  }
48
47
 
49
- const { getPlotState } = getContext<PlotContext>('svelteplot');
50
- const plot = $derived(getPlotState());
48
+ const plot = usePlot();
51
49
 
52
50
  const render = ((canvas: HTMLCanvasElement) => {
53
51
  const context = canvas.getContext('2d');
@@ -13,8 +13,8 @@ declare global {
13
13
  * of the plot frame, regardless of which element triggered the event
14
14
  */
15
15
  export declare function clientToLayerCoordinates(event: MouseEvent, plotBody: HTMLElement | null | undefined): [number, number];
16
- export declare function addEventHandlers<T extends DataRow>({ options, datum, getPlotState }: {
16
+ export declare function addEventHandlers<T extends DataRow>({ options, datum, plot }: {
17
17
  options: BaseMarkProps<T>;
18
18
  datum: DataRecord;
19
- getPlotState: () => PlotState;
19
+ plot: PlotState;
20
20
  }): Attachment;
@@ -19,7 +19,7 @@ export function clientToLayerCoordinates(event, plotBody) {
19
19
  // Calculate the coordinates relative to the plot body
20
20
  return [event.clientX - plotBodyRect.left, event.clientY - plotBodyRect.top];
21
21
  }
22
- export function addEventHandlers({ options, datum, getPlotState }) {
22
+ export function addEventHandlers({ options, datum, plot }) {
23
23
  const events = pick(options, [
24
24
  'onclick',
25
25
  'oncontextmenu',
@@ -57,7 +57,7 @@ export function addEventHandlers({ options, datum, getPlotState }) {
57
57
  for (const [eventName, eventHandler] of Object.entries(events)) {
58
58
  if (eventHandler) {
59
59
  const wrappedHandler = (origEvent) => {
60
- const { scales, body, options } = getPlotState();
60
+ const { scales, body, options: plotOptions } = plot;
61
61
  if (origEvent instanceof MouseEvent || origEvent instanceof PointerEvent) {
62
62
  let facetEl = origEvent.target;
63
63
  while (facetEl &&
@@ -69,8 +69,8 @@ export function addEventHandlers({ options, datum, getPlotState }) {
69
69
  facetEl = facetEl.parentElement;
70
70
  }
71
71
  const facetRect = (facetEl?.firstElementChild ?? body).getBoundingClientRect();
72
- const relativeX = origEvent.clientX - facetRect.left + (options.marginLeft ?? 0);
73
- const relativeY = origEvent.clientY - facetRect.top + (options.marginTop ?? 0);
72
+ const relativeX = origEvent.clientX - facetRect.left + (plotOptions.marginLeft ?? 0);
73
+ const relativeY = origEvent.clientY - facetRect.top + (plotOptions.marginTop ?? 0);
74
74
  if (scales.projection) {
75
75
  const [x, y] = scales.projection.invert([relativeX, relativeY]);
76
76
  origEvent.dataX = x;
@@ -26,6 +26,7 @@ export type PlotState = {
26
26
  */
27
27
  hasFilledDotMarks: boolean;
28
28
  css: ((d: string) => string) | null;
29
+ publicState: Readonly<PlotState>;
29
30
  };
30
31
  export type PlotContext = {
31
32
  /**
@@ -1,6 +1,10 @@
1
1
  <script>
2
+ import { usePlot } from '../hooks/usePlot.svelte';
3
+
2
4
  let { numTurns = 4, finalRadius = 10, duration = 2, ...restProps } = $props();
3
5
 
6
+ const plot = usePlot();
7
+
4
8
  const pathD = $derived.by(() => {
5
9
  const numPoints = 100;
6
10
  const k = finalRadius / (2 * Math.PI * numTurns);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.8.1-pr-298.6",
3
+ "version": "0.8.1-pr-301.0",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",