Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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 (!globalThis.CSS?.supports("color", value) || !element) {
// Not supported/invalid value, or no element to resolve the color value from
return null;
}

// One of the supported color values; resolve and cache it
element.style.backgroundColor = value;
Copy link
Member

Choose a reason for hiding this comment

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

We should store the element's previous backgroundColor so we can restore it, rather than overwriting.
Also, we should avoid the DOM I/O if the element already has that background (since any DOM operation is slow), so perhaps we can pass in an option for that.

Copy link
Member Author

Choose a reason for hiding this comment

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

We should store the element's previous backgroundColor so we can restore it, rather than overwriting.

I fixed it. Thanks!

Also, we should avoid the DOM I/O if the element already has that background (since any DOM operation is slow), so perhaps we can pass in an option for that.

Could you elaborate, please? How do you see it?

Copy link
Member

Choose a reason for hiding this comment

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

Could you elaborate, please? How do you see it?

Like, a setting (name TBD) that will tell the function "the element I'm giving you already has the right background, don't bother setting it"

let color = getComputedStyle(element).backgroundColor;
element.style.backgroundColor = "";

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

return resolvedColor;
}
};

export default Self;