As of Dec 2022, the example code in the original question works for me using Styled Components in React.
I found this question when searching for a way to specify a default StyleComponents theme. This is how I did it:
interface ITheme {
color: string;
}
const defaultTheme: ITheme = {
color: 'blue';
}
interface IButton {
theme?: Theme;
}
export const Button = styled('button')<IButton>`
color: ${({ theme }) => theme.color};
`;
Prose.defaultProps = {
theme: defaultTheme,
};
Using the above example I can use the button with and without a StyledComponents ThemeProvider
.
With ThemeProvider
:
<ThemeProvider theme={{ color: 'red' }}>
<Button>I am a red button</Button>
</ThemeProvider>
Without ThemeProvider
:
<Button>I am a blue button</Button>
There is an example of this -- though not in TypeScript -- in the Advanced docs's "Theming" code example: https://styled-components.com/docs/advanced#theming
Also -- unless the example is contrived to use defaultProps
-- there is a simpler way to have a default color in the question's example without using defaultProps
:
export interface IButton {
variant: 'action' | 'secondary';
}
export const Button = styled('button')<IButton>`
background-color: #fff;
color: ${({ variant }) => (variant === 'secondary') ? 'gray' : 'blue' };
`;