fix(theme): prevent forced light mode switch after login#16221
fix(theme): prevent forced light mode switch after login#16221charlesBochet merged 2 commits intotwentyhq:mainfrom
Conversation
Greptile OverviewGreptile SummaryThis PR fixes the theme switching issue where the application forced light mode after login even when the system was set to dark mode. The fix centralizes system color scheme resolution in Key Changes:
Issues Found:
Confidence Score: 4/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant App as AppRouterProviders
participant Base as BaseThemeProvider
participant System as useSystemColorScheme
participant User as UserAndViewsProviderEffect
participant Theme as UserThemeProviderEffect
participant Recoil as persistedColorSchemeState
Note over App,Recoil: Initial Load (Before Login)
App->>Base: Render with BaseThemeProvider
Base->>Recoil: Read persistedColorSchemeState (default: 'System')
Base->>System: useSystemColorScheme()
System-->>Base: Returns 'Dark' or 'Light' from media query
Base->>Base: effectiveColorScheme = 'System' ? systemColorScheme : persistedColorScheme
Base->>Base: Set document.documentElement.className
Base->>Base: Select theme (THEME_DARK or THEME_LIGHT)
Note over App,Recoil: After Login
App->>User: Load user data
User->>User: Query workspace member
User->>User: Set default colorScheme to 'System' (line 154)
User->>Recoil: Update currentWorkspaceMemberState
App->>Theme: UserThemeProviderEffect renders
Theme->>Theme: useColorScheme() returns user's colorScheme
Theme->>Recoil: setPersistedColorScheme(colorScheme)
Recoil->>Base: persistedColorSchemeState updated
Base->>System: useSystemColorScheme() (if 'System')
System-->>Base: Returns current system preference
Base->>Base: Resolve effectiveColorScheme
Base->>Base: Update document.documentElement.className
Base->>Base: Apply correct theme
|
| document.documentElement.className = | ||
| persistedColorScheme === 'Dark' ? 'dark' : 'light'; | ||
| effectiveColorScheme === 'Dark' ? 'dark' : 'light'; |
There was a problem hiding this comment.
style: DOM manipulation during render phase violates React best practices. This side effect should be wrapped in a useEffect to avoid potential issues with concurrent rendering and StrictMode double-invocation.
| document.documentElement.className = | |
| persistedColorScheme === 'Dark' ? 'dark' : 'light'; | |
| effectiveColorScheme === 'Dark' ? 'dark' : 'light'; | |
| useEffect(() => { | |
| document.documentElement.className = | |
| effectiveColorScheme === 'Dark' ? 'dark' : 'light'; | |
| }, [effectiveColorScheme]); |
Context Used: Context from dashboard - Use useEffect in EffectComponent or hooks only. Move effects to dedicated components when necess... (source)
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/twenty-front/src/modules/ui/theme/components/BaseThemeProvider.tsx
Line: 28:29
Comment:
**style:** DOM manipulation during render phase violates React best practices. This side effect should be wrapped in a `useEffect` to avoid potential issues with concurrent rendering and StrictMode double-invocation.
```suggestion
useEffect(() => {
document.documentElement.className =
effectiveColorScheme === 'Dark' ? 'dark' : 'light';
}, [effectiveColorScheme]);
```
**Context Used:** Context from `dashboard` - Use `useEffect` in `EffectComponent` or hooks only. Move effects to dedicated components when necess... ([source](https://app.greptile.com/review/custom-context?memory=fb506a89-cfe0-41d0-96a8-c0cc5a883fcf))
How can I resolve this? If you propose a fix, please make it concise.|
🚀 Preview Environment Ready! Your preview environment is available at: http://bore.pub:10268 This environment will automatically shut down when the PR is closed or after 5 hours. |
|
Thanks @Anshgrover23 for your contribution! |
) This pull request resolves an issue where the application switched to light mode after login even when the user’s system was set to dark mode. Before authentication the app correctly followed system preferences through CSS media queries, but that behavior broke once the user logged in. **Before** https://github.com/user-attachments/assets/75e73712-9cb2-42f9-9e25-3a22dc911b8d **After** https://github.com/user-attachments/assets/6bba45ce-3817-4e3d-9b45-ff0c7725cf82

Description
This pull request resolves an issue where the application switched to light mode after login even when the user’s system was set to dark mode. Before authentication the app correctly followed system preferences through CSS media queries, but that behavior broke once the user logged in.
Before
Screen.Recording.2025-12-01.at.9.59.46.PM.mov
After
after-video-dark.mov