-
Notifications
You must be signed in to change notification settings - Fork 552
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
How to work with composes? #195
Comments
To simplify, let's say I have a button, which is black when normal, and red on busy. Also, the button text is always white in all cases. All styles should be done in compose. This is my way: background.css .red {
background: red;
}
.black {
background: black;
} button.css ._main {
/* shared styles */
color: white;
}
.normal {
composes: _main;
composes: black from 'background.css';
}
.busy {
composes: _main;
composes: red from 'background.css';
} button.js <button className={busy ? styles.busy : styles.normal} /> The point is: don't depend on css specificity. Depend on class combination |
Also, redundant empty class is another issue (and story). We are fighting it, but I suggest to not fighting it here 😄 here let's discuss about specificity |
Thanks for your input @dvkndn. I'll post a more concrete code tonight which illustrates what I'm trying to say. |
Thank you @kumarharsh . Also try to simplify your code :D for example, instead of gradient background, you can use more simple css like color or background |
For animations you don't need compose. @keyframes pulsing {
0% {
transform: scale(1);
opacity: 1;
}
50% {
transform: scale(0.8);
opacity: 0.3;
}
100% {
transform: scale(1);
opacity: 1;
}
} Then in your other CSS just use it //Buttons.css
@import '../shared/animations.css';
.button {
animation: pulsing 1.4s infinite;
} Might be some differences due to webpack settings, but I'm pretty sure this should work out of the box if you use |
I don't know why this was closed. It doesn't look like the issue was addressed. 2 Work-arounds were suggested, but there has been no explanation if the issue @TrySound had was intended behavior or if there is some baked-in way of dealing with this problem other than trying to avoid triggering the issue. |
@GodGinrai there isn't really a better solution than what was suggested here I think. |
How exactly does the pathing work? I am getting Which is expected actually. How would I change the root to point to assets? I don't want to have obnoxious strings like '../../../../' I want to emulate what I am seeing here: https://github.com/css-modules/webpack-demo/blob/master/src/components/3-ClassComposition/StyleVariantA/StyleVariantA.css |
Use webpack's |
I am using that
What exactly needs to be changed? |
I would have expected rules to be ordered topologically based on import dependencies. That would ensure that rules always come after rules they compose and thus be able to override declarations. Tools like |
CSS modules rules can not reliably override rules from composed rules [1]. Use more specific selector to ensure menu button can override padding of unstyled button. PostCSS Rollup plugin generates duplicate rules when importing another rule in multiple places. Enable minification to remove those duplicates in extracted CSS. REDMINE-17871 [1] see css-modules/css-modules#195
CSS modules rules can not reliably override rules from composed rules [1]. Use more specific selector to ensure menu button can override padding of unstyled button. PostCSS Rollup plugin generates duplicate rules when importing another rule in multiple places [2]. Enable minification to remove those duplicates in extracted CSS. REDMINE-17871 [1] see css-modules/css-modules#195 [2] egoist/rollup-plugin-postcss#26
Well it's been a while :)) after 4 years of using CSS Modules I think it's better to not use "compose" anywhere else. Like not at all. It's better to do that at template level. It's more suitable than dealing in CSS. For example if you are using React then that's "component". |
Yeah, sorry for commenting on such an ancient issue and thanks for getting back. But wouldn't I end up with the same problem if I tried to apply the |
Taking the original example of Button and Busy, this is what I'm going to do now (which may conflict with what I said 4 years ago at the start of the thread 😄):
Ok time to show the code button.module.css .button {
border-radius: 3px; /* non conflicting styles */
}
.non-busy {
background-image: A; /* because of the busy */
} busy.module.css .busy {
background-image: B
} button.tsx import styles from "./button.module.css";
import busyStyles from "../somewhere/busy.modules.css";
const buttonClass = [
styles.button,
props.isBusy ? busyStyles.busy : styles.nonBusy
].join(" "); The thing about this approach is the trade-off to let Button knows about the implementation detail of Busy, but what we gain is attributes exclusive, which I believe is worth the trade-off. Note that this means the author of Button knows about Busy, but Busy doesn't know about Button at all. |
Thanks for the detailed example. This approach surely works, but it still creates a subtle sort of coupling: As you say, as the author of Button I need to know about Busy. But even worse I need to know about the internals of busy. Whenever I change Just to provide a bit more context, here's my original example: I wanted to create a reusable CSS module utils.css: .unstyledButton {
border: 0;
padding: 0;
background-color: transparent;
text-align: initial;
} In some cases I do want my button to have a padding, though:
As I learned the hard way, this does not work. Following your approach I could try to decompose Probably, trying to reuse rules like this is simply the wrong approach. Instead of composing an |
Sorry that I couldn't help more :D however I think this makes sense. At the end of the day, I feel these approaches are the compromises that I need. I mean.. adding props feels like boilerplate, but it's also good because a component should not be changed in anyway that it doesn't allow explicitly, even with CSS, right? I think it's a balance between safeness and convenient. In my real projects, I do find myself sometimes doing "hack" like |
Oh no, this was a very helpful and insightful discussion for me. Thanks!
Yeah, it probably all comes back to what you said originally that using |
One more thing I realized is that (as described here in the context of Webpack's |
I think
composes
is a great idea on paper. But after giving it a go, I find there are some issues in using it in a real-world project.One issue I've ran into is something like this:
But this .busy class gets the same specificity as .button, and gets overridden by .button's styles (depending on order - this happens for me).
To fix it, I try to do this:
which I can't do - because composes doesn't work with two classes. So, now I'm stuck in a catch-22 situation, with no way out.
The only thing which works is this:
Although this works, the solution has two drawbacks which I see:
.Button__busy___2QL1h
rule.Is there a better way this can be done?
The text was updated successfully, but these errors were encountered: