Skip to content

Commit 514f6ca

Browse files
committedJan 10, 2024
fix: improve performance of isColor detection (see #378)
1 parent b1a04fc commit 514f6ca

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed
 

‎src/lib/utils/typeUtils.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ export function isTimestamp(value: unknown): boolean {
7979
* Source: https://stackoverflow.com/questions/6386090/validating-css-color-names/33184805
8080
*/
8181
export function getColorCSS(color: string): string | null {
82-
const div = window.document.createElement('div')
82+
colorTestDiv = colorTestDiv || window.document.createElement('div')
8383

84-
div.style.color = color
84+
colorTestDiv.style.color = ''
85+
colorTestDiv.style.color = color
8586

86-
const applied = div.style.color
87+
const applied = colorTestDiv.style.color
8788
return applied !== '' ? applied.replace(/\s+/g, '').toLowerCase() : null
8889
}
90+
let colorTestDiv: HTMLDivElement | null = null
8991

9092
/**
9193
* Test if a string contains a valid color name or code.

0 commit comments

Comments
 (0)
Please sign in to comment.