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
Atomic CSS with Fela
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Ben Smithett
July 01, 2020
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Atomic CSS with Fela
Ben Smithett
July 01, 2020
More Decks by Ben Smithett
See All by Ben Smithett
Objects in Space: A practical guide to CSS display, position & z-index
bensmithett
0
75
The 12MB Web Page: A tale of perfect PageSpeed scores & developer happiness
bensmithett
1
350
The UI is an Application
bensmithett
0
1.2k
Hands on with the modern front end stack
bensmithett
0
1.3k
The New Front End Stack: A really really really high level introduction
bensmithett
1
550
Smarter Sass/Less Builds with Webpack (MelbJS Edition)
bensmithett
0
350
Smarter Sass builds with Webpack
bensmithett
1
260
Faking Real Time: Optimistic Updates & Eventual Consistency (Decompress 2015)
bensmithett
0
510
Other Decks in Programming
See All in Programming
作るコストが小さくなった時代 幸せに働くために改めて考えたいこと 〜エンジニアとして価値を出し続けるために注視している二分野〜
yuppeeng
0
110
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
360
Performance Engineering for Everyone
elenatanasoiu
0
270
Welcome to the "Parametricity" 🏙️ − Generic だけど Specific な世界 −
guvalif
PRO
1
170
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
270
関数型プログラミングのメリットって何だろう?
wanko_it
0
180
どこまでゆるくて許されるのか
tk3fftk
0
510
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1k
Built Our Own Background Agent at LayerX #aidevex_findy
layerx
PRO
3
720
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
0
310
5分で問診!Composer セキュリティ健康診断
codmoninc
0
430
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
150
Featured
See All Featured
Six Lessons from altMBA
skipperchong
29
4.3k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
290
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
610
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
900
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
Designing Experiences People Love
moore
143
24k
KATA
mclloyd
PRO
35
15k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
310
Building Applications with DynamoDB
mza
96
7.1k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
67
56k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
The Curious Case for Waylosing
cassininazir
1
430
Transcript
Atomic CSS with
What is Atomic CSS? compose styles directly on HTML tags
instead of in abstract CSS classes
Normal: style composition in CSS classes .header { background: black;
padding: 10px; display: flex; } <div class='header'>
Normal: style composition in CSS classes .header { background: black;
padding: 10px; display: flex; } <div class='header'> Not normal: inline styles <div style='background: black; padding: 10px; display: flex;'>
Atomic CSS .bg-black { background: black; } .padding-s { padding:
10px; } .flex { display: flex; } <div class='bg-black padding-s flex'> Normal: style composition in CSS classes .header { background: black; padding: 10px; display: flex; } <div class='header'> Not normal: inline styles <div style='background: black; padding: 10px; display: flex;'>
why? Atomic CSS .bg-black { background: black; } .padding-s {
padding: 10px; } .flex { display: flex; } <div class='bg-black padding-s flex'> Normal: style composition in CSS classes .header { background: black; padding: 10px; display: flex; } <div class='header'> Not normal: inline styles <div style='background: black; padding: 10px; display: flex;'> 🚀P🚀E🚀R🚀F🚀O🚀R🚀M🚀A🚀N🚀C🚀E🚀 (among other reasons)
developing new features the normal way:
header.css .header { background: black; padding: 10px; display: flex; }
.header-logo { border: 0; }
header.css .header { background: black; padding: 10px; display: flex; }
.header-logo { border: 0; } footer.css .footer { background: black; padding: 10px; display: grid; } .footer-links { display: flex; flex-direction: column; }
CSS size (kb) Features added over time
CSS size (kb) Features added over time 🐌
CSS size (kb) Features added over time 🐌 code splitting
CSS size (kb) Features added over time 🐌 code splitting
CSS Modules `composes`
CSS size (kb) Features added over time 🐌 code splitting
CSS Modules `composes` lazy-load below-the-fold sections
header.css .header { background: black; padding: 10px; display: flex; }
.header-logo { border: 0; } footer.css .footer { background: black; padding: 10px; display: grid; } .footer-links { display: flex; flex-direction: column; }
developing new features with Atomic CSS:
styles.css .bg-black { background: black; } .padding-s { padding: 10px;
} .flex { display: flex; } .border-0 { border: 0; } header.template <div class='bg-black padding-s flex'> <img class='border-0' /> </div>
styles.css .bg-black { background: black; } .padding-s { padding: 10px;
} .flex { display: flex; } .border-0 { border: 0; } .grid { display: grid; } .flex-col { flex-direction: column; } header.template <div class='bg-black padding-s flex'> <img class='border-0' /> </div> footer.template <div class='bg-black padding-s grid'> <div class='flex flex-col' /> </div>
styles.css .bg-black { background: black; } .padding-s { padding: 10px;
} .flex { display: flex; } .border-0 { border: 0; } .grid { display: grid; } .flex-col { flex-direction: column; } header.template <div class='bg-black padding-s flex'> <img class='border-0' /> </div> footer.template <div class='bg-black padding-s grid'> <div class='flex flex-col' /> </div>
CSS size (kb) Features added over time
yay! but...
Hand-coding Atomic CSS is gross Learning an abstraction bg-black border-xl
flex pad-s instead of just writing the CSS you already know
Hand-coding Atomic CSS is gross bg-black border-xl flex pad-s not
CSS not on MDN
what if we could write dev-friendly not-very-performant-looking real CSS ...but
ship atomic classes?
write the CSS you know generate atomic classes
header.js <div className={css({ background: 'black', padding: 10, display: 'flex' })}>
<img className={css({ border: 0 })} /> </div>
header.js <div className={css({ background: 'black', padding: 10, display: 'flex' })}>
<img className={css({ border: 0 })} /> </div> footer.js <div className={css({ background: 'black', padding: 10, display: 'grid' })}> <div className={css({ display: 'flex',( flexDirection: 'column' })} /> </div>
header.js <div className={css({ background: 'black', a padding: 10, b display:
'flex' c })}> <img className={css({ border: 0 d })} /> </div> footer.js <div className={css({ background: 'black', a padding: 10, b display: 'grid' e })}> <div className={css({ display: 'flex',( c flexDirection: 'column' f })} /> </div>
header.js <div className={css({ background: 'black', a padding: 10, b display:
'flex' c })}> <img className={css({ border: 0 d })} /> </div> footer.js <div className={css({ background: 'black', a padding: 10, b display: 'grid' e })}> <div className={css({ display: 'flex',( c flexDirection: 'column' f })} /> </div> output HTML // header <div class='a b c'> <img class='d' /> </div> // footer <div class='a b e'> <div class='c f' /> </div> output CSS .a { background: black } .b { padding: 10px } .c { display: flex } .d { border: 0 } .e { display: grid } .f { flex-direction: column }
header.js <div className={css({ background: 'black', padding: 10, display: 'flex' })}>
<img className={css({ border: 0 })} /> </div> footer.js <div className={css({ background: 'black', padding: 10, display: 'grid' })}> <div className={css({ display: 'flex',( flexDirection: 'column' })} /> </div> 😱 CSS in JS!
It's the output that matters Authoring style is flexible 🙂
not so inline const root = { ... } const
logo = { ... } <div className={css(root)}> <img className={css(logo)} /> </div>
not so inline const root = { ... } const
logo = { ... } <div className={css(root)}> <img className={css(logo)} /> </div> import from another file import {root, logo} from './header.css.js' <div className={css(root)}> <img className={css(logo)} /> </div>
not so inline const root = { ... } const
logo = { ... } <div className={css(root)}> <img className={css(logo)} /> </div> import from another file import {root, logo} from './header.css.js' <div className={css(root)}> <img className={css(logo)} /> </div> (with enough Webpack tomfoolery)
not so inline const root = { ... } const
logo = { ... } <div className={css(root)}> <img className={css(logo)} /> </div> import from another file import {root, logo} from './header.css.js' <div className={css(root)}> <img className={css(logo)} /> </div> (with enough Webpack tomfoolery) Styled Components const Root = styled.div` background: black; padding: 10px; display: flex; ` const Logo = styled.img` border: 0; ` <Root> <Logo /> </Root>
You choose how you write CSS (IMHO just use JS
objects, ask me why later) Fela gives you optimised output
Unlearn "clever" optimisations
CSS size (kb) Features added over time 🐌 code splitting
CSS Modules `composes` lazy-load below-the-fold sections
CSS Modules `composes` code splitting lazy-load below-the-fold sections 🗑
🗑 📄 📄 📄 CSS files
<html> <head> <style>.a{background:black}.b{padding:10px}.c{display:flex}.d{border:0}</style> </head> <body> <div class='a b c'> <img
class='d' /> </div> <body> </html> just inline the initial render's CSS in <head>
fela.js.org styletron.org sanblas.netlify.app inspect new facebook.com in devtools Show me
more!