@opendata-ai/openchart-react 8.0.0-rc.2 → 8.0.0-rc.21

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/src/Graph.tsx DELETED
@@ -1,253 +0,0 @@
1
- /**
2
- * React Graph component: thin wrapper around the vanilla adapter.
3
- *
4
- * Mounts a graph instance on render, updates when spec changes,
5
- * and cleans up on unmount. All heavy lifting is done by the vanilla
6
- * createGraph() function.
7
- *
8
- * Supports forwardRef for imperative control via useGraph() hook.
9
- */
10
-
11
- import type { DarkMode, GraphSpec, ThemeConfig } from '@opendata-ai/openchart-core';
12
- import {
13
- createGraph,
14
- type GraphInstance,
15
- type GraphMountOptions,
16
- } from '@opendata-ai/openchart-vanilla';
17
- import {
18
- type CSSProperties,
19
- forwardRef,
20
- useCallback,
21
- useEffect,
22
- useImperativeHandle,
23
- useRef,
24
- } from 'react';
25
- import type { GraphHandle } from './hooks/useGraph';
26
- import { useVizDarkMode, useVizTheme } from './ThemeContext';
27
-
28
- export interface GraphProps {
29
- /** The graph spec to render. */
30
- spec: GraphSpec;
31
- /** Theme overrides. */
32
- theme?: ThemeConfig;
33
- /** Dark mode: "auto", "force", or "off". */
34
- darkMode?: DarkMode;
35
- /** Callback when a node is clicked. */
36
- onNodeClick?: (node: Record<string, unknown>) => void;
37
- /** Callback when a node is double-clicked. */
38
- onNodeDoubleClick?: (node: Record<string, unknown>) => void;
39
- /** Callback when a node is hovered (null when hover ends). */
40
- onNodeHover?: (node: Record<string, unknown> | null) => void;
41
- /** Callback when an edge is hovered (null when hover ends). */
42
- onEdgeHover?: (edge: Record<string, unknown> | null) => void;
43
- /** Callback when selection changes. */
44
- onSelectionChange?: (nodeIds: string[]) => void;
45
- /** Show built-in tooltip on node/edge hover. Defaults to true. */
46
- tooltip?: boolean;
47
- /** Show built-in legend. Defaults to true. */
48
- legend?: boolean;
49
- /** CSS class name for the wrapper div. */
50
- className?: string;
51
- /** Inline styles for the wrapper div. */
52
- style?: CSSProperties;
53
- }
54
-
55
- /**
56
- * React component that renders a force-directed graph from a GraphSpec.
57
- *
58
- * Uses the vanilla adapter internally. The spec is compiled and rendered
59
- * on a canvas inside a wrapper div. Spec changes trigger re-renders via the
60
- * vanilla adapter's update() method.
61
- *
62
- * Supports ref for imperative control via useGraph() hook:
63
- * ```tsx
64
- * const { ref, search, zoomToFit } = useGraph();
65
- * return <Graph ref={ref} spec={spec} />;
66
- * ```
67
- */
68
- export const Graph = forwardRef<GraphHandle, GraphProps>(function Graph(
69
- {
70
- spec,
71
- theme: themeProp,
72
- darkMode,
73
- onNodeClick,
74
- onNodeDoubleClick,
75
- onNodeHover,
76
- onEdgeHover,
77
- onSelectionChange,
78
- tooltip,
79
- legend,
80
- className,
81
- style,
82
- },
83
- ref,
84
- ) {
85
- const contextTheme = useVizTheme();
86
- const contextDarkMode = useVizDarkMode();
87
- const theme = themeProp ?? contextTheme;
88
- const resolvedDarkMode = darkMode ?? contextDarkMode;
89
- const containerRef = useRef<HTMLDivElement>(null);
90
- const graphRef = useRef<GraphInstance | null>(null);
91
- const specRef = useRef<string>('');
92
-
93
- // Store event handlers in refs so they don't trigger graph recreation.
94
- // Inline arrow functions create new references every render, which would
95
- // destroy and recreate the entire graph instance without this pattern.
96
- const handlersRef = useRef<{
97
- onNodeClick?: GraphProps['onNodeClick'];
98
- onNodeDoubleClick?: GraphProps['onNodeDoubleClick'];
99
- onNodeHover?: GraphProps['onNodeHover'];
100
- onEdgeHover?: GraphProps['onEdgeHover'];
101
- onSelectionChange?: GraphProps['onSelectionChange'];
102
- }>({});
103
- handlersRef.current = {
104
- onNodeClick,
105
- onNodeDoubleClick,
106
- onNodeHover,
107
- onEdgeHover,
108
- onSelectionChange,
109
- };
110
-
111
- // Stable callback wrappers that read from refs
112
- const stableOnNodeClick = useCallback(
113
- (node: Record<string, unknown>) => handlersRef.current.onNodeClick?.(node),
114
- [],
115
- );
116
- const stableOnNodeDoubleClick = useCallback(
117
- (node: Record<string, unknown>) => handlersRef.current.onNodeDoubleClick?.(node),
118
- [],
119
- );
120
- const stableOnNodeHover = useCallback(
121
- (node: Record<string, unknown> | null) => handlersRef.current.onNodeHover?.(node),
122
- [],
123
- );
124
- const stableOnEdgeHover = useCallback(
125
- (edge: Record<string, unknown> | null) => handlersRef.current.onEdgeHover?.(edge),
126
- [],
127
- );
128
- const stableOnSelectionChange = useCallback(
129
- (nodeIds: string[]) => handlersRef.current.onSelectionChange?.(nodeIds),
130
- [],
131
- );
132
-
133
- // Expose imperative handle for useGraph() hook
134
- useImperativeHandle(
135
- ref,
136
- () => ({
137
- search(query: string) {
138
- graphRef.current?.search(query);
139
- },
140
- clearSearch() {
141
- graphRef.current?.clearSearch();
142
- },
143
- zoomToFit() {
144
- graphRef.current?.zoomToFit();
145
- },
146
- zoomToNode(nodeId: string) {
147
- graphRef.current?.zoomToNode(nodeId);
148
- },
149
- selectNode(nodeId: string) {
150
- graphRef.current?.selectNode(nodeId);
151
- },
152
- getSelectedNodes() {
153
- return graphRef.current?.getSelectedNodes() ?? [];
154
- },
155
- updateVisuals(spec: GraphSpec) {
156
- graphRef.current?.updateVisuals(spec);
157
- },
158
- get instance() {
159
- return graphRef.current;
160
- },
161
- }),
162
- [],
163
- );
164
-
165
- // Mount graph and recreate when theme/darkMode change.
166
- // Event handlers use stable refs so they don't trigger recreation.
167
- // biome-ignore lint/correctness/useExhaustiveDependencies: spec intentionally excluded - spec changes handled via update() in Effect 2
168
- useEffect(() => {
169
- const container = containerRef.current;
170
- if (!container) return;
171
-
172
- const options: GraphMountOptions = {
173
- theme,
174
- darkMode: resolvedDarkMode,
175
- tooltip,
176
- legend,
177
- onNodeClick: stableOnNodeClick,
178
- onNodeDoubleClick: stableOnNodeDoubleClick,
179
- onNodeHover: stableOnNodeHover,
180
- onEdgeHover: stableOnEdgeHover,
181
- onSelectionChange: stableOnSelectionChange,
182
- responsive: true,
183
- };
184
-
185
- graphRef.current = createGraph(container, spec, options);
186
- specRef.current = JSON.stringify(spec);
187
-
188
- return () => {
189
- graphRef.current?.destroy();
190
- graphRef.current = null;
191
- };
192
- // Only recreate when theme or darkMode change. Event handlers use stable refs.
193
- // eslint-disable-next-line react-hooks/exhaustive-deps
194
- }, [
195
- theme,
196
- resolvedDarkMode,
197
- tooltip,
198
- legend,
199
- stableOnNodeClick,
200
- stableOnNodeDoubleClick,
201
- stableOnNodeHover,
202
- stableOnEdgeHover,
203
- stableOnSelectionChange,
204
- ]);
205
-
206
- // Update graph when spec changes.
207
- // If only encoding/chrome/nodeOverrides changed (same node/edge IDs), use
208
- // updateVisuals() to avoid restarting the simulation.
209
- useEffect(() => {
210
- const graph = graphRef.current;
211
- if (!graph) return;
212
-
213
- const specString = JSON.stringify(spec);
214
- if (specString === specRef.current) return;
215
-
216
- // Check if this is a visual-only change (same node/edge IDs)
217
- const prevSpec = specRef.current;
218
- specRef.current = specString;
219
-
220
- if (prevSpec) {
221
- try {
222
- const prev = JSON.parse(prevSpec) as GraphSpec;
223
- const sameNodes =
224
- prev.nodes.length === spec.nodes.length &&
225
- prev.nodes.every((n, i) => n.id === spec.nodes[i].id);
226
- const sameEdges =
227
- prev.edges.length === spec.edges.length &&
228
- prev.edges.every(
229
- (e, i) => e.source === spec.edges[i].source && e.target === spec.edges[i].target,
230
- );
231
-
232
- const sameLayout = prev.layout?.clustering?.field === spec.layout?.clustering?.field;
233
-
234
- if (sameNodes && sameEdges && sameLayout) {
235
- graph.updateVisuals(spec);
236
- return;
237
- }
238
- } catch {
239
- // Parse failed, fall through to full update
240
- }
241
- }
242
-
243
- graph.update(spec);
244
- }, [spec]);
245
-
246
- return (
247
- <div
248
- ref={containerRef}
249
- className={className ? `oc-graph-root ${className}` : 'oc-graph-root'}
250
- style={style}
251
- />
252
- );
253
- });
package/src/Map.tsx DELETED
@@ -1,131 +0,0 @@
1
- /**
2
- * React Map component: thin wrapper around the vanilla adapter.
3
- *
4
- * Mounts a map instance on render, updates when spec changes,
5
- * and cleans up on unmount. All heavy lifting is done by the vanilla
6
- * createMap() function.
7
- *
8
- * Supports forwardRef for imperative control via the instance getter.
9
- */
10
-
11
- import type { DarkMode, MapSpec, ThemeConfig } from '@opendata-ai/openchart-core';
12
- import { createMap, type MapInstance, type MapMountOptions } from '@opendata-ai/openchart-vanilla';
13
- import {
14
- type CSSProperties,
15
- forwardRef,
16
- useCallback,
17
- useEffect,
18
- useImperativeHandle,
19
- useRef,
20
- } from 'react';
21
- import { useVizDarkMode, useVizTheme } from './ThemeContext';
22
-
23
- type MapFeatureEvent = {
24
- id: string | number;
25
- name?: string;
26
- data: Record<string, unknown> | null;
27
- };
28
-
29
- export interface MapProps {
30
- /** The map spec to render. */
31
- spec: MapSpec;
32
- /** Theme overrides. */
33
- theme?: ThemeConfig;
34
- /** Dark mode: "auto", "force", or "off". */
35
- darkMode?: DarkMode;
36
- /** Callback when a map feature is clicked. */
37
- onMarkClick?: (feature: MapFeatureEvent) => void;
38
- /** Callback when a map feature is hovered (null when hover ends). */
39
- onMarkHover?: (feature: MapFeatureEvent | null) => void;
40
- /** CSS class name for the wrapper div. */
41
- className?: string;
42
- /** Inline styles for the wrapper div. */
43
- style?: CSSProperties;
44
- }
45
-
46
- export interface MapHandle {
47
- /** The underlying map instance (null until mounted). */
48
- readonly instance: MapInstance | null;
49
- }
50
-
51
- export const GeoMap = forwardRef<MapHandle, MapProps>(function GeoMap(
52
- { spec, theme: themeProp, darkMode, onMarkClick, onMarkHover, className, style },
53
- ref,
54
- ) {
55
- const contextTheme = useVizTheme();
56
- const contextDarkMode = useVizDarkMode();
57
- const theme = themeProp ?? contextTheme;
58
- const resolvedDarkMode = darkMode ?? contextDarkMode;
59
- const containerRef = useRef<HTMLDivElement>(null);
60
- const instanceRef = useRef<MapInstance | null>(null);
61
- const specRef = useRef<string>('');
62
-
63
- const handlersRef = useRef<{
64
- onMarkClick?: MapProps['onMarkClick'];
65
- onMarkHover?: MapProps['onMarkHover'];
66
- }>({});
67
- handlersRef.current = {
68
- onMarkClick,
69
- onMarkHover,
70
- };
71
-
72
- const stableOnMarkClick = useCallback(
73
- (feature: MapFeatureEvent) => handlersRef.current.onMarkClick?.(feature),
74
- [],
75
- );
76
- const stableOnMarkHover = useCallback(
77
- (feature: MapFeatureEvent | null) => handlersRef.current.onMarkHover?.(feature),
78
- [],
79
- );
80
-
81
- useImperativeHandle(
82
- ref,
83
- () => ({
84
- get instance() {
85
- return instanceRef.current;
86
- },
87
- }),
88
- [],
89
- );
90
-
91
- // biome-ignore lint/correctness/useExhaustiveDependencies: spec intentionally excluded - spec changes handled via update() in Effect 2
92
- useEffect(() => {
93
- const container = containerRef.current;
94
- if (!container) return;
95
-
96
- const options: MapMountOptions = {
97
- theme,
98
- darkMode: resolvedDarkMode,
99
- onMarkClick: stableOnMarkClick,
100
- onMarkHover: stableOnMarkHover,
101
- responsive: true,
102
- };
103
-
104
- instanceRef.current = createMap(container, spec, options);
105
- specRef.current = JSON.stringify(spec);
106
-
107
- return () => {
108
- instanceRef.current?.destroy();
109
- instanceRef.current = null;
110
- };
111
- }, [theme, resolvedDarkMode, stableOnMarkClick, stableOnMarkHover]);
112
-
113
- useEffect(() => {
114
- const instance = instanceRef.current;
115
- if (!instance) return;
116
-
117
- const specString = JSON.stringify(spec);
118
- if (specString === specRef.current) return;
119
-
120
- specRef.current = specString;
121
- instance.update(spec);
122
- }, [spec]);
123
-
124
- return (
125
- <div
126
- ref={containerRef}
127
- className={className ? `oc-map-root ${className}` : 'oc-map-root'}
128
- style={style}
129
- />
130
- );
131
- });
package/src/Sankey.tsx DELETED
@@ -1,176 +0,0 @@
1
- /**
2
- * React Sankey component: thin wrapper around the vanilla adapter.
3
- *
4
- * Mounts a sankey instance on render, updates when spec changes,
5
- * and cleans up on unmount. All heavy lifting is done by the vanilla
6
- * createSankey() function.
7
- *
8
- * Supports forwardRef for imperative control via the instance getter.
9
- */
10
-
11
- import type { DarkMode, SankeySpec, ThemeConfig } from '@opendata-ai/openchart-core';
12
- import {
13
- createSankey,
14
- type SankeyInstance,
15
- type SankeyMountOptions,
16
- } from '@opendata-ai/openchart-vanilla';
17
- import {
18
- type CSSProperties,
19
- forwardRef,
20
- useCallback,
21
- useEffect,
22
- useImperativeHandle,
23
- useRef,
24
- } from 'react';
25
- import { useVizDarkMode, useVizTheme } from './ThemeContext';
26
-
27
- export interface SankeyProps {
28
- /** The sankey spec to render. */
29
- spec: SankeySpec;
30
- /** Theme overrides. */
31
- theme?: ThemeConfig;
32
- /** Dark mode: "auto", "force", or "off". */
33
- darkMode?: DarkMode;
34
- /** Callback when a node is clicked. */
35
- onNodeClick?: (node: Record<string, unknown>) => void;
36
- /** Callback when a link is clicked. */
37
- onLinkClick?: (link: Record<string, unknown>) => void;
38
- /** Callback when a node is hovered (null when hover ends). */
39
- onNodeHover?: (node: Record<string, unknown> | null) => void;
40
- /** Callback when a link is hovered (null when hover ends). */
41
- onLinkHover?: (link: Record<string, unknown> | null) => void;
42
- /** CSS class name for the wrapper div. */
43
- className?: string;
44
- /** Inline styles for the wrapper div. */
45
- style?: CSSProperties;
46
- }
47
-
48
- export interface SankeyHandle {
49
- /** The underlying sankey instance (null until mounted). */
50
- readonly instance: SankeyInstance | null;
51
- }
52
-
53
- /**
54
- * React component that renders a sankey diagram from a SankeySpec.
55
- *
56
- * Uses the vanilla adapter internally. The spec is compiled and rendered
57
- * as SVG inside a wrapper div. Spec changes trigger re-renders via the
58
- * vanilla adapter's update() method.
59
- */
60
- export const Sankey = forwardRef<SankeyHandle, SankeyProps>(function Sankey(
61
- {
62
- spec,
63
- theme: themeProp,
64
- darkMode,
65
- onNodeClick,
66
- onLinkClick,
67
- onNodeHover,
68
- onLinkHover,
69
- className,
70
- style,
71
- },
72
- ref,
73
- ) {
74
- const contextTheme = useVizTheme();
75
- const contextDarkMode = useVizDarkMode();
76
- const theme = themeProp ?? contextTheme;
77
- const resolvedDarkMode = darkMode ?? contextDarkMode;
78
- const containerRef = useRef<HTMLDivElement>(null);
79
- const instanceRef = useRef<SankeyInstance | null>(null);
80
- const specRef = useRef<string>('');
81
-
82
- // Store event handlers in refs so they don't trigger recreation.
83
- const handlersRef = useRef<{
84
- onNodeClick?: SankeyProps['onNodeClick'];
85
- onLinkClick?: SankeyProps['onLinkClick'];
86
- onNodeHover?: SankeyProps['onNodeHover'];
87
- onLinkHover?: SankeyProps['onLinkHover'];
88
- }>({});
89
- handlersRef.current = {
90
- onNodeClick,
91
- onLinkClick,
92
- onNodeHover,
93
- onLinkHover,
94
- };
95
-
96
- // Stable callback wrappers that read from refs
97
- const stableOnNodeClick = useCallback(
98
- (node: Record<string, unknown>) => handlersRef.current.onNodeClick?.(node),
99
- [],
100
- );
101
- const stableOnLinkClick = useCallback(
102
- (link: Record<string, unknown>) => handlersRef.current.onLinkClick?.(link),
103
- [],
104
- );
105
- const stableOnNodeHover = useCallback(
106
- (node: Record<string, unknown> | null) => handlersRef.current.onNodeHover?.(node),
107
- [],
108
- );
109
- const stableOnLinkHover = useCallback(
110
- (link: Record<string, unknown> | null) => handlersRef.current.onLinkHover?.(link),
111
- [],
112
- );
113
-
114
- // Expose imperative handle
115
- useImperativeHandle(
116
- ref,
117
- () => ({
118
- get instance() {
119
- return instanceRef.current;
120
- },
121
- }),
122
- [],
123
- );
124
-
125
- // Mount sankey and recreate when theme/darkMode change.
126
- // biome-ignore lint/correctness/useExhaustiveDependencies: spec intentionally excluded - spec changes handled via update() in Effect 2
127
- useEffect(() => {
128
- const container = containerRef.current;
129
- if (!container) return;
130
-
131
- const options: SankeyMountOptions = {
132
- theme,
133
- darkMode: resolvedDarkMode,
134
- onNodeClick: stableOnNodeClick,
135
- onLinkClick: stableOnLinkClick,
136
- onNodeHover: stableOnNodeHover,
137
- onLinkHover: stableOnLinkHover,
138
- responsive: true,
139
- };
140
-
141
- instanceRef.current = createSankey(container, spec, options);
142
- specRef.current = JSON.stringify(spec);
143
-
144
- return () => {
145
- instanceRef.current?.destroy();
146
- instanceRef.current = null;
147
- };
148
- }, [
149
- theme,
150
- resolvedDarkMode,
151
- stableOnNodeClick,
152
- stableOnLinkClick,
153
- stableOnNodeHover,
154
- stableOnLinkHover,
155
- ]);
156
-
157
- // Update sankey when spec changes.
158
- useEffect(() => {
159
- const instance = instanceRef.current;
160
- if (!instance) return;
161
-
162
- const specString = JSON.stringify(spec);
163
- if (specString === specRef.current) return;
164
-
165
- specRef.current = specString;
166
- instance.update(spec);
167
- }, [spec]);
168
-
169
- return (
170
- <div
171
- ref={containerRef}
172
- className={className ? `oc-sankey-root ${className}` : 'oc-sankey-root'}
173
- style={style}
174
- />
175
- );
176
- });
@@ -1,40 +0,0 @@
1
- /**
2
- * Theme context: provides a theme and dark mode preference to all
3
- * descendant Chart, DataTable, and Graph components without prop drilling.
4
- *
5
- * Components use the context values as fallbacks when no explicit
6
- * `theme` or `darkMode` prop is passed.
7
- */
8
-
9
- import type { DarkMode, ThemeConfig } from '@opendata-ai/openchart-core';
10
- import { createContext, type ReactNode, useContext } from 'react';
11
-
12
- const VizThemeContext = createContext<ThemeConfig | undefined>(undefined);
13
- const VizDarkModeContext = createContext<DarkMode | undefined>(undefined);
14
-
15
- /** Read the current theme from the nearest VizThemeProvider. */
16
- export function useVizTheme(): ThemeConfig | undefined {
17
- return useContext(VizThemeContext);
18
- }
19
-
20
- /** Read the current dark mode preference from the nearest VizThemeProvider. */
21
- export function useVizDarkMode(): DarkMode | undefined {
22
- return useContext(VizDarkModeContext);
23
- }
24
-
25
- export interface VizThemeProviderProps {
26
- /** Theme config to provide to descendant viz components. */
27
- theme: ThemeConfig | undefined;
28
- /** Dark mode preference to provide to descendant viz components. */
29
- darkMode?: DarkMode;
30
- children: ReactNode;
31
- }
32
-
33
- /** Provides a theme and dark mode preference to all nested Chart, DataTable, and Graph components. */
34
- export function VizThemeProvider({ theme, darkMode, children }: VizThemeProviderProps) {
35
- return (
36
- <VizThemeContext.Provider value={theme}>
37
- <VizDarkModeContext.Provider value={darkMode}>{children}</VizDarkModeContext.Provider>
38
- </VizThemeContext.Provider>
39
- );
40
- }