From 514f6cafdf4937d495670395028e0f326a585a9a Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 10 Jan 2024 10:59:39 +0100 Subject: [PATCH] fix: improve performance of `isColor` detection (see #378) --- src/lib/utils/typeUtils.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib/utils/typeUtils.ts b/src/lib/utils/typeUtils.ts index 7bbd3faa..6ab74aa8 100644 --- a/src/lib/utils/typeUtils.ts +++ b/src/lib/utils/typeUtils.ts @@ -79,13 +79,15 @@ export function isTimestamp(value: unknown): boolean { * Source: https://stackoverflow.com/questions/6386090/validating-css-color-names/33184805 */ export function getColorCSS(color: string): string | null { - const div = window.document.createElement('div') + colorTestDiv = colorTestDiv || window.document.createElement('div') - div.style.color = color + colorTestDiv.style.color = '' + colorTestDiv.style.color = color - const applied = div.style.color + const applied = colorTestDiv.style.color return applied !== '' ? applied.replace(/\s+/g, '').toLowerCase() : null } +let colorTestDiv: HTMLDivElement | null = null /** * Test if a string contains a valid color name or code.