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

DRY up theme switching implementation

Kabiru Mwenjaede66462a59a
diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb
index 6363f71536c..e610d296b7f 100644
--- a/app/views/layouts/base.html.erb
+++ b/app/views/layouts/base.html.erb
@@ -44,25 +44,8 @@ See COPYRIGHT and LICENSE files for more details.
<% 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 colorModePreference = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
- const body = document.body;
-
- switch (colorModePreference) {
- 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', window.matchMedia('(prefers-contrast: more)').matches ? 'light_high_contrast' : 'light');
- body.removeAttribute('data-dark-theme');
- break;
- default: // Do nothing
- break;
- }
+ window.OpenProject.theme.applySystemThemeImmediately();
})();
<% end %>
<% end %>
diff --git a/frontend/src/app/core/setup/globals/openproject.ts b/frontend/src/app/core/setup/globals/openproject.ts
index 53084e544eb..c404c4c0ff8 100644
--- a/frontend/src/app/core/setup/globals/openproject.ts
+++ b/frontend/src/app/core/setup/globals/openproject.ts
@@ -30,6 +30,7 @@ import { OpenProjectPluginContext } from 'core-app/features/plugins/plugin-conte
import { input, InputState } from '@openproject/reactivestates';
import { getMetaElement, GlobalHelpers } from 'core-app/core/setup/globals/global-helpers';
import { firstValueFrom } from 'rxjs';
+import { ThemeUtils } from './theme-utils';
export type OpenProjectPageState = 'pristine'|'edited'|'submitted';
@@ -41,6 +42,11 @@ export class OpenProject {
public helpers = new GlobalHelpers();
+ /**
+ * Theme utilities for system theme detection and application
+ */
+ public theme = new ThemeUtils();
+
/** Globally setable variable whether the page was edited or submitted */
pageState:OpenProjectPageState = 'pristine';
diff --git a/frontend/src/app/core/setup/globals/theme-utils.ts b/frontend/src/app/core/setup/globals/theme-utils.ts
new file mode 100644
index 00000000000..30865313315
--- /dev/null
+++ b/frontend/src/app/core/setup/globals/theme-utils.ts
@@ -0,0 +1,62 @@
+/*
+ * -- copyright
+ * OpenProject is an open source project management software.
+ * Copyright (C) the OpenProject GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 3.
+ *
+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+ * Copyright (C) 2006-2013 Jean-Philippe Lang
+ * Copyright (C) 2010-2013 the ChiliProject Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * See COPYRIGHT and LICENSE files for more details.
+ * ++
+ */
+
+export type OpTheme = 'light' | 'light_high_contrast' | 'dark';
+
+export class ThemeUtils {
+ public applySystemThemeImmediately():void {
+ const theme = this.detectSystemTheme();
+ this.applyThemeToBody(theme);
+ }
+
+ public detectSystemTheme():OpTheme {
+ return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
+ }
+
+ public applyThemeToBody(theme:OpTheme):void {
+ const increaseContrast = window.matchMedia('(prefers-contrast: more)').matches;
+ 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', increaseContrast ? 'light_high_contrast' : 'light');
+ body.removeAttribute('data-dark-theme');
+ break;
+ default: // Do nothing
+ break;
+ }
+ }
+}
diff --git a/frontend/src/react/OpBlockNoteContainer.tsx b/frontend/src/react/OpBlockNoteContainer.tsx
index 3c8b08332a5..0b0b71a4b6d 100644
--- a/frontend/src/react/OpBlockNoteContainer.tsx
+++ b/frontend/src/react/OpBlockNoteContainer.tsx
@@ -33,7 +33,7 @@ import { BlockNoteView } from "@blocknote/mantine";
import { getDefaultReactSlashMenuItems, SuggestionMenuController, useCreateBlockNote } from "@blocknote/react";
import { dummyBlockSpec, getDefaultOpenProjectSlashMenuItems, openProjectWorkPackageBlockSpec } from "op-blocknote-extensions";
import { useEffect, useState } from "react";
-import { OpTheme } from "../stimulus/controllers/auto-theme-switcher.controller";
+import { OpTheme } from "core-app/core/setup/globals/theme-utils";
export interface OpBlockNoteContainerProps {
inputField: HTMLInputElement;
diff --git a/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts b/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts
index b67c2d6068a..4614ee39072 100644
--- a/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts
+++ b/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts
@@ -30,8 +30,7 @@
import { Controller } from '@hotwired/stimulus';
import { useMatchMedia } from 'stimulus-use';
-
-export type OpTheme = 'light' | 'light_high_contrast' | 'dark';
+import { ThemeUtils } from 'core-app/core/setup/globals/theme-utils';
export default class AutoThemeSwitcher extends Controller {
static values = {
@@ -39,10 +38,13 @@ export default class AutoThemeSwitcher extends Controller {
};
declare readonly modeValue:string;
+ private themeUtils:ThemeUtils;
connect() {
if (this.modeValue !== 'sync_with_os') return;
+ this.themeUtils = new ThemeUtils();
+
useMatchMedia(this, {
mediaQueries: {
lightMode: '(prefers-color-scheme: light)',
@@ -51,33 +53,10 @@ export default class AutoThemeSwitcher extends Controller {
}
isLightMode():void {
- this.applyTheme('light');
+ this.themeUtils.applyThemeToBody('light');
}
notLightMode():void {
- this.applyTheme('dark');
- }
-
- private applyTheme(theme:OpTheme):void {
- 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', this.increaseContrast ? 'light_high_contrast' : 'light');
- body.removeAttribute('data-dark-theme');
- break;
- default: // Do nothing
- break;
- }
- }
-
- private get increaseContrast():boolean {
- return window.matchMedia('(prefers-contrast: more)').matches;
+ this.themeUtils.applyThemeToBody('dark');
}
}