我正在用mui创建一个应用程序。我必须在我的网站中添加黑暗和明亮的模式。我有所有的自定义调色板颜色为黑暗和明亮的模式。我已经创建了一个主题。但是,我如何定义我的自定义调色板颜色为暗模式和光从主题。
请帮帮我
palette: {
text: {
primary: "#101130",
//Text color will be white when mode is dark
},
background: {
default: "#fffff",
//background color will be red when mode is dark
}
},我已经试过了
import { createTheme } from '@mui/material/styles';
const theme = createTheme({
palette: {
mode,
...(mode === 'light'
? {
// palette values for light mode
text: {
primary: "#101130",
},
background: {
default: "#fffff"
}
}
: {
// palette values for dark mode
background: {
default: "#000",
},
text: {
primary: '#fff',
},
}
),
}
});
export default theme;但是得到的错误是mode is undefine。请帮助我使用mui版本5
发布于 2021-12-03 09:21:44
你应该能够从道具中得到mode。您可以通过在Next.js中利用getStaticProps来创建Context或更简单的方法,因为您使用的是Next.js。
// theme.js
import {
createTheme as createMuiTheme
} from '@mui/material/styles';
const themeOptions = [
{
mode: 'light',
palette: { ... }
},
{
mode: 'dark',
palette: { ... }
}
];
export const createTheme = (mode) => {
return themeOptions.find((theme) => theme.mode = mode);
}正如您所看到的,您需要构建一个自定义createTheme函数,该函数可以从您的配置中切换选定的主题。
// _app.page.js
...
import { ThemeProvider } from '@mui/material/styles';
import { createTheme } from './theme';
const App = ({ pageProps }) => {
const { mode } = pageProps;
// mode is theme, dark or light from whatever state
const theme = createTheme(mode);
return (
<ThemeProvider theme={theme}>
...
);
}你也可以看看this。
https://stackoverflow.com/questions/70211208
复制相似问题