Add support for transient props with the styled API?
See original GitHub issueSummary š”
Our team is using MUI / typescript / styled, and weād like to write styles as display: flex;
, not JS display: "flex",
, so weāve opted to use MUIās experimentalStyled
, but weāre seeing some errors with custom props and our solution is getting ugly. I believe it would be solved by transient props.
Motivation š¦
When we want to use custom props like so:
const MarqueeItems = styled(Box)<{ animate: boolean }>(
({ theme, animate }) => css`
animation: ${animate ? "marquee 10s linear infinite" : "none"};
margin-bottom: ${theme.spacing(2)};
`
);
<MarqueeItems animate={isMobile} />
They work great, but props like animate
get passed through to the DOM and cause errors.
Weāve been able to fix this by wrapping our components like so:
const MarqueeItems = styled(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
({ animate, ...rest }: { animate: boolean } & BoxProps) => <Box {...rest} />
)(
({ theme, animate }) => css`
animation: ${animate ? "marquee 10s linear infinite" : "none"};
margin-bottom: ${theme.spacing(2)};
`
);
<MarqueeItems animate={isMobile} />
But this is getting pretty ugly.
Examples š
Styled came up with a solution to this problem via transient props in styled 5.1, but they donāt seem to be working in MUI.
Iām pretty sure it would look something like this:
const MarqueeItems = styled(Box)<{ $animate: boolean }>(
({ theme, $animate }) => css`
animation: ${$animate ? "marquee 10s linear infinite" : "none"};
margin-bottom: ${theme.spacing(2)};
`
);
<MarqueeItems $animate={isMobile} />
Alternatively Iām open to a better, existing way to do this without transient props. š
Issue Analytics
- State:
- Created 2 years ago
- Reactions:24
- Comments:10 (6 by maintainers)
Top GitHub Comments
@eps1lon Agree, I have added the āwaiting for upvotesā.
Proof that this workaround is working: https://codesandbox.io/s/styled-components-forked-wu7ol?file=/src/demo.js
Currently Iām just using a util:
and then doing this:
May not be beautiful, but it works.