Using the docs/examples for overriding Material UI styling with styled-components, I\'ve managed to style the root and \"deeper elements\" within an ExpansionPanel
This difficulty is independent of using styled-components and just has to do with wrapping ExpansionPanelSummary
in another component.
You can similarly reproduce this with the following wrapping of ExpansionPanelSummary
:
const MyCustomSummary = props => {
return (
<ExpansionPanelSummary {...props} expandIcon={<ExpandMoreIcon />}>
<Typography>{props.text}</Typography>
</ExpansionPanelSummary>
);
};
There are several component groups like this where a Material-UI parent component looks for a particular type of child component and treats that child specially. For instance, you can find the following block in ExpansionPanel
if (isMuiElement(child, ['ExpansionPanelSummary'])) {
summary = React.cloneElement(child, {
disabled,
expanded,
onChange: this.handleChange,
});
return null;
}
Fortunately, Material-UI has a straightforward way to tell it that your custom component should be treated the same as a particular Material-UI component via the muiName
property:
MyCustomSummary.muiName = "ExpansionPanelSummary";
or in your case it would look like:
export const ExpansionPanelSummary = styled(props => (
<MUIExpansionPanelSummary
{...props}
/>
))``;
ExpansionPanelSummary.muiName = "ExpansionPanelSummary";