Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
CSS & React - Problemas, ejemplos y soluciones
Search
keyserfaty
December 30, 2017
120
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
51
Let's build a Virtual DOM 🌈!
keyserfaty
0
66
How to (maybe) structure a React application
keyserfaty
2
280
Featured
See All Featured
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
320
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
360
Statistics for Hackers
jakevdp
799
230k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.9k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
550
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
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