首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >自定义多个mui组件

自定义多个mui组件
EN

Stack Overflow用户
提问于 2022-02-11 00:25:58
回答 1查看 624关注 0票数 0

从MUI的文档中,您可以定制组件。但是有办法定制多个组件吗?假设我想用相同的样式自定义MuiPaper和MuiList。

例如,可自定义组件:

代码语言:javascript
运行
AI代码解释
复制
const theme = createTheme({
  components: {
    // Name of the component
    MuiPaper: {
      defaultProps: {
        // The props to change the default for.
        backgroundColor: '#101010',
      },
    },
  },
});
EN

回答 1

Stack Overflow用户

发布于 2022-02-12 06:54:13

有多种方法可以自定义默认道具。请检查下面我添加了三种方法来定制组件。此外,您还可以添加更多的组件到它。

theme.js

代码语言:javascript
运行
AI代码解释
复制
const mytheme = {
  components: {
    MuiPaper: {
      styleOverrides: {
        root: ({ theme }) => ({ // using with theme
          backgroundColor: theme.palette.primary.main,
          color: theme.palette.primary.contrastText,
        }),
      },
    },
    MuiList : {
      styleOverrides: {
        root: { // adding direct CSS properties with screensize condition.
          '@media (min-width:600px)': {
            minHeight: '4.5rem',
            backgroundColor: '#101010',
          },
        },
      },
    },
    MuiButton: {
      styleOverrides: { 
        root: ({ ownerState, theme }) => ({ // changing only 'contained' variant or adding new variant.
        textTransform: 'capitalize',
         ...(ownerState.variant === 'contained' && {
            color: theme.palette.primary.contrastText,
            backgroundColor: theme.palette.primary.light,
          }),
        }),
      },
    },
  },
};

export default mytheme;

app.js

代码语言:javascript
运行
AI代码解释
复制
const App = () => {
  <ThemeProvider theme={createTheme(mytheme)}>
    ...
  </ThemeProvider>
}
export default App;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71077070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档