@material-ui/styles#ServerStyleSheets TypeScript Examples
The following examples show how to use
@material-ui/styles#ServerStyleSheets.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: _document.tsx From Demae with MIT License | 6 votes |
static async getInitialProps(ctx: DocumentContext) {
const styledComponentsSheet = new ServerStyleSheet()
const materialSheets = new ServerStyleSheets()
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () => originalRenderPage({
enhanceApp: App => props => styledComponentsSheet.collectStyles(materialSheets.collect(<App {...props} />))
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<React.Fragment>
{initialProps.styles}
{materialSheets.getStyleElement()}
{styledComponentsSheet.getStyleElement()}
</React.Fragment>
)
}
} finally {
styledComponentsSheet.seal()
}
}
Example #2
Source File: _document.tsx From Teyvat.moe with GNU General Public License v3.0 | 6 votes |
static async getInitialProps(context: DocumentContext): Promise<DocumentInitialProps> {
// Render app and page and get the context of the page with collected side effects.
const sheets = new ServerStyleSheets({
// injectFirst: true,
serverGenerateClassName: generateClassName,
});
const originalRenderPage = context.renderPage;
context.renderPage = () =>
originalRenderPage({
// The method wraps your React node in a provider element.
// It collects the style sheets during the rendering so they can be later sent to the client.
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
console.info(`Performing SSR for path ${context.pathname} (${context.locale})`);
const initialProps = await Document.getInitialProps(context);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [...Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
}
Example #3
Source File: _document.tsx From storefront with MIT License | 6 votes |
static async getInitialProps(ctx: DocumentContext) {
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) => sheets.collect(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
// Styles fragment is rendered after the app and page rendering finish.
styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()],
};
}