File tree Expand file tree Collapse file tree 2 files changed +27
-9
lines changed
Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Original file line number Diff line number Diff 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 ( ) ;
Original file line number Diff line number Diff 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
142168export default Self ;
You can’t perform that action at this time.
0 commit comments