Commit 01da6f47 authored by Naoyuki Kanezawa's avatar Naoyuki Kanezawa Committed by Dan Zajdband

Add styletron example (#486)

* add styletron example

* example: fix link

* example: improve README

* Added styletron example reference to readme and merge with master
parent de57e921
...@@ -93,6 +93,14 @@ export default () => ( ...@@ -93,6 +93,14 @@ export default () => (
#### CSS-in-JS #### 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: It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
```jsx ```jsx
......
# 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>`.
import { StyletronProvider } from 'styletron-react'
import getStyletron from './styletron'
export default ({ children }) => (
<StyletronProvider styletron={getStyletron()}>
{children}
</StyletronProvider>
)
module.exports = {
webpack: function (config) {
config.externals = config.externals || {}
config.externals['styletron-server'] = 'styletron-server'
return config
}
}
{
"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"
}
}
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>
)
}
}
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>
)
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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment