Commit 9ddc1d7b authored by Leon's avatar Leon Committed by Tim Neutkens

update styletron example (#5573)

parent 27c0b199
import { StyletronProvider } from 'styletron-react'
import getStyletron from './styletron'
export default ({ children }) => (
<StyletronProvider styletron={getStyletron()}>
{children}
</StyletronProvider>
)
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
"next": "latest", "next": "latest",
"react": "^16.0.0", "react": "^16.0.0",
"react-dom": "^16.0.0", "react-dom": "^16.0.0",
"styletron-client": "^2.2.0", "styletron": "^3.0.4",
"styletron-react": "^2.2.1", "styletron-engine-atomic": "^1.0.8",
"styletron-server": "^2.2.0" "styletron-react": "^4.3.5"
} }
} }
import App, { Container } from 'next/app'
import React from 'react'
import { Provider as StyletronProvider } from 'styletron-react'
import getStyletron from '../styletron'
class MyApp extends App {
render () {
const { Component } = this.props
return (
<Container>
<StyletronProvider value={getStyletron()}>
<Component />
</StyletronProvider>
</Container>
)
}
}
export default MyApp
...@@ -17,7 +17,7 @@ export default class MyDocument extends Document { ...@@ -17,7 +17,7 @@ export default class MyDocument extends Document {
<style <style
className='_styletron_hydrate_' className='_styletron_hydrate_'
dangerouslySetInnerHTML={{ __html: sheet.css }} dangerouslySetInnerHTML={{ __html: sheet.css }}
media={sheet.media || ''} media={sheet.media || null}
key={i} key={i}
/> />
))} ))}
......
import { styled } from 'styletron-react' import { styled } from 'styletron-react'
import Page from '../layout'
const Title = styled('div', { const Title = styled('div', {
color: 'red', color: 'red',
...@@ -7,7 +6,7 @@ const Title = styled('div', { ...@@ -7,7 +6,7 @@ const Title = styled('div', {
}) })
export default () => ( export default () => (
<Page> <div>
<Title>My page</Title> <Title>My page</Title>
</Page> </div>
) )
const isServer = typeof window === 'undefined' const isServer = typeof window === 'undefined'
let styletron let styletron
export default function getStyletron () { export default function getStyletron () {
if (isServer) { if (isServer && !styletron) {
const Styletron = require('styletron-server') const Styletron = require('styletron-engine-atomic').Server
styletron = new Styletron() styletron = new Styletron()
} else if (!styletron) { } else if (!styletron) {
const Styletron = require('styletron-client') const Styletron = require('styletron-engine-atomic').Client
const styleElements = document.getElementsByClassName('_styletron_hydrate_') const styleElements = document.getElementsByClassName('_styletron_hydrate_')
styletron = new Styletron(styleElements) styletron = new Styletron(styleElements)
} }
return styletron 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