From 01da6f47614041cac56587078bfc19d7cc28c238 Mon Sep 17 00:00:00 2001 From: Naoyuki Kanezawa Date: Fri, 23 Dec 2016 22:01:24 +0900 Subject: [PATCH] Add styletron example (#486) * add styletron example * example: fix link * example: improve README * Added styletron example reference to readme and merge with master --- README.md | 8 ++++++ examples/with-styletron/README.md | 30 ++++++++++++++++++++++ examples/with-styletron/layout.js | 8 ++++++ examples/with-styletron/next.config.js | 7 +++++ examples/with-styletron/package.json | 15 +++++++++++ examples/with-styletron/pages/_document.js | 26 +++++++++++++++++++ examples/with-styletron/pages/index.js | 13 ++++++++++ examples/with-styletron/styletron.js | 23 +++++++++++++++++ 8 files changed, 130 insertions(+) create mode 100644 examples/with-styletron/README.md create mode 100644 examples/with-styletron/layout.js create mode 100644 examples/with-styletron/next.config.js create mode 100644 examples/with-styletron/package.json create mode 100644 examples/with-styletron/pages/_document.js create mode 100644 examples/with-styletron/pages/index.js create mode 100644 examples/with-styletron/styletron.js diff --git a/README.md b/README.md index b2f2072c..ad83d030 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,14 @@ export default () => ( #### CSS-in-JS +

+ Examples + +

+ It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles: ```jsx diff --git a/examples/with-styletron/README.md b/examples/with-styletron/README.md new file mode 100644 index 00000000..6accfc72 --- /dev/null +++ b/examples/with-styletron/README.md @@ -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 `` and injecting the server side rendered styles into the ``. diff --git a/examples/with-styletron/layout.js b/examples/with-styletron/layout.js new file mode 100644 index 00000000..9fe974aa --- /dev/null +++ b/examples/with-styletron/layout.js @@ -0,0 +1,8 @@ +import { StyletronProvider } from 'styletron-react' +import getStyletron from './styletron' + +export default ({ children }) => ( + + {children} + +) diff --git a/examples/with-styletron/next.config.js b/examples/with-styletron/next.config.js new file mode 100644 index 00000000..cd8910d8 --- /dev/null +++ b/examples/with-styletron/next.config.js @@ -0,0 +1,7 @@ +module.exports = { + webpack: function (config) { + config.externals = config.externals || {} + config.externals['styletron-server'] = 'styletron-server' + return config + } +} diff --git a/examples/with-styletron/package.json b/examples/with-styletron/package.json new file mode 100644 index 00000000..4853f9f2 --- /dev/null +++ b/examples/with-styletron/package.json @@ -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" + } +} diff --git a/examples/with-styletron/pages/_document.js b/examples/with-styletron/pages/_document.js new file mode 100644 index 00000000..ebc33b4e --- /dev/null +++ b/examples/with-styletron/pages/_document.js @@ -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 ( + + + My page +