Commit 693ba44a authored by Vojtech Miksu's avatar Vojtech Miksu Committed by Luis Fernando Alvarez D

Update with-styletron example, add debug mode and useStyletron (#7290)

parent 4309c87f
...@@ -8,10 +8,9 @@ ...@@ -8,10 +8,9 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^16.7.0", "react": "^16.8.0",
"react-dom": "^16.7.0", "react-dom": "^16.8.0",
"styletron": "^3.0.4", "styletron-engine-atomic": "^1.1.2",
"styletron-engine-atomic": "^1.0.8", "styletron-react": "^5.0.1"
"styletron-react": "^4.3.5"
} }
} }
import App, { Container } from 'next/app'
import React from 'react' import React from 'react'
import App, { Container } from 'next/app'
import { Provider as StyletronProvider } from 'styletron-react' import { Provider as StyletronProvider } from 'styletron-react'
import getStyletron from '../styletron' import { styletron, debug } from '../styletron'
class MyApp extends App { export default class MyApp extends App {
render () { render () {
const { Component } = this.props const { Component, pageProps } = this.props
return ( return (
<Container> <Container>
<StyletronProvider value={getStyletron()}> <StyletronProvider value={styletron} debug={debug} debugAfterHydration>
<Component /> <Component {...pageProps} />
</StyletronProvider> </StyletronProvider>
</Container> </Container>
) )
} }
} }
export default MyApp
import Document, { Head, Main, NextScript } from 'next/document' import Document, { Head, Main, NextScript } from 'next/document'
import { flush } from '../styletron' import { Provider as StyletronProvider } from 'styletron-react'
import { styletron } from '../styletron'
export default class MyDocument extends Document { class MyDocument extends Document {
static getInitialProps ({ renderPage }) { static getInitialProps (props) {
const page = renderPage() const page = props.renderPage(App => props => (
const styletron = flush() <StyletronProvider value={styletron}>
const stylesheets = styletron ? styletron.getStylesheets() : [] <App {...props} />
</StyletronProvider>
))
const stylesheets = styletron.getStylesheets() || []
return { ...page, stylesheets } return { ...page, stylesheets }
} }
...@@ -16,8 +20,9 @@ export default class MyDocument extends Document { ...@@ -16,8 +20,9 @@ export default class MyDocument extends Document {
{this.props.stylesheets.map((sheet, i) => ( {this.props.stylesheets.map((sheet, i) => (
<style <style
className='_styletron_hydrate_' className='_styletron_hydrate_'
dangerouslySetInnerHTML={{ __html: sheet }} dangerouslySetInnerHTML={{ __html: sheet.css }}
media={sheet.attrs ? sheet.attrs.media : null} media={sheet.attrs.media}
data-hydrate={sheet.attrs['data-hydrate']}
key={i} key={i}
/> />
))} ))}
...@@ -30,3 +35,5 @@ export default class MyDocument extends Document { ...@@ -30,3 +35,5 @@ export default class MyDocument extends Document {
) )
} }
} }
export default MyDocument
import { styled } from 'styletron-react' // DOCUMENTATION: http://styletron.org
const Title = styled('div', { import { styled, useStyletron } from 'styletron-react'
// statically styled component
const Title = styled('h1', {
color: 'red', color: 'red',
fontSize: '50px' fontSize: '82px'
}) })
export default () => ( // dynamically styled component
const SubTitle = styled('h2', ({ $size }) => ({
color: 'blue',
fontSize: `${$size}px`
}))
export default () => {
// an alternative hook based API
const [css] = useStyletron()
return (
<div> <div>
<Title>My page</Title> <Title>Title</Title>
<SubTitle $size={50}>Subtitle</SubTitle>
<p className={css({ fontSize: '32px' })}>Styled by hook</p>
</div> </div>
) )
}
const isServer = typeof window === 'undefined' import { Client, Server } from 'styletron-engine-atomic'
import { DebugEngine } from 'styletron-react'
let styletron const getHydrateClass = () =>
document.getElementsByClassName('_styletron_hydrate_')
export default function getStyletron () { export const styletron =
if (isServer && !styletron) { typeof window === 'undefined'
const Styletron = require('styletron-engine-atomic').Server ? new Server()
styletron = new Styletron() : new Client({
} else if (!styletron) { hydrate: getHydrateClass()
const Styletron = require('styletron-engine-atomic').Client })
const styleElements = document.getElementsByClassName('_styletron_hydrate_')
styletron = new Styletron(styleElements)
}
return styletron
}
export function flush () { export const debug =
const _styletron = styletron process.env.NODE_ENV === 'production' ? void 0 : new DebugEngine()
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