Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Fix theme flickering for sync_with_system preference

Kabiru Mwenjaeca76d9f67c9
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 338d7d6adc2..8a70d56793c 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -317,13 +317,8 @@ module ApplicationHelper
def user_theme_data_attributes
if User.current.pref.sync_with_system_theme?
- # Default to light mode initially before System theme is applied
- # FIXME: Causes flickering on page navigation and load
- {
- color_mode: "light",
- light_theme: "light",
- auto_theme_switcher_mode_value: User.current.pref.theme
- }
+ # Theme will be set by inline script before body renders to prevent flickering
+ { auto_theme_switcher_mode_value: User.current.pref.theme }
else
mode, _theme_suffix = User.current.pref.theme.split("_", 2)
{
diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb
index e7d71456141..5dcb558d85f 100644
--- a/app/views/layouts/base.html.erb
+++ b/app/views/layouts/base.html.erb
@@ -40,6 +40,30 @@ See COPYRIGHT and LICENSE files for more details.
<meta name="current_menu_item" content="<%= current_menu_item %>"/>
<%# Disable prefetching for now %>
<meta name="turbo-prefetch" content="false">
+ <%# Apply system theme immediately to prevent flickering %>
+ <% if User.current.pref.sync_with_system_theme? %>
+ <%= nonced_javascript_tag do %>
+ // Apply system theme before body renders to prevent flickering
+ // This duplicates logic from AutoThemeSwitcher controller for immediate execution
+ (function() {
+ const theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
+ const body = document.body;
+
+ switch (theme) {
+ case 'dark':
+ body.setAttribute('data-color-mode', 'dark');
+ body.setAttribute('data-dark-theme', 'dark');
+ body.removeAttribute('data-light-theme');
+ break;
+ case 'light':
+ body.setAttribute('data-color-mode', 'light');
+ body.setAttribute('data-light-theme', 'light');
+ body.removeAttribute('data-dark-theme');
+ break;
+ }
+ })();
+ <% end %>
+ <% end %>
</head>
<%= content_tag :body,
class: "#{body_css_classes} __overflowing_element_container __overflowing_body",