/** * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; import loggerInjectable from "../../common/logger.injectable"; import userStoreInjectable from "../../common/user-store/user-store.injectable"; import { object } from "../utils"; import type { LensTheme } from "./lens-theme"; export type ApplyLensTheme = (theme: LensTheme) => void; const applyLensThemeInjectable = getInjectable({ id: "apply-lens-theme", instantiate: (di): ApplyLensTheme => { const logger = di.inject(loggerInjectable); const userStore = di.inject(userStoreInjectable); return (theme) => { try { const colors = object.entries(theme.colors); for (const [name, value] of colors) { document.documentElement.style.setProperty(`--${name}`, value); } // Adding universal theme flag which can be used in component styles document.body.classList.toggle("theme-light", theme.type === "light"); } catch (error) { logger.error("[THEME]: Failed to apply active theme", error); userStore.resetTheme(); } }; }, causesSideEffects: true, }); export default applyLensThemeInjectable;