Skip to content

Commit f6f88bc

Browse files
Address feedback: Move to a static method
1 parent 48dd2e7 commit f6f88bc

File tree

3 files changed

+32
-39
lines changed

3 files changed

+32
-39
lines changed

src/color-swatch/color-swatch.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import ColorElement from "../common/color-element.js";
2-
import { resolveColor } from "../common/util.js";
32
import "../gamut-badge/gamut-badge.js";
43

54
let importIncrementable;
@@ -24,8 +23,6 @@ const Self = class ColorSwatch extends ColorElement {
2423
<slot name="after"></slot>
2524
</div>`;
2625

27-
static resolvedColors = new Map();
28-
2926
constructor () {
3027
super();
3128

@@ -209,13 +206,7 @@ const Self = class ColorSwatch extends ColorElement {
209206
return null;
210207
}
211208

212-
try {
213-
return ColorSwatch.Color.get(this.value);
214-
}
215-
catch {
216-
// Color.js can't parse the color value; possibly one of the values we can handle gracefully
217-
return resolveColor(this.value, this._el.swatch, Self.resolvedColors);
218-
}
209+
return ColorSwatch.resolveColor(this.value, this._el.swatch);
219210
},
220211
set (value) {
221212
this.value = ColorSwatch.Color.get(value)?.display();

src/common/color-element.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const Self = class ColorElement extends NudeElement {
3131
static Color;
3232
static all = {};
3333
static dependencies = new Set();
34+
static resolvedColors = new Map();
3435

3536
static globalStyles = [{ css: baseGlobalStyles }];
3637

@@ -137,6 +138,36 @@ const Self = class ColorElement extends NudeElement {
137138

138139
customElements.define(this.tagName, this);
139140
}
141+
142+
/**
143+
* Resolve a color value and cache it.
144+
* @param {string} value Color value to resolve.
145+
* @param {Element} element Element to get computed style from to resolve the color value.
146+
*/
147+
static resolveColor (value, element) {
148+
try {
149+
return Self.Color.get(value);
150+
}
151+
catch {
152+
// Color.js can't parse the color value; possibly one of the values we can handle gracefully
153+
if (Self.resolvedColors.has(value)) {
154+
return Self.resolvedColors.get(value);
155+
}
156+
157+
if (!CSS.supports("color", value)) {
158+
// Not supported or invalid value
159+
return null;
160+
}
161+
162+
// One of the supported color values; resolve and cache it
163+
element.style.backgroundColor = value;
164+
let color = getComputedStyle(element).backgroundColor;
165+
Self.resolvedColors.set(value, color);
166+
element.style.backgroundColor = "";
167+
168+
return color;
169+
}
170+
}
140171
};
141172

142173
export default Self;

src/common/util.js

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,3 @@ export function getType (value) {
118118
export function noOpTemplateTag (strings, ...values) {
119119
return strings.reduce((acc, string, i) => acc + string + (values[i] ?? ""), "");
120120
}
121-
122-
/**
123-
* Resolve a color value and cache it.
124-
* @param {string} value Color value to resolve.
125-
* @param {Element} element Element to get computed style from to resolve the color value.
126-
* @param {Map<string, string>} cache
127-
*/
128-
export function resolveColor (value, element, cache) {
129-
if (!value || !element || !(element instanceof Element) || !cache) {
130-
return null;
131-
}
132-
133-
if (cache.has(value)) {
134-
return cache.get(value);
135-
}
136-
137-
if (!CSS.supports("color", value)) {
138-
// Not supported or invalid value
139-
return null;
140-
}
141-
142-
// One of the supported color values; resolve and cache it
143-
element.style.backgroundColor = value;
144-
let color = getComputedStyle(element).backgroundColor;
145-
cache.set(value, color);
146-
element.style.backgroundColor = "";
147-
148-
return color;
149-
}

0 commit comments

Comments
 (0)