1

I have a couple of simple React components each using their own React CSS module in their own respective folders. The CSS is basic but I have it working. I'm building with Webpack 2.

My question is how do I share a common piece of CSS from another CSS module into the two React CSS modules ?

I'm not sure if I should have a folder with just the common CSS module in there and then import it into the two React components. If so how do I refer/reference the common CSS.

I've read the docs but I'm still confused.

Is there a way similar to SASS or Less include/composition ?

I understand this may break the pattern of isolated components, but in my simple case I have a bit of CSS that makes a button with slightly transparent boarders, and it'll be nice to have it common to all react components. I suppose I could place this CSS is a common .css file and have Webpack bundle it, then just use it as className={....}

1
  • try to use CommonsChunkPlugin.it will help me. Commented May 10, 2017 at 10:31

3 Answers 3

2

The correct way to do this given your setup, would be to use the composes feature of CSS modules

This allows you to define a localised class rule, and import it into other localised class rules, even across files

MyCommonStyles.css

/* base button styles */
.button {
  background: #000;
  color: #fff;
} 

MyModule1.css

/* use base button styles and override the color */
.loginButton {
  composes: button from './MyCommonStyles.css';
  color: #00ff00;
}

I understand this may break the pattern of isolated components

In CSS modules land there is nothing wrong with sharing snippets of code across modules - far from it, this is built in and would be very counterproductive to prescribe recreating the same styles over and over - as long as it is not done using global CSS

For what its worth, the same can be achieved in a different way using the proposed CSS custom properties @apply, which you can use today via postcss (https://github.com/postcss/postcss-custom-properties). Either approach is totally fine, but I encourage you to try that out in combination too, to create common vars or snippets that can be shared across CSS modules. Its a great way to create components with standardised theme still using purely localised CSS

Sign up to request clarification or add additional context in comments.

Comments

0

I suppose I could place this CSS is a common .css file and have Webpack bundle it, then just use it as className={....}

You are true, because actually you're building a single-page application, which means everything will be added together, after that there's just HTML, CSS and javascript. You can declare your CSS styles as usual, then set some className or id in your render method

5 Comments

Yes, this does seem the 'simple' solution
That would defeat the entire point of CSS modules, doesn't answer the question
@alechill I already answered and was accepted, if you read carefully, you will see that I agree with the author's suggestion, which is an answer, then I also suggested on how to use that way by using "className" and "id". I would like to know why you have a feeling like that, thanks! If you think my answer is not correct, you can comment or provide a better one, saying "doesn't answer the question" somehow is not a good point ^^
I did leave a real answer below actually. Yours was at best a comment and an incorrect one at that as it had nothing to do with the subject matter CSS modules. You effectively say you don't know how so just use traditional way, which is not helpful at all. The author obviously knows traditional means, but would like to learn this particular tech. Pushing them away from it because you can't help is spamming answers
@alechill thanks for your advice, however, since the author would know best about which answer to his/her own question is a spam or not, the fact that my answer was accepted is a strong proof that it's totally NOT a spam, thanks ^^!
0

This is how I wanted it done; to each their own.

// this is the only page that should import styles this way, please use getStyles() so styles are not repeated in the header
import dig from "src/style.module.scss"


// the thought has occurred to me to put this in the DropVariables file, but getStyles of a different nature exists there, oops.
// feel free to un-clobber and move this.
export function getStyles<iCSS extends {}>(overrides: iCSS = {} as iCSS): typeof dig & iCSS {
    return {
        ...dig,
        ...overrides
    }
}

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.