fix: add styled registry

This commit is contained in:
swve 2023-01-14 01:39:49 +01:00
parent 88dc060bae
commit 2fc9d90e9f
3 changed files with 31 additions and 2 deletions

1
.gitignore vendored
View file

@ -21,7 +21,6 @@ dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/

View file

@ -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";

View file

@ -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 (
<StyleSheetManager sheet={styledComponentsStyleSheet.instance}>
{children as React.ReactChild}
</StyleSheetManager>
);
}