SlideShare a Scribd company logo
Start your app the better way with
Styled System
Agenda
● History of CSS and it’s original sin
● The key problems of customizing UI component libraries’ styles
● Styled System
● QnA
● References
Everything in CSS is global.
.list { ... }
.list .item { ... }
Name collisions
.list { ... }
.list .item { ... }
.list { ... }
.list .item { ... }
Reusability
OOCSS
BEM
SMACSS
.card-list
Element
A component of a
block
Modifier
State for a block or
element.
Block
A block component
.card-list__item .card-list__item--highlight
Preprocessor /
Postprocessor
LESS
SASS
SCSS
PostCSS
.search-box-mixin(@scope) {
.@{scope}-search-box {
.search-input {
border: 1px solid @gray;
}
.tooltip {
margin: 8px 0 0 1px;
}
}
}
.search-box-mixin('page');
.page-search-box .search-input {
border: 1px solid #ddd;
}
.page-search-box .tooltip {
margin: 8px 0 0 1px;
}
Components are the building blocks of
modern web applications.
CSS Modules
Scope CSS rules locally by
using tools to transform the
class name with a hash.
.3zyde4l
.card-list__item .card-list__item--highlight
.card-list
.ucdIPRBM
.1Jf-igmQ
CSS in JS
Write CSS in components.
import styled from
'styled-components';
const Button = styled.button`
margin: 0 10px;
padding: 10px;
background: #fefefe;
border-radius: 3px;
border: 1px solid #ccc;
color: #525252;
`
Still have some problems...
● Components with inconsistent props.
● How to efficiently implement multiple themes? Like SMASCC…
● How to efficiently implement responsive styles for different devices?
● How to reduce redundant codes for setting media queries or mapping
props to css properties?
Recap
As an user, I care about...
● Is it easy to custom styles?
● Does it support global theme styles and it is easy to custom?
As an developer, I care about...
● Develop UI with HIE’s design.
Ant Design
Step 1: Find the target element by
using inspector.
Step 2: Extend LESS files to custom
button styles.
@import '~antd/lib/button/style/index.less';
@btn-default-color: blue;
button
button font-color: blue
font-color: black
Custom text color
import styled from 'styled-components';
import { Button as AntdButton } from 'antd';
const CustomButton = styled(AntdButton)`
span {
color: blue;
}
}`;
export default CustomButton;
Ant Design
Step 1: Find the target element by
using inspector.
Step 2: Wrap AntdButton by using
CustomButton.
Could it be easier?
Start your app the better way with
Styled System
Consistency Mobile-First
Why Styled System is good?
More concise
codes
Easy to
custom styles
More Concise Codes
Utility functions make codes short and clear,
expressing styles without unnecessary words
Utility functions (1/2)
<Box color='#000 bg='tomato' />
const Box = styled.div`
margin: 15px 0;
padding: 15px;
color: ${(props) => props.color};
background: ${(props) => props.bg};
border-radius: 10px;
`;
const getStyles = ({ color, bg }) => ({
color,
background: bg,
});
const Box = styled.div`
${getColor};
margin: 15px 0;
padding: 15px;
border-radius: 10px;
`;
Utility functions (2/2)
import { color } from 'styled-system';
const Box = styled.div`
${color}
margin: 15px 0;
padding: 15px;
border-radius: 10px;
`;
const getStyles = ({ color, bg }) => ({
color,
background: bg,
});
const Box = styled.div`
${getColor};
margin: 15px 0;
padding: 15px;
border-radius: 10px;
`;
Styled
System
Consistency
Make components with consistent prop names
Inconsistent props
<Button color='black'>Click</Button> <Label fontColor='white'>$</Label>
<Button color='black'>Click</Button> <Label color='white'>$</Label>
Use color utility
function in
Styled System
Easy to custom styles
● Define global theme by a config object
● Define individual component styles by variants
Theming
Utilizing `<ThemeProvider>` and pass the theme object from root node to provide
global theme.
<ThemeProvider theme={theme}>
<Box color='black' bg='tomato'/>
</ThemeProvider>
const theme = {
color: {
black: '#333',
},
bg: {
tomato: 'tomato',
},
};
Define component styles in theme object
const theme = {
buttons: {
danger: {
color: 'white',
background: '#f25e7a'
},
size: {
default: { height: 50 },
large: { height: 100 }
}
}
};
<Button variant="danger" size="large" />
const buttonStyle = variant({ key:
'buttons'});
const buttonSizeStyle = variant({
prop: 'size', key: 'buttons.size' });
const Button = styled.div`
${buttonStyle}
${buttonSizeStyle}
padding: 15px;
`;
Variants
Use variants to define component styles.
<Box variant='secondary'/>
import { variant } from 'styled-system';
const Box = styled('div')(
variant({
variants: {
primary: {
color: 'black',
bg: 'tomato'
},
secondary: {
color: 'black',
bg: 'yellow'
},
},
}),
);
<Box variant='primary' />
Mobile-First
Create mobile-first responsive layouts with
array syntax
Responsive styles
Create mobile-first responsive layouts with ease by using an array syntax.
.thing {
font-size: 16px;
width: 100%;
}
@media screen and (min-width: 40em) {
font-size: 20px;
width: 50%;
}
@media screen and (min-width: 52em) {
font-size: 24px;
}
<Thing
fontSize={[ 16, 20, 24 ]}
width={[1, 1/2]}
/>Styled
System
Consistency Mobile-First
Styled System is good!
More concise
codes
Easy to
custom styles
Custom
component styles
<Button>Button</Button>
<Button
variantColor='green'
color='#ddd'
>
Button
</Button>
<Button
bg='#41d2f2'
color='fff'
>
Button
</Button>
Theme
Use theme object to define or
extend theme styles
import { theme } from '@chakra-ui/core';
export default {
...theme,
colors: {
...theme.colors,
brand: {
white: '#fefefe',
blue: '#41d2f2',
yellow: '#f2dc6d',
purple: '#7700bb',
},
},
};
<Button bg='#41d2f2' color='brand.white' />
Responsive
styles
Define values to each
breakpoint respectively
.Box {
width: 100%;
}
@media screen and (min-width: 40em) {
width: 50%;
}
@media screen and (min-width: 52em) {
width: 25%;
}
<Box width={['100%', 1 / 2, 1 / 4]} />
Styles overlapping
Props passed by Styled System should put above the original ones.
const Box = styled.div`
margin: 15px 0;
padding: 15px;
border-radius: 10px;
${color}
`;
const Box = styled.div`
${color}
margin: 15px 0;
padding: 15px;
border-radius: 10px;
`;
Avoid css
overlap
Traditional CSS rules feat. Styled Components
#root div { color: red; } /* in site.css, score: 100 + 1 = 101 */
.jqouBD { color: black; } /* in styled component, score: 10 */
#root div { color: red; } /* in site.css, score: 100 + 1 = 101 */
.jqouBD { color: black !important; } /* in styled component, score: 10000 */
source
Class name without a hash for end-to-end testing
Use a chaninable method “attrs” to attach props to the styled component.
<Button>Click me!</Button>
const Button = styled.button.attrs({ className: 'button-submit' })`...`;
<button class="sc-EHOje button-submit">Click me!</button>
View in
browser
inspector
References
● Styled System https://styled-system.com/
● We need a better UI component library - Styled System http://bit.ly/2roJEsg
● 從 Styled System 看下一代 CSS 技術 https://bit.ly/2u0xvLJ
● The Three Tenets of Styled System http://bit.ly/35ygrcV
● Styled System: Pseudo selectors in Variant http://bit.ly/35yqGhy
● How to create responsive UI with styled-components http://bit.ly/34lhxJ3
● CSS 實戰心法 http://bit.ly/2shXUn4

