|
1 | 1 | import { VegaLite } from "react-vega"; |
2 | 2 | import type { TopLevelSpec } from "vega-lite"; |
| 3 | +import * as vegaThemes from "vega-themes"; |
3 | 4 | import { useTheme } from "@mui/material"; |
4 | 5 |
|
5 | 6 | import { type ComponentState } from "@/lib/types/state/component"; |
6 | 7 | import type { ComponentProps } from "@/lib/component/Component"; |
7 | 8 | import { useSignalListeners } from "@/lib/hooks"; |
8 | 9 |
|
| 10 | +type VegaTheme = Omit<typeof vegaThemes, "version">; |
| 11 | + |
| 12 | +const isVegaTheme = (key?: string): key is keyof VegaTheme => |
| 13 | + !!key && key in vegaThemes; |
| 14 | + |
9 | 15 | interface PlotState extends ComponentState { |
| 16 | + theme?: string; |
10 | 17 | chart?: |
11 | 18 | | TopLevelSpec // This is the vega-lite specification type |
12 | 19 | | null; |
13 | 20 | } |
14 | 21 |
|
15 | 22 | interface PlotProps extends ComponentProps, PlotState {} |
16 | 23 |
|
17 | | -export function Plot({ type, id, style, chart, onChange }: PlotProps) { |
| 24 | +export function Plot({ type, id, style, theme, chart, onChange }: PlotProps) { |
18 | 25 | const muiTheme = useTheme(); |
19 | 26 | const signalListeners = useSignalListeners(chart, type, id, onChange); |
20 | 27 |
|
21 | 28 | if (!chart) { |
22 | 29 | return <div id={id} style={style} />; |
23 | 30 | } |
24 | 31 |
|
| 32 | + const vegaTheme = isVegaTheme(theme) |
| 33 | + ? theme |
| 34 | + : muiTheme.palette.mode === "dark" |
| 35 | + ? "dark" |
| 36 | + : undefined; |
| 37 | + |
25 | 38 | return ( |
26 | 39 | <VegaLite |
27 | | - theme={muiTheme.palette.mode === "dark" ? "dark" : undefined} |
| 40 | + theme={vegaTheme} |
28 | 41 | spec={chart} |
29 | 42 | style={style} |
30 | 43 | signalListeners={signalListeners} |
|
0 commit comments