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.

Ability to set default shouldForwardProp or expose to extended components

See original GitHub issue

UPDATED TO REFLECT MORE ACCURATE ISSUE

The problem

I would love the ability to set a default shouldForwardProp value, project wide by either a simplistic factory extension or via a theme. Currently when I set the shouldForwardProp value on a styled div it works great. However if I extend that styled component to form another one leveraging the withComponent helper, that shouldForwardProp value is not exposed to that new styled component and I need to set it again.

ie:

import { css } from '@emotion/core';
import styled from '@emotion/styled';
import * as styledSystem from 'styled-system';
import { COLORS, fontAR, shouldForwardProp } from '../../constants/styles';

const baseStyle = css`
  ${fontAR};
  font-size: 14px;
  line-height: 1.43;
`;

const smallStyle = css`
  font-size: 12px;
  line-height: 1.67;
`;

const largeStyle = css`
  font-size: 16px;
  line-height: 1.91;
`;

const boldStyle = css`
  font-family: Avenir;
  font-weight: 900;
`;

const styleMap = {
  small: smallStyle,
  large: largeStyle,
  bold: boldStyle,
};

interface ParagraphProps
  extends styledSystem.ColorProps,
    styledSystem.FontFamilyProps,
    styledSystem.FontSizeProps,
    styledSystem.FontWeightProps,
    styledSystem.HeightProps,
    styledSystem.LetterSpacingProps,
    styledSystem.LineHeightProps,
    styledSystem.SpaceProps,
    styledSystem.TextAlignProps,
    styledSystem.WidthProps {
  color?: COLORS;
  size?: string;
}

export const Paragraph = styled('p', { shouldForwardProp })<ParagraphProps>`
  ${baseStyle}
  ${styledSystem.color}
  ${styledSystem.fontFamily}
  ${styledSystem.fontSize};
  ${styledSystem.fontWeight}
  ${styledSystem.height}
  ${styledSystem.letterSpacing}
  ${styledSystem.lineHeight}
  ${styledSystem.space}
  ${styledSystem.textAlign}
  ${styledSystem.width}
  ${props => (props.size ? styleMap[props.size] : null)}
`;

const Label = styled(Paragraph)`
  color: ${COLORS.slate};
  display: flex;
  align-items: center;
  margin-bottom: 4px;
`.withComponent('label');

Now Label does not know to exclude any props that are a result of the imported function which works as intended in the Paragraph component.

Proposed solution

  • Add the ability in the core code to consume the underlying shouldForwardProp value if a styled component is extending another styled component combined with withComponent.

It works if you use the as prop but my intention is to not need to remember to add that additional prop anytime I want to use the new Label component

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Andaristcommented, Sep 4, 2020

This issue got fixed by https://github.com/emotion-js/emotion/pull/1668 and the fix is going to be a part of the upcoming v11.

0reactions
Andaristcommented, Jun 17, 2019

Ok, I see what’s happening now. When using withComponent this line “fails” and things are not being merged like you might have expected.

Need to think about how this and other options are handled when extending components

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the purpose of shouldForwardProp option in styled()?
This is why shouldForwardProp exists, to prevent styling props from being passed down and create invalid attribute:
Read more >
TypeScript
Emotion allows you to specify a shouldForwardProp hook to filter properties which should be passed to the wrapped component. If you make shouldForwardProp...
Read more >
Releases
attrs() this becomes a very powerful constellation of abilities. Here's how you use it: const Comp = styled('div').withConfig({ shouldForwardProp: (prop, ...
Read more >
TypeScript
Emotion allows you to specify a shouldForwardProp hook to filter properties which should be passed to the wrapped component. If you make should ......
Read more >
styled-components | Yarn - Package Manager
Add babel macro for full-featured interop with create react app v2+, by @lucleray (see #2032) · Expose ThemeConsumer component, context consumer render prop ......
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