在 MUI 主题中动态更改主色

IT技术 reactjs themes material-ui
2021-05-19 03:56:48

有要求,我想让用户可以从蓝色、橙色、绿色等列表中选择他们的主要颜色。我在前端使用了最新的 MUI。

现在我可以将浅色主题更改为深色主题,但我的要求也是更改原色。请帮助我如何为相同的代码编写相同的代码。

请检查附加屏幕:

在此处输入图片说明

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'));

lightThemedarkTheme可以是这样的文件

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>
);

现场演示

代码沙盒演示