More Related Content

What's hot

Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSS
Chris Coyier
 
GQL CheatSheet 1.1
GQL CheatSheet 1.1GQL CheatSheet 1.1
GQL CheatSheet 1.1
sones GmbH
 
GQL cheat sheet latest
GQL cheat sheet latestGQL cheat sheet latest
GQL cheat sheet latest
sones GmbH
 
GWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXTGWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXT
niloc132
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
cfc
 
GWT.create 2013: Themeing GWT Applications with the Appearance Pattern
GWT.create 2013: Themeing GWT Applications with the Appearance PatternGWT.create 2013: Themeing GWT Applications with the Appearance Pattern
GWT.create 2013: Themeing GWT Applications with the Appearance Pattern
niloc132
 

What's hot (20)

jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
 
Building a theming system with React - Matteo Ronchi - Codemotion Rome 2017
Building a theming system with React - Matteo Ronchi - Codemotion Rome 2017Building a theming system with React - Matteo Ronchi - Codemotion Rome 2017
Building a theming system with React - Matteo Ronchi - Codemotion Rome 2017
 
The Customizer
The CustomizerThe Customizer
The Customizer
 
Using jQuery to Extend CSS
Using jQuery to Extend CSSUsing jQuery to Extend CSS
Using jQuery to Extend CSS
 
