@react-stately/color 3.0.0-beta.1 → 3.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +308 -72
- package/dist/main.js.map +1 -1
- package/dist/module.js +305 -68
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +8 -9
- package/src/Color.ts +201 -26
- package/src/useColorFieldState.ts +98 -45
- package/src/useColorWheelState.ts +11 -7
package/dist/module.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { useRef, useState, useMemo, useEffect } from "react";
|
|
2
|
-
import { useControlledState } from "@react-stately/utils";
|
|
3
2
|
import { useSliderState } from "@react-stately/slider";
|
|
4
3
|
import _babelRuntimeHelpersEsmObjectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
5
4
|
import _babelRuntimeHelpersEsmExtends from "@babel/runtime/helpers/esm/extends";
|
|
6
5
|
import { NumberFormatter } from "@internationalized/number";
|
|
7
6
|
import { MessageDictionary } from "@internationalized/message";
|
|
8
|
-
import { clamp } from "@react-
|
|
7
|
+
import { clamp, toFixedNumber, useControlledState } from "@react-stately/utils";
|
|
9
8
|
import _babelRuntimeHelpersEsmInteropRequireDefault from "@babel/runtime/helpers/esm/interopRequireDefault";
|
|
10
9
|
// ASSET: /Users/govett/dev/react-spectrum-v3/packages/@react-stately/color/intl/ar-AE.json
|
|
11
10
|
var $b2cd9264f0b716f5f6077988ebeb8d5$exports = {};
|
|
@@ -257,6 +256,14 @@ class $db5f3c2f048bca5be854d051d3f2729$var$RGBColor extends $db5f3c2f048bca5be85
|
|
|
257
256
|
case 'rgba':
|
|
258
257
|
return this;
|
|
259
258
|
|
|
259
|
+
case 'hsb':
|
|
260
|
+
case 'hsba':
|
|
261
|
+
return this.toHSB();
|
|
262
|
+
|
|
263
|
+
case 'hsl':
|
|
264
|
+
case 'hsla':
|
|
265
|
+
return this.toHSL();
|
|
266
|
+
|
|
260
267
|
default:
|
|
261
268
|
throw new Error('Unsupported color conversion: rgb -> ' + format);
|
|
262
269
|
}
|
|
@@ -265,6 +272,85 @@ class $db5f3c2f048bca5be854d051d3f2729$var$RGBColor extends $db5f3c2f048bca5be85
|
|
|
265
272
|
toHexInt() {
|
|
266
273
|
return this.red << 16 | this.green << 8 | this.blue;
|
|
267
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Converts an RGB color value to HSB.
|
|
277
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
278
|
+
* @returns An HSBColor object.
|
|
279
|
+
*/
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
toHSB() {
|
|
283
|
+
const red = this.red / 255;
|
|
284
|
+
const green = this.green / 255;
|
|
285
|
+
const blue = this.blue / 255;
|
|
286
|
+
const min = Math.min(red, green, blue);
|
|
287
|
+
const brightness = Math.max(red, green, blue);
|
|
288
|
+
const chroma = brightness - min;
|
|
289
|
+
const saturation = brightness === 0 ? 0 : chroma / brightness;
|
|
290
|
+
let hue = 0; // achromatic
|
|
291
|
+
|
|
292
|
+
if (chroma !== 0) {
|
|
293
|
+
switch (brightness) {
|
|
294
|
+
case red:
|
|
295
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
296
|
+
break;
|
|
297
|
+
|
|
298
|
+
case green:
|
|
299
|
+
hue = (blue - red) / chroma + 2;
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
case blue:
|
|
303
|
+
hue = (red - green) / chroma + 4;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
hue /= 6;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return new $db5f3c2f048bca5be854d051d3f2729$var$HSBColor(toFixedNumber(hue * 360, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), this.alpha);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Converts an RGB color value to HSL.
|
|
314
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
315
|
+
* @returns An HSLColor object.
|
|
316
|
+
*/
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
toHSL() {
|
|
320
|
+
const red = this.red / 255;
|
|
321
|
+
const green = this.green / 255;
|
|
322
|
+
const blue = this.blue / 255;
|
|
323
|
+
const min = Math.min(red, green, blue);
|
|
324
|
+
const max = Math.max(red, green, blue);
|
|
325
|
+
const lightness = (max + min) / 2;
|
|
326
|
+
const chroma = max - min;
|
|
327
|
+
let hue;
|
|
328
|
+
let saturation;
|
|
329
|
+
|
|
330
|
+
if (chroma === 0) {
|
|
331
|
+
hue = saturation = 0; // achromatic
|
|
332
|
+
} else {
|
|
333
|
+
saturation = chroma / (lightness < .5 ? max + min : 2 - max - min);
|
|
334
|
+
|
|
335
|
+
switch (max) {
|
|
336
|
+
case red:
|
|
337
|
+
hue = (green - blue) / chroma + (green < blue ? 6 : 0);
|
|
338
|
+
break;
|
|
339
|
+
|
|
340
|
+
case green:
|
|
341
|
+
hue = (blue - red) / chroma + 2;
|
|
342
|
+
break;
|
|
343
|
+
|
|
344
|
+
case blue:
|
|
345
|
+
hue = (red - green) / chroma + 4;
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
hue /= 6;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(toFixedNumber(hue * 360, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(lightness * 100, 2), this.alpha);
|
|
353
|
+
}
|
|
268
354
|
|
|
269
355
|
clone() {
|
|
270
356
|
return new $db5f3c2f048bca5be854d051d3f2729$var$RGBColor(this.red, this.green, this.blue, this.alpha);
|
|
@@ -319,6 +405,10 @@ class $db5f3c2f048bca5be854d051d3f2729$var$RGBColor extends $db5f3c2f048bca5be85
|
|
|
319
405
|
return new NumberFormatter(locale, options).format(value);
|
|
320
406
|
}
|
|
321
407
|
|
|
408
|
+
getColorSpace() {
|
|
409
|
+
return 'rgb';
|
|
410
|
+
}
|
|
411
|
+
|
|
322
412
|
} // X = <negative/positive number with/without decimal places>
|
|
323
413
|
// before/after a comma, 0 or more whitespaces are allowed
|
|
324
414
|
// - hsb(X, X%, X%)
|
|
@@ -352,11 +442,17 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSBColor extends $db5f3c2f048bca5be85
|
|
|
352
442
|
case 'css':
|
|
353
443
|
return this.toHSL().toString('css');
|
|
354
444
|
|
|
445
|
+
case 'hex':
|
|
446
|
+
return this.toRGB().toString('hex');
|
|
447
|
+
|
|
448
|
+
case 'hexa':
|
|
449
|
+
return this.toRGB().toString('hexa');
|
|
450
|
+
|
|
355
451
|
case 'hsb':
|
|
356
|
-
return "hsb(" + this.hue + ", " + this.saturation + "%, " + this.brightness + "%)";
|
|
452
|
+
return "hsb(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.brightness, 2) + "%)";
|
|
357
453
|
|
|
358
454
|
case 'hsba':
|
|
359
|
-
return "hsba(" + this.hue + ", " + this.saturation + "%, " + this.brightness + "%, " + this.alpha + ")";
|
|
455
|
+
return "hsba(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.brightness, 2) + "%, " + this.alpha + ")";
|
|
360
456
|
|
|
361
457
|
default:
|
|
362
458
|
return this.toFormat(format).toString(format);
|
|
@@ -373,24 +469,49 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSBColor extends $db5f3c2f048bca5be85
|
|
|
373
469
|
case 'hsla':
|
|
374
470
|
return this.toHSL();
|
|
375
471
|
|
|
472
|
+
case 'rgb':
|
|
473
|
+
case 'rgba':
|
|
474
|
+
return this.toRGB();
|
|
475
|
+
|
|
376
476
|
default:
|
|
377
477
|
throw new Error('Unsupported color conversion: hsb -> ' + format);
|
|
378
478
|
}
|
|
379
479
|
}
|
|
480
|
+
/**
|
|
481
|
+
* Converts a HSB color to HSL.
|
|
482
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
483
|
+
* @returns An HSLColor object.
|
|
484
|
+
*/
|
|
485
|
+
|
|
380
486
|
|
|
381
487
|
toHSL() {
|
|
382
|
-
|
|
383
|
-
|
|
488
|
+
let saturation = this.saturation / 100;
|
|
489
|
+
let brightness = this.brightness / 100;
|
|
490
|
+
let lightness = brightness * (1 - saturation / 2);
|
|
491
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
492
|
+
return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(lightness * 100, 2), this.alpha);
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Converts a HSV color value to RGB.
|
|
496
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
497
|
+
* @returns An RGBColor object.
|
|
498
|
+
*/
|
|
499
|
+
|
|
384
500
|
|
|
501
|
+
toRGB() {
|
|
385
502
|
let hue = this.hue;
|
|
386
|
-
let saturation = this.saturation
|
|
387
|
-
let
|
|
503
|
+
let saturation = this.saturation / 100;
|
|
504
|
+
let brightness = this.brightness / 100;
|
|
388
505
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
506
|
+
let fn = function fn(n, k) {
|
|
507
|
+
if (k === void 0) {
|
|
508
|
+
k = (n + hue / 60) % 6;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
return brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
512
|
+
};
|
|
392
513
|
|
|
393
|
-
return new $db5f3c2f048bca5be854d051d3f2729$var$
|
|
514
|
+
return new $db5f3c2f048bca5be854d051d3f2729$var$RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
394
515
|
}
|
|
395
516
|
|
|
396
517
|
clone() {
|
|
@@ -460,6 +581,10 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSBColor extends $db5f3c2f048bca5be85
|
|
|
460
581
|
return new NumberFormatter(locale, options).format(value);
|
|
461
582
|
}
|
|
462
583
|
|
|
584
|
+
getColorSpace() {
|
|
585
|
+
return 'hsb';
|
|
586
|
+
}
|
|
587
|
+
|
|
463
588
|
} // X = <negative/positive number with/without decimal places>
|
|
464
589
|
// before/after a comma, 0 or more whitespaces are allowed
|
|
465
590
|
// - hsl(X, X%, X%)
|
|
@@ -470,8 +595,7 @@ const $db5f3c2f048bca5be854d051d3f2729$var$HSL_REGEX = /hsl\(([-+]?\d+(?:.\d+)?\
|
|
|
470
595
|
|
|
471
596
|
function $db5f3c2f048bca5be854d051d3f2729$var$mod(n, m) {
|
|
472
597
|
return (n % m + m) % m;
|
|
473
|
-
}
|
|
474
|
-
|
|
598
|
+
}
|
|
475
599
|
|
|
476
600
|
class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be854d051d3f2729$var$Color {
|
|
477
601
|
constructor(hue, saturation, lightness, alpha) {
|
|
@@ -480,8 +604,7 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
|
|
|
480
604
|
this.saturation = saturation;
|
|
481
605
|
this.lightness = lightness;
|
|
482
606
|
this.alpha = alpha;
|
|
483
|
-
}
|
|
484
|
-
|
|
607
|
+
}
|
|
485
608
|
|
|
486
609
|
static parse(value) {
|
|
487
610
|
let m;
|
|
@@ -496,12 +619,18 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
|
|
|
496
619
|
|
|
497
620
|
toString(format) {
|
|
498
621
|
switch (format) {
|
|
622
|
+
case 'hex':
|
|
623
|
+
return this.toRGB().toString('hex');
|
|
624
|
+
|
|
625
|
+
case 'hexa':
|
|
626
|
+
return this.toRGB().toString('hexa');
|
|
627
|
+
|
|
499
628
|
case 'hsl':
|
|
500
|
-
return "hsl(" + this.hue + ", " + this.saturation + "%, " + this.lightness + "%)";
|
|
629
|
+
return "hsl(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.lightness, 2) + "%)";
|
|
501
630
|
|
|
502
631
|
case 'css':
|
|
503
632
|
case 'hsla':
|
|
504
|
-
return "hsla(" + this.hue + ", " + this.saturation + "%, " + this.lightness + "%, " + this.alpha + ")";
|
|
633
|
+
return "hsla(" + this.hue + ", " + toFixedNumber(this.saturation, 2) + "%, " + toFixedNumber(this.lightness, 2) + "%, " + this.alpha + ")";
|
|
505
634
|
|
|
506
635
|
default:
|
|
507
636
|
return this.toFormat(format).toString(format);
|
|
@@ -514,10 +643,55 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
|
|
|
514
643
|
case 'hsla':
|
|
515
644
|
return this;
|
|
516
645
|
|
|
646
|
+
case 'hsb':
|
|
647
|
+
case 'hsba':
|
|
648
|
+
return this.toHSB();
|
|
649
|
+
|
|
650
|
+
case 'rgb':
|
|
651
|
+
case 'rgba':
|
|
652
|
+
return this.toRGB();
|
|
653
|
+
|
|
517
654
|
default:
|
|
518
655
|
throw new Error('Unsupported color conversion: hsl -> ' + format);
|
|
519
656
|
}
|
|
520
657
|
}
|
|
658
|
+
/**
|
|
659
|
+
* Converts a HSL color to HSB.
|
|
660
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
661
|
+
* @returns An HSBColor object.
|
|
662
|
+
*/
|
|
663
|
+
|
|
664
|
+
|
|
665
|
+
toHSB() {
|
|
666
|
+
let saturation = this.saturation / 100;
|
|
667
|
+
let lightness = this.lightness / 100;
|
|
668
|
+
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
669
|
+
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
670
|
+
return new $db5f3c2f048bca5be854d051d3f2729$var$HSBColor(toFixedNumber(this.hue, 2), toFixedNumber(saturation * 100, 2), toFixedNumber(brightness * 100, 2), this.alpha);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Converts a HSL color to RGB.
|
|
674
|
+
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
675
|
+
* @returns An RGBColor object.
|
|
676
|
+
*/
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
toRGB() {
|
|
680
|
+
let hue = this.hue;
|
|
681
|
+
let saturation = this.saturation / 100;
|
|
682
|
+
let lightness = this.lightness / 100;
|
|
683
|
+
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
684
|
+
|
|
685
|
+
let fn = function fn(n, k) {
|
|
686
|
+
if (k === void 0) {
|
|
687
|
+
k = (n + hue / 30) % 12;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
return lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
691
|
+
};
|
|
692
|
+
|
|
693
|
+
return new $db5f3c2f048bca5be854d051d3f2729$var$RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
694
|
+
}
|
|
521
695
|
|
|
522
696
|
clone() {
|
|
523
697
|
return new $db5f3c2f048bca5be854d051d3f2729$var$HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
@@ -586,6 +760,10 @@ class $db5f3c2f048bca5be854d051d3f2729$var$HSLColor extends $db5f3c2f048bca5be85
|
|
|
586
760
|
return new NumberFormatter(locale, options).format(value);
|
|
587
761
|
}
|
|
588
762
|
|
|
763
|
+
getColorSpace() {
|
|
764
|
+
return 'hsl';
|
|
765
|
+
}
|
|
766
|
+
|
|
589
767
|
}
|
|
590
768
|
|
|
591
769
|
function $dfa319d4be3239bcd232b84e0e071$var$normalizeColor(v) {
|
|
@@ -746,6 +924,7 @@ export function useColorWheelState(props) {
|
|
|
746
924
|
let valueRef = useRef(value);
|
|
747
925
|
valueRef.current = value;
|
|
748
926
|
let [isDragging, setDragging] = useState(false);
|
|
927
|
+
let isDraggingRef = useRef(false).current;
|
|
749
928
|
let hue = value.getChannelValue('hue');
|
|
750
929
|
|
|
751
930
|
function setHue(v) {
|
|
@@ -757,7 +936,9 @@ export function useColorWheelState(props) {
|
|
|
757
936
|
v = $a54bad0097d5f20f12ac194ac13764$var$roundToStep($a54bad0097d5f20f12ac194ac13764$var$mod(v, 360), step);
|
|
758
937
|
|
|
759
938
|
if (hue !== v) {
|
|
760
|
-
|
|
939
|
+
let color = value.withChannelValue('hue', v);
|
|
940
|
+
valueRef.current = color;
|
|
941
|
+
setValue(color);
|
|
761
942
|
}
|
|
762
943
|
}
|
|
763
944
|
|
|
@@ -813,13 +994,14 @@ export function useColorWheelState(props) {
|
|
|
813
994
|
},
|
|
814
995
|
|
|
815
996
|
setDragging(isDragging) {
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
onChangeEnd(valueRef.current);
|
|
819
|
-
}
|
|
997
|
+
let wasDragging = isDraggingRef;
|
|
998
|
+
isDraggingRef = isDragging;
|
|
820
999
|
|
|
821
|
-
|
|
822
|
-
|
|
1000
|
+
if (onChangeEnd && !isDragging && wasDragging) {
|
|
1001
|
+
onChangeEnd(valueRef.current);
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
setDragging(isDragging);
|
|
823
1005
|
},
|
|
824
1006
|
|
|
825
1007
|
isDragging,
|
|
@@ -864,68 +1046,124 @@ export function useColorFieldState(props) {
|
|
|
864
1046
|
let initialValue = $a8b4ca293b41af7f07c8a5216dae5f3$export$useColor(value);
|
|
865
1047
|
let initialDefaultValue = $a8b4ca293b41af7f07c8a5216dae5f3$export$useColor(defaultValue);
|
|
866
1048
|
let [colorValue, setColorValue] = useControlledState(initialValue, initialDefaultValue, onChange);
|
|
867
|
-
let
|
|
868
|
-
let [inputValue, setInputValue] = useState(initialInputValue);
|
|
869
|
-
useEffect(() => {
|
|
870
|
-
setInputValue(inputValue => {
|
|
871
|
-
// Parse color from current inputValue.
|
|
872
|
-
// Only update the input value if the parseColorValue is not equivalent.
|
|
873
|
-
if (!inputValue.length && colorValue) {
|
|
874
|
-
return colorValue.toString('hex');
|
|
875
|
-
}
|
|
1049
|
+
let [inputValue, setInputValue] = useState(() => (value || defaultValue) && colorValue ? colorValue.toString('hex') : '');
|
|
876
1050
|
|
|
877
|
-
|
|
878
|
-
|
|
1051
|
+
let safelySetColorValue = newColor => {
|
|
1052
|
+
if (typeof newColor === 'function') {
|
|
1053
|
+
setColorValue(prev => {
|
|
1054
|
+
let resolved = newColor(prev);
|
|
879
1055
|
|
|
880
|
-
if (
|
|
881
|
-
return
|
|
1056
|
+
if (!prev || !resolved) {
|
|
1057
|
+
return resolved;
|
|
882
1058
|
}
|
|
883
|
-
} catch (err) {// ignore
|
|
884
|
-
}
|
|
885
1059
|
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
1060
|
+
if (resolved.toHexInt() !== prev.toHexInt()) {
|
|
1061
|
+
return resolved;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
return prev;
|
|
1065
|
+
});
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
if (!colorValue || !newColor) {
|
|
1070
|
+
setColorValue(newColor);
|
|
1071
|
+
return;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
if (newColor.toHexInt() !== colorValue.toHexInt()) {
|
|
1075
|
+
setColorValue(newColor);
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
889
1079
|
|
|
890
|
-
|
|
1080
|
+
useEffect(() => {
|
|
1081
|
+
setInputValue(colorValue ? colorValue.toString('hex') : '');
|
|
1082
|
+
}, [colorValue, setInputValue]);
|
|
1083
|
+
let parsedValue = useMemo(() => {
|
|
1084
|
+
let color;
|
|
1085
|
+
|
|
1086
|
+
try {
|
|
1087
|
+
color = parseColor(inputValue.startsWith('#') ? inputValue : "#" + inputValue);
|
|
1088
|
+
} catch (err) {
|
|
1089
|
+
color = null;
|
|
1090
|
+
}
|
|
891
1091
|
|
|
892
|
-
|
|
1092
|
+
return color;
|
|
1093
|
+
}, [parseColor, inputValue]);
|
|
1094
|
+
let parsed = useRef(null);
|
|
1095
|
+
parsed.current = parsedValue;
|
|
893
1096
|
|
|
894
|
-
let
|
|
1097
|
+
let commit = () => {
|
|
1098
|
+
// Set to empty state if input value is empty
|
|
1099
|
+
if (!inputValue.length) {
|
|
1100
|
+
safelySetColorValue(null);
|
|
1101
|
+
setInputValue(value === undefined ? '' : colorValue.toString('hex'));
|
|
1102
|
+
return;
|
|
1103
|
+
} // if it failed to parse, then reset input to formatted version of current number
|
|
895
1104
|
|
|
896
|
-
let decrementToMin = () => setColorValue(prevColor => $f1bad1ec575cba40fc263dc06b9eb3a9$var$addColorValue(prevColor, -$f1bad1ec575cba40fc263dc06b9eb3a9$var$MAX_COLOR_INT));
|
|
897
1105
|
|
|
898
|
-
|
|
899
|
-
|
|
1106
|
+
if (parsed.current == null) {
|
|
1107
|
+
setInputValue(colorValue ? colorValue.toString('hex') : '');
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
safelySetColorValue(parsed.current); // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
|
|
900
1112
|
|
|
901
|
-
|
|
1113
|
+
let newColorValue = '';
|
|
902
1114
|
|
|
903
|
-
if (
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
1115
|
+
if (colorValue) {
|
|
1116
|
+
newColorValue = colorValue.toString('hex');
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
setInputValue(newColorValue);
|
|
1120
|
+
};
|
|
1121
|
+
|
|
1122
|
+
let increment = () => {
|
|
1123
|
+
safelySetColorValue(prevColor => {
|
|
1124
|
+
let newValue = $f1bad1ec575cba40fc263dc06b9eb3a9$var$addColorValue(parsed.current, step); // if we've arrived at the same value that was previously in the state, the
|
|
1125
|
+
// input value should be updated to match
|
|
1126
|
+
// ex type 4, press increment, highlight the number in the input, type 4 again, press increment
|
|
1127
|
+
// you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
|
|
1128
|
+
|
|
1129
|
+
if (newValue === prevColor) {
|
|
1130
|
+
setInputValue(newValue.toString('hex'));
|
|
907
1131
|
}
|
|
908
1132
|
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
1133
|
+
return newValue;
|
|
1134
|
+
});
|
|
1135
|
+
};
|
|
1136
|
+
|
|
1137
|
+
let decrement = () => {
|
|
1138
|
+
safelySetColorValue(prevColor => {
|
|
1139
|
+
let newValue = $f1bad1ec575cba40fc263dc06b9eb3a9$var$addColorValue(parsed.current, -step); // if we've arrived at the same value that was previously in the state, the
|
|
1140
|
+
// input value should be updated to match
|
|
1141
|
+
// ex type 4, press increment, highlight the number in the input, type 4 again, press increment
|
|
1142
|
+
// you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
|
|
1143
|
+
|
|
1144
|
+
if (newValue === prevColor) {
|
|
1145
|
+
setInputValue(newValue.toString('hex'));
|
|
917
1146
|
}
|
|
918
|
-
|
|
1147
|
+
|
|
1148
|
+
return newValue;
|
|
1149
|
+
});
|
|
919
1150
|
};
|
|
920
1151
|
|
|
921
|
-
let
|
|
922
|
-
|
|
1152
|
+
let incrementToMax = () => safelySetColorValue($f1bad1ec575cba40fc263dc06b9eb3a9$var$MAX_COLOR);
|
|
1153
|
+
|
|
1154
|
+
let decrementToMin = () => safelySetColorValue($f1bad1ec575cba40fc263dc06b9eb3a9$var$MIN_COLOR);
|
|
1155
|
+
|
|
1156
|
+
let validate = value => {
|
|
1157
|
+
var _value$match;
|
|
1158
|
+
|
|
1159
|
+
return value === '' || !!((_value$match = value.match(/^#?[0-9a-f]{0,6}$/i)) != null && _value$match[0]);
|
|
923
1160
|
};
|
|
924
1161
|
|
|
925
1162
|
return {
|
|
1163
|
+
validate,
|
|
926
1164
|
colorValue,
|
|
927
1165
|
inputValue,
|
|
928
|
-
setInputValue
|
|
1166
|
+
setInputValue,
|
|
929
1167
|
commit,
|
|
930
1168
|
increment,
|
|
931
1169
|
incrementToMax,
|
|
@@ -937,11 +1175,10 @@ export function useColorFieldState(props) {
|
|
|
937
1175
|
function $f1bad1ec575cba40fc263dc06b9eb3a9$var$addColorValue(color, step) {
|
|
938
1176
|
let newColor = color ? color : $f1bad1ec575cba40fc263dc06b9eb3a9$var$MIN_COLOR;
|
|
939
1177
|
let colorInt = newColor.toHexInt();
|
|
940
|
-
let newColorString = color ? color.toString('hex') : '';
|
|
941
1178
|
let clampInt = Math.min(Math.max(colorInt + step, $f1bad1ec575cba40fc263dc06b9eb3a9$var$MIN_COLOR_INT), $f1bad1ec575cba40fc263dc06b9eb3a9$var$MAX_COLOR_INT);
|
|
942
1179
|
|
|
943
1180
|
if (clampInt !== colorInt) {
|
|
944
|
-
newColorString = "#" + clampInt.toString(16).padStart(6, '0').toUpperCase();
|
|
1181
|
+
let newColorString = "#" + clampInt.toString(16).padStart(6, '0').toUpperCase();
|
|
945
1182
|
newColor = parseColor(newColorString);
|
|
946
1183
|
}
|
|
947
1184
|
|