Repository navigation
Description
It would be great if there was a way to substitute class names from other files, possibly with a decorator such as :ref or :alias. This would allow parent elements to be able to provide a way to style children elements without having to either passing in extra classes or wrap it in extra element just to add extra classes to control it's styling.
Where is this useful?
Consider this trivial example when you have a component - for example some sort of <ButtonControlBar /> that takes a bunch of <MyAwesomeButton /> components.
<ButtonControlBar>
<MyAwesomeButton />
<MyAwesomeButton />
<MyAwesomeButton />
</ButtonControlBar>The ButtonControlBar component wants to add some margin between all of the children so that they are spaced nicely. We're going to want to add 5px of margin to the right of each button except the last. Using straight CSS we could do something like:
.button-control-bar > .my-awesome-button:not(:last-child) {
margin-right: 5px;
}With CSS Modules we could pass extra classes to each of the children, but in this case the MyAwesomeButton component hasn't been implemented with a way to do so (it's something 3rd-party that's also using CSS modules for example). Also we don't know the internal implementation of MyAwesomeButton so we can't assume it's just a <button> element.
We could instead wrap each of the children component in another element that you can add a class to allow use to control the style of the children, but this is just unnecessary wrapping to avoid what would be fairly straight forward with straight CSS .button-control-bar > .my-awesome-button.
Proposed Implementation
We could introduce a :ref pseudo-class that would allow us replace a selector value. I'm a fan of the pseudo-class as a declarative way to use the imports to replace classes. It's also less "magic" that just straight replacing the .childButton class where ever it appears.
// MyAwesomeButton.css-module
.my-awesome-button {
background: red;
}
:export {
myAwesomeButton: my-awesome-button
}
// ButtonControlBar.css-module
:import(MyAwesomeButton.css-module) {
childButton: myAwesomeButton
}
.button-control-bar > :ref(.childButton):not(:last-child) {
margin-right: 5px;
}This provides a fairly fuss free way to achieve the above, with the added bonus that if we don't want to use MyAwesomeButton anymore, because <NextBigThingButton/> component is the more suited to the project we can just fix up the :import and the rest of the CSS doesn't need to be touched.
// NextBigThingButton.css-module
.next-big-thing-button {
background: red;
}
:export {
nextBigThingButton: next-big-thing-button
}
// ButtonControlBar.css-module
:import(NextBigThingButton.css-module) {
childButton: nextBigThingButton
}
.button-control-bar > :ref(.childButton):not(:last-child) {
margin-right: 5px;
}
...more rules that are using :ref(.childButton)We built this behaviour into the webpack css-loader in this PR webpack/css-loader#212
Activity
nkbt commentedon Jan 6, 2016
Personally I would actually pass classes down or wrap them. Simple, no extra language necessary, no extra complications.
Can't figure out how it might be more difficult. Do I miss something?
KJTsanaktsidis commentedon Jan 6, 2016
I think in our case, the reason why we don't want to add classes to the children is because often the children are passed in. Consider something like the
ButtonControlBar...In the above example, the button control bar expects to be passed buttons as children. There are, as you point out, a few ways to skin this cat:
ButtonControlBarcould put a class on theMyAwesomeButtons it creates. However, the fact that the button needs special styling inside the bar is really the bar's business, not the consumers, and it feels wrong to pollute the consumer with this responsibilityclassNameprop correctly (and this isn't automatic anymore withReact.cloneElementthe way it used to be withcloneWithProps). This seems like a good way to spend 10 minutes scratching your head wondering why your button is the wrong colour.I think the most elegant way to solve this problem is to just make use of the class that's already there! No extra DOM and no confusing prop merging.
nkbt commentedon Jan 6, 2016
KJTsanaktsidis commentedon Feb 1, 2016
Hey, just wondering if anybody has any more input about this idea?
renanvalentin commentedon Feb 8, 2016
Hello guys, how did you ended up to solve this problem? I'm thinking in other workarounds but it seems too complicated, like having to pass props to child components.
KJTsanaktsidis commentedon Feb 8, 2016
@renanvalentin we're using our fork webpack/css-loader#212 in production :)
renanvalentin commentedon Feb 8, 2016
I've came up with a solution, it is more philosophical than technical:
or
Suppose I want to add an extra margin to my children buttons, that css selector should be fine, but thinking about components, thats an a bad idea, because:
-> I need to know what class I'm using inside my component
-> What if I change my HTML markup or changed the class name inside the component, this will result in breaking that css selector chaining;
So I wrote this "Layout Component" responsible for extend my components:
And I'm using it in this way:
While I was writing this, I saw lots of problems on it, but that's ok hahahaha
meta-meta commentedon Mar 4, 2016
Say I have the following React components:
ModuleA renders
ModuleB renders
and some markup:
What's the idiomatic css-modules approach to this? Pass odd/even classnames into ModuleB props?
LeZuse commentedon Aug 8, 2016
Also running into this concern a lot. A typical usecase is a parent component's hover state changing it's children styles somewhere deeper in the component tree.
The point is that by introducing the hover style you are effectively coupling those components together.
I haven't found a better solution than creating a "shared stylesheet" that expresses this connection between those components in question and then importing this stylesheet in both components, effectively adding them a new class name.
Pseudocode below:
ComponentA
ComponentB
hoverStyles
You can even put the
hoverStylesdirectly in eitherComponentAStyleorComponentBStyleif you are more or less sure about the implications.We use https://github.com/JedWatson/classnames for easier className manipulation.
In more simple scenarios I totally agree with @nkbt's #102 (comment) approach.
geelen commentedon Aug 9, 2016
Yeah I'm starting to weaken on this. I reckon we should just add it as an escape-hatch, since while I don't think you need to use these "contextual styles" as often as you might think (prop-passing is still the "CSS Modules" way), I'm no longer convinced it should be a hard rule.
The hover state is a good example, there are hit-testing reasons why you might need to attach
:hoverto a particular element in the DOM, but change the style of a descendant element.CubedEye commentedon Aug 9, 2016
@geelen @KJTsanaktsidis
We've been using our fork of the css-loader from here webpack/css-loader#212 for the last 6 months and it seems to be working well. Ideally we're trying to use CSS-modules as intended, but when you want to build some sort of UIKit that has a bunch of pieces that interact with each other without giving the developer an entry point for poking additional classes on to the elements (as you would enable with a
propsbased solution) then having a way to substitute classes is super useful.I know that in an ideal world having encapsulation is a great thing. But in the example of a UIKit above, you've already commited to all the pieces being coupled together so the CSS is likely going to be coupled anyway.
The proposal of a
:ref()decorator works really well because the CSS won't compile if a referenced class goes missing, which is really what you want if you're going to do something like this.geelen commentedon Aug 9, 2016
Cool, well this would probably need to be made into a change in https://github.com/css-modules/postcss-modules-extract-imports but given how often this gets asked for, and how in... 12 months we've not come up with a better way, unless @sokra has a really strong objection I reckon we should just go ahead with it.
Glad your fork has been working out for you while I've been a bit absent. If you feel like doing up a new PR to postcss-modules-extract-imports that would be great, otherwise I'll get a chance to look at it next week some time.
lijunle commentedon Feb 16, 2017
This is awesome dicusssion. I suppose this is defined as
:importpseudo-selector in ICSS spec:I tried to leverage
:importin CSS-Loader but the second bullet is not supported. It could be great if CSS Module Spec could support it.pferreir commentedon Apr 9, 2018
Just ran into this problem. Any news on this?
buschtoens commentedon Jul 12, 2018
Relates to #147.
Merge pull request css-modules#102 from css-modules/greenkeeper-babel…