@@ -64,15 +64,17 @@ type UUIColorPickerSize = 'small' | 'medium' | 'large';
64
64
*/
65
65
@defineElement ( 'uui-color-picker' )
66
66
export class UUIColorPickerElement extends LabelMixin ( 'label' , LitElement ) {
67
- @query ( '[part="input"]' ) _input ! : UUIInputElement ;
68
- @query ( '.color-picker__preview' ) _previewButton ! : HTMLButtonElement ;
69
- @query ( '#swatches' ) _swatches ! : UUIColorSwatchesElement ;
67
+ @query ( '[part="input"]' ) private _input ! : UUIInputElement ;
68
+ @query ( '.color-picker__preview' ) private _previewButton ! : HTMLButtonElement ;
69
+ @query ( '#swatches' ) private _swatches ! : UUIColorSwatchesElement ;
70
+
71
+ private _value : string = '' ;
70
72
71
73
@state ( ) private inputValue = '' ;
72
74
@state ( ) private hue = 0 ;
73
75
@state ( ) private saturation = 0 ;
74
76
@state ( ) private lightness = 0 ;
75
- @state ( ) private alpha = 100 ;
77
+ @state ( ) private alpha = 0 ;
76
78
@state ( ) private _colord : Colord = colord ( 'hsl(0, 0%, 0%)' ) ;
77
79
78
80
/**
@@ -81,7 +83,16 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
81
83
* @type {string }
82
84
* @default ''
83
85
**/
84
- @property ( ) value = '' ;
86
+ @property ( )
87
+ set value ( value : string ) {
88
+ if ( this . value !== value ) {
89
+ this . setColor ( value ) ;
90
+ }
91
+ this . _value = value ;
92
+ }
93
+ get value ( ) : string {
94
+ return this . _value ;
95
+ }
85
96
86
97
/**
87
98
* The format to use for the display value. If opacity is enabled, these will translate to HEXA, RGBA, HSLA, and HSVA
@@ -176,12 +187,6 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
176
187
connectedCallback ( ) : void {
177
188
super . connectedCallback ( ) ;
178
189
179
- if ( this . value ) {
180
- this . setColor ( this . value ) ;
181
- } else {
182
- this . setColor ( '#000' ) ;
183
- }
184
-
185
190
demandCustomElement ( this , 'uui-icon' ) ;
186
191
demandCustomElement ( this , 'uui-icon-registry-essential' ) ;
187
192
demandCustomElement ( this , 'uui-input' ) ;
@@ -291,45 +296,30 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
291
296
}
292
297
293
298
handleInputChange ( event : CustomEvent ) {
299
+ event . stopImmediatePropagation ( ) ;
294
300
this . _swatches ?. resetSelection ( ) ;
295
301
296
- const target = event . target as HTMLInputElement ;
297
-
298
- if ( target . value ) {
299
- this . setColor ( target . value ) ;
300
- target . value = this . value ;
301
- } else {
302
- this . setColor ( '#000' ) ;
303
- }
304
-
305
- event . stopPropagation ( ) ;
302
+ this . inputValue = this . _input . value as string ;
303
+ this . setColor ( this . inputValue ) ;
306
304
}
307
305
308
306
handleInputKeyDown ( event : KeyboardEvent ) {
307
+ event . stopImmediatePropagation ( ) ;
309
308
if ( event . key === 'Enter' ) {
310
- if ( this . inputValue ) {
311
- this . setColor ( this . inputValue ) ;
312
- this . _swatches ?. resetSelection ( ) ;
313
-
314
- this . inputValue = this . value ;
315
- setTimeout ( ( ) => this . _input . select ( ) ) ;
316
- } else {
317
- this . setColor ( '#000' ) ;
318
- }
309
+ this . _swatches ?. resetSelection ( ) ;
310
+
311
+ this . inputValue = this . _input . value as string ;
312
+ this . setColor ( this . inputValue ) ;
313
+
314
+ setTimeout ( ( ) => this . _input . select ( ) ) ;
319
315
}
320
316
}
321
317
322
318
handleColorSwatchChange ( event : UUIColorSwatchesEvent ) {
323
319
event . stopImmediatePropagation ( ) ;
324
320
325
321
const target = event . target as UUIColorSwatchElement ;
326
-
327
- if ( target . value ) {
328
- this . setColor ( target . value ) ;
329
- } else {
330
- //reset to black
331
- this . setColor ( '#000' ) ;
332
- }
322
+ this . setColor ( target . value ) ;
333
323
}
334
324
335
325
handleCopy ( ) {
@@ -360,6 +350,20 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
360
350
}
361
351
362
352
setColor ( colorString : string | HslaColor ) {
353
+ if ( colorString === this . value ) return ;
354
+
355
+ if ( ! colorString ) {
356
+ this . alpha = 0 ;
357
+ this . inputValue = '' ;
358
+ this . _value = colorString ;
359
+
360
+ this . dispatchEvent (
361
+ new UUIColorPickerChangeEvent ( UUIColorPickerChangeEvent . CHANGE ) ,
362
+ ) ;
363
+
364
+ return true ;
365
+ }
366
+
363
367
const colord = new Colord ( colorString ) ;
364
368
365
369
const { h, s, l, a } = colord . toHsl ( ) ;
@@ -383,6 +387,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
383
387
this . dispatchEvent (
384
388
new UUIColorPickerChangeEvent ( UUIColorPickerChangeEvent . CHANGE ) ,
385
389
) ;
390
+
386
391
return true ;
387
392
}
388
393
@@ -412,7 +417,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
412
417
413
418
private _syncValues ( ) {
414
419
this . inputValue = this . getFormattedValue ( this . format ) ;
415
- this . value = this . inputValue ;
420
+ this . _value = this . inputValue ;
416
421
}
417
422
418
423
private _renderColorPicker ( ) {
@@ -530,7 +535,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
530
535
}
531
536
532
537
private _renderPreviewButton ( ) {
533
- return html ` <butto n
538
+ return html `<butto n
534
539
type= "button"
535
540
part = "trigger"
536
541
aria- label= "${ this . label || 'Open Color picker' } "
0 commit comments