Skip to content

Commit ee4fb76

Browse files
committed
update component.ts and vegaCharts to include Skeleton
1 parent f84c743 commit ee4fb76

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed
Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { VegaLite } from "react-vega";
22
import type { TopLevelSpec } from "vega-lite";
33

4-
import type { ComponentState, ComponentProps } from "@/index";
4+
import type { ComponentProps, ComponentState } from "@/index";
55
import { useSignalListeners } from "./hooks/useSignalListeners";
66
import { useVegaTheme, type VegaTheme } from "./hooks/useVegaTheme";
7+
import { useEffect, useState } from "react";
8+
import { Skeleton } from "@/plugins/mui/Skeleton";
79

810
interface VegaChartState extends ComponentState {
911
theme?: VegaTheme | "default" | "system";
@@ -19,22 +21,35 @@ export function VegaChart({
1921
id,
2022
style,
2123
theme,
22-
chart,
24+
chart: initialChart,
25+
skeletonProps,
2326
onChange,
2427
}: VegaChartProps) {
25-
const signalListeners = useSignalListeners(chart, type, id, onChange);
28+
const signalListeners = useSignalListeners(initialChart, type, id, onChange);
2629
const vegaTheme = useVegaTheme(theme);
27-
if (chart) {
28-
return (
29-
<VegaLite
30-
theme={vegaTheme}
31-
spec={chart}
32-
style={style}
33-
signalListeners={signalListeners}
34-
actions={false}
35-
/>
36-
);
37-
} else {
38-
return <div id={id} style={style} />;
39-
}
30+
const [chart, setChart] = useState<TopLevelSpec | null | undefined>(null);
31+
const [loading, setLoading] = useState(true);
32+
33+
useEffect(() => {
34+
if (initialChart) {
35+
setLoading(false);
36+
setChart(initialChart);
37+
}
38+
}, [initialChart]);
39+
40+
return (
41+
<Skeleton loading={loading} {...skeletonProps}>
42+
{chart ? (
43+
<VegaLite
44+
theme={vegaTheme}
45+
spec={chart}
46+
style={style}
47+
signalListeners={signalListeners}
48+
actions={false}
49+
/>
50+
) : (
51+
<div id={id} style={style} />
52+
)}
53+
</Skeleton>
54+
);
4055
}

chartlets.js/packages/lib/src/types/state/component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type CSSProperties } from "react";
22
import { isObject } from "@/utils/isObject";
33
import { isString } from "@/utils/isString";
4+
import type { SkeletonProps } from "@mui/material";
45

56
export type ComponentNode =
67
| ComponentState
@@ -24,6 +25,7 @@ export interface ComponentState {
2425
label?: string;
2526
color?: string;
2627
tooltip?: string;
28+
skeletonProps?: SkeletonProps;
2729
}
2830

2931
export interface ContainerState extends ComponentState {

0 commit comments

Comments
 (0)