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
react-router's <Link> component and react-modal2's <Modal> component accept additional classNames via separate properties. e.g. <Link> accepts activeClassName and <Modal> accepts backdropClassName and modalClassName. It would be great to have a way to style these using styled-components. Perhaps we could have something like:
Hmm, that Link example should already work because :hover and :active are valid CSS pseudo selectors. But as for passing two classnames... I'm not sure that there's a nice way to do that.
Perhaps we need a className helper that works a bit like keyframes?
I've wanted to be able to do this occasionally when you have one or two styles to apply somewhere and you don't want to name a whole new component. There's probably a better name tho. @mxstbr what do you think?
The issue is that activeClassName for the Link refers to the route of the Link being active, not somebody clicking on it. This can't just be replicated with :active I'm pretty sure.
I really really dislike the className thing. It breaks the whole premise of styled-components, I feel like there has to be a better escape hatch for the above case?
This is why I like the idea of :active being the true pseudo-class and .active targeting the activeClassName property. I would be fine with us using an alternative prefix other than . if people prefer.
Ah I see yeah I misread &.active as &:active in the original example. To be honest, I actually think turning &.active into activeClassName is really clever, but it's already a valid thing to do so I don't want to overload it.
I suppose you could do &:pass-as-prop(activeClassName) { ... } or something else wacky like that. But tbh that's starting to put a lot of magic in there for an API that... is kinda clunky.
IMO React Router could add a data-active attribute instead of applying a different classname, then we could do:
Reacted by Forbes Lindesay, Max Stoiber, Rudin Swagerman, Tommaso Ferrari, kylesavant, Ryan De La Torre, Filipp Riabchun, Michael, Julien Bouquillon, Loïc Mahieu and 5 more
The [data-active] is a neat solution for the react-router example (if the react-router team would accept the addition of the data-attribute). I don't think it solves the modal example where you would want to style multiple separate child components.
I think this could even be used for third-party component libraries:
// lib/Modal.js// Use this class in the Modal component, notice the dot, that way users don't// have to be aware that it's even a classconstbackdrop='.__my_lib__modal_backdrop__do__not__touch__'classModalextendsReact.Component{/* ... */}export{backdrop}exportdefaultModal
@geelen If styled-components would go with the generated classNames approach from above, it might not be a bad solution to include a classname generator in styled-components that could take advantage of the better development classnames
This is interesting. It relies on the className provided by styled-components being added to the top most html element rendered by the component being styled, but that's probably true in most cases and seems reasonable.
It does seem like a lot of hoops to jump through, but I can see the desire to keep styled-components' API surface as small as possible.
As it turns out, this is an actual problem in ReactNative. Some RN primitives have props that expect styles to be passed in. (not just a value, so this is different from #149; thanks to @brunolemos for pointing that out!)
ScrollView as an example has a contentContainerStyle prop which applies the styles one passes in "to the scroll view content container which wraps all of the child views.".
The generateClassName solution doesn't work here, as there are no class names in ReactNative. 😕
We might need to add support for passing arbitrary styles to a prop of the underlying primitive in core?
Just one more comment when discussing the Link item. If passing an invalid html prop (i,e one called inactive) you will get an error in the console due to this being passed all the way down to the Link itself
I think this could be hidden behind some sort of helper:
styled(someHelper(BaseNavItem,'activeClassName'))
Reacted by Bruno Lemos, Michael Romanenko, Colin Ramsay, Carlo Zen, Lars Hampe, Forbes Lindesay, Adrian Li, John Mitchell, Nick Farina, Julien Bouquillon and 5 more
In case anyone wants it, here's an implementation of the helper described by @amccloud. Caveat, I'm using flow and ramda. You can obviously remove the flow annotations and translate omit to your implementation of choice.
Reacted by Aaron McAdam, Rob Hilgefort, Andrey Litvinov, Michael Nigh, Karolis Grinkevičius, Saranraj, Jake Peyser, Max Donchenko, sw331, Josh Leonard and 1 more
Reacted by sw331
Reacted by Saranraj and Max Donchenko
Reacted by Jeremy Grancher, Peter, Andrey Litvinov, Michael Nigh, Karolis Grinkevičius, Saranraj, Max Donchenko and M
I'm going to lock this issue now. Everything that needs to be said has been said and it's unnecessary to notify everybody for problems related to your own codebase.
If you are having problems, please go to StackOverflow or open a new issue.
Activity
geelen commentedon Nov 12, 2016
Hmm, that
Linkexample should already work because:hoverand:activeare valid CSS pseudo selectors. But as for passing two classnames... I'm not sure that there's a nice way to do that.Perhaps we need a
classNamehelper that works a bit likekeyframes?This wouldn't work with function interpolations but might be a useful alternate. Plus, it would let you do things like Glamor:
I've wanted to be able to do this occasionally when you have one or two styles to apply somewhere and you don't want to name a whole new component. There's probably a better name tho. @mxstbr what do you think?
mxstbr commentedon Nov 12, 2016
The issue is that
activeClassNamefor theLinkrefers to the route of theLinkbeing active, not somebody clicking on it. This can't just be replicated with:activeI'm pretty sure.I really really dislike the
classNamething. It breaks the whole premise ofstyled-components, I feel like there has to be a better escape hatch for the above case?ForbesLindesay commentedon Nov 13, 2016
This is why I like the idea of
:activebeing the true pseudo-class and.activetargeting theactiveClassNameproperty. I would be fine with us using an alternative prefix other than.if people prefer.Another option might be something like:
geelen commentedon Nov 13, 2016
Ah I see yeah I misread
&.activeas&:activein the original example. To be honest, I actually think turning&.activeintoactiveClassNameis really clever, but it's already a valid thing to do so I don't want to overload it.I suppose you could do
&:pass-as-prop(activeClassName) { ... }or something else wacky like that. But tbh that's starting to put a lot of magic in there for an API that... is kinda clunky.IMO React Router could add a
data-activeattribute instead of applying a different classname, then we could do:Lol, actually you can do this:
😂
ForbesLindesay commentedon Nov 14, 2016
The
[data-active]is a neat solution for the react-router example (if the react-router team would accept the addition of the data-attribute). I don't think it solves the modal example where you would want to style multiple separate child components.geelen commentedon Nov 14, 2016
Sure it would!
mxstbr commentedon Nov 15, 2016
I think this could even be used for third-party component libraries:
Kind of like a fake child component selector 👌
GertjanReynaert commentedon Nov 15, 2016
@geelen If styled-components would go with the generated classNames approach from above, it might not be a bad solution to include a classname generator in styled-components that could take advantage of the better development classnames
ForbesLindesay commentedon Nov 15, 2016
This is interesting. It relies on the
classNameprovided by styled-components being added to the top most html element rendered by the component being styled, but that's probably true in most cases and seems reasonable.It does seem like a lot of hoops to jump through, but I can see the desire to keep styled-components' API surface as small as possible.
mxstbr commentedon Nov 20, 2016
As it turns out, this is an actual problem in ReactNative. Some RN primitives have props that expect styles to be passed in. (not just a value, so this is different from #149; thanks to @brunolemos for pointing that out!)
ScrollViewas an example has acontentContainerStyleprop which applies the styles one passes in "to the scroll view content container which wraps all of the child views.".The
generateClassNamesolution doesn't work here, as there are no class names in ReactNative. 😕We might need to add support for passing arbitrary styles to a prop of the underlying primitive in core?
jontro commentedon Nov 30, 2016
Just one more comment when discussing the Link item. If passing an invalid html prop (i,e one called inactive) you will get an error in the console due to this being passed all the way down to the Link itself
jontro commentedon Dec 2, 2016
Another use case for multiple class names is
<ReactCSSTransitionGroup>which takes multiple classes like this:More info here https://facebook.github.io/react/docs/animation.html#custom-classes
amccloud commentedon Dec 7, 2016
This is how i'm currently providing an activeClassName to Link:
I think this could be hidden behind some sort of helper:
geelen commentedon Dec 21, 2016
@amccloud that's clever, nice work!
7 remaining items
rgdelato commentedon Feb 7, 2017
Funnily enough, I also ended up making a quick hack to support my own use case as well:
https://github.com/rgdelato/styled-classnames
rjhilgefort commentedon Feb 10, 2017
In case anyone wants it, here's an implementation of the helper described by @amccloud. Caveat, I'm using
flowandramda. You can obviously remove theflowannotations and translateomitto your implementation of choice.This is likely a similar implementation to @codepunkt's implementation, but as far as I can tell, it only supports one property.
svrcekmichal commentedon Feb 16, 2017
Hey guys, is there some better solution for this when v2 is going to be released?
rgdelato commentedon Feb 16, 2017
@svrcekmichal As far as I know, there's going to be a
.attrs()property added to v2 (see #365) that solves this problem.mikehazell commentedon Mar 6, 2017
FWIW, this is working for me.
GGAlanSmithee commentedon Mar 6, 2017
And in v2, using
attrssvrcekmichal commentedon Mar 6, 2017
@mikehazell @GGAlanSmithee What do you think about this API? #522
kitten commentedon Apr 19, 2017
As mentioned above, we currently don't need to follow up on this anymore, since we have the
attrsmethod in v2.karolisgrinkevicius commentedon Apr 29, 2017
In new
react-routeryou may useNavLinkinstead ofLinkcomponent. API for NavLinkwojciech-bilicki commentedon Jul 11, 2017
Combining the @GGAlanSmithee and @karolisgrinkevicius solved the issue for me.
jasan-s commentedon Aug 14, 2017
@karolisgrinkevicius it seems you still need to use attrs even with NavLink
larsnolden commentedon Oct 2, 2017
I am running into issues using the new 'NavLink' from react-router-dom 4.2.2 following GGAlanSmithee's .attr approach:
Only the 'default' styles are being rendered. Using the 'activeStyle' prop on NavLink did not help either.
mxstbr commentedon Oct 2, 2017
You need to pass the
activeClassNametoNavItem.I'm going to lock this issue now. Everything that needs to be said has been said and it's unnecessary to notify everybody for problems related to your own codebase.
If you are having problems, please go to StackOverflow or open a new issue.