Polymer 1.0
Polymer 1.0Polymer 1.0
Polymer 1.0
 
Workshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effectsWorkshop 18: CSS Animations & cool effects
Workshop 18: CSS Animations & cool effects
 
SenchaCon 2016: Improve Workflow Driven Applications with Ext JS Draw Package...
SenchaCon 2016: Improve Workflow Driven Applications with Ext JS Draw Package...SenchaCon 2016: Improve Workflow Driven Applications with Ext JS Draw Package...
SenchaCon 2016: Improve Workflow Driven Applications with Ext JS Draw Package...
 
[Quality Meetup] M. Górski, M. Boś - Testy UI w Espresso z farmą w tle
[Quality Meetup] M. Górski, M. Boś - Testy UI w Espresso z farmą w tle[Quality Meetup] M. Górski, M. Boś - Testy UI w Espresso z farmą w tle
[Quality Meetup] M. Górski, M. Boś - Testy UI w Espresso z farmą w tle
 
SenchaCon 2016: Developing COSMOS Using Sencha Ext JS 5 - Shenglin Xu
SenchaCon 2016: Developing COSMOS Using Sencha Ext JS 5 - Shenglin XuSenchaCon 2016: Developing COSMOS Using Sencha Ext JS 5 - Shenglin Xu
SenchaCon 2016: Developing COSMOS Using Sencha Ext JS 5 - Shenglin Xu
 
[ HackFest.pl 2012] Testing - what for and how
[ HackFest.pl 2012] Testing - what for and how[ HackFest.pl 2012] Testing - what for and how
[ HackFest.pl 2012] Testing - what for and how
 
A Tour to MySQL Commands
A Tour to MySQL CommandsA Tour to MySQL Commands
A Tour to MySQL Commands
 
GQL CheatSheet 1.1
GQL CheatSheet 1.1GQL CheatSheet 1.1
GQL CheatSheet 1.1
 
GQL cheat sheet latest
GQL cheat sheet latestGQL cheat sheet latest
GQL cheat sheet latest
 
Creating GUI Component APIs in Angular and Web Components
Creating GUI Component APIs in Angular and Web ComponentsCreating GUI Component APIs in Angular and Web Components
Creating GUI Component APIs in Angular and Web Components
 
GWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXTGWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXT
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
 
GWT.create 2013: Themeing GWT Applications with the Appearance Pattern
GWT.create 2013: Themeing GWT Applications with the Appearance PatternGWT.create 2013: Themeing GWT Applications with the Appearance Pattern
GWT.create 2013: Themeing GWT Applications with the Appearance Pattern
 
jQuery
jQueryjQuery
jQuery
 
Creating GUI container components in Angular and Web Components
Creating GUI container components in Angular and Web ComponentsCreating GUI container components in Angular and Web Components
Creating GUI container components in Angular and Web Components
 
HTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile Event
HTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile EventHTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile Event
HTML5 and CSS3: Exploring Mobile Possibilities - London Ajax Mobile Event
 

Similar to Start your app the better way with Styled System

Lect-4-Responsive-Web-06032024-082044am.pptx
Lect-4-Responsive-Web-06032024-082044am.pptxLect-4-Responsive-Web-06032024-082044am.pptx
Lect-4-Responsive-Web-06032024-082044am.pptx
zainm7032
 
Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013
Paris Android User Group
 

Similar to Start your app the better way with Styled System (20)

CSS- Smacss Design Rule
CSS- Smacss Design RuleCSS- Smacss Design Rule
CSS- Smacss Design Rule
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End Methodologies
 
Web components with Angular
Web components with AngularWeb components with Angular
Web components with Angular
 
Building a theming system with React - Matteo Ronchi - Codemotion Amsterdam 2017
Building a theming system with React - Matteo Ronchi - Codemotion Amsterdam 2017Building a theming system with React - Matteo Ronchi - Codemotion Amsterdam 2017
Building a theming system with React - Matteo Ronchi - Codemotion Amsterdam 2017
 
Css3
Css3Css3
Css3
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
 
Lect-4-Responsive-Web-06032024-082044am.pptx
Lect-4-Responsive-Web-06032024-082044am.pptxLect-4-Responsive-Web-06032024-082044am.pptx
Lect-4-Responsive-Web-06032024-082044am.pptx
 
Componentization css angular
Componentization css angularComponentization css angular
Componentization css angular
 
