在 MUI 主题中动态更改主色
IT技术
reactjs
themes
material-ui
2021-05-19 03:56:48
2个回答
import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import Root from './Root';
import lightTheme from 'your-light-theme-path';
import darkTheme from 'your-dark-them-path';
const theme1 = createMuiTheme(lightTheme);
const theme2 = createMuiTheme(darkTheme)
class App extends React.Component {
state = {
isThemeLight: true;
}
onChange = () => {
this.setState=({ isThemeLight: false })
}
render() {
const { isThemeLight } = this.state;
return (
<MuiThemeProvider theme={isThemeLight ? theme1 : theme2}>
<Root /> // your app here
<button onClick={this.onChange}>Change Dark</button>
</MuiThemeProvider>
);
}
}
render(<App />, document.querySelector('#app'));
您lightTheme
或darkTheme
可以是这样的文件
export default {
direction: 'ltr',
palette: {
type: 'light',
primary: {
main: '#37b44e',
},
secondary: {
main: '#000',
},
},
};
您可以在 Material UI Docs主题配置中看到所有可配置的主题列表
方法 2(对于主题更改运行时)
import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import Root from './Root';
const theme1 = createMuiTheme(lightTheme);
const theme2 = createMuiTheme(darkTheme)
class App extends React.Component {
state = {
theme1: {
palette: {
type: 'light',
primary: { main: '#37b44e' },
secondary: { main: '#000' },
},
};
theme2: {
palette: {
type: 'light',
primary: { main: '#37b44e' },
secondary: { main: '#000' },
},
};
isThemeLight: true;
}
onChange = () => {
this.setState=({ isThemeLight: false })
}
onChangeTheme1 = () => {
this.setState(({theme1}) => ({
theme1: {
...theme1,
primary: { main: 'red' },
}
}));
}
render() {
const { theme1, theme2, isThemeLight } = this.state;
return (
<MuiThemeProvider
theme={isThemeLight ? createMuiTheme(theme1) : createMuiTheme(theme2)}
>
<Root /> // your app here
<button onClick={this.onChange}>Change Dark</button>
<button onClick={this.onChangeTheme1}>Change Palette Theme 1</button>
</MuiThemeProvider>
);
}
}
render(<App />, document.querySelector('#app'));
useMemo
每当primaryColor
状态发生变化时,您都可以使用它来计算主题对象。setPrimaryColor()
用户在 UI 中选择一个选项后,可以更新状态:
const [primaryColor, setPrimaryColor] = React.useState(defaultColor);
const theme = React.useMemo(
() =>
createTheme({
palette: {
primary: {
main: primaryColor,
},
},
}),
[primaryColor],
);
这是一个最小的工作示例:
const defaultColor = '#f44336';
const [primaryColor, setPrimaryColor] = React.useState(defaultColor);
const theme = React.useMemo(
() =>
createTheme({
palette: {
primary: { main: primaryColor },
},
}),
[primaryColor],
);
return (
<ThemeProvider theme={theme}>
<Box m={2}>
<TextField
select
sx={{ width: 100 }}
label="Primary color"
defaultValue={defaultColor}
onChange={(e) => setPrimaryColor(e.target.value)}
>
<MenuItem value="#f44336">Red</MenuItem>
<MenuItem value="#2196f3">Blue</MenuItem>
<MenuItem value="#4caf50">Green</MenuItem>
</TextField>
<Checkbox defaultChecked />
</Box>
</ThemeProvider>
);
现场演示
其它你可能感兴趣的问题