Skip to content

Commit c78a6f9

Browse files
authored
Update StyledThemeProvider for SSR theme consistency
Refactor StyledThemeProvider to ensure consistent theme rendering for SSR. Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
1 parent 8d09e77 commit c78a6f9

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/theme/app/StyledThemeProvider.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
//uses isDark state to choose styled-component theme (in themeStyles.js)
2+
//and use ThemeProvider to allow all styled components access to values via props.theme
3+
14
import React, { useContext } from "react";
25
import { ThemeProvider } from "styled-components";
36
import { ThemeManagerContext } from "./ThemeManager";
47

8+
// Safe check for browser environment
59
const isBrowser = typeof window !== "undefined";
610

711
export const StyledThemeProvider = (props) => {
812
const { children, darkTheme, lightTheme } = props;
913
const { isDark, didLoad } = useContext(ThemeManagerContext);
1014

15+
// For SSR, we need to provide a consistent theme initially
16+
// This ensures the server and client render the same thing initially
1117
const currentTheme = isDark ? darkTheme : lightTheme;
1218
const theme = {
1319
...(didLoad ? currentTheme : transformTheme(currentTheme)),
@@ -20,7 +26,7 @@ export const StyledThemeProvider = (props) => {
2026
);
2127
};
2228

23-
const transformTheme = (theme) => {
29+
// Maps JS theme object to raw CSS variables (--key) initialized via onRenderBody.const transformTheme = (theme) => {
2430
const newTheme = {};
2531
Object.keys(theme).forEach((key) => {
2632
const value = theme[key];

0 commit comments

Comments
 (0)