Et sett med regler for å unngå problemene med CSS og lage komponent-baserte systemer
.card
.card--dark
.card__image
.card__contents
.card__actions
.card--opaque
.card__title
.card
.card--dark
.card--opaque
Lorem ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing.
Lorem ipsum dolor sit amet, consectetur adipiscing.
.card__image
.card__contents
.card__actions
.card__title
.card
.card__contents
.card__actions
.card__title
.card
.card .card--simple
.card__meta
.card__title
.card__title
.card__tags
Aldri .card__actions__button
.card {
display: flex;
flex-direction: column;
align-content: flex-end;
min-height: 300px;
padding: 20px;
background-image: url('...') contain center;
/*
Elements
*/
&__title {
color: white;
}
&__contents {
margin-top: 10px;
}
&__actions {
display: flex;
width: 100%;
align-content: flex-end;
margin-top: 5px;
}
&__tags {
display: flex;
width: 100%;
align-content: flex-end;
}
/*
Modifiers
*/
&--simple {
align-content: space-between;
}
}
<article class="card card--simple">
<ul class="card__tags">
<div class="tag">15 min</div>
</ul>
<div class="card__title">
<h3>Fish & chips med ertepure</h3>
</div>
</article>
1
Finn styled-component X
const Button = styled.button`
font-size: 25px;
color: red;
&:hover {
color: blue;
}
`;
2
Generer klassenavn
const className = 'sc-' + hash('sc' + x);
// -> sc-bdVaJa
start loop, x = 0
x++
3
Generer CSS
const selector = '.' + className;
const css = stylis(selector, styleString);
/*
.sc-bdVaJa {
font-size: 25px;
color: red;
}
.sc-bdVaJa:hover{
background-color: blue;
}
/*
4
Legg til CSS i <style />
<style data-styled-components>
/* sc-component-id: sc-bdVaJa */
.sc-bdVaJa { ... }
</style>
<!html>
<head>
...
<style data-styled-components>
/* sc-component-id: sc-bdVaJa */
.sc-bdVaJa { color: red; }
</style>
...
</head>
<body>
<h1 class="sc-bdVaJa">Pop pop!</h1>
</body>
</html>
Kildekode
Output i browseren
import React from 'react';
import ReactDOM from 'react-dom';
import styled from 'styled-components';
const RedBox = styled.div`
color: red;
`;
class App extends Component {
render() {
return (
<RedBox>Pop pop!</RedBox>
);
}
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
<article class="card card--simple">
<ul class="card__tags">
<div class="tag">15 min</div>
</ul>
<div class="card__title">
<h3>Fish & chips med ertepure</h3>
</div>
</article>
<Card simple>
<Card.Tags>
<Tag>15 min</Tag>
</Card.Tags>
<Card.Title>
<H3>Fish & chips med ertepure</H3>
</Card.Title>
</Card>
Kompileres på forhånd
Funker, alle kan det
Mixins, Extend, Include
Kompileres i browseren
// constants/config.js
const config = {
theme: {
colors: {
text: "#3C3C3B",
primary: "#DB3E77",
secondary: "#1446A0"
},
typography: {
font: "'Sedgwick Ave', cursive"
},
gutter: 10
},
otherStuff: {}
};
// index.js
<ThemeProvider theme={config.theme}>
<App />
</ThemeProvider>
// Button.js
const Button = styled.button`
background: ${props => props.theme.primary};
`
// Ser ut som knapp, men er link
<Button as="a" href="mailto:test@test.com">Send oss en epost</Button>
// Ser ut som h1, men er h2
<H1 as="h2">Velkommen til Jungelen</H1>
Kompileres på forhånd
Funker, alle kan det
Mixins, Extend, Include
Kompileres i browseren
Ingen globals
Isolerte komponenter
<article class="article">
<h2 class="article__title">15 min</h2>
<div class="article__contents">
<p>
Lorem, ipsum dolor sit amet consectetur adipisicing elit.
</p>
</div>
</article>
Markup
.article {
display: block;
/*
* Elements
*/
&__title {
font-size: 40px;
}
&__contents {
margin-top: 10px;
color: gray;
font-weight: 100;
}
}
Styling
<article class="article">
<h2 class="article__title">15 min</h2>
<div class="article__contents">
<p>
Lorem, ipsum dolor sit amet consectetur adipisicing elit.
</p>
</div>
</article>
Markup
.article {
display: block;
/*
* Elements
*/
&__title {
font-size: 40px;
}
&__contents {
margin-top: 10px;
color: gray;
font-weight: 100;
}
}
Styling
Kompileres på forhånd
Funker, alle kan det
Mixins, Extend, Include
Kompileres i browseren
Ingen globals
Isolerte komponenter
SASS
Ikke like utbredt
javaScript
Forhåndsgjort vurdering av egen presentasjon