@storybook/components 6.2.0-rc.6 → 6.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.
@@ -1,26 +1,20 @@
1
1
  import "core-js/modules/es.array.concat.js";
2
- import "core-js/modules/es.array.every.js";
3
2
  import "core-js/modules/es.array.filter.js";
4
3
  import "core-js/modules/es.array.index-of.js";
5
4
  import "core-js/modules/es.array.map.js";
6
5
  import "core-js/modules/es.array.slice.js";
7
- import "core-js/modules/es.function.name.js";
6
+ import "core-js/modules/es.array.some.js";
8
7
  import "core-js/modules/es.number.constructor.js";
8
+ import "core-js/modules/es.object.assign.js";
9
+ import "core-js/modules/es.object.keys.js";
9
10
  import "core-js/modules/es.object.values.js";
10
11
  import "core-js/modules/es.regexp.exec.js";
11
12
  import "core-js/modules/es.string.match.js";
12
13
  import "core-js/modules/es.string.replace.js";
14
+ import "core-js/modules/es.string.split.js";
13
15
  import "core-js/modules/es.string.starts-with.js";
14
16
 
15
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
16
-
17
- function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
18
-
19
- function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
20
-
21
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
22
-
23
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
17
+ var _ColorPicker, _fallbackColor;
24
18
 
25
19
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
26
20
 