Getting Started with Sass & Compass
Getting Started with Sass & CompassGetting Started with Sass & Compass
Getting Started with Sass & Compass
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
 
05.Blend Expression, Transformation & Animation
05.Blend Expression, Transformation & Animation05.Blend Expression, Transformation & Animation
05.Blend Expression, Transformation & Animation
 
HTML Web Devlopment presentation css.ppt
HTML Web Devlopment presentation css.pptHTML Web Devlopment presentation css.ppt
HTML Web Devlopment presentation css.ppt
 
css.ppt
css.pptcss.ppt
css.ppt
 
css.ppt
css.pptcss.ppt
css.ppt
 
Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013Making it fit - DroidCon Paris 18 june 2013
Making it fit - DroidCon Paris 18 june 2013
 
WPF - An introduction
WPF - An introductionWPF - An introduction
WPF - An introduction
 
SenchaCon 2016: Theming the Modern Toolkit - Phil Guerrant
SenchaCon 2016: Theming the Modern Toolkit - Phil Guerrant   SenchaCon 2016: Theming the Modern Toolkit - Phil Guerrant
SenchaCon 2016: Theming the Modern Toolkit - Phil Guerrant
 
Webtech File.docx
Webtech File.docxWebtech File.docx
Webtech File.docx
 
Accessibility and css - Lisa Seeman
Accessibility and css - Lisa SeemanAccessibility and css - Lisa Seeman
Accessibility and css - Lisa Seeman
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for Developers
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 

Recently uploaded (20)

GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 

