Stoop

GitHub
/preloadTheme

preloadTheme

#

Preload theme CSS variables before React renders to prevent FOUC (Flash of Unstyled Content)

Usage

#
import { preloadTheme } from "./theme";

// Preload theme by name
preloadTheme("dark");

// Preload theme object directly
preloadTheme(customTheme);

Parameters

#
ParameterTypeDescription
themestring | ThemeTheme name (string) or theme object to preload

Preventing FOUC

#

When loading a non-default theme from localStorage, use preloadTheme() before React renders:

Client Component Approach

#
"use client";

import { useEffect, type ReactNode } from "react";
import { preloadTheme } from "./theme";

export default function RootLayout({ children }: { children: ReactNode }) {
  useEffect(() => {
    const savedTheme = localStorage.getItem("stoop-theme");
    if (savedTheme) {
      preloadTheme(savedTheme);
    }
  }, []);

  return <>{children}</>;
}
#
import Script from "next/script";
import { type ReactNode } from "react";
import { getCssText } from "./theme";

export default function RootLayout({ children }: { children: ReactNode }) {
  return (
    <html>
      <head>
        <style
          id="stoop-styles"
          dangerouslySetInnerHTML={{ __html: getCssText() }}
        />
        <Script
          id="stoop-theme-preload"
          strategy="beforeInteractive"
          dangerouslySetInnerHTML={{
            __html: `
              (function() {
                const savedTheme = localStorage.getItem('stoop-theme');
                if (savedTheme) {
                  // Preload theme CSS variables synchronously
                }
              })();
            `,
          }}
        />
      </head>
      <body>{children}</body>
    </html>
  );
}

Notes

#