Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(theme): stop Dark Reader from darkening a dark theme
Elian Doranf87aec3fc19b
diff --git a/apps/client/src/services/theme.spec.ts b/apps/client/src/services/theme.spec.tsindex 53931ff815..57fb77a516 100644--- a/apps/client/src/services/theme.spec.ts+++ b/apps/client/src/services/theme.spec.ts@@ -1,6 +1,9 @@import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { applyTheme, buildThemeStylesheetRefs, getConfiguredThemeStylesheets, getEffectiveThemeStyle, getThemeStyle, initThemeChangeNotifier, onEffectiveThemeStyleChange } from "./theme.js";+import {+ applyTheme, buildThemeStylesheetRefs, getConfiguredThemeStylesheets, getEffectiveThemeStyle, getThemeStyle,+ initThemeChangeNotifier, onEffectiveThemeStyleChange, updateColorSchemeClasses+} from "./theme.js";// theme.ts lazily imports the app context to emit `themeChanged`; mock it so the tests capture the emission// without pulling the whole app graph into happy-dom.@@ -45,6 +48,9 @@ afterEach(async () => {win.matchMedia = originalMatchMedia;win.glob = originalGlob;Reflect.deleteProperty(document, "hidden");+ for (const lock of document.head.querySelectorAll("meta[name='darkreader-lock']")) {+ lock.remove();+ }triggerEvent.mockClear();vi.restoreAllMocks();});@@ -386,8 +392,44 @@ describe("initThemeChangeNotifier", () => {await new Promise((resolve) => setTimeout(resolve));expect(triggerEvent).not.toHaveBeenCalled();});++ it("adds the Dark Reader lock only while the effective style is dark", () => {+ setTheme("next-dark");+ installMatchMedia(false);+ initThemeChangeNotifier();+ expect(darkReaderLocks()).toHaveLength(1);++ setTheme("next");+ const mql = installMatchMedia(false);+ initThemeChangeNotifier();+ expect(darkReaderLocks()).toHaveLength(0);++ mql.matches = true;+ mql.listener?.();+ expect(darkReaderLocks()).toHaveLength(1);++ mql.matches = false;+ mql.listener?.();+ expect(darkReaderLocks()).toHaveLength(0);+ });++ it("locks Dark Reader for a dark custom theme once its stylesheet reports the style", () => {+ setTheme("my-theme");+ installMatchMedia(false);+ initThemeChangeNotifier();+ expect(darkReaderLocks()).toHaveLength(0);++ stubComputedStyle({ "--theme-style": "dark" });+ updateColorSchemeClasses();+ expect(darkReaderLocks()).toHaveLength(1);+ expect(document.body.classList.contains("dark-theme")).toBe(true);+ });});+function darkReaderLocks() {+ return document.head.querySelectorAll("meta[name='darkreader-lock']");+}+function themeStylesheetHrefs() {return Array.from(document.head.querySelectorAll<HTMLLinkElement>("link[data-theme-stylesheet]")).map((link) => link.getAttribute("href"));diff --git a/apps/client/src/services/theme.ts b/apps/client/src/services/theme.tsindex db1bfb2485..d00b3f3b35 100644--- a/apps/client/src/services/theme.ts+++ b/apps/client/src/services/theme.ts@@ -114,6 +114,24 @@ export function updateColorSchemeClasses() {const colorScheme = readCssVar(document.body, "theme-style").asString();document.body.classList.toggle("light-theme", colorScheme === "light");document.body.classList.toggle("dark-theme", colorScheme === "dark");+ syncDarkReaderLock();+}++/**+ * Adds `<meta name="darkreader-lock">` while the effective style is dark, so the Dark Reader extension leaves a+ * dark theme alone and still darkens a light one. Dark Reader disables itself when the tag appears, but removing+ * the tag does not re-enable it until the page reloads.+ */+function syncDarkReaderLock() {+ const lock = document.head.querySelector("meta[name='darkreader-lock']");+ const isDark = getEffectiveThemeStyle() === "dark";+ if (isDark && !lock) {+ const meta = document.createElement("meta");+ meta.name = "darkreader-lock";+ document.head.appendChild(meta);+ } else if (!isDark && lock) {+ lock.remove();+ }}let backgroundEffectsSuspended = false;@@ -265,6 +283,7 @@ export function onEffectiveThemeStyleChange(listener: (themeStyle: "light" | "da*/export function initThemeChangeNotifier() {lastKnownThemeStyle = getEffectiveThemeStyle();+ syncDarkReaderLock();// A module-level reference, so a repeated init cannot stack duplicate listeners.document.addEventListener("visibilitychange", reconcileOnceVisible);@@ -293,6 +312,7 @@ function reconcileEffectiveThemeStyle() {function notifyThemeChanged() {const themeStyle = getEffectiveThemeStyle();lastKnownThemeStyle = themeStyle;+ syncDarkReaderLock();for (const listener of themeStyleListeners) {listener(themeStyle);}diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.htmlindex 7c32f140c8..fff48cc25e 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Themes.html@@ -6,6 +6,10 @@<p><img src="Themes_dark-theme.png" alt="Dark Theme"></p>+<p>While a dark theme is active, Trilium asks the Dark Reader browser extension+ not to darken the page a second time. Dark Reader still darkens the light+ themes. After switching from a dark theme to a light one, reload the page+ (<kbd>Ctrl</kbd>+<kbd>R</kbd>) for Dark Reader to take effect again.</p><h2>Creating Custom CSS Themes</h2><p>Trilium supports custom user themes, allowing you to personalize the application'sappearance. To create a custom theme, follow these steps:</p>diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Reference.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Reference.htmlindex 99475a2c50..a85dfdebc4 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Reference.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Theme development/Reference.html@@ -146,7 +146,9 @@ body.electron:not(.native-titlebar) {<p>A light theme needs to have the following CSS:</p><pre><code class="language-text-css">:root {--theme-style: light;}</code></pre>-<p>if the theme is dark, then <code>--theme-style</code> needs to be <code>dark</code>.</p>+<p>if the theme is dark, then <code>--theme-style</code> needs to be <code>dark</code>.+ A dark <code>--theme-style</code> also tells the Dark Reader browser extension+ to leave the page alone.</p><p>If the theme is auto (e.g. supports both light or dark based on <code>prefers-color-scheme</code>)it must also declare (in addition to setting <code>--theme-style</code> toeither <code>light</code> or <code>dark</code>):</p><pre><code class="language-text-css">:root {diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md b/docs/User Guide/User Guide/Basic Concepts and Features/Themes.mdindex 98a8b4eed1..d0e36e5029 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Themes.md@@ -5,6 +5,8 @@ Trilium comes with a couple pre-installed color themes, with the default being a+While a dark theme is active, Trilium asks the Dark Reader browser extension not to darken the page a second time. Dark Reader still darkens the light themes. After switching from a dark theme to a light one, reload the page (<kbd>Ctrl</kbd>+<kbd>R</kbd>) for Dark Reader to take effect again.+## Creating Custom CSS ThemesTrilium supports custom user themes, allowing you to personalize the application's appearance. To create a custom theme, follow these steps:diff --git a/docs/User Guide/User Guide/Theme development/Reference.md b/docs/User Guide/User Guide/Theme development/Reference.mdindex edd21208d8..6b31a6133c 100644--- a/docs/User Guide/User Guide/Theme development/Reference.md+++ b/docs/User Guide/User Guide/Theme development/Reference.md@@ -185,7 +185,7 @@ A light theme needs to have the following CSS:}```-if the theme is dark, then `--theme-style` needs to be `dark`.+if the theme is dark, then `--theme-style` needs to be `dark`. A dark `--theme-style` also tells the Dark Reader browser extension to leave the page alone.If the theme is auto (e.g. supports both light or dark based on `prefers-color-scheme`) it must also declare (in addition to setting `--theme-style` to either `light` or `dark`):