SlideShare une entreprise Scribd logo
1  sur  44
Build a better UI component library with
Styled System
Agenda
● CSS methodologies
● Styled System
Everything in CSS is global.
.list { ... }
.list .item { ... }
Name collisions
.list { ... }
.list .item { ... }
.list { ... }
.list .item { ... }
Reusability
CSS methodologies
CSS methodologies are the solution.
● OOCSS (Object-Oriented CSS)
● BEM (Block, Element, Modifier)
● SMACSS (Scalable and Modular Architecture for CSS)
● CSS Modules
● CSS in JS
OOCSS (Object-Oriented CSS)
In OOCSS, style rules are written exclusively using CSS class selectors.
Rules
● Separation of structure from skin (結構與樣式分離)
● Separation of container and content (內容與容器分離)
Separation of structure from skin
margin, padding,
display, position,
vertical-align, width,
height
color, background,
opacity, font-size
Example (1/3)
.button-yellow {
width: 100px;
height: 50px;
margin: 10px;
padding: 10px;
border-radius: 5px;
background: #f2dc6d;
border: 1px solid #fefefe;
color: #ccc;
}
.button-pink {
width: 200px;
height: 100px;
margin: 10px;
padding: 10px;
border-radius: 5px;
background: #f25e7a;
border: 1px solid #fefefe;
color: #ccc;
}
.button-blue {
width: 100px;
height: 50px;
margin: 10px;
padding: 10px;
border-radius: 5px;
background: #41d2f2;
border: 1px solid #fefefe;
color: #ccc;
}
Example (2/3)
.button.default.yellow.small .button.default.pink.large .button.default.blue.small
.button {
margin: 10px;
padding: 10px;
}
.button.default {
border-radius: 5px;
border: 1px solid #fefefe;
color: #ccc;
}
.button.yellow { background: #f2dc6d; }
.button.pink { background: #f25e7a; }
.button.blue { background: #41d2f2; }
.button.green { background: #28efb7; }
.button.small { width: 100px; height: 50px; }
.button.large { width: 200px; height: 100px;}
Example (3/3)
.button.default.yellow.large
.button.default.green.small.button.default.pink.small
Separation of container and content
Example
.button.default.yellow.small .button.default.pink.small .button.default.blue.small
.form
.form { ... }
.form .button { ... }
.form .button.yellow { ... }
.form .button.pink { ... }
.form .button.blue { ... }
.form { ... }
.list { ... }
.button { ... }
.button.yellow { ... }
.button.pink { ... }
.button.blue { ... }
More flexible, reusable!
Pros and cons
Reusability Name collision
BEM (Block, Element, Modifier)
Element
A component of a block
Modifier
State for a block or element.
Block
A block component
Example
Element
.card-list__item
Modifier
.card-list__item--highlight
Block
.card-list
Pros and cons
Reusability Name collision Selector nesting for css
specificity and rendering
performance
SMACSS (Scalable and Modular Architecture for CSS )
Layout
structural
layout
Module
modular,
reusable
components
State
specify the current state of
something in the interface
Theme
affect layouts and modules,
triggered by user events
Base
set the default
CSS
properties for
the whole site
Example
Layout:
.l-sidebar
Layout: .l-container
Module:
.mod-card State: .mod-card.is-highlight
Theme: .theme-card-
dark
Theme:.theme.l-sidebar-dark
Base:h1,
div, a ...
DRY stylesheets, more flexible, modular, reusable!
What problems do OOCSS, BEM and SMACSS solve?
Global scope is still a problem!
Name collisions still happen in the global scope.
CSS Modules
Scope CSS rules locally by using tools to transform the class name with a hash.
.button.default.blue.small
.button { … }
.default { … }
.small { ... }
.blue { ... }
._3zyde4l1yATCOkgn-
DBWEL.ucdIPRBM8dj46yVBF3bcu._1Jf
-
igm_Q7n33cjbU1HWU.UwRmwF8HGfU
EMqBxpndWg._3zyde4l1yATCOkgn-DBW { … }
.ucdIPRBM8dj46yVBF3bcu { … }
._1Jf-igm_Q7n33cjbU1HWU { ... }
.UwRmwF8HGfUEMqBxpndWg { ... }
View in
browser
inspector
CSS in JS
Everything is constructed by using components.
Write CSS in components.
CSS
JS
CSS in JS
Styled Components
JSX
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;
`
<Button />
What problems does CSS in JS solve?
Name collision Selector nesting for css
specificity and rendering
performance
Refactoring
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?
Build a better component library with
Styled System
Responding
to Change
Respond to changing requirements quickly by using
utility functions
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
● Style consistently with a global theme
● Components with inconsistent props
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
Utilizing 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' />
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
Mobile-First
Create mobile-first responsive layouts with ease by
using 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
React.js Styled
Components
Styled
System
Best Solution
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
Do not filter out results when passing props to child components
<WrapperComponent color="blue" />
const WrapperComponent = styled(InnerComponent)`...`;
const InnerComponent = props => <div {...props}>Inner</div>;
<div color="blue" class="sc-TFwJa HJEpQ">Inner</div>
Demo
Simple Example
http://bit.ly/35x16cL
References
● Styled System https://styled-system.com/
● We need a better UI component library - Styled System http://bit.ly/2roJEsg
● The Three Tenets of Styled System http://bit.ly/35ygrcV
● Styled System Example http://bit.ly/35x16cL
● 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

Contenu connexe

Tendances

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
 
Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013
Sébastien Deleuze
 
GWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXTGWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXT
niloc132
 
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
 

Tendances (17)

RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
 
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
 
[ WrocLoveRb 2012] user perspective testing using ruby
[ WrocLoveRb 2012] user perspective testing using ruby[ WrocLoveRb 2012] user perspective testing using ruby
[ WrocLoveRb 2012] user perspective testing using ruby
 
Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
 
$.Template
$.Template$.Template
$.Template
 
[ 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
 
Polymer 1.0
Polymer 1.0Polymer 1.0
Polymer 1.0
 
Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013Dart : one language to rule them all - MixIT 2013
Dart : one language to rule them all - MixIT 2013
 
Web 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style SheetsWeb 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style Sheets
 
GWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXTGWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXT
 
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...
 
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
 
[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
 
Mozilla Brick - Frontend Rhein-Main June 2014
Mozilla Brick - Frontend Rhein-Main June 2014Mozilla Brick - Frontend Rhein-Main June 2014
Mozilla Brick - Frontend Rhein-Main June 2014
 
Therapeutic refactoring
Therapeutic refactoringTherapeutic refactoring
Therapeutic refactoring
 
J Query Public
J Query PublicJ Query Public
J Query Public
 

Similaire à Build a better UI component library with Styled System

CSS Methodology
CSS MethodologyCSS Methodology
CSS Methodology
Zohar Arad
 
TIBCO General Interface - CSS Guide
TIBCO General Interface - CSS GuideTIBCO General Interface - CSS Guide
TIBCO General Interface - CSS Guide
Rohan Chandane
 

Similaire à Build a better UI component library with Styled System (20)

CSS in React
CSS in ReactCSS in React
CSS in React
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
 
OOCSS, SMACSS or BEM, what is the question...
OOCSS, SMACSS or BEM, what is the question...OOCSS, SMACSS or BEM, what is the question...
OOCSS, SMACSS or BEM, what is the question...
 
OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?
 
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
 
CSS Methodology
CSS MethodologyCSS Methodology
CSS Methodology
 
BEM Methodology — @Frontenders Ticino —17/09/2014
BEM Methodology — @Frontenders Ticino —17/09/2014BEM Methodology — @Frontenders Ticino —17/09/2014
BEM Methodology — @Frontenders Ticino —17/09/2014
 
The Future State of Layout
The Future State of LayoutThe Future State of Layout
The Future State of Layout
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LA
 
TIBCO General Interface - CSS Guide
TIBCO General Interface - CSS GuideTIBCO General Interface - CSS Guide
TIBCO General Interface - CSS Guide
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End Methodologies
 
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
 
An Introduction to CSS
An Introduction to CSSAn Introduction to CSS
An Introduction to CSS
 
SMACSS Workshop
SMACSS WorkshopSMACSS Workshop
SMACSS Workshop
 
HTML CSS & Javascript
HTML CSS & JavascriptHTML CSS & Javascript
HTML CSS & Javascript
 
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
 
Cordova training - Day 2 Introduction to CSS 3
Cordova training - Day 2 Introduction to CSS 3Cordova training - Day 2 Introduction to CSS 3
Cordova training - Day 2 Introduction to CSS 3
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes
 
CSS101 - Concept Fundamentals for non UI Developers
CSS101 - Concept Fundamentals for non UI DevelopersCSS101 - Concept Fundamentals for non UI Developers
CSS101 - Concept Fundamentals for non UI Developers
 

Dernier

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 

Build a better UI component library with Styled System

  • 1. Build a better UI component library with Styled System
  • 3. Everything in CSS is global.
  • 4. .list { ... } .list .item { ... } Name collisions .list { ... } .list .item { ... } .list { ... } .list .item { ... }
  • 6. CSS methodologies CSS methodologies are the solution. ● OOCSS (Object-Oriented CSS) ● BEM (Block, Element, Modifier) ● SMACSS (Scalable and Modular Architecture for CSS) ● CSS Modules ● CSS in JS
  • 7. OOCSS (Object-Oriented CSS) In OOCSS, style rules are written exclusively using CSS class selectors. Rules ● Separation of structure from skin (結構與樣式分離) ● Separation of container and content (內容與容器分離)
  • 8. Separation of structure from skin margin, padding, display, position, vertical-align, width, height color, background, opacity, font-size
  • 9. Example (1/3) .button-yellow { width: 100px; height: 50px; margin: 10px; padding: 10px; border-radius: 5px; background: #f2dc6d; border: 1px solid #fefefe; color: #ccc; } .button-pink { width: 200px; height: 100px; margin: 10px; padding: 10px; border-radius: 5px; background: #f25e7a; border: 1px solid #fefefe; color: #ccc; } .button-blue { width: 100px; height: 50px; margin: 10px; padding: 10px; border-radius: 5px; background: #41d2f2; border: 1px solid #fefefe; color: #ccc; }
  • 10. Example (2/3) .button.default.yellow.small .button.default.pink.large .button.default.blue.small .button { margin: 10px; padding: 10px; } .button.default { border-radius: 5px; border: 1px solid #fefefe; color: #ccc; } .button.yellow { background: #f2dc6d; } .button.pink { background: #f25e7a; } .button.blue { background: #41d2f2; } .button.green { background: #28efb7; } .button.small { width: 100px; height: 50px; } .button.large { width: 200px; height: 100px;}
  • 12. Separation of container and content
  • 13. Example .button.default.yellow.small .button.default.pink.small .button.default.blue.small .form .form { ... } .form .button { ... } .form .button.yellow { ... } .form .button.pink { ... } .form .button.blue { ... } .form { ... } .list { ... } .button { ... } .button.yellow { ... } .button.pink { ... } .button.blue { ... } More flexible, reusable!
  • 14. Pros and cons Reusability Name collision
  • 15. BEM (Block, Element, Modifier) Element A component of a block Modifier State for a block or element. Block A block component
  • 17. Pros and cons Reusability Name collision Selector nesting for css specificity and rendering performance
  • 18. SMACSS (Scalable and Modular Architecture for CSS ) Layout structural layout Module modular, reusable components State specify the current state of something in the interface Theme affect layouts and modules, triggered by user events Base set the default CSS properties for the whole site
  • 19. Example Layout: .l-sidebar Layout: .l-container Module: .mod-card State: .mod-card.is-highlight Theme: .theme-card- dark Theme:.theme.l-sidebar-dark Base:h1, div, a ...
  • 20. DRY stylesheets, more flexible, modular, reusable! What problems do OOCSS, BEM and SMACSS solve?
  • 21. Global scope is still a problem! Name collisions still happen in the global scope.
  • 22. CSS Modules Scope CSS rules locally by using tools to transform the class name with a hash. .button.default.blue.small .button { … } .default { … } .small { ... } .blue { ... } ._3zyde4l1yATCOkgn- DBWEL.ucdIPRBM8dj46yVBF3bcu._1Jf - igm_Q7n33cjbU1HWU.UwRmwF8HGfU EMqBxpndWg._3zyde4l1yATCOkgn-DBW { … } .ucdIPRBM8dj46yVBF3bcu { … } ._1Jf-igm_Q7n33cjbU1HWU { ... } .UwRmwF8HGfUEMqBxpndWg { ... } View in browser inspector
  • 23. CSS in JS Everything is constructed by using components. Write CSS in components. CSS JS
  • 24. CSS in JS Styled Components JSX 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; ` <Button />
  • 25. What problems does CSS in JS solve? Name collision Selector nesting for css specificity and rendering performance Refactoring
  • 26. 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?
  • 27. Build a better component library with Styled System
  • 28. Responding to Change Respond to changing requirements quickly by using utility functions
  • 29. 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; `;
  • 30. 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
  • 31. Consistency ● Style consistently with a global theme ● Components with inconsistent props
  • 32. 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', }, };
  • 33. 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; `;
  • 34. Variants Utilizing 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' />
  • 35. 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
  • 36. Mobile-First Create mobile-first responsive layouts with ease by using array syntax
  • 37. 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
  • 39.
  • 40. 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
  • 41. 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
  • 42. Do not filter out results when passing props to child components <WrapperComponent color="blue" /> const WrapperComponent = styled(InnerComponent)`...`; const InnerComponent = props => <div {...props}>Inner</div>; <div color="blue" class="sc-TFwJa HJEpQ">Inner</div>
  • 44. References ● Styled System https://styled-system.com/ ● We need a better UI component library - Styled System http://bit.ly/2roJEsg ● The Three Tenets of Styled System http://bit.ly/35ygrcV ● Styled System Example http://bit.ly/35x16cL ● 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