You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was chatting to @markdalgleish at Reactive conf and he had this great idea to implement a other-styled-component-selector.
The API would look like this:
constButton=styled.button`some: styles;`constBox=styled.box` > ${Button} { // A button component nested within a box will have these styles other: styles; }`
This would allow you to override styles of children, and from children react to parents, too! What do you think?
Reacted by Haz, Florian Didron, David Khourshid, Giuseppe, Jonas Kello, nhducit, Nicolae Vartolomei, Abdellah, Gertjan Reynaert, Jhey Tompkins and 59 more
I have some "complex" reusable components in a library. With "complex" I mean they contain multiple <select>, <input> etc. I would like importers of these components to be able to override each part of the complex component (eg. override the styles of one of the <select> inside the component). This suggestion seems like it would fit my needs. Just wanted to check if there is some way to handle this type of "complex" components already or is styled-component only usable for primitives like <Button>, <Input> etc at the moment?
If you construct a <X/> you get <div className='random-hash-123'/>, whereas if you construct a <X alt/> you get <div className='random-hash=456'/>. So you can't use X's generated classnames inside Y's rules.
You'd have to generate a globally unique classname that was attached to all instances of X that applied no rules by itself. But then, how do you generate that classname? Elsewhere we hash the contents of the CSS to produce a deterministic, order-independent hashed classname. Order-independence is important because we don't want the order of import statements in a project to affect the generated classnames. That's particularly important in a server-side rendered context where the generated classnames need to match on the client.
So, if someone can come up with a deterministic, order-independent random classname generator that can distinguish between const X = styled.div and const Y = styled.div we can do this. Otherwise, we'll have to drop the idea.
Also, any solution can't depend on running a babel transform or other build step. You can do cool things with babel transforms I just don't want to need them for anything in this project. Basically, my threshold is that styled-components needs to be able to work with Create React App without ejecting.
Reacted by Max Stoiber, Bond, Christian Broberg and Matt Lubner
Would it be possible to hash just the static portions of the styles to get a deterministic name? If your hashing mechanism is just operating on strings and can hash invalid CSS, it seems like this should work.
When processing the tagged template literal, you replace all of the dynamic things with a placeholder:
Any components that don't have dynamic values would end up generating the same hash. This could be solved by adding a static prefix or suffix before hashing.
I propose the following API. Since we have components now, i think that every component should only styles itself and its direct children like a good and happy family.
constStyledDad=styled.View`align-items: flex-start;`StyledDad.childrenStylist(child,index,children,parentProps,theme)=>{if(child.props.chosenOne){return'align-self: center'// merge this with the child styles}}
Bonus: this would allow to separate layout and move it to the parent.
Sub-components selection is a feature that I find is really missing from styled-component. It makes me stick to CSS modules for some of my, more complex, components. However using two styling technologies is in my opinion a real problem...
Reacted by R.G., Eisi Sig, Keith Halterman, Antoine Magny, Enoh Barbu, innocent diaz, Ralph Maamari and Taylor Buckner
@srosset81 can you post some examples of what you want to do with sub-component selectors? I get that they're useful but I do think there's usually a better alternative for most cases.
@geelen For example if you want to change the style of a sub-component when the mouse goes over its parent. This is a common pattern for CSS effects. If there is just one child, you can do a "> div" but it's not possible if there are many different children.
@geelen The way around this limitation is to give an ID to the component.
<RemoveButton id="RemoveButton"/>
How about giving the possibility to auto-generate this ID for every styled component? This way the hashed class names would stay unique, but it would be dead easy to select sub-components.
It would be really cool with some kind of official way to do this.
Right now I accomplish this by adding my own CSS class (in addition to the one generated by styled-components) to the children. I then use that class in the parent as a selector to apply different styles to the children based on the props that the parent receives. This feels really hackish though.
I really appreciate the work of the styled-components team, but this issue popped up shortly after we decided to use styled-components in our project.
Without any kind of component selector we see ourself either:
break component encapsulation by using html tag/class selectors in the parent component css
or have to introduce a bunch of intermediate styled-components, even for minimal adjustments like setting a margin
So we started to look into a possible solution. With/Without babel does not matter to us, because we have an existing webpack/babel build-chain anyways.
PS:
I would love to contribute a pull request to the styled-components core to support an even cleaner syntax by hiding the ".selector" attribute. As of 1.1.3 a function as interpolation value is always invoked. Honoring an existing ".selector" (or some more unique attribute name) inside the interpolation logic would allow the following syntax:
I would love to continue the discussion in this thread on this. Even if this is no solution for the core of styled-components, you are very welcome to use and contribute to it.
Reacted by Haz, Max Stoiber, R.G., Eisi Sig, Toke Voltelen, Namit Yadav, Simon Pythoud, Pedr Browne, Javier Castillo, 純 and 1 more
@jzimmek just need to land #227 then there'll be native support for this 😉 trying to figure out how to properly do it, appreciate the intermediary solution though, that's a pretty smart workaround!
The child selectors are also very useful for these purposes.
Reacted by Nick Janssen
Reacted by Jason Quense, Zephyr Yao, Namit Yadav, Alain Perkaz, Alexandre Borrego, Keith Halterman, Kyle Wagner, Rafael Pereira, Pedr Browne, Sean Curtis and 5 more
I am not convinced that styling children from the parent is something to encourage.
It's not something we will encourage, but that doesn't mean the possibility shouldn't be there. The same thing applies to nesting, I try to avoid nesting as much as possible and we could've made it impossible in styled-components, but it does have it fringe use cases and we want to cover all the ground.
Reacted by R.G., Maxime Quandalle, Evan Scott, Kostiantyn Ko, Andrew Appleby, Josh Hawkins, Jason Quense, Alexey Avdeev, Miguel Savignano, Toke Voltelen and 8 more
@jenyckee this is something we can elaborate on in the docs potentially (issues and PRs welcome 😄) but this issue might not be the right place to discuss it (Closed in January ! 😅)
We could have nested the [rule within the parent], but then we'd have to consider both sets of rules to understand why Icon behaves as it does.
So it's about encapsulation. We always recommend to strive to keep the styling for one component in one place, meaning with that component. This way you'll always immediately know where the styling is coming from: From that component. What sounds simple is actually an easy way to scale a project to a large team and codebase.
Activity
geelen commentedon Oct 27, 2016
I like it.
stephenkingsley commentedon Oct 29, 2016
just override the
Buttonwithin a box, not change theButton?I like it
jonaskello commentedon Oct 30, 2016
I have some "complex" reusable components in a library. With "complex" I mean they contain multiple
<select>,<input>etc. I would like importers of these components to be able to override each part of the complex component (eg. override the styles of one of the<select>inside the component). This suggestion seems like it would fit my needs. Just wanted to check if there is some way to handle this type of "complex" components already or is styled-component only usable for primitives like<Button>,<Input>etc at the moment?geelen commentedon Oct 31, 2016
After a bit more thought I... don't think this is really going to be possible. Or at least, easy enough to justify its addition.
Consider this:
If you construct a
<X/>you get<div className='random-hash-123'/>, whereas if you construct a<X alt/>you get<div className='random-hash=456'/>. So you can't useX's generated classnames insideY's rules.You'd have to generate a globally unique classname that was attached to all instances of X that applied no rules by itself. But then, how do you generate that classname? Elsewhere we hash the contents of the CSS to produce a deterministic, order-independent hashed classname. Order-independence is important because we don't want the order of
importstatements in a project to affect the generated classnames. That's particularly important in a server-side rendered context where the generated classnames need to match on the client.So, if someone can come up with a deterministic, order-independent random classname generator that can distinguish between
const X = styled.divandconst Y = styled.divwe can do this. Otherwise, we'll have to drop the idea.Also, any solution can't depend on running a babel transform or other build step. You can do cool things with babel transforms I just don't want to need them for anything in this project. Basically, my threshold is that styled-components needs to be able to work with Create React App without ejecting.
AsaAyers commentedon Nov 3, 2016
Would it be possible to hash just the static portions of the styles to get a deterministic name? If your hashing mechanism is just operating on strings and can hash invalid CSS, it seems like this should work.
When processing the tagged template literal, you replace all of the dynamic things with a placeholder:
Any components that don't have dynamic values would end up generating the same hash. This could be solved by adding a static prefix or suffix before hashing.
haikyuu commentedon Nov 13, 2016
I propose the following API. Since we have components now, i think that every component should only styles itself and its direct children like a good and happy family.
Bonus: this would allow to separate layout and move it to the parent.
srosset81 commentedon Nov 17, 2016
Sub-components selection is a feature that I find is really missing from styled-component. It makes me stick to CSS modules for some of my, more complex, components. However using two styling technologies is in my opinion a real problem...
geelen commentedon Nov 18, 2016
@srosset81 can you post some examples of what you want to do with sub-component selectors? I get that they're useful but I do think there's usually a better alternative for most cases.
srosset81 commentedon Nov 18, 2016
@geelen For example if you want to change the style of a sub-component when the mouse goes over its parent. This is a common pattern for CSS effects. If there is just one child, you can do a "> div" but it's not possible if there are many different children.
srosset81 commentedon Nov 18, 2016
@geelen The way around this limitation is to give an ID to the component.
How about giving the possibility to auto-generate this ID for every styled component? This way the hashed class names would stay unique, but it would be dead easy to select sub-components.
alexanbj commentedon Nov 18, 2016
It would be really cool with some kind of official way to do this.
Right now I accomplish this by adding my own CSS class (in addition to the one generated by styled-components) to the children. I then use that class in the parent as a selector to apply different styles to the children based on the props that the parent receives. This feels really hackish though.
jzimmek commentedon Dec 15, 2016
I really appreciate the work of the styled-components team, but this issue popped up shortly after we decided to use styled-components in our project.
Without any kind of component selector we see ourself either:
So we started to look into a possible solution. With/Without babel does not matter to us, because we have an existing webpack/babel build-chain anyways.
We created our own babel plugin:
babel-plugin-styled-components-selector
It is still very early stage, but already works for various use-cases and allows you to use the following syntax:
See the README for more examples.
PS:
I would love to contribute a pull request to the styled-components core to support an even cleaner syntax by hiding the ".selector" attribute. As of 1.1.3 a function as interpolation value is always invoked. Honoring an existing ".selector" (or some more unique attribute name) inside the interpolation logic would allow the following syntax:
I would love to continue the discussion in this thread on this. Even if this is no solution for the core of styled-components, you are very welcome to use and contribute to it.
mxstbr commentedon Dec 15, 2016
@jzimmek just need to land #227 then there'll be native support for this 😉 trying to figure out how to properly do it, appreciate the intermediary solution though, that's a pretty smart workaround!
2 remaining items
SachaG commentedon Jan 4, 2017
I was actually watching @geelen's talk, which recommends this very pattern (styling a child from a parent) for flexbox layouts, if I'm not mistaken.
I agree it'd be great to have an official way to do it with styled-components.
mxstbr commentedon Jan 18, 2017
As of landing #227 in the
v2branch, this is now possible. 🎉 As soon as v2 lands onnpmyou'll be able to use it.I'll close this issue so we don't clutter our milestone, please try the
v2branch on your projects and let us know if it breaks.wmertens commentedon Jan 31, 2017
I am not convinced that styling children from the parent is something to encourage.
In any case, when it is unavoidable, like when you are making a layout component based on flexbox, I do something like
The child selectors are also very useful for these purposes.
mxstbr commentedon Jan 31, 2017
It's not something we will encourage, but that doesn't mean the possibility shouldn't be there. The same thing applies to nesting, I try to avoid nesting as much as possible and we could've made it impossible in
styled-components, but it does have it fringe use cases and we want to cover all the ground.cheesytim commentedon Feb 8, 2017
just do it!
Migrate last of React-toggle styles into our app
Migrate last of React-toggle styles into our app
jenyckee commentedon Nov 13, 2017
Could you elaborate on why this is the case?
kitten commentedon Nov 13, 2017
@jenyckee this is something we can elaborate on in the docs potentially (issues and PRs welcome 😄) but this issue might not be the right place to discuss it (Closed in January ! 😅)
Check out what we're recommending on the site/docs right now for this pattern: https://www.styled-components.com/docs/faqs#can-i-refer-to-other-components
So it's about encapsulation. We always recommend to strive to keep the styling for one component in one place, meaning with that component. This way you'll always immediately know where the styling is coming from: From that component. What sounds simple is actually an easy way to scale a project to a large team and codebase.
mbrowne commentedon Feb 8, 2018
@kitten The "can-i-refer-to-other-components" question seems to have been removed from the FAQ. Is that because the recommended practice has changed?
quantizor commentedon Feb 8, 2018
mbrowne commentedon Feb 8, 2018
Thank you