Skip to content

Commit

Permalink
fix(next): try injecting styles via a component registry
Browse files Browse the repository at this point in the history
  • Loading branch information
hackrmomo committed May 12, 2023
1 parent 0559a9e commit 975bd67
Show file tree
Hide file tree
Showing 4 changed files with 755 additions and 605 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"semantic-release": "semantic-release"
},
"dependencies": {
"next": "^13.4.2",
"styled-components": "6.0.0-rc.1"
},
"config": {
Expand Down
29 changes: 29 additions & 0 deletions src/components/next.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';

import React, { useState } from 'react';
import { useServerInsertedHTML } from 'next/navigation';
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';

export const DotsUIComponentRegistry = ({
children,
}: {
children: React.ReactNode;
}) => {
// Only create stylesheet once with lazy initial state
// x-ref: https://reactjs.org/docs/hooks-reference.html#lazy-initial-state
const [styledComponentsStyleSheet] = useState(() => new ServerStyleSheet());

useServerInsertedHTML(() => {
const styles = styledComponentsStyleSheet.getStyleElement();
styledComponentsStyleSheet.instance.clearTag();
return <>{styles}</>;
});

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

return (
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
{children}
</StyleSheetManager>
);
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export { Button, type ButtonProps } from './components/Button';
export { Background } from './components/Background';
export { TextField, type TextFieldProps } from './components/TextField';
export { Text, type TextProps } from './components/Text';
export { ThemeProvider } from './components/ThemeProvider';
export { ThemeProvider } from './components/ThemeProvider';
export { DotsUIComponentRegistry } from './components/next';
Loading

0 comments on commit 975bd67

Please sign in to comment.