1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

Add styletron example (#486)

* add styletron example

* example: fix link

* example: improve README

* Added styletron example reference to readme and merge with master
This commit is contained in:
Naoyuki Kanezawa 2016-12-23 22:01:24 +09:00 committed by Dan Zajdband
parent de57e92109
commit 01da6f4761
8 changed files with 130 additions and 0 deletions

View file

@ -93,6 +93,14 @@ export default () => (
#### CSS-in-JS
<p><details>
<summary><b>Examples</b></summary>
<ul>
<li><a href="./examples/with-styled-components">Styled components</a></li>
<li><a href="./examples/with-styletron">Styletron</a></li>
</ul>
</details></p>
It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
```jsx

View file

@ -0,0 +1,30 @@
# Example app with styletron
## How to use
Download the example (or clone the repo)[https://github.com/zeit/next.js.git]:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-styletron
cd with-styletron
```
Install it and run:
```bash
npm install
npm run dev
```
Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download))
```bash
now
```
## The idea behind the example
This example features how yo 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 [styletron](https://github.com/rtsao/styletron).
For this purpose we are extending the `<Document />` and injecting the server side rendered styles into the `<head>`.

View file

@ -0,0 +1,8 @@
import { StyletronProvider } from 'styletron-react'
import getStyletron from './styletron'
export default ({ children }) => (
<StyletronProvider styletron={getStyletron()}>
{children}
</StyletronProvider>
)

View file

@ -0,0 +1,7 @@
module.exports = {
webpack: function (config) {
config.externals = config.externals || {}
config.externals['styletron-server'] = 'styletron-server'
return config
}
}

View file

@ -0,0 +1,15 @@
{
"name": "with-styletron",
"version": "1.0.0",
"scripts": {
"dev": "next",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "^2.0.0-beta",
"styletron-client": "^2.2.0",
"styletron-react": "^2.2.1",
"styletron-server": "^2.2.0"
}
}

View file

@ -0,0 +1,26 @@
import Document, { Head, Main, NextScript } from 'next/document'
import { flush } from '../styletron'
export default class MyDocument extends Document {
static getInitialProps ({ renderPage }) {
const page = renderPage()
const styletron = flush()
const css = styletron ? styletron.getCss() : null
return { ...page, css }
}
render () {
return (
<html>
<Head>
<title>My page</title>
<style className='_styletron_hydrate_' dangerouslySetInnerHTML={{ __html: this.props.css }} />
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
)
}
}

View file

@ -0,0 +1,13 @@
import { styled } from 'styletron-react'
import Page from '../layout'
const Title = styled('div', {
color: 'red',
fontSize: '50px'
})
export default () => (
<Page>
<Title>My page</Title>
</Page>
)

View file

@ -0,0 +1,23 @@
const isServer = typeof window === 'undefined'
let styletron
export default function getStyletron () {
if (isServer) {
const Styletron = require('styletron-server')
styletron = new Styletron()
} else if (!styletron) {
const Styletron = require('styletron-client')
const styleElements = document.getElementsByClassName('_styletron_hydrate_')
styletron = new Styletron(styleElements)
}
return styletron
}
export function flush () {
const _styletron = styletron
styletron = null
return _styletron
}