Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[examples] Add Theme Mode Switch to Next.js TS example #43576

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/material-ui-nextjs-ts/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import { AppRouterCacheProvider } from '@mui/material-nextjs/v14-appRouter';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import theme from '@/theme';
import InitColorSchemeScript from '@mui/material/InitColorSchemeScript';
import ModeSwitch from '@/components/ModeSwitch';

export default function RootLayout(props: { children: React.ReactNode }) {
return (
<html lang="en">
<html lang="en" suppressHydrationWarning>
<body>
<InitColorSchemeScript attribute="class" />
<AppRouterCacheProvider options={{ enableCssLayer: true }}>
<ThemeProvider theme={theme}>
{/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */}
<CssBaseline />
<ModeSwitch />
{props.children}
</ThemeProvider>
</AppRouterCacheProvider>
Expand Down
40 changes: 40 additions & 0 deletions examples/material-ui-nextjs-ts/src/components/ModeSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use client';
import * as React from 'react';
import Box from '@mui/material/Box';
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import { useColorScheme } from '@mui/material/styles';

export default function ModeSwitch() {
const { mode, setMode } = useColorScheme();
if (!mode) {
return null;
}
return (
<Box
sx={{
display: 'flex',
justifyContent: 'flex-end',
mt: 1,
p: 1,
}}
>
<FormControl>
<InputLabel id="mode-select-label">Theme</InputLabel>
<Select
labelId="mode-select-label"
id="mode-select"
value={mode}
onChange={(event) => setMode(event.target.value as typeof mode)}
label="Theme"
>
<MenuItem value="system">System</MenuItem>
<MenuItem value="light">Light</MenuItem>
<MenuItem value="dark">Dark</MenuItem>
</Select>
</FormControl>
</Box>
);
}
6 changes: 3 additions & 3 deletions examples/material-ui-nextjs-ts/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const roboto = Roboto({
});

const theme = createTheme({
cssVariables: true,
palette: {
mode: 'light',
colorSchemes: { light: true, dark: true },
cssVariables: {
colorSchemeSelector: 'class',
},
typography: {
fontFamily: roboto.style.fontFamily,
Expand Down
Loading