Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CSS & React - Problemas, ejemplos y soluciones
Search
keyserfaty
December 30, 2017
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CSS & React - Problemas, ejemplos y soluciones
keyserfaty
December 30, 2017
More Decks by keyserfaty
See All by keyserfaty
Cómo validar una idea en 8 horas y con (casi) $0
keyserfaty
0
54
Animaciones en Vanilla JS
keyserfaty
0
50
Let's build a Virtual DOM 🌈!
keyserfaty
0
62
How to (maybe) structure a React application
keyserfaty
2
280
Featured
See All Featured
Utilizing Notion as your number one productivity tool
mfonobong
4
550
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.5k
Building Adaptive Systems
keathley
44
3.2k
Evolving SEO for Evolving Search Engines
ryanjones
0
260
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
660
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
400
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
73
41k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
800
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
YesSQL, Process and Tooling at Scale
rocio
174
15k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.7k
Transcript
@keyserfaty Problemas, ejemplos y soluciones CSS & React
@keyserfaty Karen Serfaty @keyserfaty ! Frontend Developer " No soy
experta en CSS # Organizo NodeConf y Meetup.js
@keyserfaty !
@keyserfaty
@keyserfaty Para agregar un nuevo board Para agregar un nuevo
item a un board Para marcar un item como terminado Para eliminar un item
@keyserfaty <Board /> <BoardNew />
@keyserfaty <BoardNew /> <Item /> <Board />
@keyserfaty <BoardNew /> <Item /> <InputBox /> <Board />
@keyserfaty ! CSS es global
@keyserfaty ¿qué problemas trae esto? - Naming collitions CSS no
te avisa cuando dos cosas se llaman iguales. Eso puede llevar a que dos clases se pisen.
@keyserfaty ¿qué problemas trae esto? - Subtree matches Una clase
de!nida para un padre puede afectar a un hijo que tenga la misma clase.
@keyserfaty .article .title { border-bottom: 1px solid; font-size: 1.5em; }
@keyserfaty <!-- The .article module --> <article class="article"> <h2 class="title">Article
Title</h2> <div class="content"> <p>…</p> <!-- The .widget module --> <form class="widget"> <h2 class="title">Widget Title</h2> <fieldset>…</fieldset> </form> </div> </article>
@keyserfaty <!-- The .article module --> <article class="article"> <h2 class="title">Article
Title</h2> <div class="content"> <p>…</p> <!-- The .widget module --> <form class="widget"> <h2 class="title">Widget Title</h2> <fieldset>…</fieldset> </form> </div> </article> class="title"> class="title">
@keyserfaty Isolation Naming collisions Subtree matches =
@keyserfaty Algunas cosas que estaría bueno que tuviera CSS: *
Collocation * Theming
@keyserfaty Collocation Poder integrar el CSS con JS
@keyserfaty Collocation - Reutilización de código - Poder usar el
poder de JS para escribir CSS
@keyserfaty Theming Poder usar themes para estilar la UI. (dark
version vs. light version, por ejemplo)
@keyserfaty 1) Solo BEM 2) BEM + Librería para React
3) CSS Modules 4) CSS in JS ¿qué soluciones hay para CSS?
@keyserfaty Block, element, modi!er BEM: 1) Solo BEM
@keyserfaty board board__list__item board__list__item—-completed Block Element Modifier ➡
@keyserfaty <div className="board board--full"> <div className="board__title"> {title} </div> <div className="board__list">
<ul> <li className=‘board__list__item’> <span className="board__list__item__complete"> <input title="complete" type=“checkbox" /> </span> <span className="board__list__item__body">{text}</span> <span className="board__list__item__remove"></span> </li> <li className="board__list__new-item"> <span className="board__list__item__body">+ New item</span> </li> </ul> </div> </div>
@keyserfaty ✅ BEM blocks <-> React components ✅ Tenemos una
naming convention ✅ Podemos de!nir estilos globales si queremos Cosas buenas de este approach: 1) Solo BEM
@keyserfaty 1) Solo BEM ! No resuelve el problema de
isolation en CSS ! No es collocated ! Muy verbose ! No funciona en React Native Problemas de este approach:
@keyserfaty 2) BEM + Librerías para React classnames, reBEM, (…)
BEM + Librerías para React:
@keyserfaty // bem-cn import block from 'bem-cn' var b =
block('button'); // Block b; // 'button' b(); // 'button' // Element b('icon'); // 'button__icon' // Modifier b({type: 'text'}); // 'button button_type_text' b({onlykey: true}); // 'button button_onlykey' b({without: false}); // 'button'
@keyserfaty import block from 'bem-cn' const BoardFull = props =>
{ const b = block('board'); const b_item = b('list__item') return ( <div className={b()('full')}> <div className={b('title')}> {title} </div> <div className={b('list')}> <ul> <li className={b_item()}> <span className={b_item('complete')}> <input title="complete" type=“checkbox" /> </span> <span className={b_item('body')}>{text}</span> <span className={b_item('remove')}> </span> </li> <li className={b('list__new-item')}> <span className={b_item('body')}>+ New item</span> </li> </ul> </div> </div> )}
@keyserfaty <div className={b()('full')}> <div className={b('title')}> {title} </div> <div className={b('list')}> <ul>
<li className={b_item()}> <span className={b_item('complete')}> <input title="complete" type=“checkbox" /> </span> <span className={b_item('body')}>{text}</span> <span className={b_item('remove')}></span> </li> <li className={b('list__new-item')}> <span className={b_item('body')}>+ New item</span> </li> </ul> </div> </div>
@keyserfaty <div className="board board--full"> <div className="board__title"> {title} </div> <div className="board__list">
<ul> <li className=‘board__list__item’> <span className="board__list__item__complete"> <input title="complete" type=“checkbox" /> </span> <span className="board__list__item__body">{text}</span> <span className="board__list__item__remove"></span> </li> <li className="board__list__new-item"> <span className="board__list__item__body">+ New item</span> </li> </ul> </div> </div>
@keyserfaty Cosas buenas de este approach: ✅ Nos resuelve el
problema de lo verbose ✅ El CSS se vuelve más fácil de mantener ✅ + todas las ventajas del approach anterior 2) BEM + Librerías para React
@keyserfaty 2) BEM + Librerías para React Problemas de este
approach: ! No resuelve el problema de isolation en CSS ! No es collocated ! No funciona en React Native
@keyserfaty CSS Modules 3) CSS Modules
@keyserfaty // antes de css-modules <div className='board'></span> // después de
css-modules <div className=‘board_12l3jk12k’></span>
@keyserfaty import styles from ‘./styles.css’ import cn from ‘classnames’ const
BoardFull = props => { return ( <div className={cn(styles.board, styles.full)}> <div className={styles.title}> {title} </div> <div className={styles.list}> <ul> <li className={styles.item}> <span className={styles.item-complete}> <input title="complete" type=“checkbox" /> </span> <span className={styles.item-body}>{text}</span> <span className={styles.item-remove}> </span> </li> <li className={styles.item-new}> <span className={styles.item-body}>+ New item</span> </li> </ul> </div> </div> )}
@keyserfaty 3) CSS Modules Cosas buenas de este approach: ✅
Es 100% isolated ✅ Es más o menos collocated
@keyserfaty 3) CSS Modules Problemas de este approach: ! Se
necesita un building system " ! No soporta theming ! No funciona en React Native ⚠ Soporta estilos globales pero de forma rara
@keyserfaty 4) CSS in JS CSS in JS JSS
@keyserfaty CSS in JS vs. Inline styles
@keyserfaty // Inline styles const styles = { color: ‘#fff’
} <div styles={styles}></div> // CSS in JS const styles = { color: ‘#fff’ } <div className={styles}></div>
@keyserfaty import injectSheet from 'react-jss' const styles = { button:
{ background: props => props.color }, label: { fontWeight: 'bold' } } const Button = ({classes, children}) => ( <button className={classes.button}> <span className={classes.label}> {children} </span> </button> ) export default injectSheet(styles)(Button)
@keyserfaty import injectSheet from 'react-jss' const BoardFull = ({ styles
}) => { return ( <div className={cn(styles.board, styles.full)}> <div className={styles.title}> {title} </div> <div className={styles.list}> <ul> <li className={styles.item}> <span className={styles.item-complete}> <input title="complete" type=“checkbox" /> </span> <span className={styles.item-body}>{text}</span> <span className={styles.item-remove}> </span> </li> <li className={styles.item-new}> <span className={styles.item-body}>+ New item</span> </li> </ul> </div> </div> )} export default injectSheet(styles)(BoardFull)
@keyserfaty 4) CSS in JS Cosas buenas de este approach:
✅ Es 100% isolated y colocated ✅ Se puede usar el 100% de CSS ✅ Funciona en React Native
@keyserfaty 4) CSS in JS Problemas de este approach: !
Necesita un wrapper component ⚠ Soporta más o menos estilos globales ⚠ Soporta más o menos theming
@keyserfaty ¿conclusión?
@keyserfaty CSS
@keyserfaty resumen: ✅ Isolation ✅ Collocation ⚠ Todo lo demás
@keyserfaty cosas de las que no hablé: Styled components Inline
styles
@keyserfaty cosas para seguir: https://philipwalton.com/articles/side-effects-in-css/ https://www.youtube.com/playlist?list=PL7BkG8tGKrU0Xq7KNp_j4mVTRGoKKZ61u
@keyserfaty gracias @keyserfaty