Skip to content

Commit 8bc217c

Browse files
bjarnefiOvergaard
andauthored
fix(uui-color-area): dragging the mouse do not select a color (#486)
* Fix dragging in color area and sync value * Remove import of Colord --------- Co-authored-by: Jacob Overgaard <[email protected]>
1 parent e2d8127 commit 8bc217c

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

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

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export class UUIColorAreaElement extends LitElement {
144144
// TODO: Can we move the parsing of a color string to shared utility function?
145145
const parsed = colord(newVal);
146146

147-
if (parsed) {
147+
if (parsed.isValid()) {
148148
const { h, s, l } = parsed.toHsl();
149149

150150
this.hue = h;
@@ -228,43 +228,36 @@ export class UUIColorAreaElement extends LitElement {
228228
);
229229
}
230230

231-
getValueFromMousePosition(event: MouseEvent) {
232-
return this.getValueFromCoordinates(
233-
event.clientX - event.offsetX,
234-
event.clientY - event.offsetY
235-
);
236-
}
237-
238-
getValueFromTouchPosition(event: TouchEvent) {
239-
return this.getValueFromCoordinates(
240-
event.touches[0].clientX,
241-
event.touches[0].clientY
242-
);
243-
}
244-
245-
getValueFromCoordinates(x: number, y: number) {
246-
const grid = this.shadowRoot!.querySelector<HTMLElement>('.color-area')!;
247-
const { width, height } = grid.getBoundingClientRect();
248-
249-
this.saturation = clamp((x / width) * 100, 0, 100);
250-
this.lightness = clamp(100 - (y / height) * 100, 0, 100);
251-
252-
this.syncValues();
253-
}
254-
255231
syncValues() {
256232
const color = colord({
257233
h: this.hue,
258234
s: this.saturation,
259235
l: this.lightness,
260-
a: this.alpha,
236+
a: this.alpha / 100,
261237
});
262238

263-
this.value = color.toRgbString();
239+
this._value = color.toRgbString();
264240

265241
this.dispatchEvent(new UUIColorAreaEvent(UUIColorAreaEvent.CHANGE));
266242
}
267243

244+
/** Generates a hex string from HSL values. Hue must be 0-360. All other arguments must be 0-100. */
245+
private getHexString(
246+
hue: number,
247+
saturation: number,
248+
lightness: number,
249+
alpha = 100
250+
) {
251+
const color = colord(
252+
`hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha / 100})`
253+
);
254+
if (!color.isValid()) {
255+
return '';
256+
}
257+
258+
return color.toHex();
259+
}
260+
268261
render() {
269262
const gridHandleX = this.saturation;
270263
const gridHandleY = 100 - this.brightness;
@@ -273,7 +266,9 @@ export class UUIColorAreaElement extends LitElement {
273266
<div
274267
part="grid"
275268
class="color-area"
276-
style=${styleMap({ backgroundColor: `hsl(${this.hue}deg, 100%, 50%)` })}
269+
style=${styleMap({
270+
backgroundColor: this.getHexString(this.hue, 100, 50),
271+
})}
277272
@mousedown=${this.handleGridDrag}
278273
@touchstart=${this.handleGridDrag}>
279274
<span
@@ -285,7 +280,12 @@ export class UUIColorAreaElement extends LitElement {
285280
style=${styleMap({
286281
top: `${gridHandleY}%`,
287282
left: `${gridHandleX}%`,
288-
backgroundColor: `hsla(${this.hue}deg, ${this.saturation}%, ${this.lightness}%)`,
283+
backgroundColor: this.getHexString(
284+
this.hue,
285+
this.saturation,
286+
this.lightness,
287+
this.alpha
288+
),
289289
})}
290290
role="application"
291291
tabindex=${ifDefined(this.disabled ? undefined : '0')}

0 commit comments

Comments
 (0)