-
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
8
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.
+82
−52
Open
Changes from all commits
Commits
Show all changes
8 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 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
| 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,29 +37,37 @@ 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.LIGHT; | ||
|
|
||
| // 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. |
||
| const storedTheme = localStorage.getItem(DarkThemeKey); | ||
|
|
||
| if (storedTheme && storedTheme !== ThemeSetting.SYSTEM) { | ||
| const isDarkTheme = storedTheme === ThemeSetting.DARK; | ||
| 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 +80,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 +102,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 +138,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.