Skip to content

Commit 8d8d5eb

Browse files
Robin Frischmannimpronunciable
authored andcommitted
Example using Fela (#863)
* added example using fela * update package-json * removed nested routing test * fixed linting issues * fixed typo
1 parent 2390c87 commit 8d8d5eb

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

examples/with-fela/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
# Example app with Fela
3+
4+
## How to use
5+
6+
Download the example [or clone the repo](https://github.com/zeit/next.js):
7+
8+
```bash
9+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-fela
10+
cd with-fela
11+
```
12+
13+
Install it and run:
14+
15+
```bash
16+
npm install
17+
npm run dev
18+
```
19+
20+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
21+
22+
```bash
23+
now
24+
```
25+
26+
## The idea behind the example
27+
28+
This example features how to use a different styling solution than [styled-jsx](https://github.com/zeit/styled-jsx) that also supports universal styles. That means we can serve the required styles for the first render within the HTML and then load the rest in the client. In this case we are using [fela](https://github.com/rofrischmann/fela).
29+
30+
For this purpose we are extending the `<Document />` and injecting the server side rendered styles into the `<head>`.

examples/with-fela/fela.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createRenderer } from 'fela'
2+
3+
// add your renderer configuration here
4+
const renderer = createRenderer()
5+
6+
export function getRenderer () {
7+
return renderer
8+
}
9+
10+
export function getMountNode () {
11+
if (typeof window !== 'undefined') {
12+
return document.getElementById('fela-stylesheet')
13+
}
14+
15+
return undefined
16+
}

examples/with-fela/layout.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Provider } from 'react-fela'
2+
import { getRenderer, getMountNode } from './fela'
3+
4+
export default ({ children }) => (
5+
<Provider renderer={getRenderer()} mountNode={getMountNode()}>
6+
{children}
7+
</Provider>
8+
)

examples/with-fela/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "with-fela",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"fela": "^4.1.2",
11+
"next": "^2.0.0-beta",
12+
"react-fela": "^4.1.2"
13+
}
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Document, { Head, Main, NextScript } from 'next/document'
2+
import { getRenderer } from '../fela'
3+
4+
export default class MyDocument extends Document {
5+
static getInitialProps ({ renderPage }) {
6+
const page = renderPage()
7+
const renderer = getRenderer()
8+
const css = renderer.renderToString()
9+
10+
renderer.clear()
11+
12+
return {
13+
...page,
14+
css
15+
}
16+
}
17+
18+
render () {
19+
return (
20+
<html>
21+
<Head>
22+
<title>My page</title>
23+
<style id='fela-stylesheet'>{this.props.css}</style>
24+
</Head>
25+
<body>
26+
<Main />
27+
<NextScript />
28+
</body>
29+
</html>
30+
)
31+
}
32+
}

examples/with-fela/pages/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createComponent } from 'react-fela'
2+
import Page from '../layout'
3+
4+
const title = ({ size }) => ({
5+
fontSize: size + 'px',
6+
color: 'red'
7+
})
8+
9+
const Title = createComponent(title, 'h1')
10+
11+
export default () => (
12+
<Page>
13+
<Title size={50}>My Title</Title>
14+
</Page>
15+
)

0 commit comments

Comments
 (0)