Skip to content

Commit be1bc59

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

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/color-swatch/color-swatch.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ const Self = class ColorSwatch extends ColorElement {
2424
<slot name="after"></slot>
2525
</div>`;
2626

27-
static resolvedColors = new Map();
28-
2927
constructor () {
3028
super();
3129

@@ -209,13 +207,7 @@ const Self = class ColorSwatch extends ColorElement {
209207
return null;
210208
}
211209

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-
}
210+
return Self.resolveColor(this.value, this._el.swatch);
219211
},
220212
set (value) {
221213
this.value = ColorSwatch.Color.get(value)?.display();

src/common/color-element.js

Lines changed: 26 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,31 @@ const Self = class ColorElement extends NudeElement {
137138

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

142168
export default Self;

0 commit comments

Comments
 (0)