@festo-ui/react 9.0.0-dev.681 → 9.0.0-dev.684

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,552 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.PREDEFINED_COLORS = void 0;
7
- var _react = require("react");
8
- var _reactDraggable = _interopRequireDefault(require("react-draggable"));
9
- var _classnames = _interopRequireDefault(require("classnames"));
10
- var _reactIcons = require("@festo-ui/react-icons");
11
- var _ColorHelper = _interopRequireDefault(require("./ColorHelper.js"));
12
- var _useId = _interopRequireDefault(require("../../helper/useId.js"));
13
- var _jsxRuntime = require("react/jsx-runtime");
14
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
- /* eslint-disable jsx-a11y/no-static-element-interactions */
16
- /* eslint-disable jsx-a11y/click-events-have-key-events */
17
-
18
- const PREDEFINED_COLORS = exports.PREDEFINED_COLORS = ["#0091dc", "#333333", "#ffffff", "#f0f2f3", "#e2e5e8", "#d3d8dd", "#b6bec6", "#80ca3d", "#ffd600", "#ff9600", "#d50000"];
19
- function ColorPicker(_ref) {
20
- let {
21
- palette = PREDEFINED_COLORS,
22
- useAlpha = false,
23
- alpha = 100,
24
- paletteOnly,
25
- color = "#FFFFFF",
26
- onChange,
27
- onAlphaChange,
28
- className
29
- } = _ref;
30
- const baseSize = 184;
31
- const initialRgb = _ColorHelper.default.hexToRgb(color);
32
- const defaultHsv = {
33
- h: 0,
34
- s: 0,
35
- v: 1
36
- };
37
- const initialHsv = initialRgb ? _ColorHelper.default.rgbToHsv(initialRgb) || defaultHsv : defaultHsv;
38
- const [hsv, setHsv] = (0, _react.useState)(initialHsv);
39
- const [innerColor, setInnerColor] = (0, _react.useState)(color);
40
- const [redInput, setRedInput] = (0, _react.useState)(initialRgb?.r || 255);
41
- const [greenInput, setGreenInput] = (0, _react.useState)(initialRgb?.g || 255);
42
- const [blueInput, setBlueInput] = (0, _react.useState)(initialRgb?.b || 255);
43
- const [hexInputColor, setHexInputColor] = (0, _react.useState)(color);
44
- const [inputType, setInputType] = (0, _react.useState)("RGB");
45
- const [selectOpen, setSelectOpen] = (0, _react.useState)(false);
46
- const [innerAlpha, setInnerAlpha] = (0, _react.useState)(alpha);
47
- const [changeColor, setChangeColor] = (0, _react.useState)(true);
48
- const redId = (0, _useId.default)();
49
- const greenId = (0, _useId.default)();
50
- const blueId = (0, _useId.default)();
51
- const hexId = (0, _useId.default)();
52
- const alphaId = (0, _useId.default)();
53
- const gradientKnobRef = (0, _react.useRef)(null);
54
- const hueKnobRef = (0, _react.useRef)(null);
55
- const alphaKnobRef = (0, _react.useRef)(null);
56
- function limitAlpha() {
57
- if (innerAlpha > 100) {
58
- return 100;
59
- }
60
- if (innerAlpha < 0) {
61
- return 0;
62
- }
63
- return innerAlpha;
64
- }
65
- const hueKnobOffset = hsv.h * baseSize - 14 - baseSize;
66
- const saturationKnobOffset = hsv.s * baseSize;
67
- const valueKnobOffset = (1 - hsv.v) * baseSize;
68
- const alphaKnobOffset = (100 - limitAlpha()) / 100 * baseSize;
69
- function getSaturationGradient() {
70
- return `linear-gradient(to right, white, hsl(${hsv.h * 360}, 100%, 50%) )`;
71
- }
72
- function updateInputs(hexColor) {
73
- setHexInputColor(hexColor);
74
- const rgb = _ColorHelper.default.hexToRgb(hexColor);
75
- if (rgb) {
76
- setRedInput(rgb.r);
77
- setGreenInput(rgb.g);
78
- setBlueInput(rgb.b);
79
- }
80
- }
81
- (0, _react.useEffect)(() => {
82
- if (onChange) {
83
- onChange(innerColor);
84
- }
85
- }, [onChange, innerColor]);
86
- (0, _react.useEffect)(() => {
87
- if (onAlphaChange && innerAlpha) {
88
- onAlphaChange(innerAlpha);
89
- }
90
- }, [onAlphaChange, innerAlpha]);
91
- const updateColorHsv = (0, _react.useCallback)((_hsv, _changeColor) => {
92
- const rgb = _ColorHelper.default.hsvToRgb(_hsv);
93
- const hex = _ColorHelper.default.rgbToHex(rgb);
94
- if (_changeColor) {
95
- setInnerColor(hex);
96
- }
97
- updateInputs(hex);
98
- }, []);
99
- (0, _react.useEffect)(() => {
100
- updateColorHsv(hsv, changeColor);
101
- }, [updateColorHsv, hsv, changeColor]);
102
- function updateGradient(x, y) {
103
- setChangeColor(true);
104
- setHsv(prevHsv => ({
105
- ...prevHsv,
106
- s: Math.floor(x) / baseSize,
107
- v: (baseSize - y) / baseSize
108
- }));
109
- }
110
- function onGradientDrag(e, data) {
111
- e.preventDefault();
112
- updateGradient(data.x, data.y);
113
- }
114
- function updateHue(y) {
115
- const newHue = y / baseSize;
116
- setChangeColor(true);
117
- setHsv(prevHsv => ({
118
- ...prevHsv,
119
- h: newHue
120
- }));
121
- }
122
- function onHueDrag(e, data) {
123
- e.preventDefault();
124
- updateHue(data.y + 14 + baseSize);
125
- }
126
- function updateAlpha(y) {
127
- setInnerAlpha(100 - Math.round(y / baseSize * 100));
128
- }
129
- function onAlphaDrag(e, data) {
130
- e.preventDefault();
131
- updateAlpha(data.y);
132
- }
133
- const updateColorRgb = newColor => {
134
- const currentColor = newColor || "#FFFFFF";
135
- updateInputs(currentColor);
136
- const rgb = _ColorHelper.default.hexToRgb(currentColor);
137
- setChangeColor(false);
138
- if (rgb) {
139
- const newHsv = _ColorHelper.default.rgbToHsv(rgb);
140
- if (newHsv) {
141
- setHsv(newHsv);
142
- }
143
- }
144
- setInnerColor(newColor);
145
- };
146
- const isHexColor = _color => /^#[0-9A-F]{6}$/i.test(_color);
147
- function onHexInput(event) {
148
- const newHexInputColor = event.target.value;
149
- setHexInputColor(newHexInputColor.toUpperCase());
150
- if (isHexColor(newHexInputColor)) {
151
- updateColorRgb(newHexInputColor.toUpperCase());
152
- }
153
- }
154
- function onHexBlur() {
155
- if (!isHexColor(hexInputColor) && innerColor) {
156
- setHexInputColor(innerColor);
157
- updateColorRgb(innerColor);
158
- }
159
- }
160
- function onRgbBlur(channel) {
161
- let currentInput = "";
162
- switch (channel) {
163
- case "r":
164
- currentInput = redInput;
165
- break;
166
- case "g":
167
- currentInput = greenInput;
168
- break;
169
- case "b":
170
- currentInput = blueInput;
171
- break;
172
- default:
173
- // no other value possible
174
- }
175
- if (typeof currentInput !== "number" || typeof currentInput === "number" && currentInput > 255 && currentInput < 0) {
176
- const rgb = _ColorHelper.default.hexToRgb(innerColor || "#FFFFFF");
177
- if (rgb) {
178
- const newHexColor = _ColorHelper.default.rgbToHex(rgb);
179
- updateInputs(newHexColor);
180
- }
181
- }
182
- }
183
- function onRgbInput(_value, channel) {
184
- const numberValue = Number.parseInt(_value, 10);
185
- switch (channel) {
186
- case "r":
187
- setRedInput(_value);
188
- break;
189
- case "g":
190
- setGreenInput(_value);
191
- break;
192
- case "b":
193
- setBlueInput(_value);
194
- break;
195
- default:
196
- // no other value possible
197
- }
198
- if (numberValue <= 255 && numberValue >= 0) {
199
- const rgb = _ColorHelper.default.hexToRgb(innerColor || "#FFFFFF");
200
- if (rgb) {
201
- rgb[channel] = numberValue;
202
- const newHexColor = _ColorHelper.default.rgbToHex(rgb);
203
- setInnerColor(newHexColor);
204
- updateColorRgb(newHexColor);
205
- }
206
- }
207
- }
208
- function onAlphaInput(_alpha) {
209
- const numberValue = Number.parseInt(_alpha, 10);
210
- setInnerAlpha(numberValue);
211
- }
212
- function onAlphaBlur() {
213
- setInnerAlpha(limitAlpha());
214
- }
215
- const onChangeType = type => {
216
- setInputType(type);
217
- setSelectOpen(false);
218
- };
219
- function onRemoveColor() {
220
- if (useAlpha) {
221
- setInnerAlpha(0);
222
- } else {
223
- updateColorRgb();
224
- }
225
- }
226
- return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
227
- className: (0, _classnames.default)("fwe-color-picker", {
228
- "fwe-alpha-active": useAlpha
229
- }, className),
230
- children: [!paletteOnly && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
231
- className: "fwe-d-flex",
232
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
233
- className: "fwe-gradient-picker",
234
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
235
- className: "fwe-saturation-gradient",
236
- style: {
237
- backgroundImage: getSaturationGradient()
238
- }
239
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
240
- "aria-label": "brightness-gradient",
241
- onClick: _ref2 => {
242
- let {
243
- nativeEvent: {
244
- offsetX,
245
- offsetY
246
- }
247
- } = _ref2;
248
- return updateGradient(offsetX, offsetY);
249
- },
250
- className: "fwe-brightness-gradient"
251
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactDraggable.default, {
252
- position: {
253
- x: saturationKnobOffset,
254
- y: valueKnobOffset
255
- },
256
- onDrag: (e, data) => onGradientDrag(e, data),
257
- bounds: "parent",
258
- nodeRef: gradientKnobRef,
259
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
260
- className: "fwe-knob",
261
- ref: gradientKnobRef
262
- })
263
- })]
264
- }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
265
- className: "fwe-hue-picker",
266
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
267
- className: "fwe-picker-background",
268
- onClick: _ref3 => {
269
- let {
270
- nativeEvent: {
271
- offsetY
272
- }
273
- } = _ref3;
274
- return updateHue(offsetY);
275
- }
276
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactDraggable.default, {
277
- position: {
278
- x: 3,
279
- y: hueKnobOffset
280
- },
281
- onDrag: (e, data) => onHueDrag(e, data),
282
- bounds: "parent",
283
- nodeRef: hueKnobRef,
284
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
285
- className: "fwe-knob",
286
- style: {
287
- background: `hsl(${hsv.h * 360}, 100%, 50%)`
288
- },
289
- ref: hueKnobRef
290
- })
291
- })]
292
- }), useAlpha && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
293
- className: "fwe-alpha-picker",
294
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("svg", {
295
- className: "fwe-no-color-pattern",
296
- version: "1.1",
297
- xmlns: "http://www.w3.org/2000/svg",
298
- xmlnsXlink: "http://www.w3.org/1999/xlink",
299
- id: "canvas1",
300
- width: "8",
301
- height: "184",
302
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("defs", {
303
- children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("pattern", {
304
- id: "bwsquare2px",
305
- width: "4",
306
- height: "4",
307
- patternUnits: "userSpaceOnUse",
308
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
309
- x: "0",
310
- y: "0",
311
- width: "2",
312
- height: "2",
313
- stroke: "none",
314
- fill: "#ffffff"
315
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
316
- x: "2",
317
- y: "0",
318
- width: "2",
319
- height: "2",
320
- stroke: "none",
321
- fill: "#e2e5e8"
322
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
323
- x: "0",
324
- y: "2",
325
- width: "2",
326
- height: "2",
327
- stroke: "none",
328
- fill: "#e2e5e8"
329
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
330
- x: "2",
331
- y: "2",
332
- width: "2",
333
- height: "2",
334
- stroke: "none",
335
- fill: "#ffffff"
336
- })]
337
- })
338
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
339
- x: "0",
340
- y: "0",
341
- rx: "4",
342
- ry: "4",
343
- width: "8",
344
- height: "184",
345
- fill: "url(#bwsquare2px)",
346
- strokeWidth: "0"
347
- })]
348
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
349
- onClick: _ref4 => {
350
- let {
351
- nativeEvent: {
352
- offsetY
353
- }
354
- } = _ref4;
355
- return updateAlpha(offsetY);
356
- },
357
- className: "fwe-picker-background",
358
- style: {
359
- backgroundImage: `linear-gradient( ${innerColor}, transparent)`
360
- }
361
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactDraggable.default, {
362
- position: {
363
- x: 3,
364
- y: alphaKnobOffset
365
- },
366
- onDrag: (e, data) => onAlphaDrag(e, data),
367
- bounds: "parent",
368
- nodeRef: alphaKnobRef,
369
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
370
- className: "fwe-knob",
371
- ref: alphaKnobRef
372
- })
373
- })]
374
- })]
375
- }), !paletteOnly && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
376
- className: "fwe-mt-s",
377
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
378
- className: "fwe-type-select",
379
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
380
- className: "fwe-type-indicator",
381
- onClick: () => setSelectOpen(prevOpen => !prevOpen),
382
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
383
- className: "fwe-input-type",
384
- children: inputType
385
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactIcons.IconCollapse, {
386
- className: "fwe-ml-s"
387
- })]
388
- }), selectOpen && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
389
- className: "fwe-popover",
390
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
391
- className: (0, _classnames.default)("fwe-type-item", {
392
- "fwe-selected": inputType === "HEX"
393
- }),
394
- onClick: () => onChangeType("HEX"),
395
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactIcons.IconCheckSmall, {}), " HEX"]
396
- }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
397
- className: (0, _classnames.default)("fwe-type-item", {
398
- "fwe-selected": inputType === "RGB"
399
- }),
400
- onClick: () => onChangeType("RGB"),
401
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_reactIcons.IconCheckSmall, {}), " RGB"]
402
- })]
403
- })]
404
- }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
405
- className: "fwe-d-flex",
406
- children: [inputType === "HEX" && /*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
407
- "aria-label": "Hexadecimal Color",
408
- className: "fwe-input-text fwe-hex-input",
409
- htmlFor: hexId,
410
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
411
- type: "text",
412
- value: hexInputColor,
413
- onBlur: onHexBlur,
414
- onChange: onHexInput,
415
- id: hexId
416
- })
417
- }), inputType === "RGB" && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
418
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
419
- "aria-label": "RGB Color Red",
420
- className: "fwe-input-text fwe-red-input",
421
- htmlFor: redId,
422
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
423
- type: "text",
424
- value: redInput,
425
- onBlur: () => onRgbBlur("r"),
426
- onChange: e => onRgbInput(e.target.value, "r"),
427
- id: redId
428
- })
429
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
430
- "aria-label": "RGB Color Green",
431
- className: "fwe-input-text fwe-green-input",
432
- htmlFor: greenId,
433
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
434
- type: "text",
435
- value: greenInput,
436
- onBlur: () => onRgbBlur("g"),
437
- onChange: e => onRgbInput(e.target.value, "g"),
438
- id: greenId
439
- })
440
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
441
- "aria-label": "RGB Color Blue",
442
- className: "fwe-input-text fwe-blue-input",
443
- htmlFor: blueId,
444
- children: /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
445
- type: "text",
446
- value: blueInput,
447
- onBlur: () => onRgbBlur("b"),
448
- onChange: e => onRgbInput(e.target.value, "b"),
449
- id: blueId
450
- })
451
- })]
452
- }), useAlpha && /*#__PURE__*/(0, _jsxRuntime.jsx)("label", {
453
- "aria-label": "Alpha Channel",
454
- className: "fwe-input-text fwe-alpha-input fwe-ml-auto",
455
- htmlFor: alphaId,
456
- children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("span", {
457
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
458
- type: "number",
459
- min: "0",
460
- max: "100",
461
- value: innerAlpha,
462
- onBlur: () => onAlphaBlur(),
463
- onChange: e => onAlphaInput(e.target.value),
464
- id: alphaId
465
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
466
- className: "fwe-percent-char",
467
- children: "%"
468
- })]
469
- })
470
- })]
471
- })]
472
- }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
473
- className: "fwe-mt-xs fwe-color-grid",
474
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
475
- className: "fwe-remove-color-button",
476
- onClick: onRemoveColor,
477
- children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("svg", {
478
- className: "fwe-no-color-pattern",
479
- version: "1.1",
480
- xmlns: "http://www.w3.org/2000/svg",
481
- xmlnsXlink: "http://www.w3.org/1999/xlink",
482
- id: "canvas1",
483
- width: "18",
484
- height: "18",
485
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("defs", {
486
- children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("pattern", {
487
- id: "bwsquare2px",
488
- width: "4",
489
- height: "4",
490
- patternUnits: "userSpaceOnUse",
491
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
492
- x: "0",
493
- y: "0",
494
- width: "2",
495
- height: "2",
496
- stroke: "none",
497
- fill: "#ffffff"
498
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
499
- x: "2",
500
- y: "0",
501
- width: "2",
502
- height: "2",
503
- stroke: "none",
504
- fill: "#e2e5e8"
505
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
506
- x: "0",
507
- y: "2",
508
- width: "2",
509
- height: "2",
510
- stroke: "none",
511
- fill: "#e2e5e8"
512
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
513
- x: "2",
514
- y: "2",
515
- width: "2",
516
- height: "2",
517
- stroke: "none",
518
- fill: "#ffffff"
519
- })]
520
- })
521
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)("rect", {
522
- x: "0",
523
- y: "0",
524
- rx: "0",
525
- ry: "0",
526
- width: "18",
527
- height: "18",
528
- fill: "url(#bwsquare2px)",
529
- strokeWidth: "0"
530
- })]
531
- }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactIcons.IconCheckSmall, {
532
- className: (0, _classnames.default)({
533
- "fwe-color-text": !innerColor
534
- })
535
- })]
536
- }), palette.map(colorItem => {
537
- const itemClasses = (0, _classnames.default)("fwe-color-item", {
538
- "fwe-white-item": colorItem.toUpperCase() === "#FFFFFF"
539
- });
540
- return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
541
- className: itemClasses,
542
- style: {
543
- background: colorItem
544
- },
545
- onClick: () => updateColorRgb(colorItem.toUpperCase()),
546
- children: innerColor === colorItem.toUpperCase() && /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactIcons.IconCheckSmall, {})
547
- }, colorItem);
548
- })]
549
- })]
550
- });
551
- }
552
- var _default = exports.default = ColorPicker;