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;
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.
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.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With