mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
/**
|
|
* 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 { loggerInjectionToken } from "@k8slens/logger";
|
|
import { object } from "@k8slens/utilities";
|
|
import type { LensTheme } from "./lens-theme";
|
|
import resetThemeInjectable from "../../features/user-preferences/common/reset-theme.injectable";
|
|
|
|
export type ApplyLensTheme = (theme: LensTheme) => void;
|
|
|
|
const applyLensThemeInjectable = getInjectable({
|
|
id: "apply-lens-theme",
|
|
instantiate: (di): ApplyLensTheme => {
|
|
const logger = di.inject(loggerInjectionToken);
|
|
const resetTheme = di.inject(resetThemeInjectable);
|
|
|
|
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);
|
|
resetTheme();
|
|
}
|
|
};
|
|
},
|
|
causesSideEffects: true,
|
|
});
|
|
|
|
export default applyLensThemeInjectable;
|