Answer the question
In order to leave comments, you need to log in
SASS and CSS modules?
In general, I made up everything with bootstrap + jquery and everything in static files, and about 2 months ago I decided to go further.
I started learning nodejs, builders (webpack, gulp) react, sass. Everything is cool, I like everything, convenient + interesting to develop, but the theme with component layout puts me a little in a stupor.
Just using the example of React, I want to understand how best to structure styles for components.
Today I saw a cool thing - css modules. It's great, but there are 2 things that I don't really like.
1) As I understand it, all styles go to the head tag of the document, which does not suit me.
2) You can't use preprocessors. Of course, there are compresses in css modules and you can enable some features, such as nesting, etc., but there are no cool features like functions in css modules.
So, I want to understand how best to work with styles without css modules, for example with sass.
All I could think of was to put each sass file in the folder with the component and import it into a common sass file.
React example:
src/components/Button/Button.jsx
import React, { Component } from 'react';
export default class Button extends Component {
render() {
return (
<button type="button" className="button">
{children}
</button>
);
}
}
.button {
background-color: blue;
box-shadow: 0 0 3px rgba(0,0,0,.1);
padding: 5px 20px;
}
import './components/Button/Button.scss';
Answer the question
In order to leave comments, you need to log in
if you use webpack, then you don’t need to import everything into a separate file. But css-modules combined with postcss makes life easier.
And your version has the right to life. I myself did this for a long time, then stylus with its require solved this problem, but still I advise you to look towards css-modules. And if you don’t like or have any other reason about styles.someClass, then there is an alternative to react-css-modules (as far as I understand, you work with react).
https://github.com/gajus/react-css-modules
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question