SlideShare a Scribd company logo
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

More Related Content

What's hot

RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”RubyBarCamp “Полезные gems и plugins”
RubyBarCamp “Полезные gems и plugins”
apostlion
 
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
 
[ 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
Mikstura.IT Foundation | Web & Mobile Community
 
Get AngularJS Started!
Get AngularJS Started!Get AngularJS Started!
Get AngularJS Started!
Dzmitry Ivashutsin
 
$.Template
$.Template$.Template
$.Template
Dave Furfero
 
[ 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
Mikstura.IT Foundation | Web & Mobile Community
 
Polymer 1.0
Polymer 1.0Polymer 1.0
Polymer 1.0
Cyril Balit
 
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
 
Web 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style SheetsWeb 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style Sheets
Mohammad Imam Hossain
 
GWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXTGWT.create 2013: Introduction to GXT
GWT.create 2013: Introduction to GXT
niloc132
 
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...
Sencha
 
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
 
[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
Future Processing
 
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
Carsten Sandtner
 
Therapeutic refactoring
Therapeutic refactoringTherapeutic refactoring
Therapeutic refactoring
kytrinyx
 
J Query Public
J Query PublicJ Query Public
J Query Public
pradeepsilamkoti
 

What's hot (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
 

Similar to Build a better UI component library with Styled System

CSS in React
CSS in ReactCSS in React
CSS in React
Joe Seifi
 
Pfnp slides
Pfnp slidesPfnp slides
Pfnp slides
William Myers
 
OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?
Michael Posso
 
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...
Michael Posso
 
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
Codemotion
 
CSS Methodology
CSS MethodologyCSS Methodology
CSS Methodology
Zohar Arad
 
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
vzaccaria
 
The Future State of Layout
The Future State of LayoutThe Future State of Layout
The Future State of Layout
Stephen Hay
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LA
Jake Johnson
 
TIBCO General Interface - CSS Guide
TIBCO General Interface - CSS GuideTIBCO General Interface - CSS Guide
TIBCO General Interface - CSS Guide
Rohan Chandane
 
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...
Beat Signer
 
Front-End Methodologies
Front-End MethodologiesFront-End Methodologies
Front-End Methodologies
Arash Manteghi
 
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
Codemotion
 
An Introduction to CSS
An Introduction to CSSAn Introduction to CSS
An Introduction to CSS
John Catterfeld
 
SMACSS Workshop
SMACSS WorkshopSMACSS Workshop
SMACSS Workshop
Tim Hettler
 
HTML CSS & Javascript
HTML CSS & JavascriptHTML CSS & Javascript
HTML CSS & Javascript
David Lindkvist
 
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
Sencha
 
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
Binu Paul
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes
In a Rocket
 
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
Darren Gideon
 

Similar to 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?
OOCSS, SMACSS or BEM?OOCSS, SMACSS or BEM?
OOCSS, SMACSS or BEM?
 
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...
 
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
 

Recently uploaded

“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
Edge AI and Vision Alliance
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
Fwdays
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
ScyllaDB
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 

Recently uploaded (20)

“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk"Frontline Battles with DDoS: Best practices and Lessons Learned",  Igor Ivaniuk
"Frontline Battles with DDoS: Best practices and Lessons Learned", Igor Ivaniuk
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-EfficiencyFreshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
Freshworks Rethinks NoSQL for Rapid Scaling & Cost-Efficiency
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Artificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic WarfareArtificial Intelligence and Electronic Warfare
Artificial Intelligence and Electronic Warfare
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 

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