Resolution
Traditional CSS:
很好很強大..但仍有以下問題
awesome for web component
假如我們今天要有一個button擁有四種狀態,而在不同的頁面又有不同的顏色的話,CSS該怎麼寫呢?
.button{
//button 共用屬性
&.button-special {
//button-special 共用屬性
&.normal { ... }
&.disabled { ... }
&.touched { ... }
}
&.button-normal {
//button-normal 共用屬性
&.disabled { ... }
}
}
.PageA{
.button { ... }
.button-special { ... }
.button-normal { ... }
}
.PageB{
.button { ... }
.button-special { ... }
.button-normal { ... }
}
button.scss
page.scss
.button{
//button 共用屬性
}
.normal { ... }
.disabled { ... }
.touched { ... }
button.css
//依據使用者行為切換state狀態
return(
<button styleName="button
{ this.state.buttonState }">
</button>
)
button.jsx
Using compose
.common { /* font-sizes, padding, border-radius */ }
.normal { composes: common; /* blue color, light blue background */ }
.error { composes: common; /* red color, light red background */ }
.components_submit_button__common__abc5436 { /* font-sizes, padding, border-radius */ }
.components_submit_button__normal__def6547 { /* blue color, light blue background */ }
.components_submit_button__error__1638bcd { /* red color, light red background */ }
作者不建議,不過我的建議寫法如下:
//index.html
<link rel="stylesheet" href='/vendor/vendor.css'/>
//compoent.jsx
@import style from './style.css';
return(
<div styleName="mycssClassName">
<div className="vendorCssClassName">
</div>
)
Install
{
test: /\.css$/,
loader: "style-loader!css-loader?modules&localIdentName=[name]__[local]___[hash:base64:5]",
include: __dirname
}
Loader
npm install style-loader --save-dev
npm install css-loader --save-dev
npm install react-css-modules --save-dev
npm install css-module-require-hook --save-dev
Require-hook ( for isomorphic )
import CSSModules from 'react-css-modules';
export default connect(mapStateToProps, { loadDemo })(CSSModules(Demo,style));
component decorator
( using styleName syntax )
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]',
});
Github
https://github.com/gajus/react-css-modules
未來css的趨勢:後處理器
css-modules也是postcss的一種實作喔~
前處理器
後處理器
前處理器
常用的幾種套件