23

I am using css modules for my project, and I have a file positioning.css which has some useful classes that I want to import. e.g. .right, .left

What is the best approach for this using CSS Modules?

At the moment I can see 2 options, but they are not all that great:

composition in the component's style

.right {
    composes: right from '../styles/positioning.css';
}

or

multiple css module imports in the component

import positioning from '../styles/positioning.css'
import styles from './myComponent.css';
Object.assign(styles, positioning)

class Menu extends Component {

  render() {

    return (
      <div styleName='menu'>
        <div styleName='left'>this is left</div>
        <div styleName='right'>this is right</div>
      </div>
    );
  }
};

export default CSSModules(Menu, styles);
2
  • 1
    Are you using webpack? If so there is a css loader that allows you to import css files directly like packages: github.com/webpack-contrib/css-loader Commented Aug 22, 2017 at 18:00
  • I usually make a globals.scss file that has non-component specific classes, and reference them by strings. Commented Nov 1, 2017 at 21:47

8 Answers 8

10

I have manage to get this working:

// css file
@value class-to-compose from "file-where-class-is-defined.css";

.someclass {
  composes: class-to-compose;
  // other styles
}
Sign up to request clarification or add additional context in comments.

Comments

3

One approach is to collect all app level css variables and calculations at the top level into app.css

@import "./theme/layout.css";
@import "./theme/colors.css";
...

Then reference app.css using

@import "../../app.css";

This way you can manage @import scope inside one file at the root level.

Comments

0

I'll go with the first proposition. (the result is quiet the same)

  • both proposition have quiet the same result
  • If someday you have to edit your Menu css, you'll just have to edit your Menu css and not your component.
  • You let CSSModules take decisions. (more futur proof?)

Comments

0

You could import the css files that you use frequently into a broader CSS file that you import on specific pages, this is taking the second approach but making it cleaner, especially if you have a lot of common core css files that you import on pretty much all pages.

Comments

0

I would advise you to go with [Sass] [1]. Sass allows for the usage of partials (i.e. distributed / scoped css sheets).

You write scoped (to the components you want) css and import all your partials into your main.css then.

Couple of other advantages:

  1. you can do theming by having one partial that defines your them via variables, which you import first and then all your partials can use these variables.
  2. having the css on a scoped level (at least to me) felt more "reactish" where components are supposed to be stand alone, but it also wasn't inline styling, which I find ugly and weird (I don't like to clutter down my .js files with styles)

[1] http://sass-lang.com/

2 Comments

your link is dead :)
I would recommend styled-components instead of sass
0

I find this one line very helpful with importing:

@import 'file.css';

Comments

0

You could set these as globals and update their names to be a tad more semantic, like BootStraps pull-right.

If you declare them as

:global(.right) {
    /* ... */
}

You can then just use them in your app by preferably importing globals early on in the entry point.

Comments

0
  1. You should take a look at the option by vue.js component (scoped/overall)

  2. You can choose a precompile css language like SASS, which can use @extend ...etc to reuse the common property, like below:

     %common {
       width: 100%;
       height: inherit;
     }
    
     .my-class {
       @extend %common;
     }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.