How to use global variables in CSS-Modules?

前端 未结 4 2168
猫巷女王i
猫巷女王i 2021-02-13 13:24

I\'m busy learning React with CSS-Modules and I don\'t quite understand how one would store variables? For example, in Sass you\'re able to do this:

// _variable         


        
4条回答
  •  别那么骄傲
    2021-02-13 13:53

    The CSS-Modules documentation mentions variables here: https://github.com/css-modules/css-modules/blob/master/docs/values-variables.md

    With this you can import variables as so:

    @value colors: "../../main/colors.css";
    @value primary, secondary, tertiary from colors;
    

    which can be used in your css:

    .main {
        background-color: tertiary;
        border-top: 30px solid primary;
    }
    

    To make this work postcss-loader and postcss-modules-values need to be added to your webpack config. See below:

            {
                test: /\.css$/,
                use: [{
                        loader: 'style-loader'
                    },
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            localIdentName: '[name]_[local]_[hash:base64:5]'
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            plugins: [postcssModulesValues]
                        }
                    }
                ]
            }
    

提交回复
热议问题