Skip to content

Allow mapping and filtering props #411

Closed

Description

@wmertens

When you do something like

const RoundOrNot = styled.div`
  border-radius: ${p => p.round && '50%'};
  border-color: ${p => p.round && 'red'};
  background-color: ${p => p.round && 'orange'};
`

then the round property will end up on the div. You can make a custom component that filters out the round from props, but it would be nicer to write e.g.

const RoundOrNot = styled.ownProps('round').div`
  border-radius: ${p => p.round && '50%'};
  border-color: ${p => p.round && 'red'};
  background-color: ${p => p.round && 'orange'};
`

Furthermore, perhaps it would be nice to pre-calculate something that is used in a bunch of rules, like:

const RoundOrNot = styled
.ownProps('round')
.map(p => p.round ? {rad: '50%', c: 'red', b: 'orange'} : p)
.div`
  border-radius: ${d => d.rad};
  border-color: ${d => d.c};
  background-color: ${d => d.b};
`

(in the above p is short for props, d for derived, and I'm relying on the fact that empty or false attributes result in invalid and ignored rules)

Activity

  1. k15a commented on Jan 26, 2017

    @k15a
    Member

    Properties don't end up on DOM elements unless they are valid.
    You can find the code here: StyledComponent.js#L91
    You may check out #365 which solves you second problem.

  2. mxstbr commented on Jan 26, 2017

    @mxstbr
    Member

    We're thinking about how to solve this to get rid of the custom whitelist, actually, just haven't found any nice API yet.

  3. wmertens commented on Jan 26, 2017

    @wmertens
    ContributorAuthor

    Aha, I couldn't find that issue in my searches.

    However, there is still a problem: If a prop is a valid attribute but is not meant as one, it should not be passed. For example width and label.

  4. diegohaz commented on Jan 27, 2017

    @diegohaz
    Member

    @wmertens We've discussed that in #305

    The current solution is this:

    export const StyledComponent = styled(({ width, label, ...rest }) => <div {...rest} />)
  5. wmertens commented on Jan 27, 2017

    @wmertens
    ContributorAuthor
  6. NathanielHill commented on Mar 31, 2018

    @NathanielHill

    How would one do this when using withComponent()? Seems to wipe out any filtering in place.

    For example, if I have this component:

    const WorksFine = styled(({
        children,
        customProp
        ...rest}) => (<Link {...rest}>{children}</Link>)
      )`
        // customProp used
    `
    

    and then I want to modify it slightly, like so:

    const NoWorky = WorksFine.withComponent(DifferentLinkComponent)
    

    I will get console errors as customProp is now passed down to component NoWorky.

    I'd rather not have to choose between unfiltered props and code duplication.

  7. holloway commented on Dec 4, 2018

    @holloway

    Is this solved yet in Styled Components 4?

    I guess the issue is distinguishing between what should be passed on and what shouldn't, so we need to namespace individual props while avoiding regressions. How about a naming convention based on private class properties having the # prefix?

    before

    export const CardOption = styled(({ lastItem, ...rest }) => <Link {...rest} />)`
      width: 100%;
      ${props =>
        props.lastItem && `margin-bottom: 0;`
      };
    `;
    

    after

    export const CardOption = styled(Link)`
      width: 100%;
      ${props =>
        props['#lastItem'] && `margin-bottom: 0;`
      };
    `;
    
  8. anikolaev commented on Apr 19, 2019

    @anikolaev

    One more solution. Add styledProps object property and filter it in the wrapper of styled().

    // in utils:
    const styledWithProps = Comp => (...parts) => styled(({styledProps, ...props}) => <Comp {...props}/>).apply(null, parts)
    
    // in the component:
    const StyledTest = styledWithProps(Test)`
      color: ${props => props.styledProps.isRed ? 'red': 'black'};
    `;
    ...
    <StyledTest
      testProp="test"
      styledProps={{
        isRed: true
      }}
    />

    Demo: https://codesandbox.io/s/ywl9mxrmk9
    Pro: auxiliary code can be moved to separate file, clear syntax, custom props are separated from own props of the styled component.
    Cons: if property is not defined there will be error.

  9. lcswillems commented on Nov 15, 2019

    @lcswillems

    I have just written an RFC for filtering forwarded props: #2878

    Would you like a solution like this?

    TL;DR It consists of adding a second parameter for filtering props. The current solution:

    const StyledDiv = styled(({height, width, ...props}) => <div {...props} />)`
        width: ${props => props.width}px;
        height: ${props => props.height}px;
    `

    is turned into:

    const StyledDiv = styled.div(`
        width: ${props => props.width}px;
        height: ${props => props.height}px;
    `, ({width, height, ...props}) => props);

    This is easy to write and to understand, and props filtering (which is not the important part of the definition) is sent at the end of the definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions