如何使用样式组件覆盖 material-ui css?

泰娜拉

我仍然是 ui-material 的初学者。我想自定义我自己的按钮组件styled-component

问题是根据按钮的变化覆盖 css,例如,如果它是primarysecondary

这是我在codeandbox.io中的代码

import React from "react";
import PropTypes from "prop-types";

import { CButton } from "./styles";

const CustomButton = ({ children, color }) => {
  return (
    <div>
      <CButton variant="contained" color={color}>
        {children}
      </CButton>
    </div>
  );
};

CustomButton.propTypes = {
  children: PropTypes.node,
  color: PropTypes.oneOf(["primary", "secondary"])
};

export default CustomButton;
import styled, { css } from "styled-components";
import Button from "@material-ui/core/Button";

export const CButton = styled(Button)`
  height: 80px;

  ${({ color }) =>
    color === "primary"
      ? css`
          background-color: green;
        `
      : color === "secondary" &&
        css`
          background-color: yellow;
        `}
`;

import React from "react";

import CustomButton from "./CustomButton";

const App = () => {
  return (
    <div>
      <div
        style={{
          marginBottom: "10px"
        }}
      >
        <CustomButton color="primary">Primary</CustomButton>
      </div>
      <div>
        <CustomButton color="secondary">Secondary</CustomButton>
      </div>
    </div>
  );
};

export default App;

在此处输入图片说明

你能告诉我如何让我的样式覆盖 ui-material 吗?

先感谢您。

克里斯·法默

看起来在 material-ui 文档中有一个关于如何执行此操作的很好示例:https : //material-ui.com/guides/interoperability/#styled-components

您似乎缺少的一件事是StylesProvider,它允许您的样式覆盖默认材质样式。这似乎有效......我不处理你的例子中的条件,但我认为这不是你的问题的一部分。

const MyStyledButton = styled(Button)`
  background-color: red;
  color: white;
`;

export default function App() {
  return (
    <StylesProvider injectFirst>
      <div className="App">
        <MyStyledButton color="primary">Foo</MyStyledButton>
      </div>
    </StylesProvider>
  );
}

这是一个代码和框:https ://codesandbox.io/s/infallible-khorana-gnejy

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

仅使用Material UI React Components的CSS

通过覆盖样式道具使Material-UI组件内联显示

如何在React中覆盖material-ui-next CSS?

如何在不使用MUIThemeProvider的情况下覆盖material-ui TextField组件的样式?

如何为Material-UI React组件覆盖@media CSS

在常规CSS上设置Material-UI TextField组件的underlineFocusStyle的样式

使用SASS类覆盖Material UI组件

如何覆盖深层嵌套组件的样式?(Material-UI Jss样式)

material-ui如何在JS中使用CSS设置滚动条样式?

React + Material UI-使用样式覆盖组件中的MuiTheme

如何覆盖Material UI工具提示的内联样式?

如何覆盖Material-UI Popover Paper样式?

如何忽略组件的Material UI主题样式?

ReactJS Material UI组件CSS类,澄清

如何在React中使用Material-ui将内联样式更改为CSS?

如何使用样式化组件和Material-UI为组件设置主题?

样式被Material-UI样式覆盖

如何从祖先覆盖嵌套的Material UI组件的样式?

试图从React Material Ui覆盖AppBar的css,

如何在create-react-app和Typescript中使用CSS模块覆盖Material-UI?

选中后如何覆盖Material-UI开关组件的样式?

如何覆盖 Material-UI Popover 样式?

修改或覆盖 material-ui 按钮 css

如何使用样式组件覆盖 CSS 类

如何使用组件内的 Material UI 在 CSS 中定位 input type=radio 元素?

使用 .css 文件操作 Material-UI

无法覆盖嵌套 Material UI 组件的样式

您可以使用 css/scss 覆盖 Material UI 中的样式吗?

Material-ui 中的 makeStyles 需要使用这么多(!important <= css 代码)来设置 mui 组件的样式