styled-components icon indicating copy to clipboard operation
styled-components copied to clipboard

styled-components v.6 and framer-motion (and other libraries) props API "unknown prop" warning when used with `styled()` or `as` API

Open SirMoustache opened this issue 1 year ago • 8 comments

Hey, I'm trying to update to styled-components v.6 from v.5 and I'm getting the next warning in the console:

styled-components: it looks like an unknown prop "layout" is being sent through to the DOM, which will likely trigger a React console error. If you would like automatic filtering of unknown props, you can opt-into that behavior via `<StyleSheetManager shouldForwardProp={...}>` (connect an API like `@emotion/is-prop-valid`) or consider using transient props (`$` prefix for automatic filtering.)

styled-components: it looks like an unknown prop "transition" is being sent through to the DOM, which will likely trigger a React console error. If you would like automatic filtering of unknown props, you can opt-into that behavior via `<StyleSheetManager shouldForwardProp={...}>` (connect an API like `@emotion/is-prop-valid`) or consider using transient props (`$` prefix for automatic filtering.)

Here is an example of usage

export type HeaderProps = {
  $variant: HeaderVariant;
  $color: ColorScheme;
  $position?: HeaderPosition;
};

export const Header = styled(motion.header)<HeaderProps>`
  z-index: 7;
  border-bottom: 1px solid #e9e9e7;
  transition: background 300ms ease-in-out;

  ${({ $position: position }) => position && headerPositionMap[position]};
  ${({ $variant: variant }) => variant && headerVariantMap[variant]};

  ${({ theme }) => theme.media.min('xl')} {
    height: auto;
  }
`;

const Layout = () => {
// ...
<>
  <Header
        $position={headerPosition}
        $variant={headerVariant}
        $color={colorScheme}
        layout="position"
        transition={{ duration: 0.2, type: 'tween' }}
      >
      {/* ...  */}
  </Header>
</>
}

The problem here is that layout and transition props are part of the framer-motion API and I can't use transient props, or filter them withConfig, because it's breaking component behavior:

export const Header = styled(motion.header).withConfig({
  // this will not just filter from the dom, but will not pass to the wrapped component completely
  shouldForwardProp: (prop) => !['layout', 'transition'].includes(prop),
})<HeaderProps>`
  z-index: 7;
  border-bottom: 1px solid #e9e9e7;
  transition: background 300ms ease-in-out;

  ${({ $position: position }) => position && headerPositionMap[position]};
  ${({ $variant: variant }) => variant && headerVariantMap[variant]};

  ${({ theme }) => theme.media.min('xl')} {
    height: auto;
  }
`;

And it's not only framer-motion, I'm having the same problems with the routing component, for example here route data is passed as field prop and part of the routing framework API, which gives the same warning.

const Layout = () => {
// ...
<>
        <NavigationLink
            as={Link}
            field={navItem.fields?.link}
            $isActive={isMatched}
            disabled={isDisabled}
            $color={colorScheme}
          >
            {/* ...  */}
          </NavigationLink>
</>
}

Is there a way to disable those warnings? Because there is no actual error because those libraries not passing those props to the DOM

SirMoustache avatar Jul 05 '23 10:07 SirMoustache

same issue here, using mui v5.

export const RCPasswordField = styled(({ InputProps, ...rest }) => <TextField {...rest} InputProps={InputProps} />)``

image

tried transient props, is-prop-valid from @emotion, withConfig and nothing solved. still having this issue.

lucasgdb avatar Jul 07 '23 01:07 lucasgdb

Experiencing the same issue with Radix and the asChild prop

jonsadka avatar Jul 07 '23 21:07 jonsadka

I get the warning with any prop I add to a styled component:


const Container = styled.div<{ givesTheWarning: boolean }>``

type Props = { givesTheWarning: boolean }
const Component = ({ givesTheWarning }: Props) => {
    // gives a warning 
    return <Container givesTheWarning={givesTheWarning} />
}

This only happens when it's in my next app. When I ran it in a separate project that's just a react library, I did not get these warnings

giannif avatar Jul 31 '23 22:07 giannif

Same issue with framer-motion and style-modifiers. Had to roll back to v5 until there’s a solution for this.

jstevens79 avatar Aug 17 '23 23:08 jstevens79

I also encountered the same error. I wanted to add a property to the build to receive the colors in the network request and use it in the styled components. Then the warning l appeared, and I wanted to know how to eliminate this warning )_FZ86 KZLPYVSQ_G(G$XQC }9SD{89LLB86~ 4T@7 K B](https://github.com/styled-components/styled-components/assets/76727042/e5923a31-23fa-454a-8072-1e936a04fcbc) ![IHHR48DF3_~Q@% 9{S$C0Y

Liu0520 avatar Oct 07 '23 16:10 Liu0520

I also encountered the same error. I wanted to add a property to the build to receive the colors in the network request and use it in the styled components. Then the warning l appeared, and I wanted to know how to eliminate this warning )_FZ86 KZLPYVSQ_G(G$XQC }9SD{89LLB86~ 4T@7 K B IHHR48DF3_~Q@% 9{S$C0Y

You can try this:

<ItemWrapper $verifycolor={itemData?.verify_info?/text_color || "#39576a"}>

In Your ItemWrapper Styled-component :

export const ItemWrapper = styled.div`
    color: ${({ $verifycolor }) => $verifycolor};
`

Crzek avatar Oct 10 '23 22:10 Crzek

Surprised theres no solution for this yet. Same issue here.

kinoli avatar Oct 31 '23 22:10 kinoli