React Amsterdam 2016
const style = {color: red}
<div style={style}></div>
<div style="color: red"></div>
HTML
React
const element = document.createElement('div')
element.style.color = 'red'
DOM
Selectors are the evil.
Try to write an application inside of
one function.
<button class="default-button my-button">My Button</button>
/* buttons.css */
.default-button {
cursor: pointer;
}
/* my-component.css */
.my-button {
color: red;
}
A task for a compiler.
function module() {
var styles = {
button: {
color: 'green'
},
myButton: {
color: 'red'
}
}
// Renders a button.
button({style: styles.myButton})
}
function module(){button({style:{color:"red"}})};
function test(a) {
var obj = {a: a, b: 2};
return obj.a + obj.b;
}
window.a = test(1)
window.a=3;
CSS Specificity Concept.
/* my-button-1.css */
.my-button.my-blue-button {
color: blue;
}
/* my-button-2.css */
button.my-button {
color: red;
}
<head>
<link href="my-button-1.css" rel="stylesheet" />
<link href="my-button-2.css" rel="stylesheet" />
</head>
<body>
<button class="my-button my-blue-button">My Button</button>
</body>
.article #comments ul > li > a.button
Smaller payload.
Text
Radium is using:
Designed with components in mind.
Easy to reason about.
Virtual
CSS Tree
Process
Render
Run plugins on every VRule
Output <style>
with CSS.
Abstraction for CSS Rules Manipulation
import React from 'react'
import {useSheet} from 'react-jss'
@useSheet({
button: {
color: 'green'
}
})
export default function Button(props) {
const {classes} = props.sheet
return <button className={classes.button}>{props.text}</button>
}
No magic in code.
1. Media Queries
2. Keyframes Animation
3. Font Face
4. Pseudo Selectors
5. Fallbacks
6. Rules Caching
7. Rules Sharing
8. Extensible Architecture
9. Tools Agnostic
10. Vendor Prefixer
11. Inheritance
DSL
Library
export default {
'@media (min-width: 1024px)': {
button: {
minWidth: 200
}
}
}
const breakpoint = 1024
export default {
[`@media (min-width: ${breakpoint}px)`]: {
button: {
minWidth: 200
}
}
}
@media (min-width: 1024px) {
.button-jss-0 {
min-width: 200px;
}
}
JSS in ES5
JSS in ES6
CSS
Not possible Inline.
export default {
'@keyframes my-animation': {
from: {opacity: 0},
to: {opacity: 1}
}
}
const identifier = random()
export default {
[`@keyframes ${identifier}`]: {
from: {opacity: 0},
to {opacity: 1}
}
}
@keyframes my-animation {
from { opacity: 0; }
to { opacity: 1; }
}
JSS in ES5
JSS in ES6
CSS
Not possible Inline.
export default {
'@font-face': {
fontFamily: 'MyWebFont',
src: [
'url(webfont.eot)',
'url(webfont.eot?#iefix) format(embedded-opentype)',
'url(webfont.woff2) format(woff2)'
]
}
}
@font-face {
font-family: 'MyWebFont';
src: url('webfont.eot');
src: url('webfont.eot?#iefix') format('embedded-opentype'),
url('webfont.woff2') format('woff2');
}
JSS
CSS
Not possible Inline.
export default {
button: {
color: 'green'
'&:hover': {
color: 'red'
},
'&:active': {
color: blue
},
'&:before': {
content: '"icon"'
},
'& span': {
verticalAlign: 'middle'
}
}
}
.jss-0-1 {
color: green;
}
.jss-0-1:hover {
color: red;
}
.jss-0-1:active {
color: blue;
}
.jss-0-1:before {
content: "icon";
}
.jss-0-1 span {
vertical-align: middle;
}
JSS
CSS
Not possible Inline.
Inspired by Sass.
export default {
container: {
color: [
'red',
'linear-gradient(to right, red 0%, green 100%)'
]
}
}
.jss-0-1 {
color: red;
color: linear-gradient(to right, red 0%, green 100%);
}
JSS
CSS
Not possible Inline.
Not possible Inline.
CSS Rules are created just once.
Not possible Inline.
One rule applies to all list items
Inline Styles rendering pipeline:
Up to 50% smaller payload
when rendered at runtime.
Plugin
export default {
container: {
display: 'flex'
}
}
.jss-0-1 {
display: -webkit-flex;
}
JSS
CSS
Styles inherit from parent.
Still no TRUE Isolation.
Created a JSS plugin.
and
SOLVED INHERITANCE PROBLEM
Maxim Koretskiy
This plugin protects styles from inheritance. It automatically creates a reset rule and applies it to every user's rule.
If you feel it - don't use it.
import color from 'color'
import fonts from 'theme/fonts'
import mixins from 'jss-mixins'
import {gainsboroLight, grapeTypo, grey, blue, white} from 'theme/colors'
const grapeTypoSemi = color(grapeTypo).alpha(0.5).rgbaString()
export default {
datalist: {
background: white,
border: `1px solid ${gainsboroLight}`,
boxShadow: `0px 3px 4px 0 ${grapeTypoSemi}`,
overflow: 'auto'
},
item: {
extend: [fonts.normal, mixins.ellipsis],
padding: `5px 7px`,
color: grey,
cursor: `pointer`
},
`@media (min-width: 1024px)`: {
item: {
color: blue
}
}
}
export default css`
datalist {
background: ${white};
border: 1px solid ${gainsboroLight};
box-shadow: 0px 3px 4px 0 ${grapeTypoSemi};
overflow: auto
}
item {
extend: ${[fonts.normal, mixins.ellipsis]};
padding: 5px 7px;
color: ${grey};
cursor: ${pointer};
}
@media (min-width: 1024px) {
item {
color: ${blue}
}
}
`
Tagged template literals
Styles reuse only together with the lib.
State styles. For e.g. when a "width" of a component depends on its state.
Animations.