styleoverrides未与MUI中的样式组件一起应用

发布于 2025-02-01 02:52:15 字数 918 浏览 1 评论 0原文

我正在尝试使用MUI的styleoverrides将样式应用于使用样式组件构建的组件,但是从另一个软件包中构建的。因此,例如,组件的构建类似于:

const SectionTitle = styled(Box, {
  name: 'SectionTitle',
  slot: 'Root',
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || 'auto',
  margin: 0,
}));

在HTML中,该类看起来像:

muibox-root css-d4531u-sectionTitle-root-root

我希望能够使用styleoverrides,例如

const theme = createTheme({
  palette: ...,
  components: {
    SectionTitle: {
      styleOverrides: {
          root: {
            width: '100px',
          }
       }
    }
  }
});

:我可以从 mui的全球覆盖物应该工作。另外,我可以将其与MUI组件一起使用,只是我使用stypled-components构建的自定义零件。该样式未从styleoverrides中应用。有什么想法吗?

I am trying to use MUI's styleOverrides to globally apply styles to a component that was built using styled-components, but from another package. So for example, the component is built like:

const SectionTitle = styled(Box, {
  name: 'SectionTitle',
  slot: 'Root',
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || 'auto',
  margin: 0,
}));

In the HTML, the class looks like:

MuiBox-root css-d4531u-SectionTitle-root

I want to be able to use styleOverrides, like:

const theme = createTheme({
  palette: ...,
  components: {
    SectionTitle: {
      styleOverrides: {
          root: {
            width: '100px',
          }
       }
    }
  }
});

As far as I can tell from MUI's global overrides, this should work. Also I am able to use it with MUI components, just not custom ones I build using styled-components. The styles are not applied from the styleOverrides. Any ideas why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2025-02-08 02:52:15

说明:

1. You need to define overrides in the theme, i.e. specify which components will be overridden, example:

const theme = createTheme({
  palette: ...,
  components: {
    // With this below, you have defined the "SecitonTitle" will be overriden
    SectionTitle: {
      styleOverrides: {
          //First override
          root: {
            width: '100px',
          },
          //Second override
          example: {
            height: '200px'
          }
       }
    }
  }
});

2. In addition to defining what will be replaced in the theme, it is also necessary to perform another procedure, which is choose which changes (overrides) that were defined in the theme will be applied, example:

const SectionTitle = styled(Box, {
  name: "SectionTitle",
  slot: "Root",
  overridesResolver: (props, styles) => [styles.root]
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || "auto",
  margin: 0
}));

解决方案:

1. Choose in the styled component that all overridden changes in the theme will be applied, example:

const SectionTitle = styled(Box, {
  name: "SectionTitle",
  slot: "Root",
  overridesResolver: (props, styles) => [Object.values(styles)]
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || "auto",
  margin: 0
}));

2. Apply directly (through theme) without having to choose override in styled component.

To do this, you need to define which properties will be passed to the SectionTitle component, as it is a component without properties, just leave it empty, example:

const SectionTitle = styled(Box, {
  name: "SectionTitle",
  slot: "Root",
  //note that I removed the overrideResolver
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || "auto",
  margin: 0
}));

const theme = createTheme({
  components: {
    SectionTitle: {
      variants: [
        {
          props: {},
          style: {
            width: "100%"
          }
        }
      ]
    }
  }
});

这意味着,如果您使用&lt; e节标题abc =“ test”/&gt;您将需要按以下方式定义它:

variants: [
    {
      props: { abc: 'test' },
      style: {
        width: "100%"
      }
    }
]

我希望我能有所帮助,任何问题,如果我的答案令人困惑(即使我使用的是Google翻译),请随时评论我可以澄清。

Explanation:

1. You need to define overrides in the theme, i.e. specify which components will be overridden, example:

const theme = createTheme({
  palette: ...,
  components: {
    // With this below, you have defined the "SecitonTitle" will be overriden
    SectionTitle: {
      styleOverrides: {
          //First override
          root: {
            width: '100px',
          },
          //Second override
          example: {
            height: '200px'
          }
       }
    }
  }
});

2. In addition to defining what will be replaced in the theme, it is also necessary to perform another procedure, which is choose which changes (overrides) that were defined in the theme will be applied, example:

const SectionTitle = styled(Box, {
  name: "SectionTitle",
  slot: "Root",
  overridesResolver: (props, styles) => [styles.root]
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || "auto",
  margin: 0
}));

Solution(s):

1. Choose in the styled component that all overridden changes in the theme will be applied, example:

const SectionTitle = styled(Box, {
  name: "SectionTitle",
  slot: "Root",
  overridesResolver: (props, styles) => [Object.values(styles)]
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || "auto",
  margin: 0
}));

2. Apply directly (through theme) without having to choose override in styled component.

To do this, you need to define which properties will be passed to the SectionTitle component, as it is a component without properties, just leave it empty, example:

const SectionTitle = styled(Box, {
  name: "SectionTitle",
  slot: "Root",
  //note that I removed the overrideResolver
})<WizardSectionTitleProps>(({ width }) => ({
  width: width || "auto",
  margin: 0
}));

const theme = createTheme({
  components: {
    SectionTitle: {
      variants: [
        {
          props: {},
          style: {
            width: "100%"
          }
        }
      ]
    }
  }
});

This means that if you use <Section Title abc="test"/> you will need to define it as follows:

variants: [
    {
      props: { abc: 'test' },
      style: {
        width: "100%"
      }
    }
]

I hope I could have helped, any questions, if my answer is confusing (even because I'm using Google Translate), don't hesitate to comment that I'll be available to clarify.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文