Start your app the better way with Styled System

  • 1. Start your app the better way with Styled System
  • 2. Agenda ● History of CSS and it’s original sin ● The key problems of customizing UI component libraries’ styles ● Styled System ● QnA ● References
  • 3. Everything in CSS is global.
  • 4. .list { ... } .list .item { ... } Name collisions .list { ... } .list .item { ... } .list { ... } .list .item { ... }
  • 6. OOCSS BEM SMACSS .card-list Element A component of a block Modifier State for a block or element. Block A block component .card-list__item .card-list__item--highlight
  • 7. Preprocessor / Postprocessor LESS SASS SCSS PostCSS .search-box-mixin(@scope) { .@{scope}-search-box { .search-input { border: 1px solid @gray; } .tooltip { margin: 8px 0 0 1px; } } } .search-box-mixin('page'); .page-search-box .search-input { border: 1px solid #ddd; } .page-search-box .tooltip { margin: 8px 0 0 1px; }
  • 8. Components are the building blocks of modern web applications.
  • 9. CSS Modules Scope CSS rules locally by using tools to transform the class name with a hash. .3zyde4l .card-list__item .card-list__item--highlight .card-list .ucdIPRBM .1Jf-igmQ
  • 10. CSS in JS Write CSS in components. import styled from 'styled-components'; const Button = styled.button` margin: 0 10px; padding: 10px; background: #fefefe; border-radius: 3px; border: 1px solid #ccc; color: #525252; `
  • 11. Still have some problems... ● Components with inconsistent props. ● How to efficiently implement multiple themes? Like SMASCC… ● How to efficiently implement responsive styles for different devices? ● How to reduce redundant codes for setting media queries or mapping props to css properties?
  • 12. Recap As an user, I care about... ● Is it easy to custom styles? ● Does it support global theme styles and it is easy to custom? As an developer, I care about... ● Develop UI with HIE’s design.
  • 13. Ant Design Step 1: Find the target element by using inspector. Step 2: Extend LESS files to custom button styles. @import '~antd/lib/button/style/index.less'; @btn-default-color: blue; button button font-color: blue font-color: black Custom text color
  • 14. import styled from 'styled-components'; import { Button as AntdButton } from 'antd'; const CustomButton = styled(AntdButton)` span { color: blue; } }`; export default CustomButton; Ant Design Step 1: Find the target element by using inspector. Step 2: Wrap AntdButton by using CustomButton.
  • 15. Could it be easier?
  • 16. Start your app the better way with Styled System
  • 17. Consistency Mobile-First Why Styled System is good? More concise codes Easy to custom styles
  • 18. More Concise Codes Utility functions make codes short and clear, expressing styles without unnecessary words
  • 19. Utility functions (1/2) <Box color='#000 bg='tomato' /> const Box = styled.div` margin: 15px 0; padding: 15px; color: ${(props) => props.color}; background: ${(props) => props.bg}; border-radius: 10px; `; const getStyles = ({ color, bg }) => ({ color, background: bg, }); const Box = styled.div` ${getColor}; margin: 15px 0; padding: 15px; border-radius: 10px; `;
  • 20. Utility functions (2/2) import { color } from 'styled-system'; const Box = styled.div` ${color} margin: 15px 0; padding: 15px; border-radius: 10px; `; const getStyles = ({ color, bg }) => ({ color, background: bg, }); const Box = styled.div` ${getColor}; margin: 15px 0; padding: 15px; border-radius: 10px; `; Styled System
  • 21. Consistency Make components with consistent prop names
  • 22. Inconsistent props <Button color='black'>Click</Button> <Label fontColor='white'>$</Label> <Button color='black'>Click</Button> <Label color='white'>$</Label> Use color utility function in Styled System
  • 23. Easy to custom styles ● Define global theme by a config object ● Define individual component styles by variants
  • 24. Theming Utilizing `<ThemeProvider>` and pass the theme object from root node to provide global theme. <ThemeProvider theme={theme}> <Box color='black' bg='tomato'/> </ThemeProvider> const theme = { color: { black: '#333', }, bg: { tomato: 'tomato', }, };
  • 25. Define component styles in theme object const theme = { buttons: { danger: { color: 'white', background: '#f25e7a' }, size: { default: { height: 50 }, large: { height: 100 } } } }; <Button variant="danger" size="large" /> const buttonStyle = variant({ key: 'buttons'}); const buttonSizeStyle = variant({ prop: 'size', key: 'buttons.size' }); const Button = styled.div` ${buttonStyle} ${buttonSizeStyle} padding: 15px; `;
  • 26. Variants Use variants to define component styles. <Box variant='secondary'/> import { variant } from 'styled-system'; const Box = styled('div')( variant({ variants: { primary: { color: 'black', bg: 'tomato' }, secondary: { color: 'black', bg: 'yellow' }, }, }), ); <Box variant='primary' />
  • 27. Mobile-First Create mobile-first responsive layouts with array syntax
  • 28. Responsive styles Create mobile-first responsive layouts with ease by using an array syntax. .thing { font-size: 16px; width: 100%; } @media screen and (min-width: 40em) { font-size: 20px; width: 50%; } @media screen and (min-width: 52em) { font-size: 24px; } <Thing fontSize={[ 16, 20, 24 ]} width={[1, 1/2]} />Styled System
  • 29. Consistency Mobile-First Styled System is good! More concise codes Easy to custom styles
  • 30.
  • 31.
  • 33. Theme Use theme object to define or extend theme styles import { theme } from '@chakra-ui/core'; export default { ...theme, colors: { ...theme.colors, brand: { white: '#fefefe', blue: '#41d2f2', yellow: '#f2dc6d', purple: '#7700bb', }, }, }; <Button bg='#41d2f2' color='brand.white' />
  • 34. Responsive styles Define values to each breakpoint respectively .Box { width: 100%; } @media screen and (min-width: 40em) { width: 50%; } @media screen and (min-width: 52em) { width: 25%; } <Box width={['100%', 1 / 2, 1 / 4]} />
  • 35.
  • 36. Styles overlapping Props passed by Styled System should put above the original ones. const Box = styled.div` margin: 15px 0; padding: 15px; border-radius: 10px; ${color} `; const Box = styled.div` ${color} margin: 15px 0; padding: 15px; border-radius: 10px; `; Avoid css overlap
  • 37. Traditional CSS rules feat. Styled Components #root div { color: red; } /* in site.css, score: 100 + 1 = 101 */ .jqouBD { color: black; } /* in styled component, score: 10 */ #root div { color: red; } /* in site.css, score: 100 + 1 = 101 */ .jqouBD { color: black !important; } /* in styled component, score: 10000 */ source
  • 38. Class name without a hash for end-to-end testing Use a chaninable method “attrs” to attach props to the styled component. <Button>Click me!</Button> const Button = styled.button.attrs({ className: 'button-submit' })`...`; <button class="sc-EHOje button-submit">Click me!</button> View in browser inspector
  • 39. References ● Styled System https://styled-system.com/ ● We need a better UI component library - Styled System http://bit.ly/2roJEsg ● 從 Styled System 看下一代 CSS 技術 https://bit.ly/2u0xvLJ ● The Three Tenets of Styled System http://bit.ly/35ygrcV ● Styled System: Pseudo selectors in Variant http://bit.ly/35yqGhy ● How to create responsive UI with styled-components http://bit.ly/34lhxJ3 ● CSS 實戰心法 http://bit.ly/2shXUn4