feat: theme switcher

This commit is contained in:
CyberL1 2025-01-16 17:55:49 +01:00
parent f1b7e063bb
commit 3cd59266c2

View File

@ -7,6 +7,7 @@ import {
Box, Box,
createTheme, createTheme,
Drawer, Drawer,
FormControlLabel,
Icon, Icon,
IconButton, IconButton,
List, List,
@ -14,10 +15,12 @@ import {
ListItemButton, ListItemButton,
ListItemIcon, ListItemIcon,
ListItemText, ListItemText,
Switch,
ThemeProvider, ThemeProvider,
Toolbar, Toolbar,
Tooltip, Tooltip,
Typography, Typography,
useColorScheme,
} from "@mui/material"; } from "@mui/material";
import * as Icons from "@mui/icons-material"; import * as Icons from "@mui/icons-material";
import { useState } from "react"; import { useState } from "react";
@ -40,7 +43,7 @@ const sidebarItems: Item[] = [
const drawerWidth = 240; const drawerWidth = 240;
const darkTheme = createTheme({ const theme = createTheme({
colorSchemes: { colorSchemes: {
dark: true, dark: true,
}, },
@ -48,9 +51,14 @@ const darkTheme = createTheme({
function App({ error }: { error?: boolean }) { function App({ error }: { error?: boolean }) {
const [isOpen, setOpen] = useState(false); const [isOpen, setOpen] = useState(false);
const { mode, setMode } = useColorScheme();
if (!mode) {
return null;
}
return ( return (
<ThemeProvider theme={darkTheme}> <>
<AppBar <AppBar
position="fixed" position="fixed"
sx={{ zIndex: (theme) => theme.zIndex.drawer + 1 }} sx={{ zIndex: (theme) => theme.zIndex.drawer + 1 }}
@ -67,6 +75,23 @@ function App({ error }: { error?: boolean }) {
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}> <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
DLinux DLinux
</Typography> </Typography>
<FormControlLabel
control={<Switch defaultChecked={mode === "system"} />}
label="Automatic theme"
labelPlacement="start"
onChange={() => setMode(mode === "system" ? "light" : "system")}
/>
{mode != "system" && (
<IconButton
onClick={() => setMode(mode === "dark" ? "light" : "dark")}
>
{mode === "dark" ? (
<Icons.LightMode />
) : (
<Icons.DarkMode htmlColor="#fff" />
)}
</IconButton>
)}
</Toolbar> </Toolbar>
</AppBar> </AppBar>
<Drawer <Drawer
@ -114,8 +139,14 @@ function App({ error }: { error?: boolean }) {
{error && <ErrorPage />} {error && <ErrorPage />}
{localStorage.getItem("key") ? <Outlet /> : <Login />} {localStorage.getItem("key") ? <Outlet /> : <Login />}
</Box> </Box>
</ThemeProvider> </>
); );
} }
export default App; export default function ToggleColorMode() {
return (
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>
);
}