Skip to content

Commit b42a474

Browse files
loivsennielslyngsoeiOvergaard
authored
feat(uui-color-swatches): color swatches changes form when showLabel is true (#427)
* Color swatches changes form when showLabel is true --------- Co-authored-by: Niels Lyngsø <[email protected]> Co-authored-by: Jacob Overgaard <[email protected]>
1 parent e1b8e6a commit b42a474

File tree

4 files changed

+103
-34
lines changed

4 files changed

+103
-34
lines changed

packages/uui-color-picker/lib/uui-color-picker.element.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LitElement, html, css } from 'lit';
1+
import { LitElement, html, css, nothing } from 'lit';
22
import { Colord, colord, extend, HslaColor } from 'colord';
33
import namesPlugin from 'colord/plugins/names';
44

@@ -180,6 +180,7 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
180180
181181
uui-color-swatches {
182182
border-top: solid 1px #d4d4d8;
183+
padding: 0.75rem;
183184
}
184185
185186
button[slot='trigger'] {
@@ -631,21 +632,26 @@ export class UUIColorPickerElement extends LabelMixin('label', LitElement) {
631632
: ''}
632633
</uui-button-group>
633634
</div>
634-
<uui-color-swatches
635-
id="swatches"
636-
class="color-picker__swatches"
637-
@change=${this.handleColorSwatchChange}>
638-
${this.swatches.map(
639-
swatch =>
640-
html`<uui-color-swatch
641-
label="${swatch}"
642-
.value=${swatch}></uui-color-swatch>`
643-
)}
644-
</uui-color-swatches>
635+
${this._renderSwatches()}
645636
</div>
646637
`;
647638
}
648639

640+
private _renderSwatches() {
641+
if (!this.swatches) return nothing;
642+
return html`<uui-color-swatches
643+
id="swatches"
644+
class="color-picker__swatches"
645+
@change=${this.handleColorSwatchChange}>
646+
${this.swatches.map(
647+
swatch =>
648+
html`<uui-color-swatch
649+
label="${swatch}"
650+
.value=${swatch}></uui-color-swatch>`
651+
)}
652+
</uui-color-swatches>`;
653+
}
654+
649655
private _renderPreviewButton() {
650656
return html`<button
651657
type="button"

packages/uui-color-swatch/lib/uui-color-swatch.element.ts

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Colord } from 'colord';
22
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
33
import { property } from 'lit/decorators.js';
44
import { classMap } from 'lit/directives/class-map.js';
5-
import { css, html, LitElement } from 'lit';
5+
import { css, html, LitElement, nothing } from 'lit';
66
import { iconCheck } from '@umbraco-ui/uui-icon-registry-essential/lib/svgs';
77

88
import { styleMap } from 'lit/directives/style-map.js';
@@ -49,22 +49,36 @@ export class UUIColorSwatchElement extends LabelMixin(
4949
outline: none;
5050
}
5151
52-
:host(:focus:not([disabled])) .color-swatch {
52+
:host(:focus:not([disabled])) #swatch {
5353
outline-color: var(--uui-color-focus);
5454
outline-width: var(--uui-swatch-border-width, 1px);
5555
outline-style: solid;
5656
outline-offset: var(--uui-swatch-border-width, 1px);
5757
}
5858
59-
:host([selectable]) {
59+
:host([selectable]) #swatch {
6060
cursor: pointer;
6161
}
6262
6363
:host([disabled]) {
6464
cursor: not-allowed;
6565
}
6666
67-
:host([selectable]) .color-swatch::after {
67+
#swatch {
68+
outline: none;
69+
background: none;
70+
border: none;
71+
padding: 0;
72+
margin: 0;
73+
text-align: left;
74+
border-radius: 3px;
75+
}
76+
77+
:host(:not([selectable])) #swatch:focus {
78+
outline: none;
79+
}
80+
81+
:host([selectable]) #swatch::after {
6882
content: '';
6983
position: absolute;
7084
pointer-events: none;
@@ -80,13 +94,13 @@ export class UUIColorSwatchElement extends LabelMixin(
8094
transition: opacity 100ms ease-out;
8195
opacity: 0;
8296
}
83-
:host([selectable]:not([disabled]):hover) .color-swatch::after {
97+
:host([selectable]:not([disabled]):hover) #swatch::after {
8498
opacity: 0.33;
8599
}
86-
:host([selectable][selected]:not([disabled]):hover) .color-swatch::after {
100+
:host([selectable][selected]:not([disabled]):hover) #swatch::after {
87101
opacity: 0.66;
88102
}
89-
:host([selectable][selected]:not([disabled])) .color-swatch::after {
103+
:host([selectable][selected]:not([disabled])) #swatch::after {
90104
opacity: 1;
91105
}
92106
@@ -96,6 +110,7 @@ export class UUIColorSwatchElement extends LabelMixin(
96110
height: var(--uui-swatch-size, 25px);
97111
border-radius: 3px;
98112
display: flex;
113+
flex-direction: column;
99114
justify-content: center;
100115
align-items: center;
101116
}
@@ -119,6 +134,10 @@ export class UUIColorSwatchElement extends LabelMixin(
119134
box-sizing: border-box;
120135
}
121136
137+
.color-swatch--big .color-swatch__color {
138+
border-radius: 3px 3px 0 0;
139+
}
140+
122141
.color-swatch__check {
123142
position: absolute;
124143
vertical-align: middle;
@@ -143,6 +162,31 @@ export class UUIColorSwatchElement extends LabelMixin(
143162
.label {
144163
font-size: var(--uui-size-4);
145164
}
165+
166+
.color-swatch--big {
167+
width: 120px;
168+
height: 50px;
169+
}
170+
171+
.color-swatch__label {
172+
max-width: 120px;
173+
box-sizing: border-box;
174+
padding: var(--uui-size-space-1) var(--uui-size-space-2);
175+
line-height: 1.5;
176+
display: flex;
177+
flex-direction: column;
178+
background: white;
179+
border: 1px solid var(--uui-color-border);
180+
border-radius: 0 0 3px 3px;
181+
font-size: var(--uui-size-4, 12px);
182+
}
183+
184+
.color-swatch__label strong {
185+
overflow: hidden;
186+
text-overflow: ellipsis;
187+
white-space: nowrap;
188+
box-sizing: border-box;
189+
}
146190
`,
147191
];
148192

@@ -251,23 +295,35 @@ export class UUIColorSwatchElement extends LabelMixin(
251295

252296
render() {
253297
return html`
254-
<div
255-
class=${classMap({
256-
'color-swatch': true,
257-
'color-swatch--transparent-bg': true,
258-
'color-swatch--light': this.isLight,
259-
})}
260-
role="button"
298+
<button
299+
id="swatch"
261300
aria-label=${this.label}
262-
aria-disabled="${this.disabled}">
301+
aria-disabled="${this.disabled}"
302+
title="${this.label}">
263303
<div
264-
class="color-swatch__color"
265-
style=${styleMap({ backgroundColor: this.value })}></div>
266-
<div class="color-swatch__check">${iconCheck}</div>
267-
</div>
268-
${this.showLabel ? this.renderLabel() : ''}
304+
class=${classMap({
305+
'color-swatch': true,
306+
'color-swatch--transparent-bg': true,
307+
'color-swatch--light': this.isLight,
308+
'color-swatch--big': this.showLabel,
309+
})}>
310+
<div
311+
class="color-swatch__color"
312+
style=${styleMap({ backgroundColor: this.value })}></div>
313+
<div class="color-swatch__check">${iconCheck}</div>
314+
</div>
315+
${this._renderWithLabel()}
316+
</button>
269317
`;
270318
}
319+
320+
private _renderWithLabel() {
321+
if (!this.showLabel) return nothing;
322+
return html`<div class="color-swatch__label">
323+
<strong>${this.renderLabel()}</strong>
324+
${this.value}
325+
</div>`;
326+
}
271327
}
272328

273329
declare global {

packages/uui-color-swatch/lib/uui-color-swatch.story.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ export const Selectable: Story = () =>
2121
label=${value}></uui-color-swatch>`;
2222

2323
export const InvalidValue: Story = () =>
24+
html`<uui-color-swatch
25+
.value=${'askjhsdiusyhdiudhg'}
26+
label="Invalid color"></uui-color-swatch>`;
27+
28+
export const InvalidValue2: Story = () =>
2429
html`<uui-color-swatch
2530
.value=${'askjhsdiusyhdiudhg'}
2631
label="Invalid color"
@@ -37,7 +42,10 @@ export const DisabledSelected: Story = () =>
3742
>`;
3843

3944
export const WithLabel: Story = () =>
40-
html`<uui-color-swatch selectable show-label label=${value}
45+
html`<uui-color-swatch
46+
selectable
47+
show-label
48+
label=${"This is the most beautiful color I've ever seen"}
4149
>${value}</uui-color-swatch
4250
>`;
4351

packages/uui-color-swatches/lib/uui-color-swatches.element.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export class UUIColorSwatchesElement extends LabelMixin('label', LitElement) {
2323
display: flex;
2424
flex-wrap: wrap;
2525
gap: 0.4rem;
26-
padding: 0.75rem;
2726
}
2827
`,
2928
];

0 commit comments

Comments
 (0)