Skip to content

Commit c3663b0

Browse files
loivseniOvergaard
andcommitted
feat(uui-color-picker): allow the value to be empty (#842)
* Bugfix: Allow the UUI Color Picker value to be undefined... * dispatching events * start with alpha 0 * remove state, update inputvalue when deselecting swatch * dont dispatch if color did not change --------- Co-authored-by: Jacob Overgaard <[email protected]>
1 parent d690d03 commit c3663b0

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

Diff for: packages/uui-color-picker/lib/uui-color-picker.element.ts

+44-39
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ type UUIColorPickerSize = 'small' | 'medium' | 'large';
6464
*/
6565
@defineElement('uui-color-picker')
6666
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 = '';
7072

7173
@state() private inputValue = '';
7274
@state() private hue = 0;
7375
@state() private saturation = 0;
7476
@state() private lightness = 0;
75-
@state() private alpha = 100;
77+
@state() private alpha = 0;
7678
@state() private _colord: Colord = colord('hsl(0, 0%, 0%)');
7779

7880
/**
@@ -81,7 +83,16 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
8183
* @type {string}
8284
* @default ''
8385
**/
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+
}
8596

8697
/**
8798
* 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) {
176187
connectedCallback(): void {
177188
super.connectedCallback();
178189

179-
if (this.value) {
180-
this.setColor(this.value);
181-
} else {
182-
this.setColor('#000');
183-
}
184-
185190
demandCustomElement(this, 'uui-icon');
186191
demandCustomElement(this, 'uui-icon-registry-essential');
187192
demandCustomElement(this, 'uui-input');
@@ -291,45 +296,30 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
291296
}
292297

293298
handleInputChange(event: CustomEvent) {
299+
event.stopImmediatePropagation();
294300
this._swatches?.resetSelection();
295301

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);
306304
}
307305

308306
handleInputKeyDown(event: KeyboardEvent) {
307+
event.stopImmediatePropagation();
309308
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());
319315
}
320316
}
321317

322318
handleColorSwatchChange(event: UUIColorSwatchesEvent) {
323319
event.stopImmediatePropagation();
324320

325321
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);
333323
}
334324

335325
handleCopy() {
@@ -360,6 +350,20 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
360350
}
361351

362352
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+
363367
const colord = new Colord(colorString);
364368

365369
const { h, s, l, a } = colord.toHsl();
@@ -383,6 +387,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
383387
this.dispatchEvent(
384388
new UUIColorPickerChangeEvent(UUIColorPickerChangeEvent.CHANGE),
385389
);
390+
386391
return true;
387392
}
388393

@@ -412,7 +417,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
412417

413418
private _syncValues() {
414419
this.inputValue = this.getFormattedValue(this.format);
415-
this.value = this.inputValue;
420+
this._value = this.inputValue;
416421
}
417422

418423
private _renderColorPicker() {
@@ -530,7 +535,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
530535
}
531536

532537
private _renderPreviewButton() {
533-
return html` <button
538+
return html`<button
534539
type="button"
535540
part="trigger"
536541
aria-label="${this.label || 'Open Color picker'}"

Diff for: packages/uui-color-picker/lib/uui-color-picker.story.ts

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const meta: Meta<UUIColorPickerElement> = {
4646
swatches: defaultSwatches,
4747
format: 'hex',
4848
size: 'medium',
49+
value: '#000000',
4950
},
5051
argTypes: {
5152
format: {
@@ -77,6 +78,19 @@ type Story = StoryObj<UUIColorPickerElement>;
7778

7879
export const Overview: Story = {};
7980

81+
export const EmptyValue: Story = {
82+
args: {
83+
value: '',
84+
},
85+
parameters: {
86+
docs: {
87+
source: {
88+
code: `<uui-color-picker></uui-color-picker>`,
89+
},
90+
},
91+
},
92+
};
93+
8094
export const Inline: Story = {
8195
args: {
8296
inline: true,

0 commit comments

Comments
 (0)