Skip to content

Commit

Permalink
build: add styled-components settings with next
Browse files Browse the repository at this point in the history
  • Loading branch information
Mornieur committed Sep 3, 2023
1 parent b8abdeb commit 9e448b2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
24 changes: 24 additions & 0 deletions lib/registry.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useState } from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';

export default function StyledComponentsRegistry({
children,
}: {
children: React.ReactNode;
}) {
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());

useServerInsertedHTML(() => {
const styleTags = styledComponentsStyleSheet.getStyleTags();
return <>{styleTags}</>;
});

if (typeof window !== 'undefined') return <>{children}</>;

return (
<StyleSheetManager sheet={styledComponentsStyleSheet}>
{children}
</StyleSheetManager>
);
}
28 changes: 17 additions & 11 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Html, Head, Main, NextScript } from 'next/document'
import React from 'react';
import Document, { Html, Head, Main, NextScript } from 'next/document';
import StyledComponentsRegistry from '../../lib/registry';

export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
export default class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head />
<body>
<StyledComponentsRegistry>
<Main />
</StyledComponentsRegistry>
<NextScript />
</body>
</Html>
);
}
}

0 comments on commit 9e448b2

Please sign in to comment.