Weitere ähnliche Inhalte Ähnlich wie The road to <> styled-components: CSS in component-based systems by Max Stoiber (20) Mehr von React London 2017 (9) Kürzlich hochgeladen (20) The road to <> styled-components: CSS in component-based systems by Max Stoiber10. 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>
)
}
}
11. 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>
)
}
}
15. If you’re writing React, you have
access to a more powerful styling
construct than CSS class names.
You have components.”
– Michael Chan, @chantastic
“
20. 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;
`;
21. 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;
`;
22. 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
23. 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;
`;
24. 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;
`;
25. const App = () => (
<Wrapper>
<Title>
Hello World, this is my first styled component!
</Title>
</Wrapper>
);
ReactDOM.render(
<App />,
document.getElementById(‘#app')
);
28. import styled from 'styled-components';
const ColorChanger = styled.section`
background: papayawhip;
> h2 {
color: palevioletred;
}
@media (min-width: 875px) {
background: mediumseagreen;
> h2 {
color: papayawhip;
}
}
`;
29. import styled from 'styled-components';
const ColorChanger = styled.section`
background: papayawhip;
> h2 {
color: palevioletred;
}
@media (min-width: 875px) {
background: mediumseagreen;
> h2 {
color: papayawhip;
}
}
`;
30. import styled from 'styled-components';
const ColorChanger = styled.section`
background: papayawhip;
> h2 {
color: palevioletred;
}
@media (min-width: 875px) {
background: mediumseagreen;
> h2 {
color: papayawhip;
}
}
`;
34. 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;
38. import { ThemeProvider } from 'styled-components';
const theme = {
primary: 'palevioletred',
};
const App = () => (
<ThemeProvider theme={theme}>
<Root />
</ThemeProvider>
);
39. import { ThemeProvider } from 'styled-components';
const theme = {
primary: 'palevioletred',
};
const App = () => (
<ThemeProvider theme={theme}>
<Root />
</ThemeProvider>
);
40. import { ThemeProvider } from 'styled-components';
const theme = {
primary: 'palevioletred',
};
const App = () => (
<ThemeProvider theme={theme}>
<Root />
</ThemeProvider>
);
41. import styled from 'styled-components';
const Button = styled.button`
/* Adjust background based on the theme */
background: ${props => props.theme.primary};
color: white;
`;
43. const redTheme = {
primary: 'palevioletred',
};
const greenTheme = {
primary: 'mediumseagreen',
};
// …
<ThemeProvider theme={redTheme}>
<Button>I'm red!</Button>
</ThemeProvider>
<ThemeProvider
theme={greenTheme}>
<Button>I'm green!</Button>
</ThemeProvider>
55. 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;
`;
56. 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;
`;
57. 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;
`;
58. 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;
`;
59. 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;
`;
64. $ yarn add styled-components
github.com/ styled-components/styled-components
$ npm install styled-components
71. function tone(property, color, lighten, desaturate = lighten) {
return `${property}: ${
desaturate(lighten(color, lighten), desaturate)
}`;
}
??????
72. A lightweight toolset for writing styles in JavaScript
polished.js.org
by @nikgraf, @bhough and @mxstbr
76. $ yarn add polished
github.com/ styled-components/polished
Come get some 💅 styled-components stickers!