Kyrylo Yakovenko(@blia)
Preprocessors bring a lot of cool features to CSS:
It's good for HTML documents 👍, but not for small components👎.
<
link rel="stylesheet" href="node_modules/comp1/style.css" ><
link rel="stylesheet" href="node_modules/comp2/style.css" ><
link rel="stylesheet" href="node_modules/comp3/style.css" ><
link rel="stylesheet" href="node_modules/comp4/style.css" >...
JavaSctipt is entry point
//component1/index.js
import styles from './styles.css' ;
//component2/index.js
import styles from './styles.css' ;
let btnClass = 'btn' ;
if (this .state .isPressed ) btnClass+= ' btn-pressed' ;
else if (this .state .isHovered ) btnClass+= ' btn-over' ;<
button className={btnClass}>;
import classNames from 'classnames' ;
const btnClass= classNames({btn
: true,'btn-pressed'
: this .state .isPressed ,'btn-over'
: !this .state .isPressed &&this .state .isHovered });
<
button className={btnClass}>;
A CSS Module is a CSS file in which all class names and animation names are scoped locally by default.
import classNames from 'classnames' ;
import styles from './styles.css' ;
const btnClass= classNames({styles[
'btn' ]: true,styles[
'btnPressed' ]: this .state .isPressed ,styles[
'btnOver' ]: this .state .isHovered });
<
button className={btnClass}>;
import classNames from 'classnames/bind' ;
import styles from './styles.css' ;
var cx= classNames.bind(styles);
var btnClass= cx({btn
: true,btnPressed
: this .state .isPressed ,btnOver
: this .state .isHovered });
<
button className={btnClass}>;
const styles= StyleSheet.create({red
: {color:
'red' ,fontSize:
16 },
});
<
Text style={styles.red}>just red</Text >
Actually, no.
const styles= StyleSheet.create({red
: css`
color :red ;
font-size :16 ;
` ,});
<
Text style={styles.red}>just red</Text >
import styled from 'styled-components' ;
const RedText= styled.Text`
color :red ;
font-size :16 ;
` ;<
RedText >just red</RedText >
Yes!
import styled from 'styled-components' ;
const RedText= styled.Text`
color :red ;
font-size :16 ;
` ;
const BlueText= styled.Text`
color :blue ;
font-size :16 ;
` ;<
RedText >just red</RedText ><
BlueText >just blue</BlueText >
import css from 'my-css-lib' ;
const { RedText, BlueText }= css`
.red-text {
color :red ;
font-size :16 ;}
.blue-text {
color :blue ;
font-size :16 ;}
` ;<
RedText >just red</RedText ><
BlueText >just blue</BlueText >
import css from 'my-css-lib' ;
const { RedText, BlueText }= css`
.red-text {
color :red ;
font-size :16 ;}
.blue-text {
color :blue ;
font-size :16 ;}
` ;<
RedText >just red</RedText ><
BlueText >just blue</BlueText >
import css from 'my-css-lib' ;
const { RedText, BlueText }= css` div
.red-text {
color :red ;
font-size :16 ;}
p
.blue-text {
color :blue ;
font-size :16 ;}
` ;<
RedText >just red</RedText ><
BlueText >just blue</BlueText >
import css from 'my-css-lib' ;
import ActionButton from './action-button' ;
const { LoginButton }= css` ActionButton
.login-button {
background-color :green ;
font-size :16 ;}
` ;<
LoginButton >Log in</LoginButton >
import css from 'my-css-lib' ;
const { Link }= css` a
.link {
color :blue ;
text-decoration :underline ;
:hover {
text-decoration :none ;}
}
` ;<
Link href="/" >Home</Link >
import css from 'my-css-lib' ;
const { Link }= css` a
.link {
color :blue ;
text-decoration :underline ;
:hover {
text-decoration :none ;}
:is-active {
color :red ;}
}
` ;<
Link href="/" isActive={isHomePage}>Home</Link >
import css from 'my-css-lib' ;
const { Link }= css` a
.link {
color : ${props=> props.isActive ?'red' :'blue' };
text-decoration :underline ;
:hover {
text-decoration :none ;}
}
` ;<
Link href="/" isActive={isHomePage}>Home</Link >
import css from 'my-css-lib' ;
const { MyButton }= css`
.my-button {
color :blue ;
display :block ;}
` ;
import css from 'my-css-lib' ;
const { MyButton }= css`
.my-button {css.color(
'blue' );css.display(
'block' );}
` ;
import css from 'my-css-lib' ;
const { MyButton }= css`
.my-button {...{color:
'blue' };...{display:
'block' };}
` ;
import css from 'my-css-lib' ;css.use(
'box' , size=> ({width: size, height: size}));
const { MyBox }= css`
.my-box {background-color:
green ;box:
20 px;}
` ;
import css from 'my-css-lib' ;css.use(
'box' , size=> ({width: size, height: size}));
const { MyBox }= css`
.my-box {background-color:
green ;width:
20 px;height:
20 px;}
` ;
import css from 'my-css-lib' ;
const box= size=> ({width: size, height: size});css.use({ box });
const { MyBox }= css`
.my-box {background-color:
green ;box:
20 px;}
` ;
import css from 'my-css-lib' ;
import polished from 'polished' ;css.use(
'po' , polished);
const { MyCell }= css`
.my-cell {display:
table-cell ;-po-ellipsis:
250 px;}
` ;
import css from 'my-css-lib' ;
const activeColor= color
=> ctx=> ({color: ctx.isActive ? color :'blue' });css.use({ activeColor });
const { Link }= css` a
.link {
active-color :red ;
text-decoration :underline ;}
` ;<
Link href="/" isActive={isHomePage}>Home</Link >
import css from 'my-css-lib' ;
const activeColor= color
=> ctx=> ({color: ctx.isActive ? color :'blue' });
const { Link }= css` a
.link {
-local-active-color :red ;
text-decoration :underline ;}
` ;<
Link href="/" isActive={isHomePage}>Home</Link >
import css from 'my-css-lib' ;
const activeColor= color
=> ctx=> ({color: ctx.isActive ? color :'blue' });
const { Link }= css` a
.link {
-active-color :red ;
text-decoration :underline ;}
` ;<
Link href="/" isActive={isHomePage}>Home</Link >