-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.ts
More file actions
29 lines (27 loc) · 757 Bytes
/
Copy pathindex.ts
File metadata and controls
29 lines (27 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* A function that iterates through the computed style properties of a HTML
* element and redefines them as inline styles.
*/
export default function computedStyleToInlineStyle(
element: HTMLElement | SVGElement,
options?: {
recursive?: boolean;
properties?: string[];
}
): void {
if (!element) {
throw new Error('No element specified.');
}
if (options?.recursive) {
Array.prototype.forEach.call(element.children, (child) => {
computedStyleToInlineStyle(child, options);
});
}
const computedStyle = getComputedStyle(element);
Array.prototype.forEach.call(
options?.properties || computedStyle,
(property) => {
element.style[property] = computedStyle.getPropertyValue(property);
}
);
}