3

I'm still a beginner with the ui-material. And I would like to custom my own Button Component with styled-component.

The problem is to override the css according to the button variations, for example if it is primary or secondary:

Here's my code into codesandbox.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;

enter image description here

Could you tell me how I can get my styling to override the ui-material?

Thank you in advance.

7
  • It's unhelpful having snippets that don't run. Can you create a codesandbox that runs? Commented Jul 4, 2021 at 20:37
  • Hey Chris, I'm sorry, I updated my code with codesandbox Commented Jul 4, 2021 at 20:58
  • 1
    @Tainara here is a working [codesandbox.io/s/material-ui-with-styled-components-e9fr5?file=/… of the way you're trying to create a custom component. You need to increase the specificity to make the styles working with material ui components. As Chris mentioned, there is a nice example of the [material-ui.com/guides/interoperability/… UI website. Commented Jul 4, 2021 at 21:04
  • Hey Junaid, that's work for me, thank you Commented Jul 4, 2021 at 21:05
  • Material UI docs have some examples on how to customize the components using different approaches. Commented Jul 4, 2021 at 21:07

1 Answer 1

6

It looks like there's a nice example on how to do this in the material-ui docs: https://material-ui.com/guides/interoperability/#styled-components

One thing you seem to be missing is the StylesProvider, which allows your styles to override the default material styles. This seems to work... I don't deal with the conditional in your example, but I don't think that's part of your problem here.

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

Here's a codesandbox: https://codesandbox.io/s/infallible-khorana-gnejy

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Chris, thank you, but how should I handle variations? How primary and secondary?
import { StyledEngineProvider } from '@mui/material' for latest version of material ui

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.