@synerise/ds-color-picker 0.1.8 → 0.3.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,31 @@
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.3.0](https://github.com/Synerise/synerise-design/compare/@synerise/ds-color-picker@0.2.0...@synerise/ds-color-picker@0.3.0) (2023-02-06)
7
+
8
+
9
+ ### Features
10
+
11
+ * **color-picker:** copy tooltip texts object ([929c93e](https://github.com/Synerise/synerise-design/commit/929c93e53e3679e69c44e8ee655b0dbf200ad283))
12
+ * **color-picker:** tests for component and methods ([bafa8c6](https://github.com/Synerise/synerise-design/commit/bafa8c6bbf967983fce4a65dc9bbc71a52a72953))
13
+
14
+
15
+
16
+
17
+
18
+ # [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)
19
+
20
+
21
+ ### Features
22
+
23
+ * **color-picker:** color change logic separately for each input ([8eefaf0](https://github.com/Synerise/synerise-design/commit/8eefaf0b5028b4b916110d554f06b025bd2cdf45))
24
+ * **color-picker:** color change logic separately for each input ([54283f7](https://github.com/Synerise/synerise-design/commit/54283f7ab79b0da75ef78e26671a8ad675b04f44))
25
+ * **color-picker:** descriptions of methods and names ([45be774](https://github.com/Synerise/synerise-design/commit/45be774dff8bd50b6b201dbfed0a2d1d09a2b1a8))
26
+
27
+
28
+
29
+
30
+
6
31
  ## [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
32
 
8
33
 
@@ -1,4 +1,4 @@
1
1
  /// <reference types="react" />
2
2
  import { ColorPickerProps } from './ColorPicker.types';
3
- declare const ColorPicker: ({ maxWidth, value, onChange, colors, onSaveColors, infix, placeholder, inputProps, maxSavedColors, tooltipText, isShownSavedColors, size, errorText, description, }: ColorPickerProps) => JSX.Element;
3
+ declare const ColorPicker: ({ maxWidth, value, onChange, colors, onSaveColors, infix, placeholder, inputProps, maxSavedColors, tooltip, isShownSavedColors, size, errorText, description, }: ColorPickerProps) => JSX.Element;
4
4
  export default ColorPicker;
@@ -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,
@@ -30,7 +29,7 @@ var ColorPicker = function ColorPicker(_ref) {
30
29
  inputProps = _ref.inputProps,
31
30
  _ref$maxSavedColors = _ref.maxSavedColors,
32
31
  maxSavedColors = _ref$maxSavedColors === void 0 ? 9 : _ref$maxSavedColors,
33
- tooltipText = _ref.tooltipText,
32
+ tooltip = _ref.tooltip,
34
33
  isShownSavedColors = _ref.isShownSavedColors,
35
34
  _ref$size = _ref.size,
36
35
  size = _ref$size === void 0 ? 'M' : _ref$size,
@@ -38,37 +37,84 @@ 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
+ lastValidHexColor = _React$useState3[0],
49
+ setLastValidHexColor = _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(-1),
52
+ pressed = _React$useState4[0],
53
+ setPressed = _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(false),
56
+ dropdownVisible = _React$useState5[0],
57
+ setDropdownVisible = _React$useState5[1];
59
58
 
60
- var onChangeColor = React.useCallback(function (colorValue) {
61
- setColor(colorValue);
59
+ var _React$useState6 = React.useState(colors),
60
+ savedColors = _React$useState6[0],
61
+ setSavedColors = _React$useState6[1];
62
+
63
+ var _React$useState7 = React.useState(tooltip == null ? void 0 : tooltip.copy),
64
+ tooltipText = _React$useState7[0],
65
+ setTooltipText = _React$useState7[1];
66
+
67
+ var onChangeTextColor = React.useCallback(function (colorValue) {
68
+ setColorTextInput(colorValue);
69
+
70
+ if (isValidTextColor(colorValue)) {
71
+ var standardizedColor = standardizeColor(colorValue);
72
+ setColorHexInput(standardizedColor);
73
+ setLastValidHexColor(standardizedColor);
74
+ onChange && onChange(standardizedColor);
75
+ } else if (isValidHexColor(colorValue)) {
76
+ var fullHexColor = convert3DigitHexTo6Digit(colorValue);
77
+ setColorHexInput(fullHexColor);
78
+ setLastValidHexColor(fullHexColor);
79
+ onChange && onChange(fullHexColor);
80
+ }
81
+
82
+ setPressed(-1);
83
+ }, [onChange]);
84
+ var onChangeHexColor = React.useCallback(function (colorValue) {
85
+ setColorHexInput(colorValue);
62
86
 
63
87
  if (isValidHexColor(colorValue)) {
64
- var c = convert3DigitHexTo6Digit(colorValue);
65
- setValidHexColor(c);
66
- onChange && onChange(c);
88
+ var fullHexColor = convert3DigitHexTo6Digit(colorValue);
89
+ setColorTextInput(fullHexColor);
90
+ setLastValidHexColor(fullHexColor);
91
+ onChange && onChange(fullHexColor);
67
92
  }
93
+
94
+ setPressed(-1);
68
95
  }, [onChange]);
69
96
  var onBlurHandler = React.useCallback(function () {
70
- setColor(validHexColor);
71
- }, [validHexColor]);
97
+ setColorTextInput(lastValidHexColor);
98
+ setColorHexInput(lastValidHexColor);
99
+ }, [lastValidHexColor]);
100
+ var onClickHandler = React.useCallback(function () {
101
+ setDropdownVisible(!dropdownVisible);
102
+ }, [dropdownVisible]);
103
+ var copyHandler = React.useCallback(function () {
104
+ lastValidHexColor && copy(lastValidHexColor);
105
+ setTooltipText(tooltip == null ? void 0 : tooltip.copied);
106
+ setTimeout(function () {
107
+ setTooltipText(tooltip == null ? void 0 : tooltip.copy);
108
+ }, 3000);
109
+ }, [lastValidHexColor, tooltip]);
110
+ React.useEffect(function () {
111
+ if (value && (isValidHexColor(value) || isValidTextColor(value))) {
112
+ onChangeTextColor(value);
113
+ } else {
114
+ onChangeTextColor(DEFAULT_COLOR);
115
+ } // eslint-disable-next-line react-hooks/exhaustive-deps
116
+
117
+ }, []);
72
118
  var heightOfDropdown = React.useCallback(function () {
73
119
  if (errorText || description) {
74
120
  if (errorText && description) {
@@ -87,7 +133,7 @@ var ColorPicker = function ColorPicker(_ref) {
87
133
 
88
134
  var saveColor = function saveColor() {
89
135
  setSavedColors(function (ar) {
90
- var colorsArray = (color ? [color].concat(ar) : ar).slice(0, maxSavedColors);
136
+ var colorsArray = (lastValidHexColor ? [lastValidHexColor].concat(ar) : ar).slice(0, maxSavedColors);
91
137
  onSaveColors && onSaveColors(colorsArray);
92
138
  return colorsArray;
93
139
  });
@@ -103,8 +149,8 @@ var ColorPicker = function ColorPicker(_ref) {
103
149
  size: 16,
104
150
  component: /*#__PURE__*/React.createElement(FormulaPlusM, null)
105
151
  }))), infix({
106
- color: color,
107
- setColor: onChangeColor
152
+ color: colorTextInput,
153
+ setColor: onChangeTextColor
108
154
  }), savedColors.length > 0 && /*#__PURE__*/React.createElement(Tags, {
109
155
  tagShape: TagShape.SINGLE_CHARACTER_SQUARE // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
110
156
  // @ts-ignore
@@ -120,44 +166,39 @@ var ColorPicker = function ColorPicker(_ref) {
120
166
  })),
121
167
  color: colorEntry,
122
168
  onClick: function onClick() {
123
- onChangeColor(colorEntry);
169
+ onChangeHexColor(colorEntry);
124
170
  setPressed(i);
125
171
  }
126
172
  };
127
- }),
128
- disabled: false
173
+ })
129
174
  }));
130
175
  var dropdown = /*#__PURE__*/React.createElement(ColorPickerStyles.Container, {
131
176
  ref: ref,
132
177
  size: size
133
178
  }, /*#__PURE__*/React.createElement(ReactColorful, {
134
- color: validHexColor,
135
- onChange: onChangeColor
179
+ color: lastValidHexColor,
180
+ onChange: onChangeHexColor
136
181
  }), /*#__PURE__*/React.createElement(ColorPickerStyles.PrefixTag, {
137
182
  height: isShownSavedColors,
138
183
  size: size
139
184
  }, /*#__PURE__*/React.createElement(Tag, {
140
185
  shape: TagShape.SINGLE_CHARACTER_SQUARE,
141
- color: validHexColor,
142
- disabled: false
186
+ color: lastValidHexColor
143
187
  })), /*#__PURE__*/React.createElement(ColorPickerStyles.SubContainer, {
144
188
  savedColors: isShownSavedColors
145
189
  }, /*#__PURE__*/React.createElement(ColorPickerStyles.ColorPickerInput, {
146
- value: filterAlphanumeric(color),
190
+ value: colorHexInput && filterAlphanumeric(colorHexInput),
147
191
  prefixel: /*#__PURE__*/React.createElement(ColorPickerStyles.PreffixWrapper, null, "#"),
148
192
  onChange: function onChange(ev) {
149
- var hexValue = hash + filterAlphanumeric(ev.target.value);
150
- onChangeColor(hexValue);
193
+ onChangeHexColor("#" + ev.target.value);
151
194
  },
152
195
  onBlur: onBlurHandler,
153
196
  placeholder: placeholder,
154
- icon1: /*#__PURE__*/React.createElement(ColorPickerStyles.CopyIcon, {
155
- onClick: function onClick() {
156
- copy(color);
157
- },
197
+ icon1: tooltipText ? /*#__PURE__*/React.createElement(ColorPickerStyles.CopyIcon, {
198
+ onClick: copyHandler,
158
199
  component: /*#__PURE__*/React.createElement(CopyClipboardM, null)
159
- }),
160
- icon1Tooltip: /*#__PURE__*/React.createElement("span", null, tooltipText || 'Copy to clipboard')
200
+ }) : undefined,
201
+ icon1Tooltip: tooltipText ? /*#__PURE__*/React.createElement("span", null, tooltipText) : undefined
161
202
  }), isShownSavedColors && /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Divider, null), swatchSection)));
162
203
  return /*#__PURE__*/React.createElement(React.Fragment, null, maxWidth && maxWidth >= DEFAULT_MAX_WIDTH_PICKER && /*#__PURE__*/React.createElement(ColorPickerStyles.ColorPickerModalStyle, {
163
204
  maxWidth: maxWidth
@@ -170,21 +211,20 @@ var ColorPicker = function ColorPicker(_ref) {
170
211
  overlay: dropdown,
171
212
  placement: "bottomLeft"
172
213
  }, /*#__PURE__*/React.createElement(ColorPickerStyles.ColorPickerSelect, _extends({
214
+ "data-testid": "color-picker",
173
215
  prefix: /*#__PURE__*/React.createElement(ColorPickerStyles.ColorTag, {
174
216
  shape: TagShape.SINGLE_CHARACTER_ROUND,
175
- color: validHexColor,
217
+ color: lastValidHexColor,
176
218
  disabled: false,
177
- onClick: function onClick() {
178
- return setDropdownVisible(!dropdownVisible);
179
- }
219
+ onClick: onClickHandler
180
220
  }),
221
+ onClick: onClickHandler,
181
222
  onChange: function onChange(ev) {
182
- var hexValue = hash + filterAlphanumeric(ev.target.value);
183
- onChangeColor(hexValue);
223
+ onChangeTextColor(ev.target.value);
184
224
  },
185
225
  onBlur: onBlurHandler,
186
226
  placeholder: placeholder,
187
- value: color,
227
+ value: colorTextInput,
188
228
  description: description,
189
229
  errorText: errorText
190
230
  }, inputProps))));
@@ -22,7 +22,10 @@ export interface ColorPickerProps extends Partial<Pick<HTMLInputElement, 'placeh
22
22
  infix?: (colorHooks?: Partial<ColorHookType>) => JSX.Element;
23
23
  maxSavedColors?: number;
24
24
  inputProps?: Omit<InputProps, 'value' | 'onChange' | 'defaultValue'>;
25
- tooltipText?: string;
25
+ tooltip?: {
26
+ copy: string;
27
+ copied: string;
28
+ };
26
29
  isShownSavedColors?: boolean;
27
30
  size?: 'S' | 'M' | 'L';
28
31
  errorText?: string;
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.3.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": "2bc43ef116113283aed104339dc1a50db543b45e"
49
49
  }