Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Styling React Applications
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Max Stoiber
October 28, 2016
Technology
730
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Styling React Applications
Max Stoiber
October 28, 2016
More Decks by Max Stoiber
See All by Max Stoiber
Testing React Applications
mxstbr
3
360
Styling Intro
mxstbr
3
410
Introduction to React.js
mxstbr
1
170
Scaling React.js Applications (short version)
mxstbr
2
440
Scaling React.js Applications
mxstbr
0
440
Offline is the new Black
mxstbr
3
1.1k
Exploring ES6
mxstbr
1
340
Testing React.js Applications
mxstbr
4
750
Other Decks in Technology
See All in Technology
いかに伝えるか 〜新卒エンジニアの教育のための、ライトノベル活用の一例
ikedon
1
190
はじめてのDatabricks:技術者向けワークショップ / beginner-workshop
databricksjapan
PRO
0
150
Benchmarking Vector Databases: pgvector vs. LanceDB
tsho
0
140
AIエージェントの開発・提供におけるセキュリティリスクの論点と対策
flatt_security
1
570
KAEN Company Deck
kaen
PRO
0
320
KPIだけでは評価できないプロダクトが考えるべき Evalsという第二の評価系 / Beyond KPIs: Evals as a Second Evaluation Framework for Products #PdEConf
aki_iinuma
4
3.6k
[DroidKaigi 2026] Making UI specifications visible: Android UI development in the AI agent era supported by Compose Screenshot Testing and galleries
syarihu
0
570
深夜のクラウド懺悔室 1:29:300 or 1:0:0
kazzpapa3
0
160
AndroidでHDRメディアを「壊さずに」扱う
chigichan24
0
400
enechainの内製セルフサービスプラットフォーム
hiyosi
0
110
スクラムで身についていた動き方を、XPで捉え直してみた
codmoninc
PRO
1
200
「重なり」は迎える側がつくる ― 人もAIエージェントも歓迎するプロダクトエンジニアリング ―
go0517go
PRO
0
230
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
260
Unsuck your backbone
ammeep
672
58k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.9k
The Mindset for Success: Future Career Progression
greggifford
PRO
0
490
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
The Curious Case for Waylosing
cassininazir
1
490
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
520
Accessibility Awareness
sabderemane
1
200
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Transcript
<> styled-components Max Stoiber @mxstbr Open Source Developer, Thinkmill
None
None
The Component Age
1. Small, focussed components 2. Use container components Best Practices:
Components
Small, focussed components
<Button className="btn"></Button> <Button className="btn btn--primary"></Button>
<Button className="btn"></Button> <Button className="btn btn--primary"></Button> Implementation detail! Implementation detail!
<Button></Button> <Button primary></Button>
Split containers and components
class Sidebar extends React.Component { componentDidMount() { fetch('api.com/sidebar') .then(res =>
{ this.setState({ items: res.items }) }); } render() { return ( <div className="sidebar"> {this.state.items.map(item => ( <div className="sidebar__item">{item}</div> ))} </div> ) } }
class SidebarContainer extends React.Component { componentDidMount() { fetch('api.com/sidebar') .then(res =>
{ this.setState({ items: res.items }) }); } render() { return ( <Sidebar> {this.state.items.map(item => ( <SidebarItem item={item} /> ))} </Sidebar> ) } }
Containers = How things work Components = How things look
What about styling?
1. Single-use classes 2. Components as a styling construct Best
Practices: Styling
If you’re writing React, you have access to a more
powerful styling construct than CSS class names. You have components.” – Michael Chan, @chantastic “
Enforcing best practices?
styled-components
Glen Maddern frontend.center
Remove the mapping between styles and components
import styled from 'styled-components'; const Title = styled.h1` font-size: 1.5em;
text-align: center; color: palevioletred; `; const Wrapper = styled.section` color: palevioletred; padding: 4em; width: 100%; height: 100%; background: papayawhip; `;
import styled from 'styled-components'; const Title = styled.h1` font-size: 1.5em;
text-align: center; color: palevioletred; `; const Wrapper = styled.section` color: palevioletred; padding: 4em; width: 100%; height: 100%; background: papayawhip; `;
import styled from 'styled-components'; const Title = styled.h1` font-size: 1.5em;
text-align: center; color: palevioletred; `; const Wrapper = styled.section` color: palevioletred; padding: 4em; width: 100%; height: 100%; background: papayawhip; `; styled.h1
import styled from 'styled-components'; const Title = styled.h1` font-size: 1.5em;
text-align: center; color: palevioletred; `; const Wrapper = styled.section` color: palevioletred; padding: 4em; width: 100%; height: 100%; background: papayawhip; `;
import styled from 'styled-components'; const Title = styled.h1` font-size: 1.5em;
text-align: center; color: palevioletred; `; const Wrapper = styled.section` color: palevioletred; padding: 4em; width: 100%; height: 100%; background: papayawhip; `;
const App = () => ( <Wrapper> <Title> Hello World,
this is my first styled component! </Title> </Wrapper> ); ReactDOM.render( <App />, document.getElementById(‘#app') );
styled-components
Write actual CSS
import styled from 'styled-components'; const ColorChanger = styled.section` background: papayawhip;
> h2 { color: palevioletred; } @media (min-width: 875px) { background: mediumseagreen; > h2 { color: papayawhip; } } `;
import styled from 'styled-components'; const ColorChanger = styled.section` background: papayawhip;
> h2 { color: palevioletred; } @media (min-width: 875px) { background: mediumseagreen; > h2 { color: papayawhip; } } `;
import styled from 'styled-components'; const ColorChanger = styled.section` background: papayawhip;
> h2 { color: palevioletred; } @media (min-width: 875px) { background: mediumseagreen; > h2 { color: papayawhip; } } `;
color-changer
Adapting based on props
<Button>Normal</Button> <Button primary>Primary</Button>
const Button = styled.button` /* Adapt the colors based on
primary prop */ background: ${(props) => props.primary ? 'palevioletred' : 'white' }; color: ${(props) => props.primary ? 'white' : 'palevioletred' }; font-size: 1em; margin: 1em; padding: 0.25em 1em; border: 2px solid palevioletred; border-radius: 3px; `; export default Button;
<Button>Normal</Button> <Button primary>Primary</Button>
styled-components
Theming Built In
import { ThemeProvider } from 'styled-components'; const theme = {
primary: 'palevioletred', }; const App = () => ( <ThemeProvider theme={theme}> <Root /> </ThemeProvider> );
import { ThemeProvider } from 'styled-components'; const theme = {
primary: 'palevioletred', }; const App = () => ( <ThemeProvider theme={theme}> <Root /> </ThemeProvider> );
import { ThemeProvider } from 'styled-components'; const theme = {
primary: 'palevioletred', }; const App = () => ( <ThemeProvider theme={theme}> <Root /> </ThemeProvider> );
import styled from 'styled-components'; const Button = styled.button` /* Adjust
background based on the theme */ background: ${props => props.theme.primary}; color: white; `;
styled-components
const redTheme = { primary: 'palevioletred', }; const blueTheme =
{ primary: 'mediumseagreen', }; // … <ThemeProvider theme={redTheme}> <Button>I'm red!</Button> </ThemeProvider> <ThemeProvider theme={blueTheme}> <Button>I'm green!</Button> </ThemeProvider>
styled-components
<ThemeProvider theme={redTheme}> <Button>I'm red!</Button> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Button>I'm still red!</Button> </div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <Button>I'm still red!</Button> </Container> </div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <div> <Button>I'm still red!</Button> </div> </Container>
</div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <div> <SidebarContainer> <Button>I'm still red!</Button> </SidebarContainer>
</div> </Container> </div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <div> <SidebarContainer> <Sidebar> <Button>I'm still red!</Button>
</Sidebar> </SidebarContainer> </div> </Container> </div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <div> <SidebarContainer> <Sidebar> <SidebarItem> <Button>I'm still
red!</Button> </SidebarItem> </Sidebar> </SidebarContainer> </div> </Container> </div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <div> <SidebarContainer> <Sidebar> <SidebarItem> <div> <Button>I'm
still red!</Button> </div> </SidebarItem> </Sidebar> </SidebarContainer> </div> </Container> </div> </ThemeProvider>
<ThemeProvider theme={redTheme}> <div> <Container> <div> <SidebarContainer> <Sidebar> <SidebarItem> <div> <span>
<Button>I'm still red!</Button> </span> </div> </SidebarItem> </Sidebar> </SidebarContainer> </div> </Container> </div> </ThemeProvider>
Full ReactNative Support
import styled from 'styled-components/native'; const Wrapper = styled.View` background-color: papayawhip;
flex: 1; justify-content: center; align-items: center; `; const Title = styled.Text` font-size: 20; text-align: center; margin: 10; color: palevioletred; font-weight: bold; `;
import styled from 'styled-components/native'; const Wrapper = styled.View` background-color: papayawhip;
flex: 1; justify-content: center; align-items: center; `; const Title = styled.Text` font-size: 20; text-align: center; margin: 10; color: palevioletred; font-weight: bold; `;
import styled from 'styled-components/native'; const Wrapper = styled.View` background-color: papayawhip;
flex: 1; justify-content: center; align-items: center; `; const Title = styled.Text` font-size: 20; text-align: center; margin: 10; color: palevioletred; font-weight: bold; `;
import styled from 'styled-components/native'; const Wrapper = styled.View` background-color: papayawhip;
flex: 1; justify-content: center; align-items: center; `; const Title = styled.Text` font-size: 20; text-align: center; margin: 10; color: palevioletred; font-weight: bold; `;
import styled from 'styled-components/native'; const Wrapper = styled.View` background-color: papayawhip;
flex: 1; justify-content: center; align-items: center; `; const Title = styled.Text` font-size: 20; text-align: center; margin: 10; color: palevioletred; font-weight: bold; `;
class StyledComponentsNative extends Component { render() { return ( <Wrapper>
<Title> Hello World, this is my first native styled component! </Title> </Wrapper> ); } }
None
$ yarn add styled-components github.com/styled-components/styled-components $ npm install styled-components
Thank you! Max Stoiber @mxstbr Come talk to me!
None