The documentation on css-modules is pretty sparse, so I'm not sure if I can do this or not.
This article says the way I'd style a button with normal
and error
states would look like this:
.common { /* font-sizes, padding, border-radius */ }
.normal { composes: common; /* blue color, light blue background */ }
.error { composes: common; /* red color, light red background */ }
But I'd prefer to write it like this:
.common {
/* font-sizes, padding, border-radius */
&.normal { /* blue color, light blue background */ }
&.error { /* red color, light red background */ }
}
Like I've always done, without introducing this new composes
syntax. I think it's more clear which styles build on top of other styles if I can actually nest them in code.
But I don't know what this would be exported as by css-modules? The only examples they give are simple class name selectors. I have no idea what about .common.normal
will be exported as, or what .common > .normal ~ .strange
? Do I basically have to not use any kind of CSS selector if I use css-modules?
This module introduces the ability to nest one style rule inside another, with the selector of the child rule relative to the selector of the parent rule. This increases the modularity and maintainability of CSS stylesheets.
This nesting rule is not supported yet in native CSS. At the moment, it is a working draft and only available for discussion. However, in this tutorial, we will discuss what nesting is and how it is used in preprocessors. Then, we will discuss some of its advantages and how it will be used natively in CSS.
CSS Modules let you write styles in CSS files but consume them as JavaScript objects for additional processing and safety. CSS Modules are very popular because they automatically make class and animation names unique so you don't have to worry about selector name collisions.
With styled-components you attach the styles right to the component and don't really name anything. With css-modules, you're applying the styles directly to an HTML element only. With styled-components you can apply the styles to custom components and it will slap the styles on by way of spreading props later.
.common {
/* font-sizes, padding, border-radius */
:global {
&.normal { /* blue color, light blue background */ }
&.error { /* red color, light red background */ }
}
}
That's my solution. If you use styles.common
, the css will be:
.ramdomStrings {
/* I'm not sure if this exists, but it doesn't matter */
}
.ramdomStrings.normal { /* blue color, light blue background */ }
.randomStrings.error { /* red color, light red background */ }
I am also using less with css modules, but I don't think they way you are using the '&' fits with the goal of css modules. From my understanding 'composes' is more analogous to @import than &, and I only find myself using the & for psuedo-classes.
You can definitely do things the way you have here, but don't you find it a bit strange that you have to specify both the 'common' and 'normal' classes in the HTML? Much better in my opinion to just specify 'normal', and let normal inherit the shared styles using 'compose'.
If you're using a webpack loader for css-modules you can use the postcss loader and add various plugins to get your desired behaviour.
There is for example the "postcss-nested" plugin, which allows you to write nested rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With