question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Add support for transient props with the styled API?

See original GitHub issue

Summary šŸ’”

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:open
  • Created 2 years ago
  • Reactions:24
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
oliviertassinaricommented, Aug 24, 2021

@eps1lon Agree, I have added the ā€œwaiting for upvotesā€.

The alternative answer could be to say to the developers: swap the engine to use styled-components, you will get this $ behavior.

Proof that this workaround is working: https://codesandbox.io/s/styled-components-forked-wu7ol?file=/src/demo.js

import * as React from "react";
import { styled } from "@material-ui/core/styles";

const StyledDiv = styled("div")`
  color: ${(props) => props.$color};
`;

export default function StyledComponents() {
  return <StyledDiv $color="green">Transiant color prop</StyledDiv>;
}
4reactions
nathanlambertcommented, Jul 20, 2022

Currently Iā€™m just using a util:

import { CreateStyled } from "@emotion/styled";

export const withTransientProps: Parameters<CreateStyled>[1] = {
  shouldForwardProp: (propName: string) => !propName.startsWith("$"),
};

and then doing this:

const SomeComponent = styled(
  "div",
  withTransientProps
)<{ $disabled: boolean; $textColor: string }>(
  ({ theme, $disabled, $textColor }) => css`
    margin: ${theme.spacing(1)};
    color: ${$textColor};
    :hover {
      background-color: ${$disabled
        ? theme.palette.grey[600]
        : theme.palette.background.paper};
    }
  `
);

<SomeComponent $textColor="#808" $disabled />

May not be beautiful, but it works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Reference
This is a more dynamic, granular filtering mechanism than transient props. It's handy in situations where multiple higher-order components are beingĀ ...
Read more >
mui/system -- how to pass transient props to styled()
I'm using import { styled } from "@mui/system" ; like so: const Column = styled("div")<ColumnProps>` background: ${(props) => props.Ā ...
Read more >
Transient Props in styled-components | by Jake McCambley
Overview: Transient Props allow you to pass boolean value props to styled-components such that they do not render as an element's attributesĀ ...
Read more >
Using styled-components in React
The styled API allows us to create a StyledComponent ā€” a ... props or HTML attributes to be added to a component using...
Read more >
tailwind-styled-components
First, install Tailwind CSS IntelliSense VSCode extension ... Tailwind Styled Components supports Transient Props.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found