@schemacast/react 0.0.0-stage → 0.5.1

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 (66) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +37 -2
  3. package/dist/auto-visual.d.ts +32 -0
  4. package/dist/auto-visual.js +27 -0
  5. package/dist/field-by-key.d.ts +3 -0
  6. package/dist/field-by-key.js +7 -0
  7. package/dist/format-field-value.d.ts +16 -0
  8. package/dist/format-field-value.js +84 -0
  9. package/dist/implemented-archetypes.d.ts +2 -0
  10. package/dist/implemented-archetypes.js +17 -0
  11. package/dist/index.d.ts +12 -0
  12. package/dist/index.js +10 -0
  13. package/dist/use-selected-view-id.d.ts +6 -0
  14. package/dist/use-selected-view-id.js +17 -0
  15. package/dist/view-switcher.d.ts +26 -0
  16. package/dist/view-switcher.js +63 -0
  17. package/dist/views/archetype-view.d.ts +16 -0
  18. package/dist/views/archetype-view.js +71 -0
  19. package/dist/views/bar-chart-options.d.ts +22 -0
  20. package/dist/views/bar-chart-options.js +103 -0
  21. package/dist/views/bar-chart-view.d.ts +17 -0
  22. package/dist/views/bar-chart-view.js +25 -0
  23. package/dist/views/build-hierarchy-tree-data.d.ts +8 -0
  24. package/dist/views/build-hierarchy-tree-data.js +32 -0
  25. package/dist/views/build-node-graph-data.d.ts +20 -0
  26. package/dist/views/build-node-graph-data.js +44 -0
  27. package/dist/views/chart-series-data.d.ts +61 -0
  28. package/dist/views/chart-series-data.js +150 -0
  29. package/dist/views/chart-value-axes.d.ts +45 -0
  30. package/dist/views/chart-value-axes.js +214 -0
  31. package/dist/views/data-table-constants.d.ts +3 -0
  32. package/dist/views/data-table-constants.js +3 -0
  33. package/dist/views/data-table-rows.d.ts +8 -0
  34. package/dist/views/data-table-rows.js +50 -0
  35. package/dist/views/data-table-view.d.ts +16 -0
  36. package/dist/views/data-table-view.js +76 -0
  37. package/dist/views/detail-card-view.d.ts +16 -0
  38. package/dist/views/detail-card-view.js +59 -0
  39. package/dist/views/edge-weight-width.d.ts +23 -0
  40. package/dist/views/edge-weight-width.js +47 -0
  41. package/dist/views/hierarchy-tree-view.d.ts +16 -0
  42. package/dist/views/hierarchy-tree-view.js +58 -0
  43. package/dist/views/json-tree-view.d.ts +15 -0
  44. package/dist/views/json-tree-view.js +115 -0
  45. package/dist/views/kanban-view.d.ts +16 -0
  46. package/dist/views/kanban-view.js +41 -0
  47. package/dist/views/kpi-grid-view.d.ts +16 -0
  48. package/dist/views/kpi-grid-view.js +34 -0
  49. package/dist/views/line-chart-options.d.ts +16 -0
  50. package/dist/views/line-chart-options.js +98 -0
  51. package/dist/views/line-chart-view.d.ts +16 -0
  52. package/dist/views/line-chart-view.js +31 -0
  53. package/dist/views/node-graph-options.d.ts +26 -0
  54. package/dist/views/node-graph-options.js +111 -0
  55. package/dist/views/node-graph-view.d.ts +16 -0
  56. package/dist/views/node-graph-view.js +28 -0
  57. package/dist/views/pie-chart-view.d.ts +16 -0
  58. package/dist/views/pie-chart-view.js +56 -0
  59. package/dist/views/timeline-view.d.ts +16 -0
  60. package/dist/views/timeline-view.js +86 -0
  61. package/dist/views/use-echarts.d.ts +4 -0
  62. package/dist/views/use-echarts.js +233 -0
  63. package/dist/visual-spec-view.d.ts +28 -0
  64. package/dist/visual-spec-view.js +31 -0
  65. package/package.json +56 -3
  66. package/styles.css +688 -0
