Styled components
In this section we're going to create some CSS components, also known as styled components. These components are created using styled, which like css is a function provided by Emotion.
The HTML tag that you want to style is appended with dot notation and then, again like css, the function takes a tagged template literal containing CSS as it's argument.
As a basic example let's start by creating a <Header> component in our root component file and give it a background colour, though first we need to import styled from Frontity.
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
import { connect, Global, css, styled } from "frontity"
5
// ...
6
​
7
const Root = ({ state }) => {
8
// ...
9
}
10
export default connect(Root)
11
​
12
const Header = styled.header`
13
background-color: #e5edee;
14
`
Copied!
It doesn't really matter whereabouts in the file you add your styled components, but it makes some sense to put the main exported component as the first item in the file, so we suggest that you add this new <Header> component below the <Root> component.
Once the <Header> component has been created let's use it in our <Root> component to wrap all the elements that we want contained in the header section of our site.
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
const Root = ({ state }) => {
5
const data = state.source.get(state.router.link)
6
​
7
return (
8
<>
9
<Global
10
styles={css`
11
html {
12
font-family: system-ui, Verdana, Arial, sans-serif;
13
}
14
`}
15
/>
16
<Header>
17
<h1>Frontity Workshop</h1>
18
<p>Current URL: {state.router.link}</p>
19
<nav>
20
<Link link="/">Home</Link>
21
<Link link="/page/2">More posts</Link>
22
<Link link="/about-us">About Us</Link>
23
</nav>
24
</Header>
25
<main>
26
<Switch>
27
<List when={data.isArchive} />
28
<Post when={data.isPost} />
29
<Page when={data.isPage} />
30
</Switch>
31
</main>
32
</>
33
)
34
}
Copied!
Now our header is contained within a nice light grey background. But notice the rather unsightly white border around it. Let's fix that by applying a basic CSS reset to our <Global> component. This will ensure that the styles in the reset apply anywhere in the site.
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
<Global
5
styles={css`
6
* {
7
margin: 0;
8
padding: 0;
9
box-sizing: border-box;
10
}
11
html {
12
font-family: system-ui, Verdana, Arial, sans-serif;
13
}
14
`}
15
/>
16
// ...
Copied!
CSS reset can be much more elaborate than this, but even this simple CSS reset will give us more control over the styling of our elements going forward, and enable us to have more consistent and predictable behaviour as we add CSS to style our site.
Let's continue styling our header by adding a border to the bottom, and making the <h1> element within it a little less starkly black!
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
​
5
const Header = styled.header`
6
background-color: #e5edee;
7
border-width: 0 0 8px 0;
8
border-style: solid;
9
border-color: maroon;
10
​
11
h1 {
12
color: #4a4a4a;
13
}
14
`
Copied!
Notice how we can use the LESS/SASS type syntax of including child elements. Although this is not valid CSS, Emotion will nevertheless parse this and apply the styles correctly. It enables us to be more concise in defining the styles for our styled components.
We also want to constrain our page width to 800px. To do that we will need to add a couple of extra components, <HeaderContent> and <Main>. Let's add these, and we'll also style some elements within Main.
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
const Header = styled.header`
5
background-color: #e5edee;
6
border-width: 0 0 8px 0;
7
border-style: solid;
8
border-color: maroon;
9
​
10
h1 {
11
color: #4a4a4a;
12
}
13
`
14
const HeaderContent = styled.div`
15
max-width: 800px;
16
padding: 2em 1em;
17
margin: auto;
18
`
19
const Main = styled.main`
20
max-width: 800px;
21
padding: 1em;
22
margin: auto;
23
​
24
img {
25
max-width: 100%;
26
}
27
h2 {
28
margin: 0.5em 0;
29
}
30
p {
31
line-height: 1.25em;
32
margin-bottom: 0.75em;
33
}
34
figcaption {
35
color: #828282;
36
font-size: 0.8em;
37
margin-bottom: 1em;
38
}
39
`
Copied!
Since all post and page content is going to appear within the <Main> component you can keep adding elements to the definition of that component and styling them as you wish. Thus far we've only applied some minimal styling to the img, h2, and p elements, and also to figcaption as a number of images in our content have captions.
Now let's use those styled components in our root component.
1
// File: /packages/my-first-theme/src/components/index.js
2
​
3
// ...
4
const Root = ({ state, actions }) => {
5
const data = state.source.get(state.router.link)
6
​
7
return (
8
<>
9
<Global
10
styles={css`
11
* {
12
padding: 0;
13
margin: 0;
14
box-sizing: border-box;
15
}
16
html {
17
font-family: system-ui, Arial, sans-serif;
18
}
19
`}
20
/>
21
<Header>
22
<HeaderContent>
23
<h1>Frontity Workshop</h1>
24
<p>Current URL: {state.router.link}</p>
25
<nav>
26
<Link link="/">Home</Link>
27
<Link link="/page/2">More posts</Link>
28
<Link link="/about-us">About Us</Link>
29
</nav>
30
</HeaderContent>
31
</Header>
32
<Main>
33
<Switch>
34
<List when={data.isArchive} />
35
<Post when={data.isPost} />
36
<Page when={data.isPage} />
37
</Switch>
38
</Main>
39
</>
40
)
41
}
Copied!
Have a browse around the content and notice how the styles are being applied.
Our layout is looking quite pleasing now, but the links in our menu and on our listing pages could do with being spruced up a bit. We'll address this in our next section.
Check you're on the right track by comparing your changes with the ones in this commit.
Last modified 8mo ago
Copy link