diff --git a/docs/src/modules/components/AppWrapper.js b/docs/src/modules/components/AppWrapper.js index ecd5c4f01b9824..ab54ab42747039 100644 --- a/docs/src/modules/components/AppWrapper.js +++ b/docs/src/modules/components/AppWrapper.js @@ -3,13 +3,10 @@ import React from 'react'; import PropTypes from 'prop-types'; -import { JssProvider } from 'react-jss'; -import { getContext } from 'docs/src/modules/styles/context'; +import getContext, { getTheme } from 'docs/src/modules/styles/getContext'; import { connect } from 'react-redux'; import AppFrame from 'docs/src/modules/components/AppFrame'; -import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles'; -import blue from 'material-ui/colors/blue'; -import pink from 'material-ui/colors/pink'; +import { MuiThemeProvider } from 'material-ui/styles'; import { lightTheme, darkTheme, setPrismTheme } from 'docs/src/modules/utils/prism'; // Injected the insertion-point-jss after docssearch @@ -44,13 +41,7 @@ class AppWrapper extends React.Component { componentWillReceiveProps(nextProps) { if (nextProps.dark !== this.props.dark) { - this.styleContext.theme = createMuiTheme({ - palette: { - primary: blue, - secondary: pink, - type: nextProps.dark ? 'dark' : 'light', - }, - }); + this.styleContext.theme = getTheme(nextProps.dark); if (nextProps.dark) { setPrismTheme(darkTheme); @@ -66,14 +57,12 @@ class AppWrapper extends React.Component { const { children } = this.props; return ( - - - {children} - - + + {children} + ); } } diff --git a/docs/src/modules/styles/context.js b/docs/src/modules/styles/getContext.js similarity index 85% rename from docs/src/modules/styles/context.js rename to docs/src/modules/styles/getContext.js index 89a70b17efad6c..af8fa5c2109ae0 100644 --- a/docs/src/modules/styles/context.js +++ b/docs/src/modules/styles/getContext.js @@ -39,16 +39,11 @@ function createContext() { }; } -export function setContext() { - // Singleton hack as there is no way to pass variables from _document.js to pages yet. - global.__INIT_MATERIAL_UI__ = createContext(); -} - -export function getContext() { +export default function getContext() { // Make sure to create a new store for every server-side request so that data // isn't shared between connections (which would be bad) if (!process.browser) { - return global.__INIT_MATERIAL_UI__; + return createContext(); } // Reuse context on the client-side diff --git a/examples/nextjs/components/withRoot.js b/examples/nextjs/components/withRoot.js index 4568b25c46057c..f8fb2d66284efe 100644 --- a/examples/nextjs/components/withRoot.js +++ b/examples/nextjs/components/withRoot.js @@ -1,10 +1,9 @@ /* eslint-disable flowtype/require-valid-file-annotation */ import React, { Component } from 'react'; -import { JssProvider } from 'react-jss'; import { withStyles, MuiThemeProvider } from 'material-ui/styles'; import wrapDisplayName from 'recompose/wrapDisplayName'; -import { getContext } from '../styles/context'; +import getContext from '../styles/getContext'; // Apply some reset const styles = theme => ({ @@ -34,6 +33,10 @@ function withRoot(BaseComponent) { return {}; } + componentWillMount() { + this.styleContext = getContext(); + } + componentDidMount() { // Remove the server-side injected CSS. const jssStyles = document.querySelector('#jss-server-side'); @@ -43,26 +46,12 @@ function withRoot(BaseComponent) { } render() { - const context = getContext(); - - if (process.browser) { - return ( - - - - - - ); - } - return ( - - - - - - - + + + + + ); } } diff --git a/examples/nextjs/pages/_document.js b/examples/nextjs/pages/_document.js index 4d265c6613eb2b..36996849aafd21 100644 --- a/examples/nextjs/pages/_document.js +++ b/examples/nextjs/pages/_document.js @@ -2,11 +2,11 @@ import React from 'react'; import Document, { Head, Main, NextScript } from 'next/document'; -import { getContext, setContext } from '../styles/context'; +import { JssProvider } from 'react-jss'; +import getContext from '../styles/getContext'; class MyDocument extends Document { render() { - const context = getContext(); return ( @@ -26,7 +26,7 @@ class MyDocument extends Document { */} {/* PWA primary color */} - + { // 1. page.getInitialProps // 3. page.render - // Reset the context for handling a new request. - setContext(); - const page = ctx.renderPage(); - // Get the context with the collected side effects. + // Get the context to collected side effects. const context = getContext(); + const page = ctx.renderPage(Component => props => ( + + + + )); + return { ...page, + stylesContext: context, styles: (