@synerise/ds-color-picker 0.1.8 → 0.2.0

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/CHANGELOG.md CHANGED
@@ -3,6 +3,19 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [0.2.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-color-picker@0.1.8...@synerise/ds-color-picker@0.2.0) (2023-02-03)
7
+
8
+
9
+ ### Features
10
+
11
+ * **color-picker:** color change logic separately for each input ([8eefaf0](https://github.com/Synerise/synerise-design/commit/8eefaf0b5028b4b916110d554f06b025bd2cdf45))
12
+ * **color-picker:** color change logic separately for each input ([54283f7](https://github.com/Synerise/synerise-design/commit/54283f7ab79b0da75ef78e26671a8ad675b04f44))
13
+ * **color-picker:** descriptions of methods and names ([45be774](https://github.com/Synerise/synerise-design/commit/45be774dff8bd50b6b201dbfed0a2d1d09a2b1a8))
14
+
15
+
16
+
17
+
18
+
6
19
  ## [0.1.8](https://github.com/Synerise/synerise-design/compare/@synerise/ds-color-picker@0.1.7...@synerise/ds-color-picker@0.1.8) (2023-01-30)
7
20
 
8
21
 
@@ -9,15 +9,14 @@ import * as copy from 'copy-to-clipboard';
9
9
  import Tooltip from '@synerise/ds-tooltip';
10
10
  import Dropdown from '@synerise/ds-dropdown';
11
11
  import { useOnClickOutside } from '@synerise/ds-utils';
12
- import { isValidHexColor, convert3DigitHexTo6Digit, filterAlphanumeric } from './utils';
12
+ import { isValidHexColor, isValidTextColor, convert3DigitHexTo6Digit, filterAlphanumeric, standardizeColor } from './utils';
13
13
  import ColorPickerStyles from './ColorPicker.styles';
14
- var hash = '#';
15
14
  var DEFAULT_MAX_WIDTH_PICKER = 228;
15
+ var DEFAULT_COLOR = '#ffffff';
16
16
 
17
17
  var ColorPicker = function ColorPicker(_ref) {
18
18
  var maxWidth = _ref.maxWidth,
19
- _ref$value = _ref.value,
20
- value = _ref$value === void 0 ? '#ffffff' : _ref$value,
19
+ value = _ref.value,
21
20
  onChange = _ref.onChange,
22
21
  _ref$colors = _ref.colors,
23
22
  colors = _ref$colors === void 0 ? [] : _ref$colors,
@@ -38,37 +37,80 @@ var ColorPicker = function ColorPicker(_ref) {
38
37
  description = _ref.description;
39
38
 
40
39
  var _React$useState = React.useState(value),
41
- color = _React$useState[0],
42
- setColor = _React$useState[1];
40
+ colorTextInput = _React$useState[0],
41
+ setColorTextInput = _React$useState[1];
43
42
 
44
43
  var _React$useState2 = React.useState(value),
45
- validHexColor = _React$useState2[0],
46
- setValidHexColor = _React$useState2[1];
44
+ colorHexInput = _React$useState2[0],
45
+ setColorHexInput = _React$useState2[1];
47
46
 
48
- var _React$useState3 = React.useState(-1),
49
- pressed = _React$useState3[0],
50
- setPressed = _React$useState3[1];
47
+ var _React$useState3 = React.useState(value),
48
+ lastValidTextColor = _React$useState3[0],
49
+ setLastValidTextColor = _React$useState3[1];
51
50
 
52
- var _React$useState4 = React.useState(false),
53
- dropdownVisible = _React$useState4[0],
54
- setDropdownVisible = _React$useState4[1];
51
+ var _React$useState4 = React.useState(value),
52
+ lastValidHexColor = _React$useState4[0],
53
+ setLastValidHexColor = _React$useState4[1];
55
54
 
56
- var _React$useState5 = React.useState(colors),
57
- savedColors = _React$useState5[0],
58
- setSavedColors = _React$useState5[1];
55
+ var _React$useState5 = React.useState(-1),
56
+ pressed = _React$useState5[0],
57
+ setPressed = _React$useState5[1];
59
58
 
60
- var onChangeColor = React.useCallback(function (colorValue) {
61
- setColor(colorValue);
59
+ var _React$useState6 = React.useState(false),
60
+ dropdownVisible = _React$useState6[0],
61
+ setDropdownVisible = _React$useState6[1];
62
+
63
+ var _React$useState7 = React.useState(colors),
64
+ savedColors = _React$useState7[0],
65
+ setSavedColors = _React$useState7[1];
66
+
67
+ React.useEffect(function () {
68
+ if (lastValidHexColor) {
69
+ onChange && onChange(lastValidHexColor);
70
+ }
71
+ }, [onChange, lastValidHexColor]);
72
+ var onChangeTextColor = React.useCallback(function (colorValue) {
73
+ setColorTextInput(colorValue);
74
+
75
+ if (isValidTextColor(colorValue)) {
76
+ var standardizedColor = standardizeColor(colorValue);
77
+ setLastValidTextColor(colorValue);
78
+ setColorHexInput(standardizedColor);
79
+ setLastValidHexColor(standardizedColor);
80
+ } else if (isValidHexColor(colorValue)) {
81
+ var fullHexColor = convert3DigitHexTo6Digit(colorValue);
82
+ setLastValidTextColor(fullHexColor);
83
+ setColorHexInput(fullHexColor);
84
+ setLastValidHexColor(fullHexColor);
85
+ }
86
+
87
+ setPressed(-1);
88
+ }, []);
89
+ var onChangeHexColor = React.useCallback(function (colorValue) {
90
+ setColorHexInput(colorValue);
62
91
 
63
92
  if (isValidHexColor(colorValue)) {
64
- var c = convert3DigitHexTo6Digit(colorValue);
65
- setValidHexColor(c);
66
- onChange && onChange(c);
93
+ var fullHexColor = convert3DigitHexTo6Digit(colorValue);
94
+ setColorTextInput(fullHexColor);
95
+ setLastValidTextColor(fullHexColor);
96
+ setLastValidHexColor(fullHexColor);
67
97
  }
68
- }, [onChange]);
98
+
99
+ setPressed(-1);
100
+ }, []);
69
101
  var onBlurHandler = React.useCallback(function () {
70
- setColor(validHexColor);
71
- }, [validHexColor]);
102
+ setColorTextInput(lastValidTextColor);
103
+ setColorHexInput(lastValidHexColor);
104
+ }, [lastValidHexColor, lastValidTextColor]);
105
+ var onClickHandler = React.useCallback(function () {
106
+ setDropdownVisible(!dropdownVisible);
107
+ }, [dropdownVisible]);
108
+ React.useEffect(function () {
109
+ if (!value || !isValidTextColor(value) || !isValidHexColor(value)) {
110
+ onChangeTextColor(DEFAULT_COLOR);
111
+ } // eslint-disable-next-line react-hooks/exhaustive-deps
112
+
113
+ }, []);
72
114
  var heightOfDropdown = React.useCallback(function () {
73
115
  if (errorText || description) {
74
116
  if (errorText && description) {
@@ -87,7 +129,7 @@ var ColorPicker = function ColorPicker(_ref) {
87
129
 
88
130
  var saveColor = function saveColor() {
89
131
  setSavedColors(function (ar) {
90
- var colorsArray = (color ? [color].concat(ar) : ar).slice(0, maxSavedColors);
132
+ var colorsArray = (lastValidHexColor ? [lastValidHexColor].concat(ar) : ar).slice(0, maxSavedColors);
91
133
  onSaveColors && onSaveColors(colorsArray);
92
134
  return colorsArray;
93
135
  });
@@ -103,8 +145,8 @@ var ColorPicker = function ColorPicker(_ref) {
103
145
  size: 16,
104
146
  component: /*#__PURE__*/React.createElement(FormulaPlusM, null)
105
147
  }))), infix({
106
- color: color,
107
- setColor: onChangeColor
148
+ color: colorTextInput,
149
+ setColor: onChangeTextColor
108
150
  }), savedColors.length > 0 && /*#__PURE__*/React.createElement(Tags, {
109
151
  tagShape: TagShape.SINGLE_CHARACTER_SQUARE // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
110
152
  // @ts-ignore
@@ -120,40 +162,37 @@ var ColorPicker = function ColorPicker(_ref) {
120
162
  })),
121
163
  color: colorEntry,
122
164
  onClick: function onClick() {
123
- onChangeColor(colorEntry);
165
+ onChangeHexColor(colorEntry);
124
166
  setPressed(i);
125
167
  }
126
168
  };
127
- }),
128
- disabled: false
169
+ })
129
170
  }));
130
171
  var dropdown = /*#__PURE__*/React.createElement(ColorPickerStyles.Container, {
131
172
  ref: ref,
132
173
  size: size
133
174
  }, /*#__PURE__*/React.createElement(ReactColorful, {
134
- color: validHexColor,
135
- onChange: onChangeColor
175
+ color: lastValidHexColor,
176
+ onChange: onChangeHexColor
136
177
  }), /*#__PURE__*/React.createElement(ColorPickerStyles.PrefixTag, {
137
178
  height: isShownSavedColors,
138
179
  size: size
139
180
  }, /*#__PURE__*/React.createElement(Tag, {
140
181
  shape: TagShape.SINGLE_CHARACTER_SQUARE,
141
- color: validHexColor,
142
- disabled: false
182
+ color: lastValidHexColor
143
183
  })), /*#__PURE__*/React.createElement(ColorPickerStyles.SubContainer, {
144
184
  savedColors: isShownSavedColors
145
185
  }, /*#__PURE__*/React.createElement(ColorPickerStyles.ColorPickerInput, {
146
- value: filterAlphanumeric(color),
186
+ value: colorHexInput && filterAlphanumeric(colorHexInput),
147
187
  prefixel: /*#__PURE__*/React.createElement(ColorPickerStyles.PreffixWrapper, null, "#"),
148
188
  onChange: function onChange(ev) {
149
- var hexValue = hash + filterAlphanumeric(ev.target.value);
150
- onChangeColor(hexValue);
189
+ onChangeHexColor("#" + ev.target.value);
151
190
  },
152
191
  onBlur: onBlurHandler,
153
192
  placeholder: placeholder,
154
193
  icon1: /*#__PURE__*/React.createElement(ColorPickerStyles.CopyIcon, {
155
194
  onClick: function onClick() {
156
- copy(color);
195
+ lastValidHexColor && copy(lastValidHexColor);
157
196
  },
158
197
  component: /*#__PURE__*/React.createElement(CopyClipboardM, null)
159
198
  }),
@@ -172,19 +211,17 @@ var ColorPicker = function ColorPicker(_ref) {
172
211
  }, /*#__PURE__*/React.createElement(ColorPickerStyles.ColorPickerSelect, _extends({
173
212
  prefix: /*#__PURE__*/React.createElement(ColorPickerStyles.ColorTag, {
174
213
  shape: TagShape.SINGLE_CHARACTER_ROUND,
175
- color: validHexColor,
214
+ color: lastValidHexColor,
176
215
  disabled: false,
177
- onClick: function onClick() {
178
- return setDropdownVisible(!dropdownVisible);
179
- }
216
+ onClick: onClickHandler
180
217
  }),
218
+ onClick: onClickHandler,
181
219
  onChange: function onChange(ev) {
182
- var hexValue = hash + filterAlphanumeric(ev.target.value);
183
- onChangeColor(hexValue);
220
+ onChangeTextColor(ev.target.value);
184
221
  },
185
222
  onBlur: onBlurHandler,
186
223
  placeholder: placeholder,
187
- value: color,
224
+ value: colorTextInput,
188
225
  description: description,
189
226
  errorText: errorText
190
227
  }, inputProps))));
package/dist/utils.d.ts CHANGED
@@ -1,3 +1,21 @@
1
1
  export declare const filterAlphanumeric: (colorValue: string) => string;
2
2
  export declare const isValidHexColor: (hex: string) => boolean;
3
+ /**
4
+ *
5
+ * Method that creates an option element and then assigns a colour to its styles.
6
+ * If the colour is correct, it is assigned to the element and can then be checked against the input data.
7
+ *
8
+ * @param {string} strColor
9
+ * @returns {boolean}
10
+ */
11
+ export declare const isValidTextColor: (strColor: string) => boolean;
12
+ /**
13
+ *
14
+ * A method that converts color in user-understandable notation to HEX notation.
15
+ * It uses the canvas element for this
16
+ *
17
+ * @param {string} color
18
+ * @returns {string}
19
+ */
20
+ export declare const standardizeColor: (color: string) => string;
3
21
  export declare const convert3DigitHexTo6Digit: (hexColor: string) => string;
package/dist/utils.js CHANGED
@@ -6,6 +6,41 @@ export var filterAlphanumeric = function filterAlphanumeric(colorValue) {
6
6
  export var isValidHexColor = function isValidHexColor(hex) {
7
7
  return hexColorRegex.test(hex);
8
8
  };
9
+ /**
10
+ *
11
+ * Method that creates an option element and then assigns a colour to its styles.
12
+ * If the colour is correct, it is assigned to the element and can then be checked against the input data.
13
+ *
14
+ * @param {string} strColor
15
+ * @returns {boolean}
16
+ */
17
+
18
+ export var isValidTextColor = function isValidTextColor(strColor) {
19
+ if (!strColor) return false; // eslint-disable-next-line no-undef
20
+
21
+ var s = new Option().style;
22
+ s.color = strColor;
23
+ return s.color === strColor;
24
+ };
25
+ /**
26
+ *
27
+ * A method that converts color in user-understandable notation to HEX notation.
28
+ * It uses the canvas element for this
29
+ *
30
+ * @param {string} color
31
+ * @returns {string}
32
+ */
33
+
34
+ export var standardizeColor = function standardizeColor(color) {
35
+ var ctx = document.createElement('canvas').getContext('2d');
36
+
37
+ if (ctx) {
38
+ ctx.fillStyle = color;
39
+ return ctx.fillStyle;
40
+ }
41
+
42
+ return '';
43
+ };
9
44
  export var convert3DigitHexTo6Digit = function convert3DigitHexTo6Digit(hexColor) {
10
45
  var alphaHexColor = filterAlphanumeric(hexColor);
11
46
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synerise/ds-color-picker",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "description": "Color-Picker Component for the Synerise Design System",
5
5
  "license": "ISC",
6
6
  "repository": "Synerise/synerise-design",
@@ -45,5 +45,5 @@
45
45
  "@synerise/ds-core": "*",
46
46
  "react": ">=16.9.0 < 17.0.0"
47
47
  },
48
- "gitHead": "09992e7b69930b5357e169418af91e29d1cba5ee"
48
+ "gitHead": "05f16eaafa3de3ec33cc0d9a6712864646a0617e"
49
49
  }