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