Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add official support for createStyled? #11047

Closed
gnapse opened this issue Apr 17, 2018 · 14 comments
Closed

Add official support for createStyled? #11047

gnapse opened this issue Apr 17, 2018 · 14 comments
Labels
new feature New feature or request

Comments

@gnapse
Copy link
Contributor

gnapse commented Apr 17, 2018

I was wondering if this library could provide (or if it already does) a component that would allow to use styles with a render prop pattern, instead of the withStyles HOC.

I searched for it, and the best I could find was this, but that's not quite what I would've expected. That option would need users of this api to create a new component every time:

import { createStyled } from '???';

const Styled = createStyled({ /* styles go in here */ })

// It is supposed to be used like this:
render() {
  return (
    <Styled>
      {({ classes }) => (
        // render your thing here
      )}
    </Styled>
  );
}

I wonder if it would be a bit better or even possible to have a generic component that receives styles and provides classes, instead of having to create one component tied to specific styles:

// would be something provided by this lib
import Styled from 'material-ui/Styled';

const styles = { /* styles go in here */ }

// Then use it like this:
render() {
  return (
    <Styled styles={styles}>
      {({ classes }) => (
        // render your thing here
      )}
    </Styled>
  );
}

Also, as a side note, even if this suggestion is not accepted, I wonder why the createStyled helper is not provided as an export of this library, just as withStyles is.

@oliviertassinari
Copy link
Member

@gnapse Is it a duplicate of #7633?

@gnapse
Copy link
Contributor Author

gnapse commented Apr 17, 2018

@oliviertassinari at a quick glance it does not seem so. The title of that issue is not immediately suggesting of having anything to do with a render props, the phrase "render prop" does not appear anywhere in that page, and I do not see any single code example showing a render prop pattern to access classes.

@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 17, 2018

@gnapse Ok thanks. Going back to your suggestion.

I wonder if it would be a bit better or even possible to have a generic component that receives styles and provides classes, instead of having to create one component tied to specific styles:

I fear the Styled component has to be created outside of the render method so the CSS injection order is correct. It's a technical limitation. I can't think of a simple workaround. You will find styled-components relying on the same solution.

@gnapse
Copy link
Contributor Author

gnapse commented Apr 17, 2018

Ok, yes, I totally get it.

Now again, back to my fallback suggestion here: why createStyled (as it appears here) isn't provided by this library out of the box, just as withStyles is?

@oliviertassinari oliviertassinari changed the title Add a render props component to provide styles Add official support for createStyled? Apr 17, 2018
@oliviertassinari
Copy link
Member

oliviertassinari commented Apr 17, 2018

why createStyled (as it appears here) isn't provided by this library out of the box, just as withStyles is?

@gnapse The initial motivation was: it's working, but it's an extra API to support. Let's document it and see how it's used in the wild. Now, as people start using it and rely on it in production with no issue, I'm more confidence integrating it into the core. I have no real objection to it. It just hasn't been a priority so far. You know, extending the API comes with overhead: documentation, tests, maintenance, etc.

@gnapse
Copy link
Contributor Author

gnapse commented Apr 17, 2018

Yes, sure. I get your point. Thanks for your input on this.

@gnapse gnapse closed this as completed Apr 17, 2018
@oliviertassinari
Copy link
Member

@gnapse I'm reopening as it's a good question. I don't have a definitive answer.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jun 18, 2018

@jcoreio Has published the package under https://github.com/jcoreio/material-ui-render-props-styles. Maybe we could document it?

At some point, it might be good to move @material-ui/core/styles into its own package: @material-ui/styles.

@oliviertassinari
Copy link
Member

I have added material-ui-render-props-styles of @jedwards1211 in the documentation with #11983. This should do it for now :).

@jedwards1211
Copy link
Contributor

Awesome, thanks! I guess I don't get notifications for jcoreio for some reason

@jedwards1211
Copy link
Contributor

Let me know if you want to move it to the @material-ui npm organization.

@oliviertassinari
Copy link
Member

@jedwards1211 Sure, I will let you know if we want to move the project into the npm organization.

@gnapse
Copy link
Contributor Author

gnapse commented Jul 9, 2018

This is awesome. Thanks!

@rosskevin
Copy link
Member

Just an FYI - @oliviertassinari we had a createStyled as part of our internal ui lib, reused in several apps. It presents load order issues, but copying the createStyled.ts to the app src alleviates the issue. Definitely a tricky one. I am not sure how the linked project could be making this work - I suspect it will have the same potential problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants