Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/color-swatch/color-swatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Self = class ColorSwatch extends ColorElement {
super();

this._el = {
swatch: this.shadowRoot.querySelector("slot[name=swatch]::slotted(*), #swatch"),
wrapper: this.shadowRoot.querySelector("#wrapper"),
label: this.shadowRoot.querySelector("[part=label]"),
colorWrapper: this.shadowRoot.querySelector("[part=color]"),
Expand Down Expand Up @@ -205,7 +206,7 @@ const Self = class ColorSwatch extends ColorElement {
return null;
}

return ColorSwatch.Color.get(this.value);
return ColorSwatch.resolveColor(this.value, this._el.swatch);
},
set (value) {
this.value = ColorSwatch.Color.get(value)?.display();
Expand Down
39 changes: 39 additions & 0 deletions src/common/color-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const Self = class ColorElement extends NudeElement {
static all = {};
static dependencies = new Set();

/** @type {Map<string, string>} */
static #resolvedColors = new Map();

static globalStyles = [{ css: baseGlobalStyles }];

constructor () {
Expand Down Expand Up @@ -137,6 +140,42 @@ const Self = class ColorElement extends NudeElement {

customElements.define(this.tagName, this);
}

/**
* Resolve a color value and cache it.
* @param {string} value Color value to resolve.
* @param {Element} element Element to get computed style from to resolve the color value.
* @returns {Color | null} Resolved color value or null if the value cannot be resolved.
*/
static resolveColor (value, element) {
try {
return Self.Color.get(value);
}
catch {
// Color.js can't parse the color value; possibly one of the values we can handle gracefully
if (Self.#resolvedColors.has(value)) {
let color = Self.#resolvedColors.get(value);
return Self.Color.get(color);
}

if (!CSS.supports("color", value)) {
// Not supported or invalid value
return null;
}

// One of the supported color values; resolve and cache it
element.style.backgroundColor = value;
let color = getComputedStyle(element).backgroundColor;
element.style.backgroundColor = "";

let resolvedColor = Self.resolveColor(color, element);
Copy link
Copy Markdown
Member

@LeaVerou LeaVerou Aug 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if getComputedStyle() actually produced a different color, otherwise this can end up an infinite loop. Or, better, just don't pass an element, and write the code accordingly so that when no element is passed it just doesn't use gCS().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea! Thanks. I hope I got it right now. 🫣

if (resolvedColor) {
Self.#resolvedColors.set(value, color);
}

return resolvedColor;
}
}
};

export default Self;