Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Material Icons with Styled Components

Just started playing around with Styled Components. Is there a way to style third party icons such as Material Design Icons? This is the code I have so far but obviously it isn't working. Relavant code is below Content component Thanks!

const MaterialIcon = (props) => <i className="material-icons">account_balance</i>;

const Icon = styled(MaterialIcon)`
  background-color: green;
  font-size: 50px;
`;

const CheckThisOut = props => (
  <Wrapper>
    <Header>
      <h5>{props.title}</h5>
      <Icon />
    </Header>
    <Divider />
    <Content>
      <p>5% of all participants in this campaign were first time users.</p>
    </Content>
  </Wrapper>
);

export default CheckThisOut;

enter image description here

like image 926
maxwellgover Avatar asked Aug 31 '17 01:08

maxwellgover


People also ask

How do you style material UI icons with styled-components?

Use the styled() method to style your Material UI components. The styled() method from styled-components takes as input a React component and outputs another React component with a different style. It is useful as it enables you to style your component using the exact same syntax as in your typical CSS stylesheet.

Can I use material UI icons?

MUI provides icons support in three ways: Standardized Material Icons exported as React components (SVG icons). With the SvgIcon component, a React wrapper for custom SVG icons. With the Icon component, a React wrapper for custom font icons.

Which is better material UI or styled-components?

Styled Components is similar to Material UI, but gives you the ability to build custom CSS components. Consider Material UI if you want to cut down production time by using a UI library with consistent designs and a vast community behind it.


1 Answers

For the styled(AnyComp) notation to work AnyComp needs to accept the incoming className prop and attach it to a DOM node.

For your example to work MaterialIcon has to use the passed in className, otherwise the styles are injected but no DOM node is targeted:

const MaterialIcon = (props) => (
  <i className={`material-icons ${props.className}`}>account_balance</i>
);

// WORKS 🎉
const Icon = styled(MaterialIcon)`
  background-color: green;
  font-size: 50px;
`;

See our documentation page about styling any component for more information!

like image 104
mxstbr Avatar answered Sep 25 '22 09:09

mxstbr