Skip to content

Commit 630e24e

Browse files
committed
allow switching theme modes
1 parent c545a83 commit 630e24e

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

chartlets.js/packages/demo/src/App.tsx

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { CssBaseline, ThemeProvider, createTheme } from "@mui/material";
2-
import Typography from "@mui/material/Typography";
1+
import { useMemo, useState } from "react";
2+
import {
3+
CssBaseline,
4+
ThemeProvider,
5+
createTheme,
6+
useMediaQuery,
7+
} from "@mui/material";
38

49
import { initializeContributions } from "chartlets";
510
import mui from "chartlets/plugins/mui";
@@ -10,6 +15,7 @@ import ExtensionsInfo from "./components/ExtensionInfo";
1015
import ControlBar from "./components/ControlBar";
1116
import PanelsControl from "./components/PanelsControl";
1217
import PanelsRow from "./components/PanelsRow";
18+
import Header, { type Mode } from "./components/Header";
1319

1420
initializeContributions({
1521
plugins: [mui(), vega()],
@@ -28,27 +34,35 @@ initializeContributions({
2834
logging: { enabled: true },
2935
});
3036

31-
// MUI's default font family
37+
// Material Design default font families
3238
const fontFamily = "Roboto, Arial, sans-serif";
3339

34-
const theme = createTheme({
35-
typography: { fontFamily },
36-
components: {
37-
MuiCssBaseline: {
38-
styleOverrides: {
39-
"*": { fontFamily },
40-
},
41-
},
42-
},
43-
});
44-
4540
function App() {
41+
const prefersDarkMode = useMediaQuery("(prefers-color-scheme: dark)");
42+
const systemMode = prefersDarkMode ? "dark" : "light";
43+
const [mode, setMode] = useState<Mode>(systemMode);
44+
const theme = useMemo(
45+
() =>
46+
createTheme({
47+
palette: {
48+
mode: mode === "system" ? systemMode : mode,
49+
},
50+
typography: { fontFamily },
51+
components: {
52+
MuiCssBaseline: {
53+
styleOverrides: {
54+
"*": { fontFamily },
55+
},
56+
},
57+
},
58+
}),
59+
[mode, systemMode],
60+
);
61+
4662
return (
4763
<ThemeProvider theme={theme}>
4864
<CssBaseline />
49-
<Typography fontSize="3em" fontWeight="bold">
50-
Chartlets Demo
51-
</Typography>
65+
<Header mode={mode} setMode={setMode} />
5266
<ExtensionsInfo />
5367
<ControlBar />
5468
<PanelsControl />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Box from "@mui/material/Box";
2+
import Button from "@mui/material/Button";
3+
import Typography from "@mui/material/Typography";
4+
5+
export type Mode = "dark" | "light" | "system";
6+
const modes: Mode[] = ["dark", "light", "system"];
7+
8+
interface HeaderProps {
9+
mode: Mode;
10+
setMode: (mode: Mode) => void;
11+
}
12+
13+
function Header({ mode, setMode }: HeaderProps) {
14+
return (
15+
<Box sx={{ display: "flex", alignItems: "center", gap: 2 }}>
16+
<Typography fontSize="3em" fontWeight="bold">
17+
Chartlets Demo
18+
</Typography>
19+
<Button
20+
onClick={() => {
21+
const nextMode = modes[(modes.findIndex((m) => m === mode) + 1) % 3];
22+
setMode(nextMode);
23+
}}
24+
variant="contained"
25+
size="small"
26+
>
27+
{mode}
28+
</Button>
29+
</Box>
30+
);
31+
}
32+
33+
export default Header;

0 commit comments

Comments
 (0)