@@ -34,7 +28,15 @@ function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(
34
28
 
35
29
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
36
30
 
37
- import React, { useCallback, useEffect, useMemo, useState } from 'react';
31
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
32
+
33
+ function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
34
+
35
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
36
+
37
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
38
+
39
+ import React, { useCallback, useMemo, useState } from 'react';
38
40
  import { HexColorPicker, HslaStringColorPicker, RgbaStringColorPicker } from 'react-colorful';
39
41
  import convert from 'color-convert';
40
42
  import throttle from 'lodash/throttle';
@@ -43,11 +45,103 @@ import { TooltipNote } from '../tooltip/TooltipNote';
43
45
  import { WithTooltip } from '../tooltip/lazy-WithTooltip';
44
46
  import { Form } from '../form';
45
47
  import { Icons } from '../icon/icon';
48
+ var Wrapper = styled.div({
49
+ position: 'relative',
50
+ maxWidth: 250
51
+ });
52
+ var PickerTooltip = styled(WithTooltip)({
53
+ position: 'absolute',
54
+ zIndex: 1,
55
+ top: 4,
56
+ left: 4
57
+ });
58
+ var TooltipContent = styled.div({
59
+ width: 200,
60
+ margin: 5,
61
+ '.react-colorful__saturation': {
62
+ borderRadius: '4px 4px 0 0'
63
+ },
64
+ '.react-colorful__hue': {
65
+ boxShadow: 'inset 0 0 0 1px rgb(0 0 0 / 5%)'
66
+ },
67
+ '.react-colorful__last-control': {
68
+ borderRadius: '0 0 4px 4px'
69
+ }
70
+ });
71
+ var Note = styled(TooltipNote)(function (_ref) {
72
+ var theme = _ref.theme;
73
+ return {
74
+ fontFamily: theme.typography.fonts.base
75
+ };
76
+ });
77
+ var Swatches = styled.div({
78
+ display: 'grid',
79
+ gridTemplateColumns: 'repeat(9, 16px)',
80
+ gap: 6,
81
+ padding: 3,
82
+ marginTop: 5,
83
+ width: 200
84
+ });
85
+ var SwatchColor = styled.div(function (_ref2) {
86
+ var theme = _ref2.theme,
87
+ active = _ref2.active;
88
+ return {
89
+ width: 16,
90
+ height: 16,
91
+ boxShadow: active ? "".concat(theme.appBorderColor, " 0 0 0 1px inset, ").concat(theme.color.mediumdark, "50 0 0 0 4px") : "".concat(theme.appBorderColor, " 0 0 0 1px inset"),
92
+ borderRadius: theme.appBorderRadius
93
+ };
94
+ });
95
+ var swatchBackground = "url('data:image/svg+xml;charset=utf-8,<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill-opacity=\".05\"><path d=\"M8 0h8v8H8zM0 8h8v8H0z\"/></svg>')";
96
+
97
+ var Swatch = function Swatch(_ref3) {
98
+ var value = _ref3.value,
99
+ active = _ref3.active,
100
+ onClick = _ref3.onClick,
101
+ style = _ref3.style,
102
+ props = _objectWithoutProperties(_ref3, ["value", "active", "onClick", "style"]);
103
+
104
+ var backgroundImage = "linear-gradient(".concat(value, ", ").concat(value, "), ").concat(swatchBackground, ", linear-gradient(#fff, #fff)");
105
+ return /*#__PURE__*/React.createElement(SwatchColor, _extends({}, props, {
106
+ active: active,
107
+ onClick: onClick,
108
+ style: Object.assign({}, style, {
109
+ backgroundImage: backgroundImage
110
+ })
111
+ }));
112
+ };
113
+
114
+ Swatch.displayName = "Swatch";
115
+ var Input = styled(Form.Input)(function (_ref4) {
116
+ var theme = _ref4.theme;
117
+ return {
118
+ width: '100%',
119
+ paddingLeft: 30,
120
+ paddingRight: 30,
121
+ boxSizing: 'border-box',
122
+ fontFamily: theme.typography.fonts.base
123
+ };
124
+ });
125
+ var ToggleIcon = styled(Icons)(function (_ref5) {
126
+ var theme = _ref5.theme;
127
+ return {
128
+ position: 'absolute',
129
+ zIndex: 1,
130
+ top: 6,
131
+ right: 7,
132
+ width: 20,
133
+ height: 20,
134
+ padding: 4,
135
+ boxSizing: 'border-box',
136
+ cursor: 'pointer',
137
+ color: theme.input.color
138
+ };
139
+ });
46
140
  var ColorSpace;
47
141
 
48
142
  (function (ColorSpace) {
49
- ColorSpace["RGBA"] = "rgb";
50
- ColorSpace["HSLA"] = "hsl";
143
+ ColorSpace["RGB"] = "rgb";
144
+ ColorSpace["HSL"] = "hsl";
51
145
  ColorSpace["HEX"] = "hex";
52
146
  })(ColorSpace || (ColorSpace = {}));
53
147
 
@@ -55,8 +149,10 @@ var COLOR_SPACES = Object.values(ColorSpace);
55
149
  var COLOR_REGEXP = /\(([0-9]+),\s*([0-9]+)%?,\s*([0-9]+)%?,?\s*([0-9.]+)?\)/;
56
150
  var RGB_REGEXP = /^\s*rgba?\(([0-9]+),\s*([0-9]+),\s*([0-9]+),?\s*([0-9.]+)?\)\s*$/i;
57
151
  var HSL_REGEXP = /^\s*hsla?\(([0-9]+),\s*([0-9]+)%,\s*([0-9]+)%,?\s*([0-9.]+)?\)\s*$/i;
58
- var HEX_REGEXP = /^\s*#([0-9a-f]{1,2})([0-9a-f]{1,2})([0-9a-f]{1,2})\s*$/i;
59
- var SHORTHEX_REGEXP = /^\s*#([0-9a-f])([0-9a-f])([0-9a-f])\s*$/i;
152
+ var HEX_REGEXP = /^\s*#?([0-9a-f]{3}|[0-9a-f]{6})\s*$/i;
153
+ var SHORTHEX_REGEXP = /^\s*#?([0-9a-f]{3})\s*$/i;
154
+ var ColorPicker = (_ColorPicker = {}, _defineProperty(_ColorPicker, ColorSpace.HEX, HexColorPicker), _defineProperty(_ColorPicker, ColorSpace.RGB, RgbaStringColorPicker), _defineProperty(_ColorPicker, ColorSpace.HSL, HslaStringColorPicker), _ColorPicker);
155
+ var fallbackColor = (_fallbackColor = {}, _defineProperty(_fallbackColor, ColorSpace.HEX, 'transparent'), _defineProperty(_fallbackColor, ColorSpace.RGB, 'rgba(0, 0, 0, 0)'), _defineProperty(_fallbackColor, ColorSpace.HSL, 'hsla(0, 0%, 0%, 0)'), _fallbackColor);
60
156
 
61
157
  var stringToArgs = function stringToArgs(value) {
62
158
  var match = value === null || value === void 0 ? void 0 : value.match(COLOR_REGEXP);
@@ -72,264 +168,247 @@ var stringToArgs = function stringToArgs(value) {
72
168
  return [x, y, z, a].map(Number);
73
169
  };
74
170
 
75
- var expandShorthand = function expandShorthand(value) {
76
- if (!value) return '#000000';
77
-
78
- if (!value.startsWith('#')) {
79
- try {
80
- return "#".concat(convert.keyword.hex(value));
81
- } catch (e) {
82
- return value;
83
- }
84
- }
85
-
86
- if (value.length > 4) return value;
87
- var match = value.match(SHORTHEX_REGEXP);
88
- if (!match) return value;
89
-
90
- var _match2 = _slicedToArray(match, 4),
91
- r = _match2[1],
92
- g = _match2[2],
93
- b = _match2[3];
94
-
95
- return "#".concat(r).concat(r).concat(g).concat(g).concat(b).concat(b);
96
- };
97
-
98
- var parseValue = function parseValue(input) {
99
- var _ref8;
100
-
101
- var _ref = _typeof(input) === 'object' ? input : {
102
- color: input,
103
- title: undefined
104
- },
105
- color = _ref.color,
106
- title = _ref.title;
171
+ var parseValue = function parseValue(value) {
172
+ var _ref12;
107
173
 
108
- if (!color) return undefined;
174
+ if (!value) return undefined;
175
+ var valid = true;
109
176
 
110
- if (RGB_REGEXP.test(color)) {
111
- var _ref4;
177
+ if (RGB_REGEXP.test(value)) {
178
+ var _ref8;
112
179
 
113
- var _stringToArgs = stringToArgs(color),
180
+ var _stringToArgs = stringToArgs(value),
114
181
  _stringToArgs2 = _slicedToArray(_stringToArgs, 4),
115
182
  r = _stringToArgs2[0],
116
183
  g = _stringToArgs2[1],
117
184
  b = _stringToArgs2[2],
118
185
  a = _stringToArgs2[3];
119
186
 
120
- var _ref2 = convert.rgb.hsl([r, g, b]) || [0, 0, 0],
121
- _ref3 = _slicedToArray(_ref2, 3),
122
- h = _ref3[0],
123
- s = _ref3[1],
124
- l = _ref3[2];
125
-
126
- return _ref4 = {
127
- value: color,
128
- title: title || convert.rgb.keyword([r, g, b]) || color,
129
- colorSpace: ColorSpace.RGBA
130
- }, _defineProperty(_ref4, ColorSpace.RGBA, color), _defineProperty(_ref4, ColorSpace.HSLA, "hsla(".concat(h, ", ").concat(s, "%, ").concat(l, "%, ").concat(a, ")")), _defineProperty(_ref4, ColorSpace.HEX, "#".concat(convert.rgb.hex([r, g, b]))), _ref4;
187
+ var _ref6 = convert.rgb.hsl([r, g, b]) || [0, 0, 0],
188
+ _ref7 = _slicedToArray(_ref6, 3),
189
+ h = _ref7[0],
190
+ s = _ref7[1],
191
+ l = _ref7[2];
192
+
193
+ return _ref8 = {
194
+ valid: valid,
195
+ value: value,
196
+ keyword: convert.rgb.keyword([r, g, b]),
197
+ colorSpace: ColorSpace.RGB
198
+ }, _defineProperty(_ref8, ColorSpace.RGB, value), _defineProperty(_ref8, ColorSpace.HSL, "hsla(".concat(h, ", ").concat(s, "%, ").concat(l, "%, ").concat(a, ")")), _defineProperty(_ref8, ColorSpace.HEX, "#".concat(convert.rgb.hex([r, g, b]).toLowerCase())), _ref8;
131
199
  }
132
200
 
133
- if (HSL_REGEXP.test(color)) {
134
- var _ref7;
201
+ if (HSL_REGEXP.test(value)) {
202
+ var _ref11;
135
203
 
136
- var _stringToArgs3 = stringToArgs(color),
204
+ var _stringToArgs3 = stringToArgs(value),
137
205
  _stringToArgs4 = _slicedToArray(_stringToArgs3, 4),
138
206
  _h = _stringToArgs4[0],
139
207
  _s2 = _stringToArgs4[1],
140
208
  _l = _stringToArgs4[2],
141
209
  _a = _stringToArgs4[3];
142
210
 
143
- var _ref5 = convert.hsl.rgb([_h, _s2, _l]) || [0, 0, 0],
144
- _ref6 = _slicedToArray(_ref5, 3),
145
- _r = _ref6[0],
146
- _g = _ref6[1],
147
- _b = _ref6[2];
148
-
149
- return _ref7 = {
150
- value: color,
151
- title: title || convert.rgb.keyword([_r, _g, _b]) || color,
152
- colorSpace: ColorSpace.HSLA
153
- }, _defineProperty(_ref7, ColorSpace.RGBA, "rgba(".concat(_r, ", ").concat(_g, ", ").concat(_b, ", ").concat(_a, ")")), _defineProperty(_ref7, ColorSpace.HSLA, color), _defineProperty(_ref7, ColorSpace.HEX, "#".concat(convert.hsl.hex([_h, _s2, _l]))), _ref7;
211
+ var _ref9 = convert.hsl.rgb([_h, _s2, _l]) || [0, 0, 0],
212
+ _ref10 = _slicedToArray(_ref9, 3),
213
+ _r = _ref10[0],
214
+ _g = _ref10[1],
215
+ _b = _ref10[2];
216
+
217
+ return _ref11 = {
218
+ valid: valid,
219
+ value: value,
220
+ keyword: convert.hsl.keyword([_h, _s2, _l]),
221
+ colorSpace: ColorSpace.HSL
222
+ }, _defineProperty(_ref11, ColorSpace.RGB, "rgba(".concat(_r, ", ").concat(_g, ", ").concat(_b, ", ").concat(_a, ")")), _defineProperty(_ref11, ColorSpace.HSL, value), _defineProperty(_ref11, ColorSpace.HEX, "#".concat(convert.hsl.hex([_h, _s2, _l]).toLowerCase())), _ref11;
154
223
  }
155
224
 
156
- var convertTo = HEX_REGEXP.test(color) ? convert.hex : convert.keyword;
157
- var rgb = convertTo.rgb(color.replace('#', ''));
158
- var hsl = rgb && convert.rgb.hsl(rgb);
159
- return _ref8 = {
160
- value: color,
161
- title: title || convert.rgb.keyword(rgb) || color,
225
+ var plain = value.replace('#', '');
226
+ var rgb = convert.keyword.rgb(plain) || convert.hex.rgb(plain);
227
+ var hsl = convert.rgb.hsl(rgb);
228
+ var mapped = value;
229
+ if (/[^#a-f0-9]/i.test(value)) mapped = plain;else if (HEX_REGEXP.test(value)) mapped = "#".concat(plain);
230
+
231
+ if (mapped.startsWith('#')) {
232
+ valid = HEX_REGEXP.test(mapped);
233
+ } else {
234
+ try {
235
+ convert.keyword.hex(mapped);
236
+ } catch (e) {
237
+ valid = false;
238
+ }
239
+ }
240
+
241
+ return _ref12 = {
242
+ valid: valid,
243
+ value: mapped,
244
+ keyword: convert.rgb.keyword(rgb),
162
245
  colorSpace: ColorSpace.HEX
163
- }, _defineProperty(_ref8, ColorSpace.RGBA, rgb && "rgba(".concat(rgb[0], ", ").concat(rgb[1], ", ").concat(rgb[2], ", 1)")), _defineProperty(_ref8, ColorSpace.HSLA, hsl && "hsla(".concat(hsl[0], ", ").concat(hsl[1], "%, ").concat(hsl[2], "%, 1)")), _defineProperty(_ref8, ColorSpace.HEX, color), _ref8;
246
+ }, _defineProperty(_ref12, ColorSpace.RGB, "rgba(".concat(rgb[0], ", ").concat(rgb[1], ", ").concat(rgb[2], ", 1)")), _defineProperty(_ref12, ColorSpace.HSL, "hsla(".concat(hsl[0], ", ").concat(hsl[1], "%, ").concat(hsl[2], "%, 1)")), _defineProperty(_ref12, ColorSpace.HEX, mapped), _ref12;
164
247
  };
165
248
 
166
- var Wrapper = styled.div({
167
- position: 'relative',
168
- maxWidth: 250
169
- });
170
- var PickerTooltip = styled(WithTooltip)({
171
- position: 'absolute',
172
- zIndex: 1,
173
- top: 4,
174
- left: 4
175
- });
176
- var TooltipContent = styled.div({
177
- margin: 5,
178
- '.react-colorful__saturation': {
179
- borderRadius: '4px 4px 0 0'
180
- },
181
- '.react-colorful__hue': {
182
- boxShadow: 'inset 0 0 0 1px rgb(0 0 0 / 5%)'
183
- },
184
- '.react-colorful__last-control': {
185
- borderRadius: '0 0 4px 4px'
186
- }
187
- });
188
- var Swatches = styled.div({
189
- display: 'grid',
190
- gridTemplateColumns: 'repeat(9, 15px)',
191
- gap: 7,
192
- padding: 3,
193
- marginTop: 5,
194
- width: 200
195
- });
196
- var Swatch = styled.div(function (_ref9) {
197
- var theme = _ref9.theme,
198
- active = _ref9.active,
199
- round = _ref9.round;
200
- return {
201
- width: round ? 16 : 15,
202
- height: round ? 16 : 15,
203
- margin: round ? 4 : 0,
204
- boxShadow: active ? "".concat(theme.appBorderColor, " 0 0 0 1px inset, ").concat(theme.color.mediumdark, "55 0 0 0 4px") : "".concat(theme.appBorderColor, " 0 0 0 1px inset"),
205
- borderRadius: round ? 16 : theme.appBorderRadius,
206
- cursor: 'pointer'
207
- };
208
- });
209
- var Input = styled(Form.Input)({
210
- width: '100%',
211
- paddingLeft: 30,
212
- paddingRight: 30
213
- });
214
- var ToggleIcon = styled(Icons)(function (_ref10) {
215
- var theme = _ref10.theme;
216
- return {
217
- position: 'absolute',
218
- zIndex: 1,
219
- top: 6,
220
- right: 7,
221
- width: 20,
222
- height: 20,
223
- padding: 4,
224
- cursor: 'pointer',
225
- color: theme.input.color
226
- };
227
- });
228
- var SmartPicker = /*#__PURE__*/React.memo(function (_ref11) {
229
- var value = _ref11.value,
230
- colorSpace = _ref11.colorSpace,
231
- props = _objectWithoutProperties(_ref11, ["value", "colorSpace"]);
232
-
233
- switch (colorSpace) {
234
- case ColorSpace.RGBA:
235
- return /*#__PURE__*/React.createElement(RgbaStringColorPicker, _extends({
236
- color: value
237
- }, props));
238
-
239
- case ColorSpace.HSLA:
240
- return /*#__PURE__*/React.createElement(HslaStringColorPicker, _extends({
241
- color: value
242
- }, props));
243
-
244
- default:
245
- return /*#__PURE__*/React.createElement(HexColorPicker, _extends({
246
- color: expandShorthand(value)
247
- }, props));
249
+ var getRealValue = function getRealValue(value, color, colorSpace) {
250
+ if (!value || !(color !== null && color !== void 0 && color.valid)) return fallbackColor[colorSpace];
251
+ if (colorSpace !== ColorSpace.HEX) return (color === null || color === void 0 ? void 0 : color[colorSpace]) || fallbackColor[colorSpace];
252
+
253
+ if (!color.hex.startsWith('#')) {
254
+ try {
255
+ return "#".concat(convert.keyword.hex(color.hex));
256
+ } catch (e) {
257
+ return fallbackColor.hex;
258
+ }
248
259
  }
249
- });
250
- export var ColorControl = function ColorControl(_ref12) {
251
- var name = _ref12.name,
252
- value = _ref12.value,
253
- _onChange = _ref12.onChange,
254
- onFocus = _ref12.onFocus,
255
- onBlur = _ref12.onBlur,
256
- presetColors = _ref12.presetColors,
257
- startOpen = _ref12.startOpen;
258
- var color = useMemo(function () {
259
- return parseValue(value);
260
- }, [value]);
261
- var update = useMemo(function () {
262
- return throttle(_onChange, 200);
263
- }, []);
264
260
 
265
- var _useState = useState((color === null || color === void 0 ? void 0 : color.colorSpace) || ColorSpace.HEX),
261
+ var short = color.hex.match(SHORTHEX_REGEXP);
262
+ if (!short) return HEX_REGEXP.test(color.hex) ? color.hex : fallbackColor.hex;
263
+
264
+ var _short$1$split = short[1].split(''),
265
+ _short$1$split2 = _slicedToArray(_short$1$split, 3),
266
+ r = _short$1$split2[0],
267
+ g = _short$1$split2[1],
268
+ b = _short$1$split2[2];
269
+
270
+ return "#".concat(r).concat(r).concat(g).concat(g).concat(b).concat(b);
271
+ };
272
+
273
+ var useColorInput = function useColorInput(initialValue, onChange) {
274
+ var _useState = useState(initialValue || ''),
266
275
  _useState2 = _slicedToArray(_useState, 2),
267
- colorSpace = _useState2[0],
268
- setColorSpace = _useState2[1];
276
+ value = _useState2[0],
277
+ setValue = _useState2[1];
269
278
 
270
- var currentValue = (color === null || color === void 0 ? void 0 : color[colorSpace]) || '';
271
- useEffect(function () {
272
- if (color) setColorSpace(color.colorSpace);
273
- }, [color === null || color === void 0 ? void 0 : color.colorSpace]);
279
+ var _useState3 = useState(function () {
280
+ return parseValue(value);
281
+ }),
282
+ _useState4 = _slicedToArray(_useState3, 2),
283
+ color = _useState4[0],
284
+ setColor = _useState4[1];
285
+
286
+ var _useState5 = useState((color === null || color === void 0 ? void 0 : color.colorSpace) || ColorSpace.HEX),
287
+ _useState6 = _slicedToArray(_useState5, 2),
288
+ colorSpace = _useState6[0],
289
+ setColorSpace = _useState6[1];
290
+
291
+ var realValue = useMemo(function () {
292
+ return getRealValue(value, color, colorSpace).toLowerCase();
293
+ }, [value, color, colorSpace]);
294
+ var updateValue = useCallback(function (update) {
295
+ var parsed = parseValue(update);
296
+ setValue((parsed === null || parsed === void 0 ? void 0 : parsed.value) || update || '');
297
+ if (!parsed) return;
298
+ setColor(parsed);
299
+ setColorSpace(parsed.colorSpace);
300
+ onChange(parsed.value);
301
+ }, []);
274
302
  var cycleColorSpace = useCallback(function () {
275
303
  var next = COLOR_SPACES.indexOf(colorSpace) + 1;
276
304
  if (next >= COLOR_SPACES.length) next = 0;
277
305
  setColorSpace(COLOR_SPACES[next]);
278
- }, [colorSpace]);
306
+ var update = (color === null || color === void 0 ? void 0 : color[COLOR_SPACES[next]]) || '';
307
+ setValue(update);
308
+ onChange(update);
309
+ }, [color, colorSpace]);
310
+ return {
311
+ value: value,
312
+ realValue: realValue,
313
+ updateValue: updateValue,
314
+ color: color,
315
+ colorSpace: colorSpace,
316
+ cycleColorSpace: cycleColorSpace
317
+ };
318
+ };
279
319
 
280
- var _useState3 = useState(function () {
281
- return (presetColors || []).map(parseValue).concat(color).filter(Boolean).slice(0, 27);
282
- }),
283
- _useState4 = _slicedToArray(_useState3, 2),
284
- presets = _useState4[0],
285
- setPresets = _useState4[1];
286
-
287
- var addPreset = useCallback(function (preset) {
288
- setPresets(function (arr) {
289
- return preset !== null && preset !== void 0 && preset.rgb && arr.every(function (item) {
290
- return item.rgb !== preset.rgb;
291
- }) ? arr.concat(preset).slice(-27) : arr;
320
+ var id = function id(value) {
321
+ return value.replace(/\s*/, '').toLowerCase();
322
+ };
323
+
324
+ var usePresets = function usePresets(presetColors, currentColor, colorSpace) {
325
+ var _useState7 = useState(currentColor !== null && currentColor !== void 0 && currentColor.valid ? [currentColor] : []),
326
+ _useState8 = _slicedToArray(_useState7, 2),
327
+ selectedColors = _useState8[0],
328
+ setSelectedColors = _useState8[1];
329
+
330
+ var presets = useMemo(function () {
331
+ var initialPresets = (presetColors || []).map(function (preset) {
332
+ if (typeof preset === 'string') return parseValue(preset);
333
+ if (preset.title) return Object.assign({}, parseValue(preset.color), {
334
+ keyword: preset.title
335
+ });
336
+ return parseValue(preset.color);
292
337
  });
293
- }, []);
338
+ return initialPresets.concat(selectedColors).filter(Boolean).slice(-27);
339
+ }, [presetColors, selectedColors]);
340
+ var addPreset = useCallback(function (color) {
341
+ if (!(color !== null && color !== void 0 && color.valid)) return;
342
+ if (presets.some(function (preset) {
343
+ return id(preset[colorSpace]) === id(color[colorSpace]);
344
+ })) return;
345
+ setSelectedColors(function (arr) {
346
+ return arr.concat(color);
347
+ });
348
+ }, [colorSpace, presets]);
349
+ return {
350
+ presets: presets,
351
+ addPreset: addPreset
352
+ };
353
+ };
354
+
355
+ export var ColorControl = function ColorControl(_ref13) {
356
+ var initialValue = _ref13.value,
357
+ onChange = _ref13.onChange,
358
+ onFocus = _ref13.onFocus,
359
+ onBlur = _ref13.onBlur,
360
+ presetColors = _ref13.presetColors,
361
+ startOpen = _ref13.startOpen;
362
+
363
+ var _useColorInput = useColorInput(initialValue, throttle(onChange, 200)),
364
+ value = _useColorInput.value,
365
+ realValue = _useColorInput.realValue,
366
+ updateValue = _useColorInput.updateValue,
367
+ color = _useColorInput.color,
368
+ colorSpace = _useColorInput.colorSpace,
369
+ cycleColorSpace = _useColorInput.cycleColorSpace;
370
+
371
+ var _usePresets = usePresets(presetColors, color, colorSpace),
372
+ presets = _usePresets.presets,
373
+ addPreset = _usePresets.addPreset;
374
+
375
+ var Picker = ColorPicker[colorSpace];
294
376
  return /*#__PURE__*/React.createElement(Wrapper, null, /*#__PURE__*/React.createElement(PickerTooltip, {
295
377
  trigger: "click",
296
378
  startOpen: startOpen,
297
379
  closeOnClick: true,
298
380
  onVisibilityChange: function onVisibilityChange() {
299
- return color && addPreset(color);
381
+ return addPreset(color);
300
382
  },
301
- tooltip: /*#__PURE__*/React.createElement(TooltipContent, null, /*#__PURE__*/React.createElement(SmartPicker, {
302
- value: currentValue,
303
- colorSpace: colorSpace,
304
- onChange: update,
383
+ tooltip: /*#__PURE__*/React.createElement(TooltipContent, null, /*#__PURE__*/React.createElement(Picker, {
384
+ color: realValue === 'transparent' ? '#000000' : realValue,
385
+ onChange: updateValue,
305
386
  onFocus: onFocus,
306
387
  onBlur: onBlur
307
388
  }), presets.length > 0 && /*#__PURE__*/React.createElement(Swatches, null, presets.map(function (preset) {
308
389
  return /*#__PURE__*/React.createElement(WithTooltip, {
309
390
  key: preset.value,
310
391
  hasChrome: false,
311
- tooltip: /*#__PURE__*/React.createElement(TooltipNote, {
312
- note: preset.title
392
+ tooltip: /*#__PURE__*/React.createElement(Note, {
393
+ note: preset.keyword || preset.value
313
394
  })
314
395
  }, /*#__PURE__*/React.createElement(Swatch, {
315
- style: {
316
- background: preset[colorSpace]
317
- },
318
- active: (color === null || color === void 0 ? void 0 : color.rgb) === preset.rgb,
396
+ value: preset[colorSpace],
397
+ active: color && id(preset[colorSpace]) === id(color[colorSpace]),
319
398
  onClick: function onClick() {
320
- return _onChange(preset[colorSpace]);
399
+ return updateValue(preset.value);
321
400
  }
322
401
  }));
323
402
  })))
324
403
  }, /*#__PURE__*/React.createElement(Swatch, {
325
- round: true,
404
+ value: realValue,
326
405
  style: {
327
- background: currentValue || '#000000'
406
+ margin: 4
328
407
  }
329
408
  })), /*#__PURE__*/React.createElement(Input, {
330
- value: currentValue,
409
+ value: value,
331
410
  onChange: function onChange(e) {
332
- return _onChange(e.target.value);
411
+ return updateValue(e.target.value);
333
412
  },
334
413
  onFocus: function onFocus(e) {
335
414
  return e.target.select();
@@ -28,9 +28,7 @@ var parseDate = function parseDate(value) {
28
28
  day = _value$split2[2];
29
29
 
30
30
  var result = new Date();
31
- result.setFullYear(parseInt(year, 10));
32
- result.setMonth(parseInt(month, 10) - 1);
33
- result.setDate(parseInt(day, 10));
31
+ result.setFullYear(parseInt(year, 10), parseInt(month, 10) - 1, parseInt(day, 10));
34
32
  return result;
35
33
  };
36
34
 
@@ -111,9 +109,7 @@ export var DateControl = function DateControl(_ref2) {
111
109
  var onDateChange = function onDateChange(e) {
112
110
  var parsed = parseDate(e.target.value);
113
111
  var result = new Date(value);
114
- result.setFullYear(parsed.getFullYear());
115
- result.setMonth(parsed.getMonth());
116
- result.setDate(parsed.getDate());
112
+ result.setFullYear(parsed.getFullYear(), parsed.getMonth(), parsed.getDate());
117
113
  var time = result.getTime();
118
114
  if (time) onChange(time);
119
115
  setValid(!!time);
@@ -1,3 +1,4 @@
1
+ import "core-js/modules/es.array.includes.js";
1
2
  import "core-js/modules/es.function.name.js";
2
3
 
3
4
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
@@ -17,7 +18,7 @@ import cloneDeep from 'lodash/cloneDeep';
17
18
  import React, { useCallback, useMemo, useState } from 'react';
18
19
  import { styled, useTheme } from '@storybook/theming'; // @ts-ignore
19
20
 
20
- import { JsonTree } from './react-editable-json-tree';
21
+ import { JsonTree, getObjectType } from './react-editable-json-tree';
21
22
  import { Form } from '../form';
22
23
  import { Icons } from '../icon/icon';
23
24
  import { IconButton } from '../bar/button';
@@ -294,7 +295,7 @@ export var ObjectControl = function ObjectControl(_ref7) {
294
295
  placeholder: "Enter JSON string",
295
296
  valid: parseError ? 'error' : null
296
297
  });
297
- return /*#__PURE__*/React.createElement(Wrapper, null, hasData && /*#__PURE__*/React.createElement(RawButton, {
298
+ return /*#__PURE__*/React.createElement(Wrapper, null, hasData && ['Object', 'Array'].includes(getObjectType(data)) && /*#__PURE__*/React.createElement(RawButton, {
298
299
  onClick: function onClick() {
299
300
  return setShowRaw(function (v) {
300
301
  return !v;
@@ -252,6 +252,7 @@ JsonTree.defaultProps = {
252
252
  fallback: null
253
253
  };
254
254
  export { JsonTree };
255
+ export { getObjectType };
255
256
  export { ADD_DELTA_TYPE };
256
257
  export { REMOVE_DELTA_TYPE };
257
258
  export { UPDATE_DELTA_TYPE };