From 2fc9d90e9fba248d185e2de9ff1e211ae2ddba1a Mon Sep 17 00:00:00 2001 From: swve Date: Sat, 14 Jan 2023 01:39:49 +0100 Subject: [PATCH] fix: add styled registry --- .gitignore | 1 - front/app/layout.tsx | 2 +- front/components/lib/styled-registry.tsx | 30 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 front/components/lib/styled-registry.tsx diff --git a/.gitignore b/.gitignore index 60e1bcdd..91c46fb8 100644 --- a/.gitignore +++ b/.gitignore @@ -21,7 +21,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ diff --git a/front/app/layout.tsx b/front/app/layout.tsx index dbc34036..c2124db0 100644 --- a/front/app/layout.tsx +++ b/front/app/layout.tsx @@ -1,6 +1,6 @@ "use client"; import "../styles/globals.css"; -import StyledComponentsRegistry from "../services/lib/styled-registry"; +import StyledComponentsRegistry from "../components/lib/styled-registry"; import { Menu } from "../components/UI/Elements/Menu"; import { motion } from "framer-motion"; diff --git a/front/components/lib/styled-registry.tsx b/front/components/lib/styled-registry.tsx new file mode 100644 index 00000000..663995d4 --- /dev/null +++ b/front/components/lib/styled-registry.tsx @@ -0,0 +1,30 @@ + +'use client'; + +import React, { useState } from 'react'; +import { useServerInsertedHTML } from 'next/navigation'; +import { ServerStyleSheet, StyleSheetManager } from 'styled-components'; + +export default function StyledComponentsRegistry({ + 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 ( + + {children as React.ReactChild} + + ); +} \ No newline at end of file