-
-
Notifications
You must be signed in to change notification settings - Fork 2
FIrst stab at #212 #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
FIrst stab at #212 #213
Changes from 5 commits
5ea9dd5
48dd2e7
f6f88bc
92f46d0
5df5d93
580bd65
cc39297
fe90abf
ac62f55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 () { | ||
|
|
@@ -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); | ||
|
||
| if (resolvedColor) { | ||
| Self.#resolvedColors.set(value, color); | ||
| } | ||
|
|
||
| return resolvedColor; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default Self; | ||
Uh oh!
There was an error while loading. Please reload this page.