lightning talks
Antal Andrei
KALON
Christopher Chedeau (@vjeux)
Facebook...React...other cool stuff
React: CSS in your JS (NationJS 2014) - about a year ago
var divStyle = {
color: 'white',
backgroundImage: 'url(' + imgUrl + ')',
WebkitTransition: 'all', // note the capital 'W' here
msTransition: 'all' // 'ms' is the only lowercase vendor prefix
};
ReactDOM.render(<div style={divStyle}>Hello World!</div>,
mountNode);
React inline styles
import color from 'color';
// You can create your style objects dynamically or share them for
// every instance of the component.
var styles = {
base: {
color: '#fff',
// pseudo-classes (:hover, :focus, :active, or @media)
':hover': {
background: color('#0074d9').lighten(0.2).hexString()
}
},
primary: {
background: '#0074D9'
},
warning: {
background: '#FF4136'
}
};
The styles object
import Radium from "radium";
import React from "react";
@Radium
class Button extends React.Component {
static propTypes = {
kind: React.PropTypes.oneOf(["primary", "warning"]).isRequired
};
render() {
return (
<button
style={[
styles.base,
styles[this.props.kind]
]}>
{this.props.children}
</button>
);
}
}
The @Radium decorator
A CSS Module is a CSS file in which all class names and animation names are scoped locally by default.
CSS Modules compile to a low-level interchange format called ICSS or Interoperable CSS, but are written like normal CSS files
When importing the CSS Module from a JS Module, it exports an object with all mappings from local names to global names
/* components/submit-button.css */
.Button { /* all styles for Normal */ }
.Button--disabled { /* overrides for Disabled */ }
.Button--error { /* overrides for Error */ }
.Button--in-progress { /* overrides for In Progress */
<!-- Apply styles to button -->
<button class="Button Button--in-progress">Processing...</button>
Classic CSS BEM way
/* components/submit-button.css */
.normal { /* all styles for Normal */ }
.disabled { /* all styles for Disabled */ }
.error { /* all styles for Error */ }
.inProgress { /* all styles for In Progress */
/* components/submit-button.js */
import styles from './submit-button.css';
buttonElem.outerHTML = `<button class=${styles.normal}>Submit</button>`
Scoping - CSS Modules way
<button class="components_submit_button__normal__abc5436">
Processing...
</button>
Scoping - The result
// webpack.config.js
config.loaders.push(
...
{ test: /\.css$/, loader: 'style-loader!css-loader?modules
&localIdentName=[name]__[local]___[hash:base64:5]' })
...);
Easy to configure (Webpack example)
.common {
/* all the common styles you want */
}
.normal {
composes: common;
/* anything that only applies to Normal */
}
.disabled {
composes: common;
/* anything that only applies to Disabled */
}
.error {
composes: common;
/* anything that only applies to Error */
}
.inProgress {
composes: common;
/* anything that only applies to In Progress */
}
Composition
/* colors.css */
.primary {
color: #720;
}
.secondary {
color: #777;
}
/* other helper classes... */
Sharing styles
/* submit-button.css */
.common { /* font-sizes, padding, border-radius */ }
.normal {
composes: common;
composes: primary from "../shared/colors.css";
}
.element {
composes: large from "./typography.css";
composes: dark-text from "./colors.css";
composes: padding-all-medium from "./layout.css";
composes: subtle-shadow from "./effect.css";
}
Single responsibility module
SRUCTURE
PRESENTATION
BEHAVIOUR
true separation of concerns