Skip to content

Commit a4a479a

Browse files
authored
Merge pull request #864 from santhosh-005/fix/giscus-theme-hydration
fix(giscus): initialize giscus script with correct theme
2 parents 75f2fac + cedd79d commit a4a479a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/components/giscus.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ const GiscusComments: React.FC = () => {
8383
return;
8484
}
8585

86+
// Use localStorage to get user's last theme or fallback to 'light'
87+
const storedTheme =
88+
localStorage.getItem("theme") || (
89+
document.documentElement.getAttribute("data-theme") === "dark"
90+
? "dark"
91+
: "light"
92+
);
93+
8694
const script = document.createElement("script");
8795
script.src = "https://giscus.app/client.js";
8896
script.async = true;
@@ -99,7 +107,7 @@ const GiscusComments: React.FC = () => {
99107
script.setAttribute("data-lang", "en");
100108

101109
// Use the initial colorMode from Docusaurus for the initial theme
102-
script.setAttribute("data-theme", colorMode);
110+
script.setAttribute("data-theme", storedTheme);
103111

104112
ref.current.appendChild(script);
105113
}, []); // <-- Empty dependency array ensures this runs only once on mount.

0 commit comments

Comments
 (0)