2

I've got this in a global style.css:

:root {
    --font: {
        font-family: "Source Sans Pro", sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}

... and this in a react component's style.css:

.rightnav {
    @apply --font;
    float: right;
    color: white;
}

In the component itself:

import React from "react"
import cssModules from "react-css-modules"
import style from "./style.css"

render() {
  return (
        <ul>
          <li className={style.rightnav}>Blog</li>
        </ul>
  )
}

I get an error: No custom properties set declared for font.

1 Answer 1

1

You've got to import the global stylesheet into the local one. Because postcss-apply has no way to know about that global file while parsing the local one. So it's a good idea to have a partial with only custom properties and custom property sets declarations.

You probably want something like postcss-import

@import 'path/to/variables.css';

.rightnav {
  @apply --font;
  float: right;
  color: white;
}
Sign up to request clarification or add additional context in comments.

1 Comment

If any classes or element styles were defined in the file you want to import variables from, this technique would also duplicate those classes/element styles. Would be good if there was a way to only import the css properties set that you need rather than importing the whole file, but I haven't found a way yet...

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.