styled-components / styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
https://styled-components.com
MIT License
40.12k stars 2.48k forks source link

correct work in nextjs app route with plugins #4274

Open mmspring13 opened 2 months ago

mmspring13 commented 2 months ago

Environment

System:

Reproduction

code like:

'use client'

import React, { useState } from 'react'
import { useServerInsertedHTML } from 'next/navigation'
import rtlPlugin from "stylis-plugin-rtl";
import { ServerStyleSheet, StyleSheetManager } from 'styled-components';

export default function StyledComponentsRegistry({
  children,
}: {
  children: React.ReactNode
}) {

  const [styledComponentsStyleSheet, seet] = useState(() => {
    return new ServerStyleSheet();
  });

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

  if (typeof window !== 'undefined') return children;

  return (
    <StyleSheetManager
      sheet={styledComponentsStyleSheet.instance}
      stylisPlugins={[rtlPlugin]}
    >
      {children}
    </StyleSheetManager>
  )
}

Steps to reproduce

yarn build && yarn start or example bug git-repo

Expected Behavior

same styles in server and client

Actual Behavior

different styles in server and client

problem similar to emotion-2928