Skip to content

Conversation

@DmitrySharabin
Copy link
Member

@DmitrySharabin DmitrySharabin commented Aug 4, 2025

[color-swatch] Handle complex color values gracefully


This is part 1 of 2 in a stack made with GitButler:

@netlify
Copy link

netlify bot commented Aug 4, 2025

Deploy Preview for color-elements ready!

Name Link
🔨 Latest commit ac62f55
🔍 Latest deploy log https://app.netlify.com/projects/color-elements/deploys/68935cf3c551890007bdf6d7
😎 Deploy Preview https://deploy-preview-213--color-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@DmitrySharabin DmitrySharabin force-pushed the issue-212 branch 2 times, most recently from c62524b to d671338 Compare August 4, 2025 23:39
// One of the supported color values; resolve and cache it
element.style.backgroundColor = value;
let color = getComputedStyle(element).backgroundColor;
color = Self.Color.get(color);
Copy link
Member

Choose a reason for hiding this comment

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

This can still error (e.g. if the color includes currentColor I believe it won't resolve fully), I would just call resolveColor() with a check to make sure it has actually changed (otherwise you'll be in an infinite loop).

We should also handle the case where value is non-empty and color is null better across components.

element.style.backgroundColor = value;
let color = getComputedStyle(element).backgroundColor;
color = Self.Color.get(color);
Self.#resolvedColors.set(value, color);
Copy link
Member

Choose a reason for hiding this comment

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

One issue with this is that now multiple components can share the same Color object if they use the same string, and I'm pretty sure we modify it. So perhaps we should cache the resolved string and create a new instance every time.

@DmitrySharabin DmitrySharabin requested a review from LeaVerou August 5, 2025 12:36
let color = getComputedStyle(element).backgroundColor;
element.style.backgroundColor = "";

let resolvedColor = Self.resolveColor(color, element);
Copy link
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
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. 🫣

DmitrySharabin and others added 2 commits August 5, 2025 18:41
Co-authored-by: Lea Verou <lea@verou.me>
}

// 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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants