-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix theme "flickering" from light to dark on load of the page #3520
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe pull request introduces modifications to two components in the desktop client application, specifically the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
✅ Deploy Preview for actualbudget ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Bundle Stats — desktop-clientHey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle. As this PR is updated, I'll keep you updated on how the bundle size is impacted. Total
Changeset
View detailed bundle breakdownAdded No assets were added Removed No assets were removed Bigger
Smaller No assets were smaller Unchanged
|
Bundle Stats — loot-coreHey there, this message comes from a GitHub action that helps you and reviewers to understand how these changes affect the size of this project's bundle. As this PR is updated, I'll keep you updated on how the bundle size is impacted. Total
Changeset No files were changed View detailed bundle breakdownAdded No assets were added Removed No assets were removed Bigger No assets were bigger Smaller No assets were smaller Unchanged
|
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.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
packages/desktop-client/src/components/App.tsx (1)
Line range hint
131-155
: Consider separating layout effects from other side effects.The change from
useEffect
touseLayoutEffect
is generally appropriate for thecheckScrollbars
function, as it may affect the layout and should run before the browser paints to prevent potential flickering.However, the
onVisibilityChange
function, which triggers a sync operation, might not need to be inuseLayoutEffect
as it doesn't directly affect the layout. Consider separating these concerns:
- Keep
checkScrollbars
and its related event listener inuseLayoutEffect
.- Move
onVisibilityChange
and its event listener to a separateuseEffect
.This separation would optimize performance by ensuring that only layout-related effects run synchronously, while other side effects run in the usual asynchronous manner.
Here's a suggested refactor:
useLayoutEffect(() => { function checkScrollbars() { if (hiddenScrollbars !== hasHiddenScrollbars()) { setHiddenScrollbars(hasHiddenScrollbars()); } } window.addEventListener('focus', checkScrollbars); return () => { window.removeEventListener('focus', checkScrollbars); }; }, [hiddenScrollbars]); useEffect(() => { let isSyncing = false; async function onVisibilityChange() { if (!isSyncing) { console.debug('triggering sync because of visibility change'); isSyncing = true; await dispatch(sync()); isSyncing = false; } } window.addEventListener('visibilitychange', onVisibilityChange); return () => { window.removeEventListener('visibilitychange', onVisibilityChange); }; }, [dispatch]);This refactoring separates the layout-affecting code from the sync operation, potentially improving performance and maintainability.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
⛔ Files ignored due to path filters (1)
upcoming-release-notes/3520.md
is excluded by!**/*.md
📒 Files selected for processing (2)
- packages/desktop-client/src/components/App.tsx (2 hunks)
- packages/desktop-client/src/style/theme.tsx (2 hunks)
🔇 Additional comments (2)
packages/desktop-client/src/components/App.tsx (2)
2-2
: LGTM: Import statement updated correctly.The addition of
useLayoutEffect
to the import statement is consistent with the changes in the component. This ensures that the necessary hook is available for use.
Line range hint
1-200
: Summary: Addressing late theme application with room for optimizationThe changes in this file directly address the PR objective of fixing themes being applied late. By moving the
checkScrollbars
function touseLayoutEffect
, you ensure that scrollbar-related updates happen synchronously before the browser paints, which can help prevent flickering and ensure the theme is applied earlier in the rendering process.However, as mentioned in the previous comment, there's room for optimization by separating the layout effects from other side effects. This separation would further refine the solution, ensuring that only absolutely necessary operations run synchronously while maintaining the benefits of the current implementation.
Overall, the changes are a step in the right direction for addressing the late theme application issue. With the suggested optimizations, the implementation could be even more efficient and maintainable.
@@ -52,7 +52,7 @@ export function ThemeStyle() { | |||
| undefined | |||
>(undefined); | |||
|
|||
useEffect(() => { | |||
useLayoutEffect(() => { |
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.
🛠️ Refactor suggestion
Consider the performance implications of using useLayoutEffect
The change from useEffect
to useLayoutEffect
can help prevent a flash of unstyled content (FOUC) by ensuring the theme is applied before the browser paints. This aligns with the PR objective of fixing themes being applied late.
However, useLayoutEffect
runs synchronously and can potentially block visual updates, especially given the complex logic within the effect (event listeners, media queries). This might impact the component's initial render performance.
Consider the following improvements:
-
Split the effect into two parts:
a. UseuseLayoutEffect
for the initial theme application.
b. UseuseEffect
for setting up event listeners and handling subsequent theme changes. -
Memoize the event listener function to prevent unnecessary re-creation.
Here's a potential refactor:
const darkThemeMediaQueryListener = useCallback((event: MediaQueryListEvent) => {
if (event.matches) {
setThemeColors(themes[darkThemePreference].colors);
} else {
setThemeColors(themes['light'].colors);
}
}, [darkThemePreference]);
useLayoutEffect(() => {
if (theme === 'auto') {
const darkThemeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
if (darkThemeMediaQuery.matches) {
setThemeColors(themes[darkThemePreference].colors);
} else {
setThemeColors(themes['light'].colors);
}
} else {
setThemeColors(themes[theme].colors);
}
}, [theme, darkThemePreference]);
useEffect(() => {
if (theme === 'auto') {
const darkThemeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
darkThemeMediaQuery.addEventListener('change', darkThemeMediaQueryListener);
return () => {
darkThemeMediaQuery.removeEventListener('change', darkThemeMediaQueryListener);
};
}
}, [theme, darkThemeMediaQueryListener]);
This approach maintains the benefits of early theme application while potentially improving performance.
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.
Nice suggestion. I have implemented 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.
@joel-jeremy, glad to hear that you've implemented the suggestion!
(^‿^)/
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
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.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (2)
packages/desktop-client/src/style/theme.tsx (2)
66-79
: Excellent: ImplementeduseLayoutEffect
for immediate theme applicationThe use of
useLayoutEffect
ensures that theme colors are set synchronously before the browser paints, effectively addressing the flash of unstyled content (FOUC) issue. The logic correctly handles both 'auto' and specific theme selections.Consider memoizing the
darkThemeMediaQuery
to avoid recreating it on every render:const darkThemeMediaQuery = useMemo( () => window.matchMedia('(prefers-color-scheme: dark)'), [] ); useLayoutEffect(() => { if (theme === 'auto') { if (darkThemeMediaQuery.matches) { setThemeColors(themes[darkThemePreference].colors); } else { setThemeColors(themes['light'].colors); } } else { setThemeColors(themes[theme].colors); } }, [theme, darkThemePreference, darkThemeMediaQuery]);This optimization can further improve performance by avoiding unnecessary media query object creation.
81-97
: Well done: Improved event listener managementThe
useEffect
hook has been correctly implemented to manage the event listener for theme changes. It properly uses the memoizeddarkThemeMediaQueryListener
and includes a cleanup function to remove the listener when necessary.Consider moving the
darkThemeMediaQuery
creation outside the effect to avoid recreating it on every render:const darkThemeMediaQuery = useMemo( () => window.matchMedia('(prefers-color-scheme: dark)'), [] ); useEffect(() => { if (theme === 'auto') { darkThemeMediaQuery.addEventListener('change', darkThemeMediaQueryListener); return () => { darkThemeMediaQuery.removeEventListener('change', darkThemeMediaQueryListener); }; } }, [theme, darkThemeMediaQueryListener, darkThemeMediaQuery]);This change would be consistent with the optimization suggested for the
useLayoutEffect
hook and further improve performance.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- packages/desktop-client/src/style/theme.tsx (2 hunks)
🔇 Additional comments (3)
packages/desktop-client/src/style/theme.tsx (3)
2-2
: LGTM: Import statement updated correctlyThe addition of
useLayoutEffect
to the import statement is consistent with its usage in the component and aligns with the PR objective of fixing themes being applied late.
55-64
: Great job: Implemented performance optimizationThe
darkThemeMediaQueryListener
function is well-implemented usinguseCallback
. This memoization addresses the performance concern raised in the previous review by preventing unnecessary re-creation of the function. The logic for handling theme changes based on the media query is correct and concise.
Line range hint
1-107
: Overall: Excellent improvements to theme handlingThe changes in this file successfully address the PR objective of fixing themes being applied late. The implementation of
useLayoutEffect
for immediate theme application and the optimized event listener management withuseEffect
are well-done. The code is well-structured and follows React best practices.A few minor optimization suggestions have been provided to further improve performance. Additionally, consider addressing the
@ts-strict-ignore
comment to enhance type safety.Great job on these improvements!
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.
38a6c02
to
794ec36
Compare
This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
Fixes the theme color change (from light theme to configured theme) that happens when initially loading the page.
Switching to
useLayoutEffect
so that the proper theme colors are set before the page is painted on screen