1

I use CSS modules and my css/stylesheet files live next to my components.

What is the fastest way to support a global colors file that can be imported into my stylesheet?

Does my css-modules and webpack support this by default? Help.

1 Answer 1

4

Are you using CRA? If so, you can. You just have to make sure that you are using postcss and the postcss-modules-values plugin. NOTE: Haven't tested this without Create React App (yet), so if anyone has, it would help to know.

Now, all you need is create a stylesheet (or simply use the index.css, your choice), and do this:

@value blue: #0c77f8;
@value red: #ff0000;
@value green: #aaf200;

And now, from your component-scoped css file, just import and use (almost JS-like):

/* import your colors... */
@value colors: "./colors.css";
@value blue, red, green from colors;

.button {
  color: blue;
  display: inline-block;
}

Source(docs).

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

1 Comment

Of course you can also make it shorter, for example like this: @value yourBlue, yourOtherColor from "../../index.css"; Your preference.

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.