Skip to content

Proposal:HOC for composing styles in one place #522

Closed

Description

@svrcekmichal

Hey guys, today I wanted to style Link from react-router with styled-components and I didn't find any good solution how to handle className and activeClassName. I didn't like solution with defaultProps and neither i like random generated className as can be seen #184 . I've been little been hacking on top of your lib and I've create HOC for composing styles together

This is the example of HOC usage:

const StyledButton = composeStyled({
  className: css`
    color: white;
    margin: 10px;
    padding: 8px 16px;
    font-size: 14px;
    border: 1px solid ${props => props.color};
    border-radius: 4px;
    background-color: ${props => props.color}; 
  `,
  activeClassName: css`
    color: ${props => props.color};
    background-color: white;
  `
})(Link)

and here you can see the implementation:

const composeStyled = styles => WrappedComponent => Object.keys(styles).reduce((PreviousComponent, styleName) => {
  return styled(({className, ...rest}) => {
    const props = {...rest, [styleName]: className};
    return <PreviousComponent {...props} />
  })`
    ${styles[styleName]}
  `;
}, WrappedComponent);

You can see it live here:
http://www.webpackbin.com/Vk-lGmBFG

I hope you like it, if you want i can create PR with implementation. This solved my problem with Link styling and I hope you will like it too! :)

Activity

  1. svrcekmichal commented on Feb 22, 2017

    @svrcekmichal
    Author

    @mxstbr @geelen @patrick91 @relekang You're the most active contributors/owners, what do you think about my idea? I know you have your own business and are probably quite busy, I just want to know if you like it because if not, I will probably create small package with this HOC for people to use. Thanks for your time! 👍

  2. geelen commented on Feb 22, 2017

    @geelen
    Member

    This is a nice idea but we're hoping to provide native support for this exact use case in #509 (part of v2, in particular the attrs method described in #365).

    Glad to see you were able to get a HOC to do the same thing in the mean time, but yeah this use case is general enough that I think it'll be good to support it in core.

  3. geelen commented on Feb 22, 2017

    @geelen
    Member

    Oh, wait, sorry. That's not what #509 is doing—it's generating style blocks not classnames. Hmm, ok I'll have to have a bit more of a think about how this could fit in.

  4. reopened this on Feb 22, 2017
  5. svrcekmichal commented on Feb 23, 2017

    @svrcekmichal
    Author

    well, I hope I don't overrated my solution, but I believed this is the most pure solution with few lines of code for supporting 3rd party libs. The great part is that it's just styled-components, so it works great with all the props passed down and theming etc.

  6. GGAlanSmithee commented on Mar 6, 2017

    @GGAlanSmithee
    Member

    @svrcekmichal I think there are both pros and cons to your approach.

    As It doesn't require the user to learn new vocabulary (compared to attrs) it lessens the cognitive load, which is always a good thing (attrs is also a bit ambigious in the context of react and props IMO).

    What I don't like is that it forces the user to explicitly use className, which might be confusing, since it is not self-exploratory that the className styles are the ones applied by styled-components. This would, in some sense, require the user to know that the className is injected by styled-components, and one could argue that is a implementation detail.

    Given that attrs is in v2, I don think this addition to the API would be justified, since it doesn't cover a new use-case as far as I understand it.

  7. mxstbr commented on Dec 15, 2017

    @mxstbr
    Member

    I agree with @GGAlanSmithee, I don't really see a reason to add this API anymore since we're covering all the use cases now. Thanks for kicking off the discussion @svrcekmichal!

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