From a5c4a7d719892bd8fc050f620be860ca9ff51b60 Mon Sep 17 00:00:00 2001 From: ankitrewar11 Date: Sat, 21 Mar 2026 19:04:39 +0530 Subject: [PATCH 1/7] fix: resolve dark mode flicker issue Signed-off-by: ankitrewar11 --- onRenderBody.js | 109 +++++++++++++++------------ src/theme/app/StyledThemeProvider.js | 10 +-- src/theme/app/ThemeManager.js | 33 ++++---- 3 files changed, 76 insertions(+), 76 deletions(-) diff --git a/onRenderBody.js b/onRenderBody.js index 370709d6c1ceb..1d0cf975d41d8 100644 --- a/onRenderBody.js +++ b/onRenderBody.js @@ -1,57 +1,70 @@ import React from "react"; -import { DarkThemeKey, ThemeSetting } from "./src/theme/app/ThemeManager.js"; +import { DarkThemeKey, ThemeSetting } from "./src/theme/app/ThemeManager"; import lighttheme, { darktheme } from "./src/theme/app/themeStyles"; -const themes = { light: lighttheme, dark: darktheme }; +const themes = { + light: lighttheme, + dark: darktheme, +}; + +const MagicScriptTag = ({ theme }) => { + const themeJSON = JSON.stringify(theme); -const MagicScriptTag = (props) => { const codeToRunOnClient = ` - (function() { - // 1. Keeps SYSTEM as the priority preference - const themeFromLocalStorage = localStorage.getItem('${DarkThemeKey}') || '${ThemeSetting.SYSTEM}'; - - // 2. We change the check to look for LIGHT mode explicitly - const systemLightModeSetting = () => window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null; - - const isLightModeActive = () => { - return !!systemLightModeSetting()?.matches; - }; - - let colorMode; - switch (themeFromLocalStorage) { - case '${ThemeSetting.SYSTEM}': - // LOGIC CHANGE: If Light is active -> Light. Otherwise (Dark, No Preference, or Error) -> Dark. - colorMode = isLightModeActive() ? '${ThemeSetting.LIGHT}' : '${ThemeSetting.DARK}' - break - case '${ThemeSetting.DARK}': - case '${ThemeSetting.LIGHT}': - colorMode = themeFromLocalStorage - break - default: - // 3. Fallback to DARK in case of error - colorMode = '${ThemeSetting.DARK}' - } - - const root = document.documentElement; - const iterate = (obj) => { - if (!obj) return; - Object.keys(obj).forEach(key => { - if (typeof obj[key] === 'object') { - iterate(obj[key]) - } else { - root.style.setProperty("--" + key, obj[key]) - } - }) - } - const parsedTheme = JSON.parse('${JSON.stringify(props.theme)}') - const theme = parsedTheme[colorMode] - iterate(theme) - root.style.setProperty('--initial-color-mode', colorMode); - })() - `; +(function() { + try { + var themeFromLocalStorage = localStorage.getItem('${DarkThemeKey}') || '${ThemeSetting.SYSTEM}'; + var systemLightModeSetting = function() { + return window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null; + }; + var isLightModeActive = function() { + var mql = systemLightModeSetting(); + return mql ? mql.matches : false; + }; + + var colorMode; + switch (themeFromLocalStorage) { + case '${ThemeSetting.SYSTEM}': + colorMode = isLightModeActive() ? '${ThemeSetting.LIGHT}' : '${ThemeSetting.DARK}'; + break; + case '${ThemeSetting.DARK}': + case '${ThemeSetting.LIGHT}': + colorMode = themeFromLocalStorage; + break; + default: + colorMode = '${ThemeSetting.DARK}'; + } + + var root = document.documentElement; + var parsedTheme = ${themeJSON}; + var selectedTheme = parsedTheme[colorMode]; + + var iterate = function(obj) { + if (!obj) return; + Object.keys(obj).forEach(function(key) { + if (typeof obj[key] === 'object' && obj[key] !== null) { + iterate(obj[key]); + } else { + root.style.setProperty('--' + key, obj[key]); + } + }); + }; + + iterate(selectedTheme); + root.style.setProperty('--initial-color-mode', colorMode); + root.setAttribute('data-theme', colorMode); + + window.__theme = colorMode; + + } catch (e) {} +})(); +`; + return