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

code maintenance/66397 DRY up theme application & detection logic (#19942)

Kabiru Mwenjac25f46a8443d
diff --git a/frontend/src/app/core/setup/globals/theme-utils.ts b/frontend/src/app/core/setup/globals/theme-utils.ts
index dfe4ca10ad7..d81c25a55c4 100644
--- a/frontend/src/app/core/setup/globals/theme-utils.ts
+++ b/frontend/src/app/core/setup/globals/theme-utils.ts
@@ -28,15 +28,21 @@
* ++
*/
-export type OpTheme = 'light' | 'light_high_contrast' | 'dark' | 'dark_high_contrast';
+export type OpColorMode = 'light' | 'dark';
+
+export type OpTheme = OpColorMode | `${OpColorMode}_high_contrast`;
export class ThemeUtils {
public applySystemThemeImmediately():void {
- const theme = this.detectSystemTheme();
- this.applyThemeToBody(theme);
+ const colorMode = this.detectSystemColorMode();
+ this.applyThemeToBody(colorMode);
+ }
+
+ public detectOpColorMode():OpColorMode {
+ return document.body.getAttribute('data-color-mode') as OpColorMode;
}
- public detectSystemTheme():OpTheme {
+ public detectSystemColorMode():OpColorMode {
return this.prefersSystemLightMode() ? 'light' : 'dark';
}
@@ -52,23 +58,13 @@ export class ThemeUtils {
return window.matchMedia('(prefers-contrast: more)').matches;
}
- public applyThemeToBody(theme:OpTheme):void {
- const increaseContrast = this.prefersSystemHighContrast();
+ public applyThemeToBody(colorMode:OpColorMode):void {
const body = document.body;
+ const increaseContrast = this.prefersSystemHighContrast();
+ const otherColorMode = (colorMode === 'light' ? 'dark' : 'light');
- switch (theme) {
- case 'dark':
- body.setAttribute('data-color-mode', 'dark');
- body.setAttribute('data-dark-theme', increaseContrast ? 'dark_high_contrast' : '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;
- }
+ body.setAttribute('data-color-mode', colorMode);
+ body.setAttribute(`data-${colorMode}-theme`, increaseContrast ? `${colorMode}_high_contrast` : colorMode);
+ body.removeAttribute(`data-${otherColorMode}-theme`);
}
}
diff --git a/frontend/src/react/OpBlockNoteContainer.tsx b/frontend/src/react/OpBlockNoteContainer.tsx
index 0b0b71a4b6d..b76118ac380 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 "core-app/core/setup/globals/theme-utils";
+import { OpColorMode } from "core-app/core/setup/globals/theme-utils";
export interface OpBlockNoteContainerProps {
inputField: HTMLInputElement;
@@ -48,12 +48,7 @@ const schema = BlockNoteSchema.create({
},
});
-const detectTheme = (): OpTheme => {
- if (document.body.getAttribute('data-color-mode') === 'dark') {
- return 'dark';
- }
- return 'light';
-};
+const detectTheme = ():OpColorMode => { return window.OpenProject.theme.detectOpColorMode(); };
export default function OpBlockNoteContainer({ inputField, inputText }: OpBlockNoteContainerProps) {
const [isLoading, setIsLoading] = useState(true);