Skip to content

Commit 1bddb11

Browse files
miguelcasttimneutkens
authored andcommitted
Example/add react-with-styles example (vercel#2417)
* Example/add react-with-styles example * fix standars next
1 parent 4a0278b commit 1bddb11

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[![Deploy to now](https://deploy.now.sh/static/button.svg)](https://deploy.now.sh/?repo=https://github.com/zeit/next.js/tree/master/examples/with-react-with-styles)
2+
3+
# Example app with react-with-styles
4+
5+
## How to use
6+
7+
Download the example [or clone the repo](https://github.com/zeit/next.js):
8+
9+
```bash
10+
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-react-with-styles
11+
cd with-react-with-styles
12+
```
13+
14+
Install it and run:
15+
16+
```bash
17+
npm install
18+
npm run dev
19+
```
20+
21+
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
22+
23+
```bash
24+
now
25+
```
26+
27+
## The idea behind the example
28+
29+
This example features how you use a different styling solution than [styled-jsx](https://github.com/zeit/styled-jsx) that also supports universal styles.
30+
That means we can serve the required styles for the first render within the HTML and then load the rest in the client.
31+
In this case we are using [react-with-styles](https://github.com/airbnb/react-with-styles).
32+
33+
For this purpose we are extending the `<Document />` and injecting the server side rendered styles into the `<head>`.
34+
35+
We are using `pages/_index.js` from this example [with-aphrodite](https://github.com/zeit/next.js/tree/v3-beta/examples/with-aphrodite).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default {
2+
color: {
3+
primary: 'salmon'
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "with-react-with-styles",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"dev": "next",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"aphrodite": "^1.2.1",
11+
"next": "latest",
12+
"react": "^15.6.1",
13+
"react-dom": "^15.6.1",
14+
"react-with-styles": "^1.4.0",
15+
"react-with-styles-interface-aphrodite": "^1.2.0"
16+
},
17+
"license": "MIT"
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Document, { Head, Main, NextScript } from 'next/document'
2+
import { StyleSheetServer } from 'aphrodite'
3+
4+
export default class MyDocument extends Document {
5+
static async getInitialProps ({ renderPage }) {
6+
const { html, css } = StyleSheetServer.renderStatic(() => renderPage())
7+
const ids = css.renderedClassNames
8+
return { ...html, css, ids }
9+
}
10+
11+
constructor (props) {
12+
super(props)
13+
/* Take the renderedClassNames from aphrodite (as generated
14+
in getInitialProps) and assign them to __NEXT_DATA__ so that they
15+
are accessible to the client for rehydration. */
16+
const { __NEXT_DATA__, ids } = props
17+
if (ids) {
18+
__NEXT_DATA__.ids = this.props.ids
19+
}
20+
}
21+
22+
render () {
23+
/* Make sure to use data-aphrodite attribute in the style tag here
24+
so that aphrodite knows which style tag it's in control of when
25+
the client goes to render styles. If you don't you'll get a second
26+
<style> tag */
27+
return (
28+
<html>
29+
<Head>
30+
<style data-aphrodite dangerouslySetInnerHTML={{ __html: this.props.css.content }} />
31+
</Head>
32+
<body>
33+
<Main />
34+
<NextScript />
35+
</body>
36+
</html>
37+
)
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import { css, withStyles } from '../withStyles'
3+
4+
function Home ({ styles }) {
5+
return (
6+
<div>
7+
<h1 {...css(styles.title)}>My page</h1>
8+
</div>
9+
)
10+
}
11+
12+
export default withStyles(({ color }) => ({
13+
title: {
14+
color: color.primary
15+
}
16+
}))(Home)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import ThemedStyleSheet from 'react-with-styles/lib/ThemedStyleSheet'
2+
import aphroditeInterface from 'react-with-styles-interface-aphrodite'
3+
import { css, withStyles, ThemeProvider } from 'react-with-styles'
4+
import MyDefaultTheme from './defaultTheme'
5+
6+
ThemedStyleSheet.registerDefaultTheme(MyDefaultTheme)
7+
ThemedStyleSheet.registerInterface(aphroditeInterface)
8+
9+
export { css, withStyles, ThemeProvider, ThemedStyleSheet }

0 commit comments

Comments
 (0)