react-iro-gradient-picker 1.2.3 → 1.2.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/index.es.js CHANGED
@@ -6430,10 +6430,20 @@ var validGradient = (function (input) {
6430
6430
  var colorStops = [];
6431
6431
  // Determine if first part is direction/angle or color stop
6432
6432
  var firstPart = parts[0];
6433
- var isDirection = /^\d+deg$/i.test(firstPart) ||
6434
- /^to\s+/.test(firstPart) ||
6435
- /^(?:circle|ellipse)/.test(firstPart) ||
6436
- /at\s+/.test(firstPart);
6433
+ var isDirection = false;
6434
+ if (type === 'linear') {
6435
+ isDirection =
6436
+ /^\d+deg$/i.test(firstPart) ||
6437
+ /^to\s+/.test(firstPart);
6438
+ }
6439
+ else if (type === 'radial') {
6440
+ // For radial gradients, check for size, shape, or position keywords
6441
+ isDirection =
6442
+ /^(?:circle|ellipse)/.test(firstPart) ||
6443
+ /at\s+/.test(firstPart) ||
6444
+ /^(?:closest-side|closest-corner|farthest-side|farthest-corner)$/i.test(firstPart) ||
6445
+ /^\d+(?:%|px|em|rem)?$/i.test(firstPart); // Size values like "70", "70%", "70px"
6446
+ }
6437
6447
  if (isDirection) {
6438
6448
  if (type === 'linear') {
6439
6449
  if (/^\d+deg$/i.test(firstPart)) {
@@ -6470,10 +6480,11 @@ var validGradient = (function (input) {
6470
6480
  var stops_1 = [];
6471
6481
  for (var i = 0; i < colorStops.length; i++) {
6472
6482
  var stopString = colorStops[i].trim();
6473
- // Try to extract color and position
6474
- var stopMatch = stopString.match(/^(.+?)(?:\s+(\d+(?:\.\d+)?)(%)?)?$/);
6483
+ // Improved regex to handle various gradient stop formats
6484
+ // Matches: color position%, color position, or just color
6485
+ var stopMatch = stopString.match(/^(.+?)(?:\s+([\d.]+)(%|px|em|rem)?)?$/);
6475
6486
  if (stopMatch) {
6476
- var _b = __read(stopMatch, 4), colorStr = _b[1], positionStr = _b[2], isPercent = _b[3];
6487
+ var _b = __read(stopMatch, 4), colorStr = _b[1], positionStr = _b[2], unit = _b[3];
6477
6488
  var tinyColorInstance = tinycolor(colorStr.trim());
6478
6489
  if (tinyColorInstance.isValid()) {
6479
6490
  var stop_1 = {
@@ -6481,14 +6492,21 @@ var validGradient = (function (input) {
6481
6492
  };
6482
6493
  if (positionStr) {
6483
6494
  var position = parseFloat(positionStr);
6484
- // Assume percentage if no unit specified
6485
- if (isPercent || !isPercent) {
6495
+ // Convert percentage to decimal (0-1 range)
6496
+ if (unit === '%' || !unit) {
6497
+ // If no unit specified, assume percentage
6486
6498
  position = position / 100;
6487
6499
  }
6488
6500
  stop_1.position = Math.max(0, Math.min(1, position));
6489
6501
  }
6490
6502
  stops_1.push(stop_1);
6491
6503
  }
6504
+ else {
6505
+ console.warn('Invalid color in gradient stop:', colorStr.trim());
6506
+ }
6507
+ }
6508
+ else {
6509
+ console.warn('Could not parse gradient stop:', stopString);
6492
6510
  }
6493
6511
  }
6494
6512
  // Auto-assign positions if missing
@@ -6586,7 +6604,7 @@ var parseGradient = (function (str) {
6586
6604
  var helperAngle = type === 'linear' ? '180' : 'circle at center';
6587
6605
  var modifier = findF || angle_1 || helperAngle;
6588
6606
  return {
6589
- gradient: "".concat(type, "-gradient(").concat(typeof gradient !== 'string' ? gradient.original : str, ")"),
6607
+ gradient: typeof gradient !== 'string' ? gradient.original : str,
6590
6608
  type: type,
6591
6609
  modifier: modifier.match(/\d+/) !== null
6592
6610
  ? Number((_b = modifier.match(/\d+/)) === null || _b === void 0 ? void 0 : _b.join(''))
@@ -6603,7 +6621,7 @@ var parseGradient = (function (str) {
6603
6621
  });
6604
6622
 
6605
6623
  /**
6606
- * Convert gradient object to CSS gradient string
6624
+ * Convert gradient object to CSS gradient string (strict typing)
6607
6625
  */
6608
6626
  function gradientObjectToCss(gradientData) {
6609
6627
  var type = gradientData.type, _a = gradientData.angle, angle = _a === void 0 ? 90 : _a, stops = gradientData.stops;
@@ -6622,6 +6640,38 @@ function gradientObjectToCss(gradientData) {
6622
6640
  return "radial-gradient(circle, ".concat(cssStops, ")");
6623
6641
  }
6624
6642
  }
6643
+ /**
6644
+ * Convert flexible gradient object to CSS gradient string (loose typing)
6645
+ */
6646
+ function flexibleGradientToCss(gradientData) {
6647
+ var _a = gradientData.type, type = _a === void 0 ? 'linear' : _a, angle = gradientData.angle, direction = gradientData.direction, position = gradientData.position, stops = gradientData.stops;
6648
+ // Convert stops to CSS format with flexible position handling
6649
+ var cssStops = stops
6650
+ .map(function (stop) {
6651
+ var color = tinycolor(stop.color);
6652
+ var positionStr = '';
6653
+ if (stop.position !== undefined) {
6654
+ if (typeof stop.position === 'string') {
6655
+ positionStr = " ".concat(stop.position);
6656
+ }
6657
+ else {
6658
+ positionStr = " ".concat(stop.position, "%");
6659
+ }
6660
+ }
6661
+ return "".concat(color.toRgbString()).concat(positionStr);
6662
+ })
6663
+ .join(', ');
6664
+ if (type === 'linear') {
6665
+ // Use direction if provided, otherwise use angle
6666
+ var gradientDirection = direction || (angle ? "".concat(angle, "deg") : '90deg');
6667
+ return "linear-gradient(".concat(gradientDirection, ", ").concat(cssStops, ")");
6668
+ }
6669
+ else {
6670
+ // Handle radial gradients with flexible positioning
6671
+ var radialPosition = position || 'circle';
6672
+ return "radial-gradient(".concat(radialPosition, ", ").concat(cssStops, ")");
6673
+ }
6674
+ }
6625
6675
  /**
6626
6676
  * Convert CSS gradient string to gradient object
6627
6677
  */
@@ -7211,9 +7261,22 @@ var DefaultColorPanel = function (_a) {
7211
7261
  var _d = __read(useState([]), 2), formatedDefColors = _d[0], setFormatedDefColors = _d[1];
7212
7262
  useEffect(function () {
7213
7263
  if (colorType === 'gradient') {
7214
- setFormatedDefColors(checkValidColorsArray(defaultColors, 'grad').map(function (item) {
7215
- return parseGradient(item);
7216
- }));
7264
+ var validGradients = checkValidColorsArray(defaultColors, 'grad');
7265
+ // For popular colors display, create minimal IColor objects using original CSS strings
7266
+ // This avoids complex parsing issues while still providing functional display
7267
+ var displayGradients = validGradients.map(function (gradientString) {
7268
+ return {
7269
+ gradient: gradientString, // Use the original CSS string directly for display
7270
+ type: gradientString.startsWith('radial-') ? 'radial' : 'linear',
7271
+ modifier: 180, // Default value, not critical for display
7272
+ stops: [
7273
+ // Dummy stops that help identify display objects in onChooseColor
7274
+ ['rgba(255, 0, 0, 1)', 0, 0],
7275
+ ['rgba(0, 255, 0, 1)', 1, 1]
7276
+ ]
7277
+ };
7278
+ });
7279
+ setFormatedDefColors(displayGradients);
7217
7280
  }
7218
7281
  else {
7219
7282
  setFormatedDefColors(checkValidColorsArray(defaultColors, 'solid'));
@@ -7225,6 +7288,41 @@ var DefaultColorPanel = function (_a) {
7225
7288
  return;
7226
7289
  }
7227
7290
  if (colorType === 'gradient' && typeof item !== 'string') {
7291
+ // If this is a simplified display object (identified by dummy stops), parse the gradient properly
7292
+ if (item.stops.length === 2 &&
7293
+ item.stops[0][0] === 'rgba(255, 0, 0, 1)') {
7294
+ try {
7295
+ var properlyParsed = parseGradient(item.gradient);
7296
+ if (properlyParsed &&
7297
+ properlyParsed.stops &&
7298
+ properlyParsed.stops.length > 0) {
7299
+ var stops_1 = properlyParsed.stops;
7300
+ var lastStop_1 = rgbaToArray(stops_1[stops_1.length - 1][0]);
7301
+ var lastStopLoc_1 = stops_1[stops_1.length - 1][1];
7302
+ var activeStop_1 = rgbaToHex([
7303
+ lastStop_1[0],
7304
+ lastStop_1[1],
7305
+ lastStop_1[2]
7306
+ ]);
7307
+ var activeIdx_1 = stops_1[stops_1.length - 1][2];
7308
+ setInit(false);
7309
+ setColor(properlyParsed);
7310
+ setActiveColor &&
7311
+ setActiveColor({
7312
+ hex: activeStop_1,
7313
+ alpha: Number(Math.round(lastStop_1[3] * 100)),
7314
+ loc: lastStopLoc_1,
7315
+ index: activeIdx_1
7316
+ });
7317
+ setActive(index);
7318
+ return;
7319
+ }
7320
+ }
7321
+ catch (error) {
7322
+ console.warn('Failed to parse popular gradient on click:', item.gradient, error);
7323
+ }
7324
+ }
7325
+ // Normal gradient object processing (fallback)
7228
7326
  var stops = item.stops;
7229
7327
  var lastStop = rgbaToArray(stops[stops.length - 1][0]);
7230
7328
  var lastStopLoc = stops[stops.length - 1][1];
@@ -7367,9 +7465,18 @@ var Markers = function (_a) {
7367
7465
  };
7368
7466
  var onDrag = function (e) {
7369
7467
  var _a;
7370
- var x = e.clientX;
7371
- var y = e.clientY;
7468
+ // Defensive check for event object
7469
+ if (!e) {
7470
+ console.warn('onDrag called with undefined event object');
7471
+ return;
7472
+ }
7473
+ var x = e.clientX || 0;
7474
+ var y = e.clientY || 0;
7372
7475
  var rect = (_a = node === null || node === void 0 ? void 0 : node.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
7476
+ if (!rect) {
7477
+ console.warn('Unable to get bounding rect for drag operation');
7478
+ return;
7479
+ }
7373
7480
  var rootDistance = y - rect.y;
7374
7481
  // Now we know user is actually dragging
7375
7482
  setIsDragging(true);
@@ -7624,10 +7731,15 @@ var GradientPanel = function (_a) {
7624
7731
  }
7625
7732
  };
7626
7733
  var onDrag = function (e) {
7627
- var x = e.clientX;
7628
- var y = e.clientY;
7629
- var shiftKey = e.shiftKey;
7630
- var ctrlKey = e.ctrlKey * 2;
7734
+ // Defensive check for event object
7735
+ if (!e) {
7736
+ console.warn('onDrag called with undefined event object');
7737
+ return;
7738
+ }
7739
+ var x = e.clientX || 0;
7740
+ var y = e.clientY || 0;
7741
+ var shiftKey = e.shiftKey || false;
7742
+ var ctrlKey = (e.ctrlKey || false) * 2;
7631
7743
  pointMoveTo({
7632
7744
  x: x,
7633
7745
  y: y,
@@ -7764,25 +7876,81 @@ var IroGradient = function (_a) {
7764
7876
  // If parsing failed, return fallback gradient
7765
7877
  if (typeof parsed === 'string') {
7766
7878
  console.warn('Gradient parsing failed, using fallback:', parsed);
7767
- return parseGradient('linear-gradient(90deg, #ffffff 0%, #000000 100%)');
7879
+ var fallback = parseGradient('linear-gradient(90deg, #ffffff 0%, #000000 100%)');
7880
+ // Ensure fallback has valid structure
7881
+ if (fallback &&
7882
+ typeof fallback === 'object' &&
7883
+ Array.isArray(fallback.stops) &&
7884
+ fallback.stops.length > 0) {
7885
+ return fallback;
7886
+ }
7887
+ // Ultimate fallback with guaranteed structure
7888
+ return {
7889
+ stops: [
7890
+ ['rgba(255, 255, 255, 1)', 0, 0],
7891
+ ['rgba(0, 0, 0, 1)', 1, 1]
7892
+ ],
7893
+ gradient: 'linear-gradient(90deg, #ffffff 0%, #000000 100%)',
7894
+ modifier: 90,
7895
+ type: 'linear'
7896
+ };
7897
+ }
7898
+ // Validate parsed result has required structure
7899
+ if (parsed &&
7900
+ typeof parsed === 'object' &&
7901
+ Array.isArray(parsed.stops) &&
7902
+ parsed.stops.length > 0) {
7903
+ return parsed;
7768
7904
  }
7769
- return parsed;
7905
+ // If parsed result is invalid, use ultimate fallback
7906
+ console.warn('Parsed gradient has invalid structure:', parsed);
7907
+ return {
7908
+ stops: [
7909
+ ['rgba(255, 255, 255, 1)', 0, 0],
7910
+ ['rgba(0, 0, 0, 1)', 1, 1]
7911
+ ],
7912
+ gradient: 'linear-gradient(90deg, #ffffff 0%, #000000 100%)',
7913
+ modifier: 90,
7914
+ type: 'linear'
7915
+ };
7770
7916
  }
7771
7917
  catch (error) {
7772
- console.warn('Error parsing gradient, using fallback:', error);
7773
- return parseGradient('linear-gradient(90deg, #ffffff 0%, #000000 100%)');
7918
+ console.warn('Error parsing gradient, using ultimate fallback:', error);
7919
+ return {
7920
+ stops: [
7921
+ ['rgba(255, 255, 255, 1)', 0, 0],
7922
+ ['rgba(0, 0, 0, 1)', 1, 1]
7923
+ ],
7924
+ gradient: 'linear-gradient(90deg, #ffffff 0%, #000000 100%)',
7925
+ modifier: 90,
7926
+ type: 'linear'
7927
+ };
7774
7928
  }
7775
7929
  // eslint-disable-next-line react-hooks/exhaustive-deps
7776
7930
  }, [value]);
7777
7931
  var _s = parsedColors(), stops = _s.stops, type = _s.type, modifier = _s.modifier;
7778
- var lastStop = rgbaToArray(stops[stops.length - 1][0]);
7779
- var activeStopIndex = stops.length - 1;
7780
- var activeStop = rgbaToHex([lastStop[0], lastStop[1], lastStop[2]]);
7781
- var activeAlpha = Math.round(lastStop[3] * 100);
7782
7932
  var iroPickerRef = useRef(null);
7783
7933
  var containerRef = useRef(null);
7784
7934
  var isUpdatingFromGradientStop = useRef(false);
7785
7935
  var _t = __read(useState(200), 2), pickerWidth = _t[0], setPickerWidth = _t[1];
7936
+ // Safe extraction of stop data with fallbacks
7937
+ var safeStops = Array.isArray(stops) && stops.length > 0
7938
+ ? stops
7939
+ : [
7940
+ ['rgba(255, 255, 255, 1)', 0, 0],
7941
+ ['rgba(0, 0, 0, 1)', 1, 1]
7942
+ ];
7943
+ var safeLastStop = rgbaToArray(safeStops[safeStops.length - 1][0]);
7944
+ var safeParsedLastStop = Array.isArray(safeLastStop) && safeLastStop.length >= 4
7945
+ ? safeLastStop
7946
+ : [255, 255, 255, 1];
7947
+ var activeStopIndex = safeStops.length - 1;
7948
+ var activeStop = rgbaToHex([
7949
+ safeParsedLastStop[0],
7950
+ safeParsedLastStop[1],
7951
+ safeParsedLastStop[2]
7952
+ ]);
7953
+ var activeAlpha = Math.round(safeParsedLastStop[3] * 100);
7786
7954
  // Responsive width for IroColorPicker - match solid picker logic
7787
7955
  useEffect(function () {
7788
7956
  if (!containerRef.current)
@@ -7825,12 +7993,12 @@ var IroGradient = function (_a) {
7825
7993
  gradient: value,
7826
7994
  type: type,
7827
7995
  modifier: modifier,
7828
- stops: stops
7996
+ stops: safeStops
7829
7997
  }), 2), color = _u[0], setColor = _u[1];
7830
7998
  var _v = __read(useState({
7831
7999
  hex: activeStop,
7832
8000
  alpha: activeAlpha,
7833
- loc: stops[activeStopIndex][1],
8001
+ loc: safeStops[activeStopIndex][1],
7834
8002
  index: activeStopIndex
7835
8003
  }), 2), activeColor = _v[0], setActiveColor = _v[1];
7836
8004
  var debounceColor = useDebounce(color, debounceMS);
@@ -8063,11 +8231,11 @@ var IroGradient = function (_a) {
8063
8231
  var handleColorFromPanel = function (newColor) {
8064
8232
  setColor(newColor);
8065
8233
  if (newColor === null || newColor === void 0 ? void 0 : newColor.stops) {
8066
- var lastStop_1 = rgbaToArray(newColor.stops[newColor.stops.length - 1][0]);
8067
- var activeStop_1 = rgbaToHex([lastStop_1[0], lastStop_1[1], lastStop_1[2]]);
8234
+ var lastStop = rgbaToArray(newColor.stops[newColor.stops.length - 1][0]);
8235
+ var activeStop_1 = rgbaToHex([lastStop[0], lastStop[1], lastStop[2]]);
8068
8236
  var newActiveColor = {
8069
8237
  hex: activeStop_1,
8070
- alpha: Math.round(lastStop_1[3] * 100),
8238
+ alpha: Math.round(lastStop[3] * 100),
8071
8239
  loc: newColor.stops[newColor.stops.length - 1][1],
8072
8240
  index: newColor.stops.length - 1
8073
8241
  };
@@ -8512,5 +8680,5 @@ var ColorPicker = function (_a) {
8512
8680
  gradient ? (React.createElement(IroGradient, { onChange: onChangeGradient, value: cssValue, format: format, defaultColors: defaultColors, debounceMS: debounceMS, debounce: debounce, showAlpha: showAlpha, showInputs: showInputs, showGradientResult: showGradientResult, showGradientStops: showGradientStops, showGradientMode: showGradientMode, showGradientAngle: showGradientAngle, showGradientPosition: showGradientPosition, allowAddGradientStops: allowAddGradientStops, colorBoardHeight: colorBoardHeight, showReset: showReset, onReset: onReset })) : (React.createElement(Fragment, null))))) : null)));
8513
8681
  };
8514
8682
 
8515
- export { ColorPicker, DEFAULT_COLORS, InputRgba, PopupTabs, PopupTabsBody, PopupTabsBodyItem, PopupTabsHeader, PopupTabsHeaderLabel, RADIALS_POS, ThemeProvider, ThemeToggle, TinyColor, checkFormat, cn, cssToGradientObject, ColorPicker as default, getGradient, getHexAlpha, gradientObjectToCss, hexToRgba, isGradientObject, isValidHex, isValidRgba, normalizeGradientValue, parseGradient, rgbaToArray, rgbaToHex, useDebounce, validGradient };
8683
+ export { ColorPicker, DEFAULT_COLORS, InputRgba, PopupTabs, PopupTabsBody, PopupTabsBodyItem, PopupTabsHeader, PopupTabsHeaderLabel, RADIALS_POS, ThemeProvider, ThemeToggle, TinyColor, checkFormat, cn, cssToGradientObject, ColorPicker as default, flexibleGradientToCss, getGradient, getHexAlpha, gradientObjectToCss, hexToRgba, isGradientObject, isValidHex, isValidRgba, normalizeGradientValue, parseGradient, rgbaToArray, rgbaToHex, useDebounce, validGradient };
8516
8684
  //# sourceMappingURL=index.es.js.map