Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use css-modules with LESS + nesting?

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?

like image 790
mpen Avatar asked Oct 31 '15 01:10

mpen


People also ask

Can you nest in 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.

Does CSS support nesting of classes?

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.

What is the advantage of using CSS modules compared to regular 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.

Should I use CSS modules or styled-components?

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.


3 Answers

.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 */ }
like image 106
Jason Peng Avatar answered Oct 21 '22 05:10

Jason Peng


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'.

like image 26
mikestaub Avatar answered Oct 21 '22 05:10

mikestaub


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.

like image 3
Patrick Hübl-Neschkudla Avatar answered Oct 21 '22 05:10

Patrick Hübl-Neschkudla