Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
<!doctype html>
<html lang="en">
<head> </head>
<head>
<script>
(function () {
try {
var d = document.documentElement;
var s = localStorage.getItem("theme");
var isDark = false;
if (s) {
isDark = s === '"dark"' || s === "dark";
} else {
isDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
if (isDark) {
localStorage.setItem("theme", '"dark"');
}
}
if (isDark) {
d.setAttribute("data-theme", "dark");
d.classList.add("dark");
}
} catch (e) {}
})();
</script>
</head>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we fix it without such code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the inline script is the most reliable way to avoid flash 'cause it handles both the system theme and the users saved preference. BUT, another option is using CSS with prefers-color-scheme, which covers the OS setting without any script.

The trade off is that if the user has chosen a different theme than the OS, there might be a brief flash until React loads.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the inline script is the most reliable way to avoid flash 'cause it handles both the system theme and the users saved preference. BUT, another option is using CSS with prefers-color-scheme, which covers the OS setting without any script.

I think it is a good choose

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented the CSS-only route , but it creates a reversed flash for users who override their OS theme preference.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This , CSS-only approach works for most users, but if someone's OS is set to dark but they've manually picked light mode on the site (or vice versa), they still see a brief flash. So we've just moved the problem. Let me know how you'd like me to proceed.

<body>
<div id="root"></div>
</body>
Expand Down
6 changes: 5 additions & 1 deletion src/server.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ export default (locals) => {
const hoistedTags = headTagsMatch ? headTagsMatch[1] : "";
const bodyHtml = renderedHtml.slice(hoistedTags.length);

return `<!DOCTYPE html><html><head>${css}${hoistedTags}</head><body><div id="root">${bodyHtml}</div>${scripts}</body></html>`;
// Blocking inline script to prevent dark mode FOUC
const themeScript =
"<script>(function(){try{var d=document.documentElement;var s=localStorage.getItem('theme');var isDark=false;if(s){isDark=s==='\"dark\"'||s==='dark'}else{isDark=window.matchMedia('(prefers-color-scheme: dark)').matches;if(isDark){localStorage.setItem('theme','\"dark\"')}}if(isDark){d.setAttribute('data-theme','dark');d.classList.add('dark')}}catch(e){}})();</script>";

return `<!DOCTYPE html><html lang="en"><head>${themeScript}${css}${hoistedTags}</head><body><div id="root">${bodyHtml}</div>${scripts}</body></html>`;
};
Loading