首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >styleOverrides中伪选择器在MUI v5主题化中的应用

styleOverrides中伪选择器在MUI v5主题化中的应用
EN

Stack Overflow用户
提问于 2022-02-07 07:41:51
回答 1查看 447关注 0票数 2

MUI(V5)的文档给出了这个示例:

代码语言:javascript
复制
const theme = createTheme({
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          // How to use pseudo-class here?
          // I've tried this but it doesn't work:
          // "&:hover": {
          //   backgroundColor: "#000"
          //  }
          fontSize: '1rem',
        },
      },
    },
  },
});

但是,如果我想在Button (或其他伪类状态)徘徊时将CSS样式添加到Button中怎么办?styleOverrides也能做到这一点吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-07 21:46:44

嗨,你的代码在我的回购中运行得很好。

代码语言:javascript
复制
const theme = createTheme({
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          '&:hover': {
            backgroundColor: '#000',
          },
          fontSize: '1rem',
        },
      },
    },
  },
});

但是,如果要创建主题,则还应该使用theme.pallete和其他主题选项,而不是直接在按钮中添加css颜色。

下面是我如何创建自定义梅按钮。

代码语言:javascript
复制
const myTheme = {
  ...
  components: {
    MuiButton: {
      styleOverrides: {
        root: ({ ownerState, theme }) => ({
          textTransform: 'capitalize',
          fontSize: '0.75rem',
          fontWeight: 500,
          padding: '10px 16px 10px 16px',
          // '&:hover': { 
          //   backgroundColor: theme.palette.primary.dark,
          // },  // here worked as well 
          ...(ownerState.variant === 'semicontained' && {
            color: theme.palette.primary.main,
            backgroundColor: theme.palette.primary.light,
          }),
        }),
      },
    },
  },
};

export default myTheme;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71014933

复制
相关文章

相似问题

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