-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: resolve dark mode flicker issue #7533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AnkitRewar11
wants to merge
9
commits into
layer5io:master
Choose a base branch
from
AnkitRewar11:fix/dark-mode-flicker-clean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+85
−78
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a5c4a7d
fix: resolve dark mode flicker issue
8d09e77
Merge branch 'master' into fix/dark-mode-flicker-clean
AnkitRewar11 c78a6f9
Update StyledThemeProvider for SSR theme consistency
AnkitRewar11 7642946
Merge branch 'master' into fix/dark-mode-flicker-clean
AnkitRewar11 21df90e
Refactor ThemeManager for improved theme handling
AnkitRewar11 ed0aa74
Refactor theme injection logic in onRenderBody.js
AnkitRewar11 9ad62cb
Refactor StyledThemeProvider with improved comments
AnkitRewar11 f60787a
chore: restore original onRenderBody structure and apply precise FOUC…
AnkitRewar11 4491cc1
add one more comment
AnkitRewar11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ const isBrowser = typeof window !== "undefined"; | |
|
|
||
| const systemDarkModeSetting = () => | ||
| isBrowser && window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null; | ||
|
|
||
| const isDarkModeActive = () => { | ||
| return !!systemDarkModeSetting()?.matches; | ||
| }; | ||
|
|
@@ -36,19 +37,28 @@ const applyThemeToDOM = (theme) => { | |
| const root = window.document.documentElement; | ||
| root.style.setProperty("--initial-color-mode", theme); | ||
| root.setAttribute("data-theme", theme); | ||
| window.__theme = theme; | ||
| }; | ||
|
|
||
| export const ThemeManagerProvider = (props) => { | ||
| const [themeSetting, setThemeSetting] = useState(ThemeSetting.SYSTEM); | ||
| const [didLoad, setDidLoad] = useState(false); | ||
| const [isDark, setIsDark] = useState(false); | ||
|
|
||
| const [isDark, setIsDark] = useState(() => { | ||
| if (isBrowser) { | ||
| if (window.__theme === ThemeSetting.DARK) return true; | ||
| if (window.__theme === ThemeSetting.LIGHT) return false; | ||
| } | ||
| return false; | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (!isBrowser) return; | ||
|
|
||
| const root = window.document.documentElement; | ||
| const initialColorValue = root.style.getPropertyValue("--initial-color-mode"); | ||
|
|
||
| const initialColorValue = (root.style.getPropertyValue("--initial-color-mode") || "").trim(); | ||
| const actualTheme = window.__theme || initialColorValue || ThemeSetting.DARK; | ||
|
|
||
| // Get stored theme from localStorage | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don’t remove the comments.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @rishiraj38, I hope everything got added properly. I’ve added that one comment as you suggested. |
||
| const storedTheme = localStorage.getItem(DarkThemeKey); | ||
|
|
||
|
|
@@ -57,8 +67,8 @@ export const ThemeManagerProvider = (props) => { | |
| setIsDark(isDarkTheme); | ||
| setThemeSetting(storedTheme); | ||
| applyThemeToDOM(storedTheme); | ||
| } else if (initialColorValue) { | ||
| setIsDark(initialColorValue === ThemeSetting.DARK); | ||
| } else if (actualTheme) { | ||
| setIsDark(actualTheme === ThemeSetting.DARK); | ||
| setThemeSetting(ThemeSetting.SYSTEM); | ||
| } else { | ||
| // Fallback to system preference | ||
|
|
@@ -71,7 +81,7 @@ export const ThemeManagerProvider = (props) => { | |
| setDidLoad(true); | ||
| }, []); | ||
|
|
||
| // Listen to system color scheme changes only when on SYSTEM mode | ||
| // Listen to system color scheme changes only when on SYSTEM mode | ||
| useEffect(() => { | ||
| if (!isBrowser || themeSetting !== ThemeSetting.SYSTEM) return; | ||
|
|
||
|
|
@@ -93,11 +103,11 @@ export const ThemeManagerProvider = (props) => { | |
| const newIsDark = !isDark; | ||
| const newTheme = newIsDark ? ThemeSetting.DARK : ThemeSetting.LIGHT; | ||
|
|
||
| // Update state | ||
| // Update state | ||
| setIsDark(newIsDark); | ||
| setThemeSetting(newTheme); | ||
|
|
||
| // Apply to DOM immediately | ||
| // Apply to DOM immediately | ||
| applyThemeToDOM(newTheme); | ||
|
|
||
| // Persist to localStorage | ||
|
|
@@ -129,14 +139,14 @@ export const ThemeManagerProvider = (props) => { | |
| return; | ||
| } | ||
|
|
||
| // Update state | ||
| // Update state | ||
| setIsDark(newIsDark); | ||
| setThemeSetting(setting); | ||
|
|
||
| // Apply to DOM immediately | ||
| applyThemeToDOM(themeToApply); | ||
|
|
||
| // Persist to localStorage | ||
| // Persist to localStorage | ||
| localStorage.setItem(DarkThemeKey, setting); | ||
| }, | ||
| [isDark] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was the existing approach completely ripped out and replaced?
What is precisely wrong with the current approach? Fix that.
If the current approach is completely offbase - ok - explain that and why a complete rewrite is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AnkitRewar11 can you answer this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @saurabhraghuvanshii, just wanted to clearly explain why the master branch approach was failing and how this PR fixes it.
I’ve only made a few focused fixes to solve the dark mode flicker issue:
Wrong Injection Placement: The script was running too late (inside ), so the page first showed light mode. I moved it to so the correct theme loads before anything is displayed.
Missing data-theme: The code wasn’t setting the data-theme attribute on the HTML root, which is needed for dark mode styles. I’ve added that.
SSR / Hydration Mismatch: The server knew the theme, but the frontend didn’t, so it defaulted to light mode and caused flicker. I fixed this by passing the theme (window.__theme) so both stay in sync.
JSON Parsing Issue: The previous way of parsing theme data could break if there were quotes in the string. I made it safer to avoid crashes.
Now the changes are minimal, clean, and focused and the flicker issue is completely resolved. I hope all your questions have been answered. If there’s anything else you’d like to know, please let me know.