我使用css-模块以及postcss-cssnext,特别是它的postcss自定义属性特性。
我还使用了react boilerplate (https://github.com/mxstbr/react-boilerplate/),这个非常好的样板附带了这些模块。
我的文件夹结构是:
Main folder
app
containers
App
styles.css
index.js
HomePage
styles.css
index.js
components
Component1
styles.css
index.js
Component2
styles.css
index.jsApp/styes.css
:root {
--varA: #1eb3ab;
--varB: #1eb3ab;
--varC: #1eb3ab;
--varD: #1eb3ab;
--varE: #1eb3ab;
}
body {
color: var(--varA) /*works fine */
}我想将这些var应用到App/styes.css中(或者我很乐意在其他地方定义它们,在这种情况下如何实现),在其他容器中--组件
我已经尝试过(用于css-模块)
主页/index.js
..
..
<div className={styles.myClass}> Content </div>
..首页/风格.方法1
:root{
--varA: tealish from "containers/App/styles.css"; /* DOESNT WORK */
}
.myClass {
color: var(--varA)
}首页/风格.方法2
.myClass {
color: var(--varA from "containers/App/styles.css") /* DOESNT WORK AS WELL*/
}最优雅的解决方案是什么?我希望将所有变量放在一个文件/一个位置中,并在所有css文件中很好地使用它们。
我还在postcss自定义属性variables中看到了https://github.com/postcss/postcss-custom-properties#variables。但是,我不知道如何在webpack的定义中使用postcss-cssnext。
webpack.dev.babe.js -如何在这里放置变量选项
// Process the CSS with PostCSS
postcssPlugins: [
postcssFocus(), // Add a :focus to every :hover
cssnext({ // Allow future CSS features to be used, also auto-prefixes the CSS...
browsers: ['last 5 versions', 'IE > 8'], // ...based on this browser list
// I think I need to add some line here, but not sure what
}),
postcssReporter({ // Posts messages from plugins to the terminal
clearMessages: true,
}),
postcssAnimation(),
],发布于 2016-10-26 19:35:04
我推荐postcss-自定义属性“变量”选项。
文档中有一个示例
http://cssnext.io/usage/#features
cssnext({
features: {
customProperties: {
variables: {
mainColor: "red",
altColor: "blue",
}
}
}
})发布于 2016-10-27 07:18:18
我通常在postcss-进口的帮助下实现这一点,因此每个需要使用“变量”的模块都需要导入部分。
@import 'path/to/variables';
.myClass {
color: var(--colorGray);
}该方法适用于自定义属性,但也适用于自定义属性集。
类似的问题。
https://stackoverflow.com/questions/40267003
复制相似问题