I'm attempting to use the Material UI styled components API to inject both a custom theme, and some props to a particular custom element.
I have either a custom theme or some props working, but not both together
Custom Theme
My custom theme is defined as follows in a separate file...
export interface ITheme extends Theme {
sidebar: SideBarTheme;
}
type SideBarTheme = {
// Various properties
};
const theme = createMuiTheme(
{
// Normal theme modifications
},
{
sidebar: {
// Custom theme modifications
},
} as ITheme
);
Which I then use with the Material-UI styled component API as follows....
const Profile = styled(Button)(({ theme }: { theme: ITheme }) => ({
backgroundColor: theme.sidebar.profile.backgroundColor,
paddingTop: 20,
paddingBottom: 20
}));
This works fine within my jsx with <Profile />.
Props
For props I'm essentially using an exact copy of the example here
interface MyButtonProps {
color: string;
}
const Profile = styled( ({ color, ...other }: MyButtonProps & Omit<ButtonProps, keyof MyButtonProps>) => (
<Button {...other} />
))({
backgroundColor: (props: MyButtonProps) => props.color,
paddingTop: 20,
paddingBottom: 20
});
This works fine within my jsx when using <Profile color="red"/>
How do I get both of these working together with typescript?
I literally tried combining them like below but the props don't pass down...
const Profile = styled( ({ color, ...other }: MyButtonProps & Omit<ButtonProps, keyof MyButtonProps>) => (
<Button {...other} />
))(({ theme }: { theme: ITheme }) => ({
backgroundColor: (props: MyButtonProps) => props.color,
color: theme.sidebar.profile.backgroundColor,
paddingTop: 20,
paddingBottom: 20
}));
Anything else I try just upsets TypeScript. The material-ui styled function is also too complicated for me to understand.
Many thanks,