-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(next): try injecting styles via a component registry
- Loading branch information
Showing
4 changed files
with
755 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.