@opendata-ai/openchart-react 8.0.0-rc.8 → 8.0.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.
- package/dist/index.d.ts +149 -43
- package/dist/index.js +294 -157
- package/dist/index.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/styles.css.map +1 -1
- package/package.json +5 -6
- package/src/BarList.tsx +0 -126
- package/src/Chart.tsx +0 -341
- package/src/ChartStory.tsx +0 -203
- package/src/DataTable.tsx +0 -165
- package/src/Graph.tsx +0 -253
- package/src/Map.tsx +0 -130
- package/src/Sankey.tsx +0 -176
- package/src/ThemeContext.tsx +0 -40
- package/src/TileMap.tsx +0 -165
- package/src/Visualization.tsx +0 -85
- package/src/__tests__/Chart.test.tsx +0 -134
- package/src/__tests__/ChartStory.test.tsx +0 -92
- package/src/__tests__/DataTable.test.tsx +0 -126
- package/src/__tests__/Graph.test.tsx +0 -130
- package/src/__tests__/ThemeContext.test.tsx +0 -238
- package/src/__tests__/hooks.test.tsx +0 -160
- package/src/__tests__/update-vs-remount.test.tsx +0 -133
- package/src/__tests__/useTableState.test.tsx +0 -218
- package/src/hooks/useGraph.ts +0 -87
- package/src/hooks/useTable.ts +0 -98
- package/src/hooks/useTableState.ts +0 -76
- package/src/hooks.ts +0 -154
- package/src/index.ts +0 -104
package/src/Chart.tsx
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React Chart component: thin wrapper around the vanilla adapter.
|
|
3
|
-
*
|
|
4
|
-
* Mounts a chart instance on render, updates when spec changes,
|
|
5
|
-
* and cleans up on unmount. All heavy lifting is done by the vanilla
|
|
6
|
-
* createChart() function.
|
|
7
|
-
*/
|
|
8
|
-
// biome-ignore-all lint/correctness/useHookAtTopLevel: Biome doesn't recognize overloaded function declarations as React components; hooks are correctly at the top level of ChartInner
|
|
9
|
-
|
|
10
|
-
import type {
|
|
11
|
-
ChartEventHandlers,
|
|
12
|
-
ChartSpec,
|
|
13
|
-
DarkMode,
|
|
14
|
-
DataRow,
|
|
15
|
-
ElementRef,
|
|
16
|
-
GraphSpec,
|
|
17
|
-
LayerSpec,
|
|
18
|
-
ThemeConfig,
|
|
19
|
-
} from '@opendata-ai/openchart-core';
|
|
20
|
-
import { type ChartInstance, createChart, type MountOptions } from '@opendata-ai/openchart-vanilla';
|
|
21
|
-
import {
|
|
22
|
-
type CSSProperties,
|
|
23
|
-
forwardRef,
|
|
24
|
-
useCallback,
|
|
25
|
-
useEffect,
|
|
26
|
-
useImperativeHandle,
|
|
27
|
-
useRef,
|
|
28
|
-
} from 'react';
|
|
29
|
-
import { useVizDarkMode, useVizTheme } from './ThemeContext';
|
|
30
|
-
|
|
31
|
-
export interface ChartHandle {
|
|
32
|
-
/** Get the currently selected element, or null if none. */
|
|
33
|
-
getSelectedElement(): ElementRef | null;
|
|
34
|
-
/** Programmatically select an element. */
|
|
35
|
-
select(ref: ElementRef): void;
|
|
36
|
-
/** Deselect the current element. */
|
|
37
|
-
deselect(): void;
|
|
38
|
-
/** The underlying chart instance (null until mounted). */
|
|
39
|
-
readonly instance: ChartInstance | null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface ChartProps<TData extends DataRow = DataRow> extends ChartEventHandlers {
|
|
43
|
-
/** The visualization spec to render. */
|
|
44
|
-
spec: ChartSpec<TData> | LayerSpec<TData> | GraphSpec;
|
|
45
|
-
/** Theme overrides. */
|
|
46
|
-
theme?: ThemeConfig;
|
|
47
|
-
/** Dark mode: "auto", "force", or "off". */
|
|
48
|
-
darkMode?: DarkMode;
|
|
49
|
-
/** Callback when a data point is clicked. @deprecated Use onMarkClick instead. */
|
|
50
|
-
onDataPointClick?: (data: Record<string, unknown>) => void;
|
|
51
|
-
/** The currently selected element (controlled). */
|
|
52
|
-
selectedElement?: ElementRef;
|
|
53
|
-
/** Highlight specific color-encoding values. Pass null to clear. */
|
|
54
|
-
highlight?: string[] | null;
|
|
55
|
-
/** CSS class name for the wrapper div. */
|
|
56
|
-
className?: string;
|
|
57
|
-
/** Inline styles for the wrapper div. */
|
|
58
|
-
style?: CSSProperties;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* React component that renders a visualization from a spec.
|
|
63
|
-
*
|
|
64
|
-
* Uses the vanilla adapter internally. The spec is compiled and rendered
|
|
65
|
-
* as SVG inside a wrapper div. Spec changes trigger re-renders via the
|
|
66
|
-
* vanilla adapter's update() method.
|
|
67
|
-
*
|
|
68
|
-
* Pass a typed data row as the generic to get field autocomplete:
|
|
69
|
-
* ```tsx
|
|
70
|
-
* type SalesRow = { date: string; revenue: number };
|
|
71
|
-
* <Chart<SalesRow> spec={{ mark: 'line', data: rows, encoding: { x: { field: 'date', ... } } }} />
|
|
72
|
-
* ```
|
|
73
|
-
*/
|
|
74
|
-
// forwardRef doesn't support generics natively in TypeScript, so we use the
|
|
75
|
-
// inner function + cast pattern to preserve the generic on the public API.
|
|
76
|
-
function ChartInner<TData extends DataRow = DataRow>(
|
|
77
|
-
props: ChartProps<TData> & React.RefAttributes<ChartHandle>,
|
|
78
|
-
ref: React.Ref<ChartHandle>,
|
|
79
|
-
): React.ReactElement | null;
|
|
80
|
-
function ChartInner(
|
|
81
|
-
props: ChartProps & { ref?: React.Ref<ChartHandle> },
|
|
82
|
-
ref: React.Ref<ChartHandle>,
|
|
83
|
-
): React.ReactElement | null;
|
|
84
|
-
function ChartInner(
|
|
85
|
-
{
|
|
86
|
-
spec,
|
|
87
|
-
theme: themeProp,
|
|
88
|
-
darkMode,
|
|
89
|
-
onDataPointClick,
|
|
90
|
-
onMarkClick,
|
|
91
|
-
onMarkHover,
|
|
92
|
-
onMarkLeave,
|
|
93
|
-
onLegendToggle,
|
|
94
|
-
onHighlightChange,
|
|
95
|
-
onAnnotationClick,
|
|
96
|
-
onAnnotationEdit,
|
|
97
|
-
onEdit,
|
|
98
|
-
onSelect,
|
|
99
|
-
onDeselect,
|
|
100
|
-
onTextEdit,
|
|
101
|
-
onReveal,
|
|
102
|
-
selectedElement: selectedElementProp,
|
|
103
|
-
highlight,
|
|
104
|
-
className,
|
|
105
|
-
style,
|
|
106
|
-
}: ChartProps,
|
|
107
|
-
ref: React.Ref<ChartHandle>,
|
|
108
|
-
) {
|
|
109
|
-
const contextTheme = useVizTheme();
|
|
110
|
-
const contextDarkMode = useVizDarkMode();
|
|
111
|
-
const theme = themeProp ?? contextTheme;
|
|
112
|
-
const resolvedDarkMode = darkMode ?? contextDarkMode;
|
|
113
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
114
|
-
const chartRef = useRef<ChartInstance | null>(null);
|
|
115
|
-
const specRef = useRef<string>('');
|
|
116
|
-
|
|
117
|
-
// Store event handlers in refs so they don't trigger chart recreation.
|
|
118
|
-
// Inline arrow functions create new references every render, which would
|
|
119
|
-
// destroy and recreate the entire chart instance without this pattern.
|
|
120
|
-
const handlersRef = useRef<{
|
|
121
|
-
onDataPointClick?: ChartProps['onDataPointClick'];
|
|
122
|
-
onMarkClick?: ChartProps['onMarkClick'];
|
|
123
|
-
onMarkHover?: ChartProps['onMarkHover'];
|
|
124
|
-
onMarkLeave?: ChartProps['onMarkLeave'];
|
|
125
|
-
onLegendToggle?: ChartProps['onLegendToggle'];
|
|
126
|
-
onHighlightChange?: ChartProps['onHighlightChange'];
|
|
127
|
-
onAnnotationClick?: ChartProps['onAnnotationClick'];
|
|
128
|
-
onAnnotationEdit?: ChartProps['onAnnotationEdit'];
|
|
129
|
-
onEdit?: ChartProps['onEdit'];
|
|
130
|
-
onSelect?: ChartProps['onSelect'];
|
|
131
|
-
onDeselect?: ChartProps['onDeselect'];
|
|
132
|
-
onTextEdit?: ChartProps['onTextEdit'];
|
|
133
|
-
onReveal?: ChartProps['onReveal'];
|
|
134
|
-
}>({});
|
|
135
|
-
handlersRef.current = {
|
|
136
|
-
onDataPointClick,
|
|
137
|
-
onMarkClick,
|
|
138
|
-
onMarkHover,
|
|
139
|
-
onMarkLeave,
|
|
140
|
-
onLegendToggle,
|
|
141
|
-
onHighlightChange,
|
|
142
|
-
onAnnotationClick,
|
|
143
|
-
onAnnotationEdit,
|
|
144
|
-
onEdit,
|
|
145
|
-
onSelect,
|
|
146
|
-
onDeselect,
|
|
147
|
-
onTextEdit,
|
|
148
|
-
onReveal,
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
// Stable callback wrappers that read from refs
|
|
152
|
-
const stableOnDataPointClick = useCallback(
|
|
153
|
-
(data: Record<string, unknown>) => handlersRef.current.onDataPointClick?.(data),
|
|
154
|
-
[],
|
|
155
|
-
);
|
|
156
|
-
const stableOnMarkClick = useCallback(
|
|
157
|
-
(event: import('@opendata-ai/openchart-core').MarkEvent) =>
|
|
158
|
-
handlersRef.current.onMarkClick?.(event),
|
|
159
|
-
[],
|
|
160
|
-
);
|
|
161
|
-
const stableOnMarkHover = useCallback(
|
|
162
|
-
(event: import('@opendata-ai/openchart-core').MarkEvent) =>
|
|
163
|
-
handlersRef.current.onMarkHover?.(event),
|
|
164
|
-
[],
|
|
165
|
-
);
|
|
166
|
-
const stableOnMarkLeave = useCallback(() => handlersRef.current.onMarkLeave?.(), []);
|
|
167
|
-
const stableOnLegendToggle = useCallback(
|
|
168
|
-
(series: string, visible: boolean) => handlersRef.current.onLegendToggle?.(series, visible),
|
|
169
|
-
[],
|
|
170
|
-
);
|
|
171
|
-
const stableOnHighlightChange = useCallback(
|
|
172
|
-
(values: string[]) => handlersRef.current.onHighlightChange?.(values),
|
|
173
|
-
[],
|
|
174
|
-
);
|
|
175
|
-
const stableOnAnnotationClick = useCallback(
|
|
176
|
-
(annotation: import('@opendata-ai/openchart-core').Annotation, event: MouseEvent) =>
|
|
177
|
-
handlersRef.current.onAnnotationClick?.(annotation, event),
|
|
178
|
-
[],
|
|
179
|
-
);
|
|
180
|
-
const stableOnAnnotationEdit = useCallback(
|
|
181
|
-
(
|
|
182
|
-
annotation: import('@opendata-ai/openchart-core').TextAnnotation,
|
|
183
|
-
updatedOffset: import('@opendata-ai/openchart-core').AnnotationOffset,
|
|
184
|
-
) => handlersRef.current.onAnnotationEdit?.(annotation, updatedOffset),
|
|
185
|
-
[],
|
|
186
|
-
);
|
|
187
|
-
const stableOnEdit = useCallback(
|
|
188
|
-
(edit: import('@opendata-ai/openchart-core').ElementEdit) => handlersRef.current.onEdit?.(edit),
|
|
189
|
-
[],
|
|
190
|
-
);
|
|
191
|
-
const stableOnSelect = useCallback(
|
|
192
|
-
(element: ElementRef) => handlersRef.current.onSelect?.(element),
|
|
193
|
-
[],
|
|
194
|
-
);
|
|
195
|
-
const stableOnDeselect = useCallback(
|
|
196
|
-
(element: ElementRef) => handlersRef.current.onDeselect?.(element),
|
|
197
|
-
[],
|
|
198
|
-
);
|
|
199
|
-
const stableOnTextEdit = useCallback(
|
|
200
|
-
(element: ElementRef, oldText: string, newText: string) =>
|
|
201
|
-
handlersRef.current.onTextEdit?.(element, oldText, newText),
|
|
202
|
-
[],
|
|
203
|
-
);
|
|
204
|
-
const stableOnReveal = useCallback(
|
|
205
|
-
(guess: Array<{ x: string | number; y: number }>) => handlersRef.current.onReveal?.(guess),
|
|
206
|
-
[],
|
|
207
|
-
);
|
|
208
|
-
|
|
209
|
-
// Expose imperative handle for ref-based control
|
|
210
|
-
useImperativeHandle(
|
|
211
|
-
ref,
|
|
212
|
-
() => ({
|
|
213
|
-
getSelectedElement() {
|
|
214
|
-
return chartRef.current?.getSelectedElement() ?? null;
|
|
215
|
-
},
|
|
216
|
-
select(elementRef: ElementRef) {
|
|
217
|
-
chartRef.current?.select(elementRef);
|
|
218
|
-
},
|
|
219
|
-
deselect() {
|
|
220
|
-
chartRef.current?.deselect();
|
|
221
|
-
},
|
|
222
|
-
get instance() {
|
|
223
|
-
return chartRef.current;
|
|
224
|
-
},
|
|
225
|
-
}),
|
|
226
|
-
[],
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
// Mount chart and recreate when theme/darkMode change.
|
|
230
|
-
// Event handlers use stable refs so they don't trigger recreation.
|
|
231
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: spec intentionally excluded - spec changes handled via update() in Effect 2
|
|
232
|
-
useEffect(() => {
|
|
233
|
-
const container = containerRef.current;
|
|
234
|
-
if (!container) return;
|
|
235
|
-
|
|
236
|
-
const options: MountOptions = {
|
|
237
|
-
theme,
|
|
238
|
-
darkMode: resolvedDarkMode,
|
|
239
|
-
onDataPointClick: stableOnDataPointClick,
|
|
240
|
-
onMarkClick: stableOnMarkClick,
|
|
241
|
-
onMarkHover: stableOnMarkHover,
|
|
242
|
-
onMarkLeave: stableOnMarkLeave,
|
|
243
|
-
onLegendToggle: stableOnLegendToggle,
|
|
244
|
-
onHighlightChange: stableOnHighlightChange,
|
|
245
|
-
onAnnotationClick: stableOnAnnotationClick,
|
|
246
|
-
// Only include editing callbacks when the consumer provides them.
|
|
247
|
-
// The stable wrappers are always truthy, so gating on handlersRef
|
|
248
|
-
// avoids adding unstable prop references to the effect deps.
|
|
249
|
-
...(handlersRef.current.onAnnotationEdit ? { onAnnotationEdit: stableOnAnnotationEdit } : {}),
|
|
250
|
-
...(handlersRef.current.onEdit ? { onEdit: stableOnEdit } : {}),
|
|
251
|
-
...(handlersRef.current.onSelect ? { onSelect: stableOnSelect } : {}),
|
|
252
|
-
...(handlersRef.current.onDeselect ? { onDeselect: stableOnDeselect } : {}),
|
|
253
|
-
...(handlersRef.current.onTextEdit ? { onTextEdit: stableOnTextEdit } : {}),
|
|
254
|
-
...(handlersRef.current.onReveal ? { onReveal: stableOnReveal } : {}),
|
|
255
|
-
...(selectedElementProp ? { selectedElement: selectedElementProp } : {}),
|
|
256
|
-
responsive: true,
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
chartRef.current = createChart(container, spec, options);
|
|
260
|
-
specRef.current = JSON.stringify(spec);
|
|
261
|
-
|
|
262
|
-
return () => {
|
|
263
|
-
chartRef.current?.destroy();
|
|
264
|
-
chartRef.current = null;
|
|
265
|
-
};
|
|
266
|
-
// Only recreate when theme or darkMode change. Event handlers use stable refs.
|
|
267
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
268
|
-
}, [
|
|
269
|
-
theme,
|
|
270
|
-
resolvedDarkMode,
|
|
271
|
-
stableOnAnnotationClick,
|
|
272
|
-
stableOnDataPointClick,
|
|
273
|
-
stableOnEdit,
|
|
274
|
-
stableOnLegendToggle,
|
|
275
|
-
stableOnHighlightChange,
|
|
276
|
-
stableOnMarkClick,
|
|
277
|
-
stableOnMarkHover,
|
|
278
|
-
stableOnMarkLeave,
|
|
279
|
-
stableOnAnnotationEdit,
|
|
280
|
-
stableOnSelect,
|
|
281
|
-
stableOnDeselect,
|
|
282
|
-
stableOnTextEdit,
|
|
283
|
-
stableOnReveal,
|
|
284
|
-
]);
|
|
285
|
-
|
|
286
|
-
// Update chart when spec changes
|
|
287
|
-
useEffect(() => {
|
|
288
|
-
const chart = chartRef.current;
|
|
289
|
-
if (!chart) return;
|
|
290
|
-
|
|
291
|
-
const specString = JSON.stringify(spec);
|
|
292
|
-
if (specString !== specRef.current) {
|
|
293
|
-
specRef.current = specString;
|
|
294
|
-
chart.update(spec);
|
|
295
|
-
}
|
|
296
|
-
}, [spec]);
|
|
297
|
-
|
|
298
|
-
// Handle selectedElement prop changes separately (like Vue/Svelte adapters)
|
|
299
|
-
useEffect(() => {
|
|
300
|
-
const chart = chartRef.current;
|
|
301
|
-
if (!chart || !chart.select) return;
|
|
302
|
-
|
|
303
|
-
if (selectedElementProp) {
|
|
304
|
-
chart.select(selectedElementProp);
|
|
305
|
-
} else if (chart.getSelectedElement?.()) {
|
|
306
|
-
chart.deselect();
|
|
307
|
-
}
|
|
308
|
-
}, [selectedElementProp]);
|
|
309
|
-
|
|
310
|
-
// Handle highlight prop changes
|
|
311
|
-
useEffect(() => {
|
|
312
|
-
if (chartRef.current && highlight !== undefined) {
|
|
313
|
-
chartRef.current.setHighlight(highlight);
|
|
314
|
-
}
|
|
315
|
-
}, [highlight]);
|
|
316
|
-
|
|
317
|
-
return (
|
|
318
|
-
<div
|
|
319
|
-
ref={containerRef}
|
|
320
|
-
className={className ? `oc-chart-root ${className}` : 'oc-chart-root'}
|
|
321
|
-
style={style}
|
|
322
|
-
/>
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Generic Chart component with field autocomplete support.
|
|
328
|
-
*
|
|
329
|
-
* Pass your data row type to get compile-time field validation:
|
|
330
|
-
* ```tsx
|
|
331
|
-
* type SalesRow = { date: string; revenue: number; region: string };
|
|
332
|
-
* <Chart<SalesRow> spec={{ mark: 'line', data: rows, encoding: { x: { field: 'date', type: 'temporal' } } }} />
|
|
333
|
-
* ```
|
|
334
|
-
*
|
|
335
|
-
* Without a type parameter, behaves identically to before — no migration needed.
|
|
336
|
-
*/
|
|
337
|
-
export const Chart = forwardRef(ChartInner) as <TData extends DataRow = DataRow>(
|
|
338
|
-
props: ChartProps<TData> & { ref?: React.Ref<ChartHandle> },
|
|
339
|
-
) => React.ReactElement | null;
|
|
340
|
-
// Restore display name lost by the forwardRef + cast pattern
|
|
341
|
-
(Chart as React.FC).displayName = 'Chart';
|
package/src/ChartStory.tsx
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* React scrollytelling shell around the vanilla `createChartStory`.
|
|
3
|
-
*
|
|
4
|
-
* The chart panel is sticky-pinned; the narrative column scrolls past it and
|
|
5
|
-
* each step block advances the story. The heavy lifting (cumulative patch
|
|
6
|
-
* application, mark morphing, crossfade fallback, camera) lives in the vanilla
|
|
7
|
-
* story driver: this component owns only React lifecycle and the sticky layout.
|
|
8
|
-
*
|
|
9
|
-
* Self-driving by default (the vanilla `ScrollDriver` reads step positions),
|
|
10
|
-
* or controlled by passing `step={n}` to drive it externally.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import type {
|
|
14
|
-
ChartSpec,
|
|
15
|
-
DataRow,
|
|
16
|
-
GraphSpec,
|
|
17
|
-
LayerSpec,
|
|
18
|
-
MapSpec,
|
|
19
|
-
} from '@opendata-ai/openchart-core';
|
|
20
|
-
import type { MountOptions } from '@opendata-ai/openchart-vanilla';
|
|
21
|
-
import {
|
|
22
|
-
type ChartStoryInstance,
|
|
23
|
-
type ChartStoryOptions,
|
|
24
|
-
createChartStory,
|
|
25
|
-
} from '@opendata-ai/openchart-vanilla/story';
|
|
26
|
-
import type { Ref } from 'react';
|
|
27
|
-
import { type CSSProperties, type ReactNode, useEffect, useImperativeHandle, useRef } from 'react';
|
|
28
|
-
|
|
29
|
-
export interface ChartStoryHandle {
|
|
30
|
-
/** Jump the story to a step index (applies patches 0..n, animates the diff). */
|
|
31
|
-
goTo(index: number): void;
|
|
32
|
-
/** The underlying vanilla story instance, or null before mount. */
|
|
33
|
-
readonly instance: ChartStoryInstance | null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface ChartStoryProps<TData extends DataRow = DataRow> {
|
|
37
|
-
/** Base spec the story drives. */
|
|
38
|
-
spec: ChartSpec<TData> | LayerSpec<TData> | GraphSpec | MapSpec;
|
|
39
|
-
/** Ordered step patches. `steps[i]` is applied cumulatively onto the base. */
|
|
40
|
-
steps: ChartStoryOptions<TData>['steps'];
|
|
41
|
-
/**
|
|
42
|
-
* Narrative content per step: `narrative[i]` renders in the scrolling column
|
|
43
|
-
* beside the sticky chart and triggers step `i` when it crosses the trigger
|
|
44
|
-
* line. Length should match `steps`.
|
|
45
|
-
*/
|
|
46
|
-
narrative: ReactNode[];
|
|
47
|
-
/**
|
|
48
|
-
* Controlled step index. When provided, the built-in scroll driver is left
|
|
49
|
-
* in place but `goTo` is called on change so an external scroller can drive.
|
|
50
|
-
* Omit for self-driving scroll.
|
|
51
|
-
*/
|
|
52
|
-
step?: number;
|
|
53
|
-
/** Fraction of viewport height where the scroll trigger sits. Default 0.4. */
|
|
54
|
-
triggerPosition?: number;
|
|
55
|
-
/** Camera driving mode. Default 'step'. */
|
|
56
|
-
cameraMode?: 'step' | 'scrub';
|
|
57
|
-
/** Options forwarded to the underlying chart mount (theme, dark mode, etc.). */
|
|
58
|
-
mountOptions?: MountOptions;
|
|
59
|
-
/** Class on the outer section. */
|
|
60
|
-
className?: string;
|
|
61
|
-
/** Inline styles merged onto the outer section. */
|
|
62
|
-
style?: CSSProperties;
|
|
63
|
-
ref?: Ref<ChartStoryHandle>;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const sectionStyle: CSSProperties = {
|
|
67
|
-
position: 'relative',
|
|
68
|
-
display: 'grid',
|
|
69
|
-
gridTemplateColumns: 'minmax(0, 26rem) minmax(0, 1fr)',
|
|
70
|
-
gap: '3rem',
|
|
71
|
-
alignItems: 'start',
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const narrativeColumnStyle: CSSProperties = {
|
|
75
|
-
position: 'relative',
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const stepStyle: CSSProperties = {
|
|
79
|
-
minHeight: '80vh',
|
|
80
|
-
display: 'flex',
|
|
81
|
-
alignItems: 'center',
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const panelStyle: CSSProperties = {
|
|
85
|
-
position: 'sticky',
|
|
86
|
-
top: '2rem',
|
|
87
|
-
height: 'calc(100vh - 4rem)',
|
|
88
|
-
display: 'flex',
|
|
89
|
-
alignItems: 'center',
|
|
90
|
-
justifyContent: 'center',
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const chartMountStyle: CSSProperties = {
|
|
94
|
-
width: '100%',
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export function ChartStory<TData extends DataRow = DataRow>({
|
|
98
|
-
spec,
|
|
99
|
-
steps,
|
|
100
|
-
narrative,
|
|
101
|
-
step,
|
|
102
|
-
triggerPosition,
|
|
103
|
-
cameraMode,
|
|
104
|
-
mountOptions,
|
|
105
|
-
className,
|
|
106
|
-
style,
|
|
107
|
-
ref,
|
|
108
|
-
}: ChartStoryProps<TData>) {
|
|
109
|
-
const sectionRef = useRef<HTMLElement | null>(null);
|
|
110
|
-
const chartMountRef = useRef<HTMLDivElement | null>(null);
|
|
111
|
-
const stepRefs = useRef<Array<HTMLDivElement | null>>([]);
|
|
112
|
-
const storyRef = useRef<ChartStoryInstance | null>(null);
|
|
113
|
-
|
|
114
|
-
if (narrative.length !== steps.length) {
|
|
115
|
-
console.warn(
|
|
116
|
-
`[openchart] ChartStory: narrative length (${narrative.length}) does not match steps length (${steps.length}). Each step should have a corresponding narrative element.`,
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Mount once; the vanilla driver owns updates so React never re-mounts on
|
|
121
|
-
// spec/step churn (matching the <Chart /> imperative-update pattern).
|
|
122
|
-
const optionsRef = useRef<{
|
|
123
|
-
spec: typeof spec;
|
|
124
|
-
steps: typeof steps;
|
|
125
|
-
triggerPosition?: number;
|
|
126
|
-
cameraMode?: 'step' | 'scrub';
|
|
127
|
-
mountOptions?: MountOptions;
|
|
128
|
-
}>({ spec, steps, triggerPosition, cameraMode, mountOptions });
|
|
129
|
-
optionsRef.current = { spec, steps, triggerPosition, cameraMode, mountOptions };
|
|
130
|
-
|
|
131
|
-
useEffect(() => {
|
|
132
|
-
const mount = chartMountRef.current;
|
|
133
|
-
const section = sectionRef.current;
|
|
134
|
-
if (!mount) return;
|
|
135
|
-
|
|
136
|
-
const opts = optionsRef.current;
|
|
137
|
-
const story = createChartStory(
|
|
138
|
-
mount,
|
|
139
|
-
{
|
|
140
|
-
spec: opts.spec,
|
|
141
|
-
steps: opts.steps,
|
|
142
|
-
triggerPosition: opts.triggerPosition,
|
|
143
|
-
cameraMode: opts.cameraMode,
|
|
144
|
-
},
|
|
145
|
-
opts.mountOptions,
|
|
146
|
-
);
|
|
147
|
-
storyRef.current = story;
|
|
148
|
-
|
|
149
|
-
if (section) story.setContainer(section);
|
|
150
|
-
stepRefs.current.forEach((el, i) => {
|
|
151
|
-
if (el) story.registerStep(i, el);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return () => {
|
|
155
|
-
story.destroy();
|
|
156
|
-
storyRef.current = null;
|
|
157
|
-
};
|
|
158
|
-
// Mount-once: options are read from the ref so changing them doesn't
|
|
159
|
-
// remount. Spec/step diffing at runtime is out of scope for v1.
|
|
160
|
-
}, []);
|
|
161
|
-
|
|
162
|
-
// Controlled mode: drive the story when `step` changes.
|
|
163
|
-
useEffect(() => {
|
|
164
|
-
if (step === undefined) return;
|
|
165
|
-
storyRef.current?.goTo(step);
|
|
166
|
-
}, [step]);
|
|
167
|
-
|
|
168
|
-
useImperativeHandle(
|
|
169
|
-
ref,
|
|
170
|
-
() => ({
|
|
171
|
-
goTo(index: number) {
|
|
172
|
-
storyRef.current?.goTo(index);
|
|
173
|
-
},
|
|
174
|
-
get instance() {
|
|
175
|
-
return storyRef.current;
|
|
176
|
-
},
|
|
177
|
-
}),
|
|
178
|
-
[],
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
return (
|
|
182
|
-
<section ref={sectionRef} className={className} style={{ ...sectionStyle, ...style }}>
|
|
183
|
-
<div style={narrativeColumnStyle}>
|
|
184
|
-
{narrative.map((content, i) => (
|
|
185
|
-
<div
|
|
186
|
-
// biome-ignore lint/suspicious/noArrayIndexKey: steps are a fixed ordered list; index is the stable identity
|
|
187
|
-
key={i}
|
|
188
|
-
ref={(el) => {
|
|
189
|
-
stepRefs.current[i] = el;
|
|
190
|
-
}}
|
|
191
|
-
data-oc-story-step={i}
|
|
192
|
-
style={stepStyle}
|
|
193
|
-
>
|
|
194
|
-
<div style={{ maxWidth: '36rem' }}>{content}</div>
|
|
195
|
-
</div>
|
|
196
|
-
))}
|
|
197
|
-
</div>
|
|
198
|
-
<div style={panelStyle}>
|
|
199
|
-
<div ref={chartMountRef} style={chartMountStyle} />
|
|
200
|
-
</div>
|
|
201
|
-
</section>
|
|
202
|
-
);
|
|
203
|
-
}
|
package/src/DataTable.tsx
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* DataTable component: React wrapper around the vanilla table adapter.
|
|
3
|
-
*
|
|
4
|
-
* Mounts a table instance on render, updates when spec changes,
|
|
5
|
-
* and cleans up on unmount. Supports both controlled and uncontrolled modes
|
|
6
|
-
* for sort, search, and pagination state.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { DarkMode, SortState, TableSpec, ThemeConfig } from '@opendata-ai/openchart-core';
|
|
10
|
-
import {
|
|
11
|
-
createTable,
|
|
12
|
-
type TableInstance,
|
|
13
|
-
type TableMountOptions,
|
|
14
|
-
} from '@opendata-ai/openchart-vanilla';
|
|
15
|
-
import { type CSSProperties, useCallback, useEffect, useRef } from 'react';
|
|
16
|
-
import { useVizDarkMode, useVizTheme } from './ThemeContext';
|
|
17
|
-
|
|
18
|
-
export interface DataTableProps {
|
|
19
|
-
/** The table spec to render. */
|
|
20
|
-
spec: TableSpec;
|
|
21
|
-
/** Theme overrides. */
|
|
22
|
-
theme?: ThemeConfig;
|
|
23
|
-
/** Dark mode: "auto", "force", or "off". */
|
|
24
|
-
darkMode?: DarkMode;
|
|
25
|
-
/** Row click handler. */
|
|
26
|
-
onRowClick?: (row: Record<string, unknown>) => void;
|
|
27
|
-
/** Callback when sort changes. */
|
|
28
|
-
onSortChange?: (sort: SortState | null) => void;
|
|
29
|
-
/** Callback when search changes. */
|
|
30
|
-
onSearchChange?: (query: string) => void;
|
|
31
|
-
/** Callback when page changes. */
|
|
32
|
-
onPageChange?: (page: number) => void;
|
|
33
|
-
/** CSS class name for the wrapper div. */
|
|
34
|
-
className?: string;
|
|
35
|
-
/** Inline styles for the wrapper div. */
|
|
36
|
-
style?: CSSProperties;
|
|
37
|
-
/** Controlled sort state. */
|
|
38
|
-
sort?: SortState | null;
|
|
39
|
-
/** Controlled search query. */
|
|
40
|
-
search?: string;
|
|
41
|
-
/** Controlled page number. */
|
|
42
|
-
page?: number;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* React component that renders a data table from a TableSpec.
|
|
47
|
-
*
|
|
48
|
-
* Uses the vanilla adapter internally. Supports controlled and uncontrolled
|
|
49
|
-
* modes for sort, search, and pagination state.
|
|
50
|
-
*/
|
|
51
|
-
export function DataTable({
|
|
52
|
-
spec,
|
|
53
|
-
theme: themeProp,
|
|
54
|
-
darkMode,
|
|
55
|
-
onRowClick,
|
|
56
|
-
onSortChange,
|
|
57
|
-
onSearchChange,
|
|
58
|
-
onPageChange,
|
|
59
|
-
className,
|
|
60
|
-
style,
|
|
61
|
-
sort,
|
|
62
|
-
search,
|
|
63
|
-
page,
|
|
64
|
-
}: DataTableProps) {
|
|
65
|
-
const contextTheme = useVizTheme();
|
|
66
|
-
const contextDarkMode = useVizDarkMode();
|
|
67
|
-
const theme = themeProp ?? contextTheme;
|
|
68
|
-
const resolvedDarkMode = darkMode ?? contextDarkMode;
|
|
69
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
70
|
-
const tableRef = useRef<TableInstance | null>(null);
|
|
71
|
-
|
|
72
|
-
// Store event handlers in refs so they don't trigger table recreation.
|
|
73
|
-
const handlersRef = useRef<{
|
|
74
|
-
onRowClick?: DataTableProps['onRowClick'];
|
|
75
|
-
onSortChange?: DataTableProps['onSortChange'];
|
|
76
|
-
onSearchChange?: DataTableProps['onSearchChange'];
|
|
77
|
-
onPageChange?: DataTableProps['onPageChange'];
|
|
78
|
-
}>({});
|
|
79
|
-
handlersRef.current = { onRowClick, onSortChange, onSearchChange, onPageChange };
|
|
80
|
-
|
|
81
|
-
// Stable callback wrappers that read from refs
|
|
82
|
-
const stableOnRowClick = useCallback(
|
|
83
|
-
(row: Record<string, unknown>) => handlersRef.current.onRowClick?.(row),
|
|
84
|
-
[],
|
|
85
|
-
);
|
|
86
|
-
const stableOnStateChange = useCallback(
|
|
87
|
-
(state: { sort?: SortState | null; search?: string; page?: number }) => {
|
|
88
|
-
if (state.sort !== undefined) handlersRef.current.onSortChange?.(state.sort);
|
|
89
|
-
if (state.search !== undefined) handlersRef.current.onSearchChange?.(state.search);
|
|
90
|
-
if (state.page !== undefined) handlersRef.current.onPageChange?.(state.page);
|
|
91
|
-
},
|
|
92
|
-
[],
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
const prevSpecRef = useRef<string>('');
|
|
96
|
-
|
|
97
|
-
// Determine if we're in controlled mode
|
|
98
|
-
const isControlled = sort !== undefined || search !== undefined || page !== undefined;
|
|
99
|
-
|
|
100
|
-
// Effect 1: Mount/unmount. Only recreate when structural options change.
|
|
101
|
-
// biome-ignore lint/correctness/useExhaustiveDependencies: spec, sort, search, page intentionally excluded - handled via update()/setState() in Effects 2-3
|
|
102
|
-
useEffect(() => {
|
|
103
|
-
const container = containerRef.current;
|
|
104
|
-
if (!container) return;
|
|
105
|
-
|
|
106
|
-
const mountOptions: TableMountOptions = {
|
|
107
|
-
theme,
|
|
108
|
-
darkMode: resolvedDarkMode,
|
|
109
|
-
onRowClick: stableOnRowClick,
|
|
110
|
-
responsive: true,
|
|
111
|
-
onStateChange: stableOnStateChange,
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
if (isControlled) {
|
|
115
|
-
mountOptions.externalState = {
|
|
116
|
-
sort: sort ?? null,
|
|
117
|
-
search: search ?? '',
|
|
118
|
-
page: page ?? 0,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
tableRef.current = createTable(container, spec, mountOptions);
|
|
123
|
-
prevSpecRef.current = JSON.stringify(spec);
|
|
124
|
-
|
|
125
|
-
return () => {
|
|
126
|
-
tableRef.current?.destroy();
|
|
127
|
-
tableRef.current = null;
|
|
128
|
-
};
|
|
129
|
-
// Only recreate on structural option changes (theme, darkMode, onRowClick).
|
|
130
|
-
// Controlled state updates are handled in Effect 2.
|
|
131
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
132
|
-
}, [theme, resolvedDarkMode, isControlled, stableOnRowClick, stableOnStateChange]);
|
|
133
|
-
|
|
134
|
-
// Effect 2: Sync controlled state without remounting.
|
|
135
|
-
useEffect(() => {
|
|
136
|
-
const table = tableRef.current;
|
|
137
|
-
if (!table || !isControlled) return;
|
|
138
|
-
|
|
139
|
-
table.setState({
|
|
140
|
-
sort: sort ?? null,
|
|
141
|
-
search: search ?? '',
|
|
142
|
-
page: page ?? 0,
|
|
143
|
-
});
|
|
144
|
-
}, [sort, search, page, isControlled]);
|
|
145
|
-
|
|
146
|
-
// Effect 3: Sync spec changes via update().
|
|
147
|
-
useEffect(() => {
|
|
148
|
-
const table = tableRef.current;
|
|
149
|
-
if (!table) return;
|
|
150
|
-
|
|
151
|
-
const specString = JSON.stringify(spec);
|
|
152
|
-
if (specString !== prevSpecRef.current) {
|
|
153
|
-
prevSpecRef.current = specString;
|
|
154
|
-
table.update(spec);
|
|
155
|
-
}
|
|
156
|
-
}, [spec]);
|
|
157
|
-
|
|
158
|
-
return (
|
|
159
|
-
<div
|
|
160
|
-
ref={containerRef}
|
|
161
|
-
className={className ? `oc-table-root ${className}` : 'oc-table-root'}
|
|
162
|
-
style={style}
|
|
163
|
-
/>
|
|
164
|
-
);
|
|
165
|
-
}
|