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
/* Card.css in the npm module `cards`*/.card{background: white;}
In another project, I want to target .card from cards.
/* Thing.css */.row{/* some random grid stuff */.cardfrom"cards"{grid-width:1/3;}}
I've seen :import from ICSS but have hanged my local setup (webpack, etc) trying to figure out how I can import an identifier from a different file.
Is this css-modules territory or should I look at other ways of importing the identifiers?
Reacted by Elad Ossadon, Nav Garcha, Myroslav Kiurchev, Ezequiel A. Schwartzman, Iddan Aaronsohn, Anatoly Ostrovsky, alican karalar, 文宇祥, Oleg Shilov, Mesqalito and 12 more
@NekR I thought it might be, but #25 seems to be talking about importing variables such as colors. What I am trying to achieve seems more limited in scope since it is only for .card to be compiled to the same classname across two files.
@ChristopherBiscardi hmm.. I might be wrong, but card should be compiled with the same name anyway.
Okay, here when accesing .row from JS you will get class names of .row and .card in one string. If you will then create .row2 and compose it with .card then .row2 will use same class of .card as ``row`. Although I am not sure how this will work with external modules, but if you are using webpack and css-loader then I think everything should be fine.
.row{composes: card from 'cards.css';/* some random grid stuff */grid-width:1/3;}
Ok, I've just tried using composes to solve this and I've stumbled on another issue before I can even test it. I'm using sass (via sass-loader) and each of my component sass files imports a utils.scss a couple of directories down, which works fine without composes, but with it I am getting a file not found on the utils.scss from the file being imported for composition.
Module not found: Error: Cannot resolve 'file' or 'directory' ../sass/utils in <path to project>/src/components
I don't fully understand how this all fits together but It seems to me that this mechanism is trying to resolve a js/jsx file rather than going through the sass pipeline. Does this make sense to you? It's resolving ./Card.scss just fine - it's the secondary import that doesn't seem to be working.
Edit: apologies, it was late last night and today I realised i should take this issue to webpack/css-loader!
which works perfectly, but is not what I'm trying to do.
What I am trying to do is render the following final css, where Card__card___1RA4j is an identifier defined in another file.
.Thing__row___9NgKF{/* some grid stuff here */}.Thing__row___9NgKF.Card__card___1RA4j{grid-width:1/3;}
Reacted by DecadeMoon, Oleg Shilov, Dmitry Pashkevich, Ethan Miller, Itay Ganor, Robin Neal, Jeff Escalante, Javier Fernandes, Alexandra Petrova, Mikhail Molchanov and 4 more
That's why I'm wondering, do you have a specific need to do that? In this case .card in Thing.css composes .card from cards so in your template when you do:
import styles from "./Thing.css"
`<div class="${styles.row}">
<div class="${styles.card}" />
</div>`
it should give you the specificity of "a card inside a row" that you're after. Or does that example not cover some of the functionality you're after?
The specificity desired isn't "a card, inside a row" it's "the card class from cards, inside of a row".
To expand on this a bit, let's say I am rendering this Card React element, which comes from (for all intents and purposes) a third party node module named cards.
So specifically, I'd like to target the card class from Card.css inside App.css.
Reacted by Elad Ossadon, Xuan Huang (黄玄), Nav Garcha, Bob Toney, Sunny Gonnabathula, Ezequiel A. Schwartzman, Iddan Aaronsohn, Christian Prescott, Stu Kabakoff, Mehmet Kamil Morçay and 30 more
I see, thanks for clarifying. So in this case the Card component doesn't allow you to supply your own className. If it did we could sidestep this issue altogether.
At the moment there's no support in css-modules for using .card from "cards" as a selector. It's not out of the question, but I'm trying to think through the ramifications.
To use this approach would involve a fair bit of knowledge about the internals of the 3rd-party module, correct? You'd need to assume that it was also using the same version of css-modules, otherwise the className Thing.css gets for .card won't be guaranteed to match the className that the <Card> component applies to its children.
I think this is where ICSS comes into play. If a 3rd-party module was distributed with styles and classNames in this common format there would be less assumptions involved.
If it did we could sidestep this issue altogether.
Indeed. In this specific case I control all of the relevant code and could add the className support, but the intent is to distribute a set of abstractions as modules themselves. Part of this is exploring where the boundaries actually lie in terms of what has to be packaged with what (clearly that would mean shipping today would mean implementing className support, which I already have a working model of). Ideally I'd like to build upon this to create elements which can work as a group or individually without requiring passing new classes around. Which leads to:
To use this approach would involve a fair bit of knowledge about the internals of the 3rd-party module, correct?
Yes, it would. Rather, it would except for the fact that the constellation of element packages will all be built on a common base; Which makes the base assumptions a known quantity shared across all elements. There are other significant facets, such as the style of grid system that lost grid is, etc. but all that is stuff I'm finalizing after I figure out the base issue of "Can elements integrate deeply with other elements based on pre-existing class names".
You'd need to assume that it was also using the same version of css-modules
The elements are being distributed as raw code (ie: as pre babel and pre PostCSS compilation processes) rather than pre-compiled for various other reasons, such as enabling per-project configuration of constants such as "default grid width", "media query bases" and "default border radius". The intent being to enable deep theming, "element collections" (roughly analogous to a Bootstrap, Foundation, Semantic, etc), among other things. As a result, they will all be compiled at the same time, with the same version of css-modules. (pre-compilation for use as raw css is an interesting discussion topic I've explored briefly but is not currently the focus).
I think this is where ICSS comes into play. If a 3rd-party module was distributed with styles and classNames in this common format there would be less assumptions involved.
Is this the kind of thing you had in mind?
yes, pretty much, although I don't know if ICSS is the solution. How would one go about distributing as ICSS? I haven't come across any "Dump ICSS" debug options, icss-loader, etc.
hmm.. modifying classes from another modules seems like violation of module principles. While this all makes sense in global CSS, it does not makes sense in CSS Modules since the idea is to eliminate such side effects. Image that you could modify a constructor of imported class and this will affect all other users of that class. Looks like a mot such a best practice, right? What is common in OOP is inheritance, and while it's called differently, composition from the CSS Modules is almost the same. e.g. to be able to modify (override) Card class in your code, you need to inherit from original Card and then use that subclass across your application. I believe same applies for .card and composition.
P.S. I understand that Card is React class and it provides already an interface to modifying/customizing it, but this is just an implementation detail of that class.
Reacted by alican karalar, yang, Grayson Hicks, Marc Knaup and TonCherAmi
I am currently having the same issue when assessing our use of css-modules within the dojo 2 toolkit. We are considering using css modules to ship structural styles along with our widgets and themes in a separate repo. In order to use the generated class names at build time we would need to be able to load class names from the structural files into the theming files. Otherwise we'd have to fall back to custom tag names, or adding separate class names specifically for theming which will break the encapsulation that css modules provides.
@nkbt thats the same thing as composes:? The discussion here is the need for an API that allows selecting for a class e.g. .toolbar > .button {} vs .toolbar > .form-control {} which is not possible by either existing means but generally required (albeit infrequently)
@hrasoa if you want composition of component styles, you need to act like you do with components, to step above and compose them. So it would be something like
With support of className in each component respectively.
I do believe that idea of doing .cover + .contentinside either of those components is straight up incorrect, since components should be able to work in isolation and not know anything about any adjacent components. Only parent should be able to compose components and their styles if styles depend on composition.
@jquense we do not use composes:, it's pretty much unofficially banned from being used entirely. Each css is self contained and never aware of any other things on a page except its own component. For theming we use var() which works perfectly fine in all modern browsers we support. For any styles that depend on multiple component - we step above and style element that composes those components instead (see my previous comment for example of that)
In this way we are as close to StyledComponents as it is technically possible. Like full on "css-in-js" but with nice and familiar CSS syntax with all its advantages and no drawbacks.
Ya'll if anyone wants to see this happen, I think it think it should. Having used css-modules exclusively for the last few years on large and small projects there are definitely valid use cases for it.
The issue here is there isn't anyone to work on this, so if someone wants to see it happen, the best way forward would be to show up with a PR. no one is really working on css-modules anymore, i think it just takes someone who wants to do the work, see: #187 and #187
Reacted by Mathieu M-Gosselin, Marc Knaup, Kutner JS, Itay Ganor, Jeff Escalante, Imamudin Naseem, gzliew and Алексей Соснин
Activity
NekR commentedon Aug 26, 2015
Seems like this one is dupe of #25
ChristopherBiscardi commentedon Aug 26, 2015
@NekR I thought it might be, but #25 seems to be talking about importing variables such as colors. What I am trying to achieve seems more limited in scope since it is only for
.cardto be compiled to the same classname across two files.NekR commentedon Aug 26, 2015
@ChristopherBiscardi hmm.. I might be wrong, but card should be compiled with the same name anyway.
Okay, here when accesing
.rowfrom JS you will get class names of.rowand.cardin one string. If you will then create.row2and compose it with.cardthen.row2will use same class of.cardas ``row`. Although I am not sure how this will work with external modules, but if you are using webpack and css-loader then I think everything should be fine.cliffrowley commentedon Aug 27, 2015
This is rather good timing for me, I'm also having exactly this issue. I'll post back with any success / failure.
joshwnj commentedon Aug 27, 2015
If it's coming from an npm module
cardsshould it becomposes: card from "cards/cards.css"?cliffrowley commentedon Aug 27, 2015
Ok, I've just tried using
composesto solve this and I've stumbled on another issue before I can even test it. I'm using sass (via sass-loader) and each of my component sass files imports autils.scssa couple of directories down, which works fine withoutcomposes, but with it I am getting a file not found on theutils.scssfrom the file being imported for composition.E.g. this is my directory structure:
My (simplified)
Card.scsslooks like this:My (again simplified)
CardContainer.scsslooks like this:The error I see is:
I don't fully understand how this all fits together but It seems to me that this mechanism is trying to resolve a js/jsx file rather than going through the sass pipeline. Does this make sense to you? It's resolving
./Card.scssjust fine - it's the secondary import that doesn't seem to be working.Edit: apologies, it was late last night and today I realised i should take this issue to webpack/css-loader!
ChristopherBiscardi commentedon Aug 27, 2015
@joshwnj Consider the second snippet in my original post as pseduocode.
@NekR using composes as such:
results in this JS object:
which works perfectly, but is not what I'm trying to do.
What I am trying to do is render the following final css, where
Card__card___1RA4jis an identifier defined in another file.joshwnj commentedon Aug 27, 2015
@ChristopherBiscardi i see, that makes sense thanks.
Would this give the result you're after?
ChristopherBiscardi commentedon Aug 27, 2015
That produces:
so it would work if I was using the
.cardfromThing.css, but what I want to do is target the.cardfromcards.joshwnj commentedon Aug 27, 2015
That's why I'm wondering, do you have a specific need to do that? In this case
.cardinThing.csscomposes.cardfromcardsso in your template when you do:it should give you the specificity of "a card inside a row" that you're after. Or does that example not cover some of the functionality you're after?
ChristopherBiscardi commentedon Aug 27, 2015
The specificity desired isn't "a card, inside a row" it's "the card class from cards, inside of a row".
To expand on this a bit, let's say I am rendering this
CardReact element, which comes from (for all intents and purposes) a third party node module namedcards.The usage (in an app) would look something like this:
Which brings us to
App.css.So specifically, I'd like to target the
cardclass fromCard.cssinsideApp.css.joshwnj commentedon Aug 27, 2015
I see, thanks for clarifying. So in this case the
Cardcomponent doesn't allow you to supply your ownclassName. If it did we could sidestep this issue altogether.At the moment there's no support in css-modules for using
.card from "cards"as a selector. It's not out of the question, but I'm trying to think through the ramifications.To use this approach would involve a fair bit of knowledge about the internals of the 3rd-party module, correct? You'd need to assume that it was also using the same version of css-modules, otherwise the className
Thing.cssgets for.cardwon't be guaranteed to match the className that the<Card>component applies to its children.I think this is where ICSS comes into play. If a 3rd-party module was distributed with styles and classNames in this common format there would be less assumptions involved.
Is this the kind of thing you had in mind?
ChristopherBiscardi commentedon Aug 27, 2015
Indeed. In this specific case I control all of the relevant code and could add the className support, but the intent is to distribute a set of abstractions as modules themselves. Part of this is exploring where the boundaries actually lie in terms of what has to be packaged with what (clearly that would mean shipping today would mean implementing className support, which I already have a working model of). Ideally I'd like to build upon this to create elements which can work as a group or individually without requiring passing new classes around. Which leads to:
Yes, it would. Rather, it would except for the fact that the constellation of element packages will all be built on a common base; Which makes the base assumptions a known quantity shared across all elements. There are other significant facets, such as the style of grid system that lost grid is, etc. but all that is stuff I'm finalizing after I figure out the base issue of "Can elements integrate deeply with other elements based on pre-existing class names".
The elements are being distributed as raw code (ie: as pre babel and pre PostCSS compilation processes) rather than pre-compiled for various other reasons, such as enabling per-project configuration of constants such as "default grid width", "media query bases" and "default border radius". The intent being to enable deep theming, "element collections" (roughly analogous to a Bootstrap, Foundation, Semantic, etc), among other things. As a result, they will all be compiled at the same time, with the same version of css-modules. (pre-compilation for use as raw css is an interesting discussion topic I've explored briefly but is not currently the focus).
yes, pretty much, although I don't know if ICSS is the solution. How would one go about distributing as ICSS? I haven't come across any "Dump ICSS" debug options, icss-loader, etc.
joshwnj commentedon Aug 27, 2015
I don't know of any examples of this, just thinking ahead of possible solutions :)
Thanks for sharing the context of the problem you're trying to solve, that makes more sense now.
NekR commentedon Aug 27, 2015
hmm.. modifying classes from another modules seems like violation of module principles. While this all makes sense in global CSS, it does not makes sense in CSS Modules since the idea is to eliminate such side effects. Image that you could modify a constructor of imported class and this will affect all other users of that class. Looks like a mot such a best practice, right? What is common in OOP is inheritance, and while it's called differently, composition from the CSS Modules is almost the same. e.g. to be able to modify (override)
Cardclass in your code, you need to inherit from originalCardand then use that subclass across your application. I believe same applies for.cardand composition.P.S. I understand that
Cardis React class and it provides already an interface to modifying/customizing it, but this is just an implementation detail of that class.29 remaining items
tomdye commentedon Dec 5, 2016
I am currently having the same issue when assessing our use of
css-moduleswithin the dojo 2 toolkit. We are considering using css modules to ship structural styles along with our widgets and themes in a separate repo. In order to use the generated class names at build time we would need to be able to load class names from the structural files into the theming files. Otherwise we'd have to fall back to custom tag names, or adding separate class names specifically for theming which will break the encapsulation that css modules provides.Billy- commentedon Mar 3, 2017
Hi all, have just come across this issue. Has there been any updates on this?
Kind regards,
vinipace commentedon May 2, 2017
Any update?
tivac commentedon May 2, 2017
:external(...)has been working well for us the few times we've needed this capability when usingmodular-css.arahansen commentedon Jul 18, 2017
@tivac could you provide an example of how this works? That syntax doesn't seem to do the trick for me. Is there an additional loader I need in place?
tivac commentedon Jul 18, 2017
@arahansen I don't think CSS Modules ever added support for
:external, I built my own thing for a variety of reasons and implemented it there.https://github.com/tivac/modular-css/blob/master/docs/features.md#overriding-styles
Reverbe commentedon Nov 2, 2017
any update?
lawrencebla commentedon Aug 16, 2018
any update?
nkbt commentedon Aug 17, 2018
@lawrencebla I guess since issue is closed it is not going to happen.
On a topic:
We already have pretty good dependency/composition management in JS, adding another layer of dependency management to CSS seems like an overkill.
This seems like a perfect solution to the problem. And you get if for free with all the existing tooling
tomdye commentedon Aug 17, 2018
@nkbt that's essentially what we have adopted for Dojo
hrasoa commentedon Sep 12, 2018
So i'm facing the same problem. I have:
Cover.scss
.cover { ... }Cover.js
Content.scss
.content { ... }Content.js
App.js
As the Cover component may not always be in the page, I want to style the Content component depending on the Cover with this simple rule:
jquense commentedon Sep 12, 2018
@nkbt thats the same thing as
composes:? The discussion here is the need for an API that allows selecting for a class e.g..toolbar > .button {}vs.toolbar > .form-control {}which is not possible by either existing means but generally required (albeit infrequently)nkbt commentedon Sep 13, 2018
@hrasoa if you want composition of component styles, you need to act like you do with components, to step above and compose them. So it would be something like
App.js
With support of
classNamein each component respectively.I do believe that idea of doing
.cover + .contentinside either of those components is straight up incorrect, since components should be able to work in isolation and not know anything about any adjacent components. Only parent should be able to compose components and their styles if styles depend on composition.nkbt commentedon Sep 13, 2018
@jquense we do not use
composes:, it's pretty much unofficially banned from being used entirely. Each css is self contained and never aware of any other things on a page except its own component. For theming we usevar()which works perfectly fine in all modern browsers we support. For any styles that depend on multiple component - we step above and style element that composes those components instead (see my previous comment for example of that)In this way we are as close to StyledComponents as it is technically possible. Like full on "css-in-js" but with nice and familiar CSS syntax with all its advantages and no drawbacks.
jquense commentedon Sep 13, 2018
Ya'll if anyone wants to see this happen, I think it think it should. Having used css-modules exclusively for the last few years on large and small projects there are definitely valid use cases for it.
The issue here is there isn't anyone to work on this, so if someone wants to see it happen, the best way forward would be to show up with a PR. no one is really working on css-modules anymore, i think it just takes someone who wants to do the work, see: #187 and #187