1
- import { Colord } from 'colord' ;
2
1
import { defineElement } from '@umbraco-ui/uui-base/lib/registration' ;
3
2
import { property } from 'lit/decorators.js' ;
4
- import { classMap } from 'lit/directives/class-map.js' ;
5
3
import { css , html , LitElement , nothing } from 'lit' ;
6
4
import { iconCheck } from '@umbraco-ui/uui-icon-registry-essential/lib/svgs' ;
7
-
8
- import { styleMap } from 'lit/directives/style-map.js' ;
9
-
10
5
import {
11
6
ActiveMixin ,
12
7
LabelMixin ,
13
8
SelectableMixin ,
14
9
} from '@umbraco-ui/uui-base/lib/mixins' ;
15
10
16
11
/**
17
- * Color swatch, can have label and be selectable. Depends on colord library and exposes it's utility functions under color property.
12
+ * Color swatch, can have label and be selectable.
18
13
*
19
14
* @element uui-color-swatch
20
15
* @cssprop --uui-swatch-size - The size of the swatch.
21
16
* @cssprop --uui-swatch-border-width - The width of the border.
17
+ * @cssprop --uui-swatch-color - The width of the border.
22
18
* @slot label - Default slot for the label.
23
19
*/
24
20
@defineElement ( 'uui-color-swatch' )
25
21
export class UUIColorSwatchElement extends LabelMixin (
26
22
'label' ,
27
23
SelectableMixin ( ActiveMixin ( LitElement ) )
28
24
) {
29
- private _value : string | undefined = '' ;
30
-
31
25
/**
32
- * Value of the swatch. Should be a valid hex, hexa, rgb, rgba, hsl or hsla string. Should fulfill this [css spec](https://www.w3.org/TR/css-color-4/#color-type). If not provided element will look at its text content.
33
- *
34
- * @attr
26
+ * Value of the swatch. This will become the color value if color is left undefined, see the property `color` for more details.
35
27
*/
36
28
@property ( )
37
29
get value ( ) : string {
38
- return this . _value ? this . _value : this . textContent ?. trim ( ) || '' ;
30
+ return this . _value ?? '' ;
39
31
}
40
-
41
32
set value ( newValue : string ) {
42
33
const oldValue = this . _value ;
43
34
this . _value = newValue ;
44
35
this . requestUpdate ( 'value' , oldValue ) ;
45
36
}
37
+ private _value ?: string ;
38
+
39
+ /**
40
+ * Color of the swatch. Should be a valid hex, hexa, rgb, rgba, hsl or hsla string. Should fulfill this [css spec](https://www.w3.org/TR/css-color-4/#color-type). If not provided element will look at its text content.
41
+ */
42
+ @property ( )
43
+ get color ( ) : string | undefined {
44
+ return this . _color ;
45
+ }
46
+ set color ( newValue : string ) {
47
+ const oldValue = this . _color ;
48
+ this . _color = newValue ;
49
+ this . requestUpdate ( 'color' , oldValue ) ;
50
+ }
51
+ private _color ?: string ;
46
52
47
53
/**
48
54
* Determines if the options is disabled. If true the option can't be selected
@@ -58,62 +64,24 @@ export class UUIColorSwatchElement extends LabelMixin(
58
64
* @attr
59
65
* @memberof UUIColorSwatchElement
60
66
*/
61
- @property ( { type : Boolean , attribute : 'show-label' } )
67
+ @property ( { type : Boolean , attribute : 'show-label' , reflect : true } )
62
68
showLabel = false ;
63
- /**
64
- * Colord object instance based on the value provided to the element. If the value is not a valid color, it falls back to black (like Amy Winehouse). For more information about Colord, see [Colord](https://omgovich.github.io/colord/)
65
- *
66
- * @memberof UUIColorSwatchElement
67
- */
68
- get color ( ) : Colord | null {
69
- return this . _color ;
70
- }
71
-
72
- set color ( _ ) {
73
- // do nothing, this is just to prevent the color from being set from outside
74
- return ;
75
- }
76
- private _color : Colord | null = null ;
77
-
78
- /**
79
- * Returns true if the color brightness is >= 0.5
80
- *
81
- * @readonly
82
- * @memberof UUIColorSwatchElement
83
- */
84
- get isLight ( ) {
85
- return this . color ?. isLight ( ) ?? false ;
86
- }
87
69
88
70
constructor ( ) {
89
71
super ( ) ;
90
72
this . addEventListener ( 'click' , this . _setAriaAttributes ) ;
91
73
}
92
74
93
- private _initializeColor ( ) {
94
- this . _color = new Colord ( this . value ?? '' ) ;
95
- if ( ! this . _color . isValid ( ) ) {
96
- this . disabled = true ;
97
- console . error (
98
- `Invalid color provided to uui-color-swatch: ${ this . value } `
99
- ) ;
100
- }
101
- }
102
-
103
75
private _setAriaAttributes ( ) {
104
76
if ( this . selectable )
105
77
this . setAttribute ( 'aria-checked' , this . selected . toString ( ) ) ;
106
78
}
107
79
108
80
firstUpdated ( ) {
109
- this . _initializeColor ( ) ;
110
81
this . _setAriaAttributes ( ) ;
111
82
}
112
83
113
84
willUpdate ( changedProperties : Map < string , any > ) {
114
- if ( changedProperties . has ( 'value' ) ) {
115
- this . _initializeColor ( ) ;
116
- }
117
85
if ( changedProperties . has ( 'disabled' ) ) {
118
86
if ( this . selectable ) {
119
87
this . selectable = ! this . disabled ;
@@ -135,17 +103,16 @@ export class UUIColorSwatchElement extends LabelMixin(
135
103
aria-label = ${ this . label }
136
104
aria- dis abled= "${ this . disabled } "
137
105
title = "${ this . label } " >
138
- <div
139
- class= ${ classMap ( {
140
- 'color-swatch' : true ,
141
- 'color-swatch--transparent-bg' : true ,
142
- 'color-swatch--light' : this . isLight ,
143
- 'color-swatch--big' : this . showLabel ,
144
- } ) } >
106
+ <div class= "color-swatch color-swatch--transparent-bg" >
145
107
<div
146
108
class= "color-swatch__color"
147
- style = ${ styleMap ( { backgroundColor : this . value } ) } > </ div>
148
- <div class= "color-swatch__check" > ${ iconCheck } </ div>
109
+ style = "background-color: var(--uui-swatch-color, ${ this . color ??
110
+ this . value } )"> </ div>
111
+ <div
112
+ class= "color-swatch__check"
113
+ style = "fill: var(--uui-swatch-color, ${ this . color ?? this . value } )" >
114
+ ${ iconCheck }
115
+ </ div>
149
116
</ div>
150
117
${ this . _renderWithLabel ( ) }
151
118
</ butto n>
@@ -222,7 +189,7 @@ export class UUIColorSwatchElement extends LabelMixin(
222
189
width : calc (100% + calc (var (--uui-swatch-border-width , 1px ) * 2 ));
223
190
height : calc (100% + calc (var (--uui-swatch-border-width , 1px ) * 2 ));
224
191
box-sizing : border-box;
225
- border : var (--uui-swatch-border-width , 1 px ) solid
192
+ border : var (--uui-swatch-border-width , 2 px ) solid
226
193
var (--uui-color-selected );
227
194
border-radius : calc (
228
195
var (--uui-border-radius ) + var (--uui-swatch-border-width , 1px )
@@ -250,7 +217,13 @@ export class UUIColorSwatchElement extends LabelMixin(
250
217
justify-content : center;
251
218
align-items : center;
252
219
}
253
- .color-swatch--transparent-bg {
220
+
221
+ : host ([show-label ]) .color-swatch {
222
+ width : 120px ;
223
+ height : 50px ;
224
+ }
225
+
226
+ .color-swatch .color-swatch--transparent-bg {
254
227
background-image : linear-gradient (
255
228
45deg ,
256
229
var (--uui-palette-grey ) 25% ,
@@ -270,7 +243,7 @@ export class UUIColorSwatchElement extends LabelMixin(
270
243
box-sizing : border-box;
271
244
}
272
245
273
- . color-swatch--big .color-swatch__color {
246
+ : host ([ show-label ]) .color-swatch__color {
274
247
border-radius : 3px 3px 0 0 ;
275
248
}
276
249
@@ -280,16 +253,11 @@ export class UUIColorSwatchElement extends LabelMixin(
280
253
width : calc (var (--uui-swatch-size , 25px ) / 2 );
281
254
height : calc (var (--uui-swatch-size , 25px ) / 2 );
282
255
line-height : 0 ;
283
- transition : fill 120ms , opacity 120ms ;
284
- fill : # fff ;
256
+ filter : invert (1 ) grayscale (1 ) contrast (9 );
285
257
pointer-events : none;
286
258
opacity : 0 ;
287
259
}
288
260
289
- .color-swatch--light .color-swatch__check {
290
- fill : # 000 ;
291
- }
292
-
293
261
: host ([selected ]) .color-swatch__check {
294
262
opacity : 1 ;
295
263
}
@@ -299,11 +267,6 @@ export class UUIColorSwatchElement extends LabelMixin(
299
267
font-size : var (--uui-size-4 );
300
268
}
301
269
302
- .color-swatch--big {
303
- width : 120px ;
304
- height : 50px ;
305
- }
306
-
307
270
.color-swatch__label {
308
271
max-width : 120px ;
309
272
box-sizing : border-box;
0 commit comments