NODEDC_DESIGN_GUIDELINE/packages/ui-core/src/theme.ts

29 lines
759 B
TypeScript

import { createAccentVariables, type RgbTuple } from "./accent";
export type NodedcTheme = "dark" | "light";
export interface ApplyThemeOptions {
theme?: NodedcTheme;
accent?: RgbTuple;
}
export function applyNodedcTheme(
target: HTMLElement,
{ theme = "dark", accent }: ApplyThemeOptions = {},
): void {
target.dataset.nodedcTheme = theme;
target.dataset.nodedcUi = "true";
if (!accent) return;
const variables = createAccentVariables(accent);
Object.entries(variables).forEach(([property, value]) => {
target.style.setProperty(property, value);
});
}
export function clearNodedcAccent(target: HTMLElement): void {
target.style.removeProperty("--nodedc-accent-rgb");
target.style.removeProperty("--nodedc-on-accent-rgb");
}