mirror of
https://github.com/jnsahaj/tweakcn.git
synced 2026-08-28 23:02:07 +08:00
b629ff8d5a
Fix the issue of style value accumulation in root elements when switching themes.
18 lines
417 B
TypeScript
18 lines
417 B
TypeScript
export function applyStyleToElement(
|
|
element: HTMLElement,
|
|
key: string,
|
|
value: string
|
|
) {
|
|
const currentStyle = element.getAttribute("style") || "";
|
|
// Remove the existing variable definitions with the same name
|
|
const cleanedStyle = currentStyle.replace(
|
|
new RegExp(`--${key}:\\s*[^;]+;?`, "g"),
|
|
""
|
|
).trim();
|
|
|
|
element.setAttribute(
|
|
"style",
|
|
`${cleanedStyle}--${key}: ${value};`
|
|
);
|
|
}
|