@@ -0,0 +1,233 @@
1
+ import { BarChart, GraphChart, LineChart, PieChart, TreeChart } from 'echarts/charts';
2
+ import { GridComponent, LegendComponent, TooltipComponent } from 'echarts/components';
3
+ import { init, use } from 'echarts/core';
4
+ import { SVGRenderer } from 'echarts/renderers';
5
+ import { useEffect, useRef } from 'react';
6
+ const PIE_PALETTE_FALLBACK = [
7
+ '#2867b2',
8
+ '#c44e52',
9
+ '#55a868',
10
+ '#8172b3',
11
+ '#cc8c2f',
12
+ '#4c9f9a',
13
+ '#b55d9e',
14
+ '#7a7a4e',
15
+ ];
16
+ use([
17
+ BarChart,
18
+ GraphChart,
19
+ LineChart,
20
+ PieChart,
21
+ TreeChart,
22
+ GridComponent,
23
+ LegendComponent,
24
+ TooltipComponent,
25
+ SVGRenderer,
26
+ ]);
27
+ export function useEcharts(option, isStreaming, useAutoVisualTheme = false) {
28
+ const containerRef = useRef(null);
29
+ useEffect(() => {
30
+ const element = containerRef.current;
31
+ if (element === null || option === null) {
32
+ return;
33
+ }
34
+ const instance = init(element, undefined, { renderer: 'svg' });
35
+ const updateOption = () => {
36
+ const themedOption = useAutoVisualTheme ? applyAutoVisualTheme(option, element) : option;
37
+ instance.setOption({ ...themedOption, animation: !isStreaming });
38
+ };
39
+ updateOption();
40
+ const resizeObserver = typeof ResizeObserver === 'undefined'
41
+ ? null
42
+ : new ResizeObserver(() => {
43
+ instance.resize();
44
+ });
45
+ resizeObserver?.observe(element);
46
+ const documentView = element.ownerDocument.defaultView;
47
+ const colorScheme = useAutoVisualTheme && documentView !== null && typeof documentView.matchMedia === 'function'
48
+ ? documentView.matchMedia('(prefers-color-scheme: dark)')
49
+ : null;
50
+ const themeObserver = useAutoVisualTheme && typeof MutationObserver !== 'undefined'
51
+ ? new MutationObserver(updateOption)
52
+ : null;
53
+ themeObserver?.observe(element.ownerDocument.documentElement, {
54
+ attributes: true,
55
+ attributeFilter: ['data-theme'],
56
+ });
57
+ colorScheme?.addEventListener('change', updateOption);
58
+ return () => {
59
+ colorScheme?.removeEventListener('change', updateOption);
60
+ themeObserver?.disconnect();
61
+ resizeObserver?.disconnect();
62
+ instance.dispose();
63
+ };
64
+ }, [isStreaming, option, useAutoVisualTheme]);
65
+ return containerRef;
66
+ }
67
+ function applyAutoVisualTheme(option, element) {
68
+ const themeRoot = element.closest('.schemacast-auto-visual') ?? element;
69
+ const computedStyle = themeRoot.ownerDocument.defaultView?.getComputedStyle(themeRoot);
70
+ const getThemeColor = (token, fallback) => computedStyle?.getPropertyValue(token).trim() || fallback;
71
+ const foreground = getThemeColor('--schemacast-foreground', '#202a36');
72
+ const muted = getThemeColor('--schemacast-muted', '#637083');
73
+ const tooltip = Array.isArray(option.tooltip) ? option.tooltip[0] : option.tooltip;
74
+ const border = getThemeColor('--schemacast-border', '#d7dee8');
75
+ const categoricalPalette = PIE_PALETTE_FALLBACK.map((fallback, index) => getThemeColor(`--schemacast-pie-color-${index + 1}`, fallback));
76
+ const legend = Array.isArray(option.legend)
77
+ ? option.legend.map((item) => ({
78
+ ...item,
79
+ textStyle: { ...item.textStyle, color: foreground },
80
+ }))
81
+ : option.legend === undefined
82
+ ? undefined
83
+ : { ...option.legend, textStyle: { ...option.legend.textStyle, color: foreground } };
84
+ return {
85
+ ...option,
86
+ color: [getThemeColor('--schemacast-accent', '#2867b2'), muted],
87
+ legend,
88
+ xAxis: themeXAxisOptions(option.xAxis, foreground, border),
89
+ yAxis: themeYAxisOptions(option.yAxis, foreground, border),
90
+ series: themeSeriesLabels(option.series, foreground, muted, categoricalPalette),
91
+ textStyle: { ...option.textStyle, color: foreground },
92
+ tooltip: {
93
+ ...tooltip,
94
+ backgroundColor: getThemeColor('--schemacast-background', '#ffffff'),
95
+ borderColor: border,
96
+ textStyle: { ...tooltip?.textStyle, color: foreground },
97
+ },
98
+ };
99
+ }
100
+ function themeSeriesLabels(series, foreground, muted, categoricalPalette) {
101
+ if (!Array.isArray(series)) {
102
+ return series;
103
+ }
104
+ return series.map((seriesOption) => {
105
+ if (seriesOption.type === 'pie') {
106
+ return themePieSeries(seriesOption, foreground, categoricalPalette);
107
+ }
108
+ if (seriesOption.type === 'graph') {
109
+ return themeGraphSeries(seriesOption, foreground, muted, categoricalPalette);
110
+ }
111
+ if (seriesOption.type === 'tree') {
112
+ return themeTreeSeries(seriesOption, foreground);
113
+ }
114
+ return seriesOption;
115
+ });
116
+ }
117
+ function themeGraphSeries(series, foreground, muted, palette) {
118
+ return {
119
+ ...series,
120
+ itemStyle: { ...series.itemStyle, color: palette[0] },
121
+ lineStyle: { ...series.lineStyle, color: muted },
122
+ categories: series.categories?.map((category, index) => ({
123
+ ...category,
124
+ itemStyle: { ...category.itemStyle, color: palette[index % palette.length] },
125
+ })),
126
+ label: {
127
+ ...series.label,
128
+ color: foreground,
129
+ textBorderColor: 'transparent',
130
+ textBorderWidth: 0,
131
+ },
132
+ edgeLabel: {
133
+ ...series.edgeLabel,
134
+ color: foreground,
135
+ textBorderColor: 'transparent',
136
+ textBorderWidth: 0,
137
+ },
138
+ };
139
+ }
140
+ function themePieSeries(series, foreground, palette) {
141
+ const label = typeof series.label === 'object' && series.label !== null ? series.label : {};
142
+ const data = series.data?.map((item, index) => {
143
+ if (typeof item !== 'object' || item === null || Array.isArray(item)) {
144
+ return item;
145
+ }
146
+ return {
147
+ ...item,
148
+ itemStyle: {
149
+ ...item.itemStyle,
150
+ color: palette[index % palette.length],
151
+ },
152
+ };
153
+ });
154
+ return {
155
+ ...series,
156
+ data,
157
+ label: {
158
+ ...label,
159
+ color: foreground,
160
+ textBorderColor: 'transparent',
161
+ textBorderWidth: 0,
162
+ },
163
+ };
164
+ }
165
+ function themeTreeSeries(series, foreground) {
166
+ const label = typeof series.label === 'object' && series.label !== null ? series.label : {};
167
+ const leafLabel = typeof series.leaves?.label === 'object' && series.leaves.label !== null
168
+ ? series.leaves.label
169
+ : {};
170
+ return {
171
+ ...series,
172
+ label: {
173
+ ...label,
174
+ color: foreground,
175
+ textBorderColor: 'transparent',
176
+ textBorderWidth: 0,
177
+ },
178
+ leaves: {
179
+ ...series.leaves,
180
+ label: {
181
+ ...leafLabel,
182
+ color: foreground,
183
+ textBorderColor: 'transparent',
184
+ textBorderWidth: 0,
185
+ },
186
+ },
187
+ };
188
+ }
189
+ function themeXAxisOptions(axes, foreground, border) {
190
+ if (Array.isArray(axes)) {
191
+ return axes.map((axis) => themeXAxis(axis, foreground, border));
192
+ }
193
+ return axes === undefined ? undefined : themeXAxis(axes, foreground, border);
194
+ }
195
+ function themeYAxisOptions(axes, foreground, border) {
196
+ if (Array.isArray(axes)) {
197
+ return axes.map((axis) => themeYAxis(axis, foreground, border));
198
+ }
199
+ return axes === undefined ? undefined : themeYAxis(axes, foreground, border);
200
+ }
201
+ function themeXAxis(axis, foreground, border) {
202
+ const axisLine = typeof axis.axisLine === 'object' && axis.axisLine !== null ? axis.axisLine : undefined;
203
+ const axisTick = typeof axis.axisTick === 'object' && axis.axisTick !== null ? axis.axisTick : undefined;
204
+ const splitLine = typeof axis.splitLine === 'object' && axis.splitLine !== null ? axis.splitLine : undefined;
205
+ return {
206
+ ...axis,
207
+ axisLabel: {
208
+ ...(typeof axis.axisLabel === 'object' && axis.axisLabel !== null ? axis.axisLabel : {}),
209
+ color: foreground,
210
+ },
211
+ axisLine: { ...axisLine, lineStyle: { ...axisLine?.lineStyle, color: border } },
212
+ axisTick: { ...axisTick, lineStyle: { ...axisTick?.lineStyle, color: border } },
213
+ splitLine: { ...splitLine, lineStyle: { ...splitLine?.lineStyle, color: border } },
214
+ nameTextStyle: { ...axis.nameTextStyle, color: foreground },
215
+ };
216
+ }
217
+ function themeYAxis(axis, foreground, border) {
218
+ const axisLine = typeof axis.axisLine === 'object' && axis.axisLine !== null ? axis.axisLine : undefined;
219
+ const axisTick = typeof axis.axisTick === 'object' && axis.axisTick !== null ? axis.axisTick : undefined;
220
+ const splitLine = typeof axis.splitLine === 'object' && axis.splitLine !== null ? axis.splitLine : undefined;
221
+ return {
222
+ ...axis,
223
+ axisLabel: {
224
+ ...(typeof axis.axisLabel === 'object' && axis.axisLabel !== null ? axis.axisLabel : {}),
225
+ color: foreground,
226
+ },
227
+ axisLine: { ...axisLine, lineStyle: { ...axisLine?.lineStyle, color: border } },
228
+ axisTick: { ...axisTick, lineStyle: { ...axisTick?.lineStyle, color: border } },
229
+ splitLine: { ...splitLine, lineStyle: { ...splitLine?.lineStyle, color: border } },
230
+ nameTextStyle: { ...axis.nameTextStyle, color: foreground },
231
+ };
232
+ }
233
+ //# sourceMappingURL=use-echarts.js.map
@@ -0,0 +1,28 @@
1
+ import type { VisualSpec } from '@schemacast/core';
2
+ import type { ReactElement } from 'react';
3
+ import '../styles.css';
4
+ /**
5
+ * Props for {@link VisualSpecView}.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * <VisualSpecView spec={spec} isStreaming={spec.isStreaming} />
10
+ * ```
11
+ */
12
+ export interface VisualSpecViewProps {
13
+ /** Existing AST returned by `cast()`, `castStream()`, or `createStreamCaster()`. */
14
+ spec: VisualSpec;
15
+ /** Optional override for the streaming state carried by `spec`. */
16
+ isStreaming?: boolean;
17
+ }
18
+ /**
19
+ * Renders an existing `VisualSpec` using the same view shell and renderers as `AutoVisual`.
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * const spec = cast(data);
24
+ * return <VisualSpecView spec={spec} />;
25
+ * ```
26
+ */
27
+ export declare function VisualSpecView({ spec, isStreaming }: VisualSpecViewProps): ReactElement;
28
+ //# sourceMappingURL=visual-spec-view.d.ts.map
@@ -0,0 +1,31 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo } from 'react';
3
+ import { useSelectedViewId } from './use-selected-view-id.js';
4
+ import { isImplementedArchetype } from './implemented-archetypes.js';
5
+ import { ArchetypeView } from './views/archetype-view.js';
6
+ import { ViewSwitcher, viewTabPanelId } from './view-switcher.js';
7
+ import '../styles.css';
8
+ /**
9
+ * Renders an existing `VisualSpec` using the same view shell and renderers as `AutoVisual`.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * const spec = cast(data);
14
+ * return <VisualSpecView spec={spec} />;
15
+ * ```
16
+ */
17
+ export function VisualSpecView({ spec, isStreaming }) {
18
+ const renderSpec = useMemo(() => {
19
+ if (isStreaming === undefined || isStreaming === spec.isStreaming) {
20
+ return spec;
21
+ }
22
+ return { ...spec, isStreaming };
23
+ }, [isStreaming, spec]);
24
+ const [selectedViewId, setSelectedViewId] = useSelectedViewId(renderSpec);
25
+ const activeView = renderSpec.views.find((view) => view.id === selectedViewId) ?? renderSpec.primaryView;
26
+ const showStreamingPlaceholder = renderSpec.isStreaming && renderSpec.data.length === 0;
27
+ const showSwitcher = renderSpec.views.length > 1;
28
+ const canRenderView = !showStreamingPlaceholder && isImplementedArchetype(activeView.archetype);
29
+ return (_jsxs("section", { "aria-label": "Auto visual", className: "schemacast-auto-visual", "data-testid": "auto-visual", children: [showSwitcher ? (_jsx(ViewSwitcher, { views: renderSpec.views, selectedViewId: selectedViewId, onSelectViewId: setSelectedViewId })) : null, _jsxs("div", { role: "tabpanel", className: "schemacast-view-panel", id: viewTabPanelId(activeView.id), "aria-labelledby": showSwitcher ? `schemacast-tab-${activeView.id}` : undefined, "data-testid": "view-panel", children: [_jsx("header", { className: "schemacast-view-panel-header", children: _jsxs("details", { className: "schemacast-view-rationale", children: [_jsx("summary", { className: "schemacast-view-rationale-summary", children: "Why this view?" }), _jsxs("div", { className: "schemacast-view-rationale-content", children: [_jsxs("p", { className: "schemacast-view-rationale-item", children: [_jsx("span", { className: "schemacast-view-rationale-label", children: "View" }), _jsx("span", { "data-testid": "primary-archetype", children: activeView.archetype })] }), _jsxs("p", { className: "schemacast-view-rationale-item", children: [_jsx("span", { className: "schemacast-view-rationale-label", children: "Score" }), _jsx("span", { "data-testid": "primary-score", children: activeView.score.toFixed(2) })] }), _jsxs("p", { className: "schemacast-view-rationale-item", children: [_jsx("span", { className: "schemacast-view-rationale-label", children: "Reason" }), _jsx("span", { "data-testid": "primary-reason", children: activeView.reason })] })] })] }) }), showStreamingPlaceholder ? (_jsx("p", { "data-testid": "streaming-placeholder", children: "Preparing visual from stream\u2026" })) : canRenderView ? (_jsx(ArchetypeView, { spec: renderSpec, view: activeView })) : (_jsxs("p", { "data-testid": "view-placeholder", children: ["No renderer is available for archetype \u201C", activeView.archetype, "\u201D."] }))] })] }));
30
+ }
31
+ //# sourceMappingURL=visual-spec-view.js.map
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@schemacast/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.5.1",
4
+ "description": "React renderer for schemacast VisualSpec output.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "engines": {
8
+ "node": ">=20"
9
+ },
10
+ "keywords": [
11
+ "schemacast",
12
+ "react",
13
+ "charts",
14
+ "echarts",
15
+ "visualization",
16
+ "json"
17
+ ],
18
+ "sideEffects": [
19
+ "*.css"
20
+ ],
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js"
27
+ },
28
+ "./styles.css": "./styles.css"
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "styles.css",
33
+ "!dist/**/*.map"
34
+ ],
35
+ "dependencies": {
36
+ "@schemacast/core": "0.5.1",
37
+ "echarts": "^5.6.0"
38
+ },
39
+ "peerDependencies": {
40
+ "react": "^18.0.0 || ^19.0.0",
41
+ "react-dom": "^18.0.0 || ^19.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@testing-library/react": "^16.0.0",
45
+ "@types/react": "^19.0.0",
46
+ "@types/react-dom": "^19.0.0",
47
+ "jsdom": "^25.0.0",
48
+ "react": "^19.0.0",
49
+ "react-dom": "^19.0.0",
50
+ "typescript": "^5.7.0"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "scripts": {
56
+ "typecheck": "tsc --noEmit",
57
+ "build": "tsc"
58
+ }
6
59
  }