diff --git a/apps/client/src/widgets/containers/root_container.ts b/apps/client/src/widgets/containers/root_container.ts index 2e852033c4..ccc3e8a9a1 100644 --- a/apps/client/src/widgets/containers/root_container.ts +++ b/apps/client/src/widgets/containers/root_container.ts @@ -13,6 +13,8 @@ import FlexContainer from "./flex_container.js"; * * For convenience, the root container has a few class selectors that can be used to target some global state: * + * - `#root-container.light-theme`, indicates whether the current color scheme is light. + * - `#root-container.dark-theme`, indicates whether the current color scheme is dark. * - `#root-container.virtual-keyboard-opened`, on mobile devices if the virtual keyboard is open. * - `#root-container.horizontal-layout`, if the current layout is horizontal. * - `#root-container.vertical-layout`, if the current layout is horizontal. @@ -34,6 +36,7 @@ export default class RootContainer extends FlexContainer { window.visualViewport?.addEventListener("resize", () => this.#onMobileResize()); } + this.#initTheme(); this.#setDeviceSpecificClasses(); this.#setMaxContentWidth(); this.#setMotion(); @@ -67,6 +70,20 @@ export default class RootContainer extends FlexContainer { } } + #initTheme() { + const colorSchemeChangeObserver = matchMedia("(prefers-color-scheme: dark)") + colorSchemeChangeObserver.addEventListener("change", () => this.#updateColorScheme()); + + this.#updateColorScheme(); + } + + #updateColorScheme() { + const colorScheme = readCssVar(document.body, "theme-style").asString(); + + document.body.classList.toggle("light-theme", colorScheme === "light"); + document.body.classList.toggle("dark-theme", colorScheme === "dark"); + } + #onMobileResize() { const viewportHeight = window.visualViewport?.height ?? window.innerHeight; const windowHeight = Math.max(window.innerHeight, this.originalWindowHeight); // inner height changes when keyboard is opened, we need to compare with the original height to detect it.