Skip to content

Commit 975bd67

Browse files
committed
fix(next): try injecting styles via a component registry
1 parent 0559a9e commit 975bd67

File tree

4 files changed

+755
-605
lines changed

4 files changed

+755
-605
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"semantic-release": "semantic-release"
5050
},
5151
"dependencies": {
52+
"next": "^13.4.2",
5253
"styled-components": "6.0.0-rc.1"
5354
},
5455
"config": {

src/components/next.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
3+
import React, { useState } from 'react';
4+
import { useServerInsertedHTML } from 'next/navigation';
5+
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';
6+
7+
export const DotsUIComponentRegistry = ({
8+
children,
9+
}: {
10+
children: React.ReactNode;
11+
}) => {
12+
// Only create stylesheet once with lazy initial state
13+
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
14+
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());
15+
16+
useServerInsertedHTML(() => {
17+
const styles = styledComponentsStyleSheet.getStyleElement();
18+
styledComponentsStyleSheet.instance.clearTag();
19+
return <>{styles}</>;
20+
});
21+
22+
if (typeof window !== 'undefined') return <>{children}</>;
23+
24+
return (
25+
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
26+
{children}
27+
</StyleSheetManager>
28+
);
29+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ export { Button, type ButtonProps } from './components/Button';
44
export { Background } from './components/Background';
55
export { TextField, type TextFieldProps } from './components/TextField';
66
export { Text, type TextProps } from './components/Text';
7-
export { ThemeProvider } from './components/ThemeProvider';
7+
export { ThemeProvider } from './components/ThemeProvider';
8+
export { DotsUIComponentRegistry } from './components/next';

0 commit comments

Comments
 (0)