@zendeskgarden/react-colorpickers 9.0.0-next.2 → 9.0.0-next.21

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.
Files changed (67) hide show
  1. package/README.md +72 -2
  2. package/dist/esm/elements/ColorPicker/ColorWell.js +110 -0
  3. package/dist/esm/elements/ColorPicker/index.js +237 -0
  4. package/dist/esm/elements/ColorPicker/reducer.js +275 -0
  5. package/dist/esm/elements/ColorPickerDialog/index.js +153 -0
  6. package/dist/esm/elements/ColorSwatch/index.js +149 -0
  7. package/dist/esm/elements/ColorSwatchDialog/index.js +179 -0
  8. package/dist/esm/index.js +10 -0
  9. package/dist/esm/node_modules/@zendeskgarden/svg-icons/src/12/check-sm-fill.svg.js +29 -0
  10. package/dist/esm/node_modules/@zendeskgarden/svg-icons/src/16/chevron-down-stroke.svg.js +25 -0
  11. package/dist/esm/styled/ColorPicker/StyledAlphaRange.js +42 -0
  12. package/dist/esm/styled/ColorPicker/StyledColorPicker.js +25 -0
  13. package/dist/esm/styled/ColorPicker/StyledColorWell.js +36 -0
  14. package/dist/esm/styled/ColorPicker/StyledColorWellThumb.js +51 -0
  15. package/dist/esm/styled/ColorPicker/StyledHexField.js +24 -0
  16. package/dist/esm/styled/ColorPicker/StyledHueRange.js +23 -0
  17. package/dist/esm/styled/ColorPicker/StyledInput.js +24 -0
  18. package/dist/esm/styled/ColorPicker/StyledInputGroup.js +22 -0
  19. package/dist/esm/styled/ColorPicker/StyledLabel.js +23 -0
  20. package/dist/esm/styled/ColorPicker/StyledPreview.js +39 -0
  21. package/dist/esm/styled/ColorPicker/StyledRGBAField.js +32 -0
  22. package/dist/esm/styled/ColorPicker/StyledSliderGroup.js +22 -0
  23. package/dist/esm/styled/ColorPicker/StyledSliders.js +45 -0
  24. package/dist/esm/styled/ColorPickerDialog/StyledButton.js +22 -0
  25. package/dist/esm/styled/ColorPickerDialog/StyledButtonPreview.js +52 -0
  26. package/dist/esm/styled/ColorPickerDialog/StyledTooltipBody.js +23 -0
  27. package/dist/esm/styled/ColorPickerDialog/StyledTooltipModal.js +23 -0
  28. package/dist/esm/styled/ColorSwatch/StyledCell.js +22 -0
  29. package/dist/esm/styled/ColorSwatch/StyledColorSwatch.js +22 -0
  30. package/dist/esm/styled/ColorSwatch/StyledColorSwatchInput.js +22 -0
  31. package/dist/esm/styled/ColorSwatch/StyledColorSwatchLabel.js +53 -0
  32. package/dist/esm/styled/ColorSwatch/StyledIcon.js +24 -0
  33. package/dist/esm/styled/common/StyledRange.js +160 -0
  34. package/dist/esm/utils/conversion.js +34 -0
  35. package/dist/esm/utils/saturation.js +64 -0
  36. package/dist/esm/utils/validation.js +12 -0
  37. package/dist/index.cjs.js +267 -243
  38. package/dist/typings/elements/{Colorpicker → ColorPicker}/index.d.ts +2 -2
  39. package/dist/typings/elements/{ColorpickerDialog → ColorPickerDialog}/index.d.ts +2 -2
  40. package/dist/typings/index.d.ts +3 -3
  41. package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledAlphaRange.d.ts +2 -2
  42. package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledColorPicker.d.ts +1 -1
  43. package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledColorWell.d.ts +1 -1
  44. package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledPreview.d.ts +3 -3
  45. package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledSliders.d.ts +3 -2
  46. package/dist/typings/styled/{ColorpickerDialog → ColorPickerDialog}/StyledButton.d.ts +0 -1
  47. package/dist/typings/styled/{ColorpickerDialog → ColorPickerDialog}/StyledButtonPreview.d.ts +2 -2
  48. package/dist/typings/styled/ColorSwatch/StyledColorSwatchLabel.d.ts +3 -3
  49. package/dist/typings/styled/common/StyledRange.d.ts +2 -2
  50. package/dist/typings/styled/index.d.ts +17 -17
  51. package/dist/typings/styled/types.d.ts +12 -0
  52. package/dist/typings/types/index.d.ts +2 -2
  53. package/package.json +12 -12
  54. package/dist/index.esm.js +0 -1612
  55. package/dist/typings/styled/common/checkeredBackground.d.ts +0 -8
  56. /package/dist/typings/elements/{Colorpicker → ColorPicker}/ColorWell.d.ts +0 -0
  57. /package/dist/typings/elements/{Colorpicker → ColorPicker}/reducer.d.ts +0 -0
  58. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledColorWellThumb.d.ts +0 -0
  59. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledHexField.d.ts +0 -0
  60. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledHueRange.d.ts +0 -0
  61. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledInput.d.ts +0 -0
  62. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledInputGroup.d.ts +0 -0
  63. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledLabel.d.ts +0 -0
  64. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledRGBAField.d.ts +0 -0
  65. /package/dist/typings/styled/{Colorpicker → ColorPicker}/StyledSliderGroup.d.ts +0 -0
  66. /package/dist/typings/styled/{ColorpickerDialog → ColorPickerDialog}/StyledTooltipBody.d.ts +0 -0
  67. /package/dist/typings/styled/{ColorpickerDialog → ColorPickerDialog}/StyledTooltipModal.d.ts +0 -0
package/dist/index.esm.js DELETED
@@ -1,1612 +0,0 @@
1
- /**
2
- * Copyright Zendesk, Inc.
3
- *
4
- * Use of this source code is governed under the Apache License, Version 2.0
5
- * found at http://www.apache.org/licenses/LICENSE-2.0.
6
- */
7
-
8
- import * as React from 'react';
9
- import React__default, { useContext, useRef, useState, useEffect, useMemo, useCallback, forwardRef, useReducer, cloneElement, Children } from 'react';
10
- import PropTypes from 'prop-types';
11
- import { Range, Field, Label, Input } from '@zendeskgarden/react-forms';
12
- import throttle from 'lodash.throttle';
13
- import styled, { ThemeContext } from 'styled-components';
14
- import { retrieveComponentStyles, DEFAULT_THEME, getColor, useText, useDocument } from '@zendeskgarden/react-theming';
15
- import { stripUnit, math, rgba, hsl, parseToRgb, readableColor, parseToHsl, rgb } from 'polished';
16
- import { Button } from '@zendeskgarden/react-buttons';
17
- import { TooltipModal, PLACEMENT } from '@zendeskgarden/react-modals';
18
- import isEqual from 'lodash.isequal';
19
- import { composeEventHandlers, useId } from '@zendeskgarden/container-utilities';
20
- import { mergeRefs } from 'react-merge-refs';
21
- import { useGrid } from '@zendeskgarden/container-grid';
22
- import { Tooltip } from '@zendeskgarden/react-tooltips';
23
-
24
- function _extends$2() {
25
- _extends$2 = Object.assign ? Object.assign.bind() : function (target) {
26
- for (var i = 1; i < arguments.length; i++) {
27
- var source = arguments[i];
28
- for (var key in source) {
29
- if (Object.prototype.hasOwnProperty.call(source, key)) {
30
- target[key] = source[key];
31
- }
32
- }
33
- }
34
- return target;
35
- };
36
- return _extends$2.apply(this, arguments);
37
- }
38
-
39
- function hslToHsv(h, s, l) {
40
- let saturation = s;
41
- saturation *= (l < 50 ? l : 100 - l) / 100;
42
- const v = l + saturation;
43
- return {
44
- h,
45
- s: v === 0 ? 0 : 2 * saturation / v * 100,
46
- v
47
- };
48
- }
49
- function hsvToHsl(h, s, v) {
50
- let saturation = s;
51
- let value = v;
52
- saturation /= 100;
53
- value /= 100;
54
- let l = (2 - saturation) * value;
55
- let sl = saturation * value;
56
- sl /= l <= 1 ? l : 2 - l;
57
- sl = sl || 0;
58
- l /= 2;
59
- return {
60
- h,
61
- s: sl * 100,
62
- l: l * 100
63
- };
64
- }
65
-
66
- const getSaturationLightness = (element, x, y, rtl) => {
67
- const {
68
- width,
69
- height
70
- } = element.getBoundingClientRect();
71
- let left = x - (element.getBoundingClientRect().left + window.pageXOffset);
72
- let top = y - (element.getBoundingClientRect().top + window.pageYOffset);
73
- if (left < 0) {
74
- left = 0;
75
- } else if (left > width) {
76
- left = width;
77
- }
78
- if (top < 0) {
79
- top = 0;
80
- } else if (top > height) {
81
- top = height;
82
- }
83
- if (rtl) {
84
- left = width - left;
85
- }
86
- const saturation = left / width;
87
- const bright = 1 - top / height;
88
- return {
89
- saturation,
90
- bright
91
- };
92
- };
93
- function getNextHsv(e, hue, container, rtl) {
94
- const {
95
- saturation,
96
- bright
97
- } = getSaturationLightness(container, e.pageX, e.pageY, rtl);
98
- return {
99
- h: hue,
100
- s: saturation,
101
- v: bright
102
- };
103
- }
104
- const getThumbPosition = (x, y, rtl, container) => {
105
- if (container.current) {
106
- const {
107
- saturation,
108
- bright
109
- } = getSaturationLightness(container.current, x, y, rtl);
110
- const topFromMouse = (1 - bright) * 100;
111
- const leftFromMouse = rtl ? 100 - saturation * 100 : saturation * 100;
112
- return {
113
- topFromMouse,
114
- leftFromMouse
115
- };
116
- }
117
- return {
118
- topFromMouse: 0,
119
- leftFromMouse: 0
120
- };
121
- };
122
-
123
- const COMPONENT_ID$m = 'colorpickers.colorpicker';
124
- const getColorPickerWidth = props => {
125
- return props.isOpaque ? 268 : 312;
126
- };
127
- const StyledColorPicker = styled.div.attrs({
128
- 'data-garden-id': COMPONENT_ID$m,
129
- 'data-garden-version': '9.0.0-next.2'
130
- }).withConfig({
131
- displayName: "StyledColorPicker",
132
- componentId: "sc-1donyl9-0"
133
- })(["width:", "px;min-width:", "px;", ";"], getColorPickerWidth, getColorPickerWidth, props => retrieveComponentStyles(COMPONENT_ID$m, props));
134
- StyledColorPicker.defaultProps = {
135
- theme: DEFAULT_THEME
136
- };
137
-
138
- const COMPONENT_ID$l = 'colorpickers.colorpicker_range';
139
- const thumbStyles = function (styles) {
140
- let modifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
141
- return `
142
- &${modifier}::-moz-range-thumb {
143
- ${styles}
144
- }
145
-
146
- &${modifier}::-ms-thumb {
147
- ${styles}
148
- }
149
-
150
- &${modifier}::-webkit-slider-thumb {
151
- ${styles}
152
- }
153
- `;
154
- };
155
- const trackStyles = function (styles) {
156
- let modifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
157
- return `
158
- &${modifier}::-moz-range-track {
159
- ${styles}
160
- }
161
-
162
- &${modifier}::-ms-track {
163
- ${styles}
164
- }
165
-
166
- &${modifier}::-webkit-slider-runnable-track {
167
- ${styles}
168
- }
169
- `;
170
- };
171
- const trackLowerStyles = function (styles) {
172
- let modifier = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
173
- return `
174
- &${modifier}::-moz-range-progress {
175
- ${styles}
176
- }
177
-
178
- &${modifier}::-ms-fill-lower {
179
- ${styles}
180
- }
181
- `;
182
- };
183
- const colorStyles$1 = props => {
184
- const thumbBackgroundColor = getColor('neutralHue', 100, props.theme);
185
- const thumbBorderColor = thumbBackgroundColor;
186
- const thumbActiveBackgroundColor = getColor('neutralHue', 200, props.theme);
187
- const thumbActiveBorderColor = getColor('primaryHue', 600, props.theme);
188
- const thumbFocusBorderColor = getColor('primaryHue', 400, props.theme);
189
- const thumbHoverBackgroundColor = thumbActiveBackgroundColor;
190
- const thumbHoverBorderColor = thumbHoverBackgroundColor;
191
- return `
192
- border-color: ${props.isOpaque && props.theme.colors.background};
193
-
194
- ${trackStyles(`
195
- background-color: transparent;
196
- `)}
197
-
198
- ${thumbStyles(`
199
- border-color: ${thumbBorderColor};
200
- background-color: ${thumbBackgroundColor};
201
- `)}
202
-
203
- ${trackLowerStyles(`
204
- background-color: transparent;
205
- `)}
206
-
207
- ${thumbStyles(`
208
- border-color: ${thumbHoverBorderColor};
209
- background-color: ${thumbHoverBackgroundColor};
210
- `, ':hover')}
211
-
212
- ${thumbStyles(`
213
- background-color: ${thumbBackgroundColor};
214
- border-color: ${thumbFocusBorderColor};
215
- `, '[data-garden-focus-visible="true"]')}
216
-
217
- ${thumbStyles(`
218
- border-color: ${thumbActiveBorderColor};
219
- background-color: ${thumbActiveBackgroundColor}
220
- `, ':active')}
221
- `;
222
- };
223
- const getThumbSize = props => props.theme.space.base * (props.isOpaque ? 6 : 4);
224
- const getTrackHeight = props => props.theme.space.base * (props.isOpaque ? 6 : 3);
225
- const getTrackMargin = props => (getThumbSize(props) - getTrackHeight(props)) / 2 + stripUnit(props.theme.shadowWidths.md);
226
- const sizeStyles$2 = props => {
227
- const thumbSize = getThumbSize(props);
228
- const trackHeight = getTrackHeight(props);
229
- const trackMargin = getTrackMargin(props);
230
- const thumbMargin = (trackHeight - thumbSize) / 2;
231
- const trackOffset = props.theme.space.base * (props.isOpaque ? -2 : -1);
232
- const height = props.isOpaque ? trackHeight : trackMargin * 2 + trackHeight;
233
- let marginHorizontal;
234
- let border;
235
- let borderRadius;
236
- if (props.isOpaque) {
237
- marginHorizontal = `-${trackMargin}px`;
238
- border = `${trackMargin}px ${props.theme.borderStyles.solid}`;
239
- borderRadius = `${trackMargin + stripUnit(props.theme.shadowWidths.md)}px`;
240
- }
241
- return `
242
- /* stylelint-disable-next-line declaration-no-important */
243
- margin-top: 0 !important;
244
- margin-${props.theme.rtl ? 'right' : 'left'}: ${marginHorizontal};
245
- height: ${height}px; /* [1] */
246
- box-sizing: content-box; /* [2] */
247
- border: ${border};
248
- border-radius: ${borderRadius};
249
-
250
- ${trackStyles(`
251
- margin: ${trackMargin}px ${trackOffset}px;
252
- height: ${trackHeight}px;
253
- `)}
254
-
255
- ${thumbStyles(`
256
- margin: ${thumbMargin}px 0;
257
- border-width: ${math(`${props.theme.borderWidths.sm} * 2`)};
258
- height: ${thumbSize}px;
259
- width: ${thumbSize}px;
260
- `)};
261
- `;
262
- };
263
- const StyledRange = styled(Range).attrs({
264
- 'data-garden-id': COMPONENT_ID$l,
265
- 'data-garden-version': '9.0.0-next.2',
266
- hasLowerTrack: false
267
- }).withConfig({
268
- displayName: "StyledRange",
269
- componentId: "sc-ug3wi9-0"
270
- })(["", ";", " ", ";", ";"], sizeStyles$2, trackStyles(`
271
- border-radius: 0;
272
- `), colorStyles$1, props => retrieveComponentStyles(COMPONENT_ID$l, props));
273
- StyledRange.defaultProps = {
274
- theme: DEFAULT_THEME
275
- };
276
-
277
- const COMPONENT_ID$k = 'colorpickers.colorpicker_hue';
278
- const StyledHueRange = styled(StyledRange).attrs({
279
- 'data-garden-id': COMPONENT_ID$k,
280
- 'data-garden-version': '9.0.0-next.2'
281
- }).withConfig({
282
- displayName: "StyledHueRange",
283
- componentId: "sc-13ly7p-0"
284
- })(["background:linear-gradient( to ", ",#f00 0%,#ff0 17%,#0f0 33%,#0ff 50%,#00f 67%,#f0f 83%,#f00 100% ) no-repeat;background-position:", ";background-size:100% ", "px;"], props => props.theme.rtl ? 'left' : 'right', props => !props.isOpaque && `0 ${getTrackMargin(props)}px`, props => getTrackHeight(props));
285
- StyledHueRange.defaultProps = {
286
- theme: DEFAULT_THEME
287
- };
288
-
289
- const checkeredBackground = function (theme, size) {
290
- let positionY = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
291
- let repeat = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'repeat';
292
- const color = getColor('neutralHue', 400, theme);
293
- const dimensions = `${size}px ${size}px`;
294
- const positionX1 = theme.rtl ? '100%' : '0';
295
- const positionX2 = theme.rtl ? `calc(100% - ${size / 2}px)` : `${size / 2}px`;
296
- const position1 = `${positionX1} ${positionY}px`;
297
- const position2 = `${positionX2} ${size / 2 + positionY}px`;
298
- const position3 = `${positionX2} ${positionY}px`;
299
- const position4 = `${positionX1} ${size / -2 + positionY}px`;
300
- return `
301
- linear-gradient(45deg, ${color} 25%, transparent 25%) ${position1} / ${dimensions} ${repeat},
302
- linear-gradient(45deg, transparent 75%, ${color} 75%) ${position2} / ${dimensions} ${repeat},
303
- linear-gradient(135deg, ${color} 25%, transparent 25%) ${position3} / ${dimensions} ${repeat},
304
- linear-gradient(135deg, transparent 75%, ${color} 75%) ${position4} / ${dimensions} ${repeat}
305
- `;
306
- };
307
-
308
- const COMPONENT_ID$j = 'colorpickers.colorpicker_alpha';
309
- const background$3 = props => {
310
- const direction = `to ${props.theme.rtl ? 'left' : 'right'}`;
311
- const fromColor = `rgba(${props.red}, ${props.green}, ${props.blue}, 0)`;
312
- const toColor = `rgb(${props.red}, ${props.green}, ${props.blue})`;
313
- const positionY = getTrackMargin(props);
314
- const height = getTrackHeight(props);
315
- const gradientBackground = `linear-gradient(${direction}, ${fromColor}, ${toColor}) 0 ${positionY}px / 100% ${height}px no-repeat`;
316
- return `${gradientBackground}, ${checkeredBackground(props.theme, height, positionY, 'repeat-x')}`;
317
- };
318
- const StyledAlphaRange = styled(StyledRange).attrs(props => ({
319
- style: {
320
- backgroundSize: 'auto',
321
- background: background$3(props)
322
- },
323
- 'data-garden-id': COMPONENT_ID$j,
324
- 'data-garden-version': '9.0.0-next.2'
325
- })).withConfig({
326
- displayName: "StyledAlphaRange",
327
- componentId: "sc-kuh2xc-0"
328
- })([""]);
329
- StyledAlphaRange.defaultProps = {
330
- theme: DEFAULT_THEME
331
- };
332
-
333
- const COMPONENT_ID$i = 'colorpickers.colorpicker_preview_box';
334
- const background$2 = props => {
335
- const alpha = props.alpha ? props.alpha / 100 : 0;
336
- const color = `rgba(${props.red}, ${props.green}, ${props.blue}, ${alpha})`;
337
- let retVal = `linear-gradient(${color}, ${color})`;
338
- if (!props.isOpaque) {
339
- retVal = `${retVal}, ${checkeredBackground(props.theme, 13)}`;
340
- }
341
- return retVal;
342
- };
343
- const StyledPreview = styled.div.attrs(props => ({
344
- style: {
345
- background: background$2(props)
346
- },
347
- 'data-garden-id': COMPONENT_ID$i,
348
- 'data-garden-version': '9.0.0-next.2',
349
- 'data-test-id': 'preview-box'
350
- })).withConfig({
351
- displayName: "StyledPreview",
352
- componentId: "sc-d70mj1-0"
353
- })(["flex-shrink:0;border-radius:", ";box-shadow:inset 0 0 0 ", " ", ";width:", "px;height:", "px;", ";"], props => props.theme.borderRadii.md, props => props.theme.borderWidths.sm, props => rgba(props.theme.palette.black, 0.19), props => props.theme.space.base * (props.isOpaque ? 6 : 8), props => props.theme.space.base * (props.isOpaque ? 6 : 8), props => retrieveComponentStyles(COMPONENT_ID$i, props));
354
- StyledPreview.defaultProps = {
355
- theme: DEFAULT_THEME
356
- };
357
-
358
- const COMPONENT_ID$h = 'colorpickers.colorpicker_colorwell';
359
- const background$1 = props => {
360
- const blackAlpha = rgba(props.theme.palette.black, 0.9);
361
- const black = `linear-gradient(0deg, ${props.theme.palette.black}, ${blackAlpha} 1%, transparent 99%)`;
362
- const whiteAngle = `${props.theme.rtl ? -90 : 90}deg`;
363
- const white = `linear-gradient(${whiteAngle}, ${props.theme.palette.white} 1%, transparent)`;
364
- const colorValue = hsl(props.hue, 1, 0.5);
365
- const color = `linear-gradient(${colorValue}, ${colorValue})`;
366
- return `${black}, ${white}, ${color}`;
367
- };
368
- const StyledColorWell = styled.div.attrs(props => ({
369
- 'data-garden-id': COMPONENT_ID$h,
370
- 'data-garden-version': '9.0.0-next.2',
371
- 'data-test-id': 'colorwell',
372
- style: {
373
- background: background$1(props)
374
- }
375
- })).withConfig({
376
- displayName: "StyledColorWell",
377
- componentId: "sc-54ly7s-0"
378
- })(["position:relative;margin-bottom:", "px;cursor:pointer;height:208px;overflow:hidden;", ";"], props => props.theme.space.base * 2, props => retrieveComponentStyles(COMPONENT_ID$h, props));
379
- StyledColorWell.defaultProps = {
380
- theme: DEFAULT_THEME
381
- };
382
-
383
- const COMPONENT_ID$g = 'colorpickers.colorpicker_colorwell_thumb';
384
- const sizeStyles$1 = theme => {
385
- const borderWidth = stripUnit(theme.borderWidths.sm) * 2;
386
- const size = theme.space.base * 5;
387
- const translateValue = size / -2;
388
- return `
389
- transform: translate(${translateValue}px, ${translateValue}px);
390
- box-sizing: border-box;
391
- border-width: ${borderWidth}px;
392
- width: ${size}px;
393
- height: ${size}px;
394
- `;
395
- };
396
- const StyledColorWellThumb = styled.div.attrs(props => ({
397
- 'data-garden-id': COMPONENT_ID$g,
398
- 'data-garden-version': '9.0.0-next.2',
399
- 'data-test-id': 'colorwell-thumb',
400
- style: {
401
- top: `${props.top}%`,
402
- left: `${props.left}%`
403
- }
404
- })).withConfig({
405
- displayName: "StyledColorWellThumb",
406
- componentId: "sc-1pnz3jz-0"
407
- })(["position:absolute;border:solid ", ";border-radius:50%;box-shadow:", ";", ";", ";"], props => props.theme.palette.white, props => props.theme.shadows.lg(`${props.theme.space.base}px`, `${props.theme.space.base * 2}px`, getColor('neutralHue', 800, props.theme, 0.24)), props => sizeStyles$1(props.theme), props => retrieveComponentStyles(COMPONENT_ID$g, props));
408
- StyledColorWellThumb.defaultProps = {
409
- theme: DEFAULT_THEME
410
- };
411
-
412
- const COMPONENT_ID$f = 'colorpickers.colorpicker_slider_group';
413
- const StyledSliderGroup = styled.div.attrs({
414
- 'data-garden-id': COMPONENT_ID$f,
415
- 'data-garden-version': '9.0.0-next.2'
416
- }).withConfig({
417
- displayName: "StyledSliderGroup",
418
- componentId: "sc-rsq0ak-0"
419
- })(["display:flex;justify-content:space-between;margin-bottom:", "px;", ";"], props => props.theme.space.base * 2, props => retrieveComponentStyles(COMPONENT_ID$f, props));
420
- StyledSliderGroup.defaultProps = {
421
- theme: DEFAULT_THEME
422
- };
423
-
424
- const COMPONENT_ID$e = 'colorpickers.colorpicker_hex_field';
425
- const StyledHexField = styled(Field).attrs({
426
- 'data-garden-id': COMPONENT_ID$e,
427
- 'data-garden-version': '9.0.0-next.2',
428
- spellcheck: false
429
- }).withConfig({
430
- displayName: "StyledHexField",
431
- componentId: "sc-1lk14t4-0"
432
- })(["display:flex;flex-basis:0;flex-direction:column;flex-grow:1;width:auto;text-align:center;input{direction:ltr;}", ";"], props => retrieveComponentStyles(COMPONENT_ID$e, props));
433
- StyledHexField.defaultProps = {
434
- theme: DEFAULT_THEME
435
- };
436
-
437
- const COMPONENT_ID$d = 'colorpickers.colorpicker_label';
438
- const StyledLabel = styled(Label).attrs({
439
- 'data-garden-id': COMPONENT_ID$d,
440
- 'data-garden-version': '9.0.0-next.2'
441
- }).withConfig({
442
- displayName: "StyledLabel",
443
- componentId: "sc-1klhp6m-0"
444
- })(["overflow:hidden;text-overflow:ellipsis;white-space:nowrap;", ";"], props => retrieveComponentStyles(COMPONENT_ID$d, props));
445
- StyledLabel.defaultProps = {
446
- theme: DEFAULT_THEME
447
- };
448
-
449
- const COMPONENT_ID$c = 'colorpickers.colorpicker_input';
450
- const StyledInput = styled(Input).attrs({
451
- 'data-garden-id': COMPONENT_ID$c,
452
- 'data-garden-version': '9.0.0-next.2',
453
- focusInset: false
454
- }).withConfig({
455
- displayName: "StyledInput",
456
- componentId: "sc-1iap93p-0"
457
- })(["text-align:center;", ";"], props => retrieveComponentStyles(COMPONENT_ID$c, props));
458
- StyledInput.defaultProps = {
459
- theme: DEFAULT_THEME
460
- };
461
-
462
- const COMPONENT_ID$b = 'colorpickers.colorpicker_input_group';
463
- const StyledInputGroup = styled.div.attrs({
464
- 'data-garden-id': COMPONENT_ID$b,
465
- 'data-garden-version': '9.0.0-next.2'
466
- }).withConfig({
467
- displayName: "StyledInputGroup",
468
- componentId: "sc-mmy93w-0"
469
- })(["display:flex;justify-content:space-between;", ";"], props => retrieveComponentStyles(COMPONENT_ID$b, props));
470
- StyledInputGroup.defaultProps = {
471
- theme: DEFAULT_THEME
472
- };
473
-
474
- const COMPONENT_ID$a = 'colorpickers.colorpicker_rgb_field';
475
- const sizeStyles = theme => {
476
- const margin = `${theme.space.base * 1.5}px`;
477
- const width = `${theme.space.base * 12.75}px`;
478
- return `
479
- margin-${theme.rtl ? 'right' : 'left'}: ${margin};
480
- width: ${width};
481
- min-width: ${width};
482
- `;
483
- };
484
- const StyledRGBAField = styled(Field).attrs({
485
- 'data-garden-id': COMPONENT_ID$a,
486
- 'data-garden-version': '9.0.0-next.2'
487
- }).withConfig({
488
- displayName: "StyledRGBAField",
489
- componentId: "sc-ibo1yw-0"
490
- })(["display:flex;flex-direction:column;text-align:center;", ";", ";"], props => sizeStyles(props.theme), props => retrieveComponentStyles(COMPONENT_ID$a, props));
491
- StyledRGBAField.defaultProps = {
492
- theme: DEFAULT_THEME
493
- };
494
-
495
- const COMPONENT_ID$9 = 'colorpickers.colorpicker_sliders';
496
- const StyledSliders = styled.div.attrs({
497
- 'data-garden-id': COMPONENT_ID$9,
498
- 'data-garden-version': '9.0.0-next.2'
499
- }).withConfig({
500
- displayName: "StyledSliders",
501
- componentId: "sc-aclca2-0"
502
- })(["position:relative;margin-", ":", "px;width:100%;& > *{position:absolute;width:100%;height:", "px;}& > :first-child{top:-", "px;}& > :last-child{bottom:-", "px;}", ";"], props => props.theme.rtl ? 'right' : 'left', props => props.theme.space.base * 2, props => getTrackMargin(props) * 2 + getTrackHeight(props), getTrackMargin, getTrackMargin, props => retrieveComponentStyles(COMPONENT_ID$9, props));
503
- StyledSliders.defaultProps = {
504
- theme: DEFAULT_THEME
505
- };
506
-
507
- const COMPONENT_ID$8 = 'colorpickers.colordialog_button';
508
- const StyledButton = styled(Button).attrs({
509
- isNeutral: true,
510
- 'data-garden-id': COMPONENT_ID$8,
511
- 'data-garden-version': '9.0.0-next.2'
512
- }).withConfig({
513
- displayName: "StyledButton",
514
- componentId: "sc-101xjve-0"
515
- })(["padding:0;width:", "px;max-width:", "px;&:last-of-type:not(:first-child){border-top-", "-radius:", " !important;border-bottom-", "-radius:", " !important;}", ";"], props => props.theme.space.base * 17, props => props.theme.space.base * 17, props => props.theme.rtl ? 'left' : 'right', props => props.theme.borderRadii.md, props => props.theme.rtl ? 'left' : 'right', props => props.theme.borderRadii.md, props => retrieveComponentStyles(COMPONENT_ID$8, props));
516
- StyledButton.defaultProps = {
517
- theme: DEFAULT_THEME
518
- };
519
-
520
- const COMPONENT_ID$7 = 'colorpickers.colordialog_preview';
521
- const background = props => {
522
- const {
523
- backgroundColor
524
- } = props;
525
- let color;
526
- if (typeof backgroundColor === 'string') {
527
- color = backgroundColor;
528
- } else if (backgroundColor === undefined) {
529
- color = props.theme.palette.white;
530
- } else {
531
- const {
532
- red,
533
- green,
534
- blue,
535
- alpha = 1
536
- } = backgroundColor;
537
- color = `rgba(${red}, ${green}, ${blue}, ${alpha ? alpha / 100 : 0})`;
538
- }
539
- return `linear-gradient(${color}, ${color})`;
540
- };
541
- const StyledButtonPreview = styled.span.attrs(props => ({
542
- style: {
543
- background: `${background(props)}, ${checkeredBackground(props.theme, 8)}`
544
- },
545
- 'data-garden-id': COMPONENT_ID$7,
546
- 'data-garden-version': '9.0.0-next.2',
547
- 'data-test-id': 'dialog-preview'
548
- })).withConfig({
549
- displayName: "StyledButtonPreview",
550
- componentId: "sc-1jzysg3-0"
551
- })(["display:inline-block;bottom:", "px;border-radius:", ";box-shadow:inset 0 0 0 ", " ", ";width:", "px;height:", "px;", ";"], props => props.theme.space.base, props => props.theme.borderRadii.sm, props => props.theme.borderWidths.sm, props => rgba(props.theme.palette.black, 0.19), props => props.theme.space.base * 5, props => props.theme.space.base * 5, props => retrieveComponentStyles(COMPONENT_ID$7, props));
552
- StyledButtonPreview.defaultProps = {
553
- theme: DEFAULT_THEME
554
- };
555
-
556
- const COMPONENT_ID$6 = 'colorpickers.colordialog_tooltipmodal';
557
- const StyledTooltipModal = styled(TooltipModal).attrs({
558
- 'data-garden-id': COMPONENT_ID$6,
559
- 'data-garden-version': '9.0.0-next.2'
560
- }).withConfig({
561
- displayName: "StyledTooltipModal",
562
- componentId: "sc-o022s8-0"
563
- })(["width:auto !important;", ";"], props => retrieveComponentStyles(COMPONENT_ID$6, props));
564
- StyledTooltipModal.defaultProps = {
565
- theme: DEFAULT_THEME
566
- };
567
-
568
- const COMPONENT_ID$5 = 'colorpickers.colordialog_tooltipmodal_body';
569
- const StyledTooltipBody = styled(TooltipModal.Body).attrs({
570
- 'data-garden-id': COMPONENT_ID$5,
571
- 'data-garden-version': '9.0.0-next.2'
572
- }).withConfig({
573
- displayName: "StyledTooltipBody",
574
- componentId: "sc-6gsgsy-0"
575
- })(["padding:0;", ";"], props => retrieveComponentStyles(COMPONENT_ID$5, props));
576
- StyledTooltipBody.defaultProps = {
577
- theme: DEFAULT_THEME
578
- };
579
-
580
- const COMPONENT_ID$4 = 'colorpickers.color_swatch';
581
- const StyledColorSwatch = styled.table.attrs({
582
- 'data-garden-id': COMPONENT_ID$4,
583
- 'data-garden-version': '9.0.0-next.2'
584
- }).withConfig({
585
- displayName: "StyledColorSwatch",
586
- componentId: "sc-ajx440-0"
587
- })(["border-collapse:collapse;line-height:normal;", ";"], props => retrieveComponentStyles(COMPONENT_ID$4, props));
588
- StyledColorSwatch.defaultProps = {
589
- theme: DEFAULT_THEME
590
- };
591
-
592
- const COMPONENT_ID$3 = 'colorpickers.color_swatch_input';
593
- const StyledColorSwatchInput = styled.input.attrs({
594
- 'data-garden-id': COMPONENT_ID$3,
595
- 'data-garden-version': '9.0.0-next.2'
596
- }).withConfig({
597
- displayName: "StyledColorSwatchInput",
598
- componentId: "sc-em45h0-0"
599
- })(["position:absolute;opacity:0;z-index:1;margin:0;width:100%;height:100%;", ";"], props => retrieveComponentStyles(COMPONENT_ID$3, props));
600
- StyledColorSwatchInput.defaultProps = {
601
- theme: DEFAULT_THEME
602
- };
603
-
604
- const COMPONENT_ID$2 = 'colorpickers.color_swatch_label';
605
- const colorStyles = props => {
606
- const boxShadow = props.theme.shadows.md(getColor('primaryHue', 600, props.theme, 0.35));
607
- const {
608
- alpha
609
- } = parseToRgb(props.backgroundColor);
610
- let foregroundColor;
611
- if (alpha && alpha < 0.4) {
612
- foregroundColor = props.theme.colors.foreground;
613
- } else {
614
- foregroundColor = readableColor(props.backgroundColor, props.theme.colors.foreground, props.theme.colors.background);
615
- }
616
- return `
617
- color: ${foregroundColor};
618
-
619
- &[data-garden-focus-visible] {
620
- box-shadow: ${boxShadow};
621
- `;
622
- };
623
- const StyledColorSwatchLabel = styled(StyledButtonPreview).attrs({
624
- as: 'label',
625
- 'data-garden-id': COMPONENT_ID$2,
626
- 'data-garden-version': '9.0.0-next.2'
627
- }).withConfig({
628
- displayName: "StyledColorSwatchLabel",
629
- componentId: "sc-1aghocg-0"
630
- })(["position:relative;border-radius:", ";cursor:pointer;", ";", ";"], props => props.theme.borderRadii.md, colorStyles, props => retrieveComponentStyles(COMPONENT_ID$2, props));
631
- StyledColorSwatchLabel.defaultProps = {
632
- theme: DEFAULT_THEME
633
- };
634
-
635
- var _path$1;
636
- function _extends$1() { _extends$1 = Object.assign ? Object.assign.bind() : 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$1.apply(this, arguments); }
637
- var SvgCheckSmFill = function SvgCheckSmFill(props) {
638
- return /*#__PURE__*/React.createElement("svg", _extends$1({
639
- xmlns: "http://www.w3.org/2000/svg",
640
- width: 12,
641
- height: 12,
642
- focusable: "false",
643
- viewBox: "0 0 12 12",
644
- "aria-hidden": "true"
645
- }, props), _path$1 || (_path$1 = /*#__PURE__*/React.createElement("path", {
646
- fill: "none",
647
- stroke: "currentColor",
648
- strokeLinecap: "round",
649
- strokeLinejoin: "round",
650
- strokeWidth: 2,
651
- d: "M3 6l2 2 4-4"
652
- })));
653
- };
654
-
655
- const COMPONENT_ID$1 = 'colorpickers.colorswatch_check';
656
- const StyledIcon = styled(SvgCheckSmFill).attrs({
657
- 'data-garden-id': COMPONENT_ID$1,
658
- 'data-garden-version': '9.0.0-next.2'
659
- }).withConfig({
660
- displayName: "StyledIcon",
661
- componentId: "sc-8oigif-0"
662
- })(["position:absolute;top:0;left:0;transition:opacity 0.2s ease-in-out;opacity:0;width:100%;height:100%;", ":checked ~ &{opacity:1;}", ";"], StyledColorSwatchInput, props => retrieveComponentStyles(COMPONENT_ID$1, props));
663
- StyledIcon.defaultProps = {
664
- theme: DEFAULT_THEME
665
- };
666
-
667
- const COMPONENT_ID = 'colorpickers.swatch_cell';
668
- const StyledCell = styled.td.attrs({
669
- 'data-garden-id': COMPONENT_ID,
670
- 'data-garden-version': '9.0.0-next.2'
671
- }).withConfig({
672
- displayName: "StyledCell",
673
- componentId: "sc-qr3oit-0"
674
- })(["padding:", "px;font-size:0;", ";"], props => props.theme.space.base, props => retrieveComponentStyles(COMPONENT_ID, props));
675
- StyledCell.defaultProps = {
676
- theme: DEFAULT_THEME
677
- };
678
-
679
- const ColorWell = React__default.memo(_ref => {
680
- let {
681
- hue,
682
- saturation,
683
- lightness,
684
- onChange
685
- } = _ref;
686
- const {
687
- rtl
688
- } = useContext(ThemeContext);
689
- const containerRef = useRef(null);
690
- const hsv = hslToHsv(hue, saturation, lightness);
691
- const mouseActiveRef = useRef(false);
692
- const [x, setX] = useState(0);
693
- const [y, setY] = useState(0);
694
- const {
695
- topFromMouse,
696
- leftFromMouse
697
- } = getThumbPosition(x, y, rtl, containerRef);
698
- const [topPosition, setTopPosition] = useState(0);
699
- const [leftPosition, setLeftPosition] = useState(0);
700
- useEffect(() => {
701
- setTopPosition(100 - hsv.v);
702
- setLeftPosition(rtl ? 100 - hsv.s : hsv.s);
703
- }, [hsv.s, hsv.v, rtl]);
704
- const throttledChange = useMemo(() => {
705
- return throttle(e => {
706
- if (containerRef.current) {
707
- const nextHsv = getNextHsv(e, hue, containerRef.current, rtl);
708
- onChange && onChange(nextHsv, e);
709
- }
710
- }, 50);
711
- }, [hue, rtl, onChange]);
712
- const handleMouseMove = useCallback(e => {
713
- mouseActiveRef.current = true;
714
- setX(e.pageX);
715
- setY(e.pageY);
716
- throttledChange(e);
717
- }, [throttledChange]);
718
- const handleMouseUp = useCallback(() => {
719
- mouseActiveRef.current = true;
720
- setTimeout(() => {
721
- mouseActiveRef.current = false;
722
- });
723
- throttledChange.cancel();
724
- window.removeEventListener('mousemove', handleMouseMove);
725
- window.removeEventListener('mouseup', handleMouseUp);
726
- }, [throttledChange, handleMouseMove]);
727
- const handleMouseDown = useCallback(e => {
728
- mouseActiveRef.current = true;
729
- e.persist();
730
- handleMouseMove(e);
731
- throttledChange(e);
732
- window.addEventListener('mousemove', handleMouseMove);
733
- window.addEventListener('mouseup', handleMouseUp);
734
- }, [throttledChange, handleMouseMove, handleMouseUp]);
735
- useEffect(() => {
736
- return () => {
737
- throttledChange.cancel();
738
- };
739
- }, [throttledChange]);
740
- return (
741
- React__default.createElement(StyledColorWell, {
742
- hue: hue,
743
- ref: containerRef,
744
- role: "presentation",
745
- onMouseDown: handleMouseDown
746
- }, React__default.createElement(StyledColorWellThumb, {
747
- top: mouseActiveRef.current ? topFromMouse : topPosition,
748
- left: mouseActiveRef.current ? leftFromMouse : leftPosition
749
- }))
750
- );
751
- });
752
- ColorWell.displayName = 'ColorWell';
753
-
754
- const isValidHex = hexString => {
755
- const regEx = /^#(?:(?:[0-9A-F]{6}(?:[0-9A-F]{2})?)|(?:[0-9A-F]{3})(?:[0-9A-F]?))$/iu;
756
- return regEx.test(hexString);
757
- };
758
-
759
- function convertStringToColor(colorString) {
760
- if (colorString.includes('#') && !isValidHex(colorString)) {
761
- return undefined;
762
- }
763
- const {
764
- hue,
765
- saturation,
766
- lightness
767
- } = parseToHsl(colorString);
768
- const {
769
- red,
770
- green,
771
- blue,
772
- alpha
773
- } = parseToRgb(colorString);
774
- const computedAlpha = alpha === undefined ? 100 : alpha * 100;
775
- const computedHex = rgb({
776
- red,
777
- green,
778
- blue
779
- });
780
- return {
781
- hue,
782
- saturation: saturation * 100,
783
- lightness: lightness * 100,
784
- red,
785
- green,
786
- blue,
787
- alpha: computedAlpha,
788
- hex: computedHex
789
- };
790
- }
791
- const areColorsEqual = (a, b) => {
792
- if (a === undefined || b === undefined) {
793
- return false;
794
- }
795
- const colorA = typeof a === 'string' ? convertStringToColor(a) : a;
796
- const colorB = typeof b === 'string' ? convertStringToColor(b) : b;
797
- if (colorA === undefined || colorB === undefined) {
798
- return false;
799
- }
800
- return isEqual(colorA, colorB);
801
- };
802
- function getInitialState(color) {
803
- const whiteColor = {
804
- hue: 0,
805
- saturation: 0,
806
- lightness: 0,
807
- red: 255,
808
- green: 255,
809
- blue: 255,
810
- alpha: 100,
811
- hex: '#ffffff'
812
- };
813
- if (color === undefined) {
814
- return getInitialState(whiteColor);
815
- }
816
- if (typeof color === 'string') {
817
- const computedColor = convertStringToColor(color);
818
- return getInitialState(computedColor || whiteColor);
819
- }
820
- return {
821
- color,
822
- hexInput: color.hex,
823
- redInput: color.red.toString(),
824
- blueInput: color.blue.toString(),
825
- greenInput: color.green.toString(),
826
- alphaInput: color.alpha.toString()
827
- };
828
- }
829
- const reducer = (state, action) => {
830
- switch (action.type) {
831
- case 'SATURATION_CHANGE':
832
- {
833
- const hsl$1 = hsvToHsl(action.payload.h, action.payload.s * 100, action.payload.v * 100);
834
- const hex = hsl({
835
- hue: action.payload.h,
836
- saturation: hsl$1.s / 100,
837
- lightness: hsl$1.l / 100
838
- });
839
- const rgb = parseToRgb(hex);
840
- return {
841
- ...state,
842
- color: {
843
- ...state.color,
844
- saturation: hsl$1.s,
845
- lightness: hsl$1.l,
846
- hex,
847
- red: rgb.red,
848
- green: rgb.green,
849
- blue: rgb.blue
850
- }
851
- };
852
- }
853
- case 'HUE_CHANGE':
854
- {
855
- const hue = Number(action.payload);
856
- const hex = hsl({
857
- hue,
858
- saturation: state.color.saturation / 100,
859
- lightness: state.color.lightness / 100
860
- });
861
- const rgb = parseToRgb(hex);
862
- return {
863
- ...state,
864
- color: {
865
- ...state.color,
866
- hue,
867
- hex,
868
- red: rgb.red,
869
- green: rgb.green,
870
- blue: rgb.blue
871
- }
872
- };
873
- }
874
- case 'ALPHA_SLIDER_CHANGE':
875
- {
876
- return {
877
- ...state,
878
- color: {
879
- ...state.color,
880
- alpha: Math.round(Number(action.payload) * 100)
881
- }
882
- };
883
- }
884
- case 'HEX_CHANGE':
885
- {
886
- let color = state.color;
887
- if (isValidHex(action.payload)) {
888
- const rgb = parseToRgb(action.payload);
889
- const hsl = parseToHsl(`rgb(${rgb.red}, ${rgb.green}, ${rgb.blue})`);
890
- color = {
891
- ...color,
892
- hue: hsl.hue,
893
- saturation: hsl.saturation * 100,
894
- lightness: hsl.lightness * 100,
895
- hex: action.payload,
896
- red: rgb.red,
897
- green: rgb.green,
898
- blue: rgb.blue
899
- };
900
- }
901
- return {
902
- ...state,
903
- hexInput: action.payload,
904
- color
905
- };
906
- }
907
- case 'RED_CHANGE':
908
- {
909
- let red = parseInt(action.payload, 10);
910
- let color = state.color;
911
- if (!isNaN(red)) {
912
- if (red >= 255) {
913
- red = 255;
914
- }
915
- if (red < 0) {
916
- red = 0;
917
- }
918
- const hsl = parseToHsl(`rgb(${red}, ${color.green}, ${color.blue})`);
919
- const hex = rgb(red, color.green, color.blue);
920
- color = {
921
- ...color,
922
- hex,
923
- red: action.payload === '' ? color.red : red,
924
- hue: hsl.hue,
925
- saturation: hsl.saturation * 100,
926
- lightness: hsl.lightness * 100
927
- };
928
- }
929
- return {
930
- ...state,
931
- redInput: action.payload,
932
- color
933
- };
934
- }
935
- case 'GREEN_CHANGE':
936
- {
937
- let green = parseInt(action.payload, 10);
938
- let color = state.color;
939
- if (!isNaN(green)) {
940
- if (green >= 255) {
941
- green = 255;
942
- }
943
- if (green < 0) {
944
- green = 0;
945
- }
946
- const hsl = parseToHsl(`rgb(${color.red}, ${green}, ${color.blue})`);
947
- const hex = rgb(color.red, green, color.blue);
948
- color = {
949
- ...color,
950
- hex,
951
- green: action.payload === '' ? color.green : green,
952
- hue: hsl.hue,
953
- saturation: hsl.saturation * 100,
954
- lightness: hsl.lightness * 100
955
- };
956
- }
957
- return {
958
- ...state,
959
- greenInput: action.payload,
960
- color
961
- };
962
- }
963
- case 'BLUE_CHANGE':
964
- {
965
- let blue = parseInt(action.payload, 10);
966
- let color = state.color;
967
- if (!isNaN(blue)) {
968
- if (blue >= 255) {
969
- blue = 255;
970
- }
971
- if (blue < 0) {
972
- blue = 0;
973
- }
974
- const hsl = parseToHsl(`rgb(${color.red}, ${color.green}, ${blue})`);
975
- const hex = rgb(color.red, color.green, blue);
976
- color = {
977
- ...color,
978
- hex,
979
- blue: action.payload === '' ? color.blue : blue,
980
- hue: hsl.hue,
981
- saturation: hsl.saturation * 100,
982
- lightness: hsl.lightness * 100
983
- };
984
- }
985
- return {
986
- ...state,
987
- blueInput: action.payload,
988
- color
989
- };
990
- }
991
- case 'ALPHA_CHANGE':
992
- {
993
- let alpha = parseInt(action.payload, 10);
994
- let color = state.color;
995
- if (!isNaN(alpha)) {
996
- if (alpha > 100) {
997
- alpha = 100;
998
- }
999
- if (alpha < 0) {
1000
- alpha = 0;
1001
- }
1002
- color = {
1003
- ...color,
1004
- alpha
1005
- };
1006
- }
1007
- return {
1008
- ...state,
1009
- alphaInput: action.payload,
1010
- color
1011
- };
1012
- }
1013
- case 'RESET_COLOR':
1014
- {
1015
- return getInitialState(action.payload);
1016
- }
1017
- default:
1018
- throw new Error('Unknown reducer case.');
1019
- }
1020
- };
1021
-
1022
- const Colorpicker = forwardRef((_ref, ref) => {
1023
- let {
1024
- color,
1025
- defaultColor,
1026
- isOpaque,
1027
- labels = {},
1028
- autofocus,
1029
- onChange,
1030
- ...props
1031
- } = _ref;
1032
- const [state, dispatch] = useReducer(reducer, getInitialState(color || defaultColor));
1033
- const previousComputedColorRef = useRef(state.color);
1034
- const previousStateColorRef = useRef(state.color);
1035
- const computedColor = useMemo(() => {
1036
- let retVal = state.color;
1037
- if (color) {
1038
- if (typeof color === 'string') {
1039
- const convertedColor = convertStringToColor(color);
1040
- if (convertedColor) {
1041
- retVal = convertedColor;
1042
- }
1043
- } else {
1044
- retVal = color;
1045
- }
1046
- }
1047
- if (isOpaque) {
1048
- retVal.alpha = 100;
1049
- }
1050
- if (areColorsEqual(retVal, previousComputedColorRef.current)) {
1051
- return previousComputedColorRef.current;
1052
- }
1053
- return retVal;
1054
- }, [color, isOpaque, state.color]);
1055
- useEffect(() => {
1056
- if (!areColorsEqual(previousStateColorRef.current, state.color) && !areColorsEqual(color, state.color)) {
1057
- onChange && onChange(state.color);
1058
- }
1059
- previousStateColorRef.current = state.color;
1060
- }, [color, onChange, state.color]);
1061
- if (previousComputedColorRef.current !== computedColor) {
1062
- dispatch({
1063
- type: 'RESET_COLOR',
1064
- payload: computedColor
1065
- });
1066
- previousComputedColorRef.current = computedColor;
1067
- }
1068
- const handleColorWellChange = useCallback(hsv => {
1069
- dispatch({
1070
- type: 'SATURATION_CHANGE',
1071
- payload: hsv
1072
- });
1073
- }, []);
1074
- const handleHueChange = useCallback(e => {
1075
- dispatch({
1076
- type: 'HUE_CHANGE',
1077
- payload: e.target.value
1078
- });
1079
- }, []);
1080
- const handleAlphaSliderChange = useCallback(e => {
1081
- dispatch({
1082
- type: 'ALPHA_SLIDER_CHANGE',
1083
- payload: e.target.value
1084
- });
1085
- }, []);
1086
- const handleHexChange = useCallback(e => {
1087
- dispatch({
1088
- type: 'HEX_CHANGE',
1089
- payload: e.target.value
1090
- });
1091
- }, []);
1092
- const handleRedChange = useCallback(e => {
1093
- dispatch({
1094
- type: 'RED_CHANGE',
1095
- payload: e.target.value
1096
- });
1097
- }, []);
1098
- const handleGreenChange = useCallback(e => {
1099
- dispatch({
1100
- type: 'GREEN_CHANGE',
1101
- payload: e.target.value
1102
- });
1103
- }, []);
1104
- const handleBlueChange = useCallback(e => {
1105
- dispatch({
1106
- type: 'BLUE_CHANGE',
1107
- payload: e.target.value
1108
- });
1109
- }, []);
1110
- const handleAlphaChange = useCallback(e => {
1111
- dispatch({
1112
- type: 'ALPHA_CHANGE',
1113
- payload: e.target.value
1114
- });
1115
- }, []);
1116
- const handleBlur = useCallback(() => {
1117
- dispatch({
1118
- type: 'RESET_COLOR',
1119
- payload: computedColor
1120
- });
1121
- }, [computedColor]);
1122
- return React__default.createElement(StyledColorPicker, _extends$2({
1123
- ref: ref,
1124
- isOpaque: isOpaque
1125
- }, props), React__default.createElement(ColorWell, {
1126
- hue: computedColor.hue,
1127
- saturation: computedColor.saturation,
1128
- lightness: computedColor.lightness,
1129
- onChange: handleColorWellChange
1130
- }), React__default.createElement(StyledSliderGroup, null, React__default.createElement(StyledPreview, {
1131
- red: computedColor.red,
1132
- green: computedColor.green,
1133
- blue: computedColor.blue,
1134
- alpha: computedColor.alpha,
1135
- isOpaque: isOpaque
1136
- }), React__default.createElement(StyledSliders, {
1137
- isOpaque: isOpaque
1138
- }, React__default.createElement(Field, null, React__default.createElement(Label, {
1139
- hidden: true
1140
- }, labels.hueSlider || 'Hue slider'), React__default.createElement(StyledHueRange, {
1141
- step: 1,
1142
- max: 360,
1143
- value: computedColor.hue,
1144
- isOpaque: isOpaque,
1145
- onChange: handleHueChange
1146
- })), !isOpaque && React__default.createElement(Field, null, React__default.createElement(Label, {
1147
- hidden: true
1148
- }, labels.alphaSlider || 'Alpha slider'), React__default.createElement(StyledAlphaRange, {
1149
- max: 1,
1150
- step: 0.01,
1151
- value: computedColor.alpha / 100,
1152
- onChange: handleAlphaSliderChange,
1153
- red: computedColor.red,
1154
- green: computedColor.green,
1155
- blue: computedColor.blue
1156
- })))), React__default.createElement(StyledInputGroup, null, React__default.createElement(StyledHexField, null, React__default.createElement(StyledLabel, {
1157
- isRegular: true
1158
- }, labels.hex || 'Hex'), React__default.createElement(StyledInput, {
1159
- isCompact: true,
1160
- maxLength: 7,
1161
- value: state.hexInput
1162
- ,
1163
- autoFocus: autofocus,
1164
- spellCheck: false,
1165
- onBlur: handleBlur,
1166
- onChange: handleHexChange
1167
- })), React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
1168
- isRegular: true
1169
- }, labels.red || 'R'), React__default.createElement(StyledInput, {
1170
- isCompact: true,
1171
- type: "number",
1172
- min: "0",
1173
- max: "255",
1174
- maxLength: 3,
1175
- value: state.redInput,
1176
- onBlur: handleBlur,
1177
- onChange: handleRedChange
1178
- })), React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
1179
- isRegular: true
1180
- }, labels.green || 'G'), React__default.createElement(StyledInput, {
1181
- isCompact: true,
1182
- type: "number",
1183
- min: "0",
1184
- max: "255",
1185
- maxLength: 3,
1186
- value: state.greenInput,
1187
- onBlur: handleBlur,
1188
- onChange: handleGreenChange
1189
- })), React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
1190
- isRegular: true
1191
- }, labels.blue || 'B'), React__default.createElement(StyledInput, {
1192
- isCompact: true,
1193
- type: "number",
1194
- min: "0",
1195
- max: "255",
1196
- maxLength: 3,
1197
- value: state.blueInput,
1198
- onBlur: handleBlur,
1199
- onChange: handleBlueChange
1200
- })), !isOpaque && React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
1201
- isRegular: true
1202
- }, labels.alpha || 'A'), React__default.createElement(StyledInput, {
1203
- isCompact: true,
1204
- type: "number",
1205
- min: "0",
1206
- max: "100",
1207
- value: state.alphaInput,
1208
- onBlur: handleBlur,
1209
- onChange: handleAlphaChange
1210
- }))));
1211
- });
1212
- Colorpicker.defaultProps = {
1213
- defaultColor: '#fff'
1214
- };
1215
- Colorpicker.displayName = 'Colorpicker';
1216
- Colorpicker.propTypes = {
1217
- color: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
1218
- isOpaque: PropTypes.bool,
1219
- onChange: PropTypes.func,
1220
- labels: PropTypes.object,
1221
- defaultColor: PropTypes.oneOfType([PropTypes.object, PropTypes.string])
1222
- };
1223
-
1224
- var _path;
1225
- function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
1226
- var SvgChevronDownStroke = function SvgChevronDownStroke(props) {
1227
- return /*#__PURE__*/React.createElement("svg", _extends({
1228
- xmlns: "http://www.w3.org/2000/svg",
1229
- width: 16,
1230
- height: 16,
1231
- focusable: "false",
1232
- viewBox: "0 0 16 16",
1233
- "aria-hidden": "true"
1234
- }, props), _path || (_path = /*#__PURE__*/React.createElement("path", {
1235
- fill: "currentColor",
1236
- d: "M12.688 5.61a.5.5 0 01.69.718l-.066.062-5 4a.5.5 0 01-.542.054l-.082-.054-5-4a.5.5 0 01.55-.83l.074.05L8 9.359l4.688-3.75z"
1237
- })));
1238
- };
1239
-
1240
- const ColorpickerDialog = forwardRef((_ref, ref) => {
1241
- let {
1242
- color,
1243
- defaultColor,
1244
- placement,
1245
- onChange,
1246
- onClose,
1247
- labels,
1248
- hasArrow,
1249
- isAnimated,
1250
- isOpaque,
1251
- isOpen,
1252
- zIndex,
1253
- focusInset,
1254
- disabled,
1255
- buttonProps,
1256
- onDialogChange,
1257
- 'aria-label': ariaLabel,
1258
- children,
1259
- ...props
1260
- } = _ref;
1261
- const isControlled = color !== null && color !== undefined;
1262
- const isDialogControlled = isOpen !== undefined && isOpen !== null;
1263
- const buttonRef = useRef(null);
1264
- const colorPickerRef = useRef(null);
1265
- const [referenceElement, setReferenceElement] = useState();
1266
- const [uncontrolledColor, setUncontrolledColor] = useState(defaultColor);
1267
- const ariaLabelText = useText(ColorpickerDialog, {
1268
- 'aria-label': ariaLabel
1269
- }, 'aria-label', 'Color picker');
1270
- const openDialog = () => {
1271
- setReferenceElement(buttonRef.current);
1272
- onDialogChange && onDialogChange({
1273
- isOpen: true
1274
- });
1275
- };
1276
- const closeDialog = () => {
1277
- setReferenceElement(null);
1278
- onDialogChange && onDialogChange({
1279
- isOpen: false
1280
- });
1281
- };
1282
- const onClick = composeEventHandlers(props.onClick, () => {
1283
- if (referenceElement) {
1284
- closeDialog();
1285
- } else {
1286
- openDialog();
1287
- }
1288
- });
1289
- useEffect(() => {
1290
- if (isDialogControlled) {
1291
- if (isOpen) {
1292
- setReferenceElement(buttonRef.current);
1293
- } else {
1294
- setReferenceElement(null);
1295
- }
1296
- }
1297
- }, [isOpen, isDialogControlled]);
1298
- return React__default.createElement(React__default.Fragment, null, children ? ( cloneElement(Children.only(children), {
1299
- onClick,
1300
- ref: buttonRef
1301
- })) : React__default.createElement(StyledButton, _extends$2({
1302
- disabled: disabled,
1303
- focusInset: focusInset,
1304
- ref: buttonRef,
1305
- onClick: onClick
1306
- }, buttonProps), React__default.createElement(StyledButtonPreview, {
1307
- backgroundColor: isControlled ? color : uncontrolledColor
1308
- }), React__default.createElement(Button.EndIcon, {
1309
- isRotated: referenceElement != null
1310
- }, React__default.createElement(SvgChevronDownStroke, null))), React__default.createElement(StyledTooltipModal, _extends$2({
1311
- ref: ref,
1312
- hasArrow: hasArrow,
1313
- zIndex: zIndex,
1314
- isAnimated: isAnimated,
1315
- isOpaque: isOpaque,
1316
- focusOnMount: false,
1317
- placement: placement,
1318
- referenceElement: referenceElement,
1319
- onClose: () => {
1320
- closeDialog();
1321
- onClose && onClose(isControlled ? color : uncontrolledColor);
1322
- },
1323
- "aria-label": ariaLabelText
1324
- }, props), React__default.createElement(StyledTooltipBody, null, React__default.createElement(Colorpicker, {
1325
- autofocus: true,
1326
- color: color,
1327
- isOpaque: isOpaque,
1328
- labels: labels,
1329
- ref: colorPickerRef,
1330
- defaultColor: uncontrolledColor,
1331
- onChange: isControlled ? onChange : setUncontrolledColor
1332
- }))));
1333
- });
1334
- ColorpickerDialog.propTypes = {
1335
- ...Colorpicker.propTypes,
1336
- placement: PropTypes.oneOf(PLACEMENT),
1337
- onClose: PropTypes.func,
1338
- onDialogChange: PropTypes.func,
1339
- disabled: PropTypes.bool,
1340
- labels: PropTypes.object,
1341
- buttonProps: PropTypes.object,
1342
- zIndex: PropTypes.number,
1343
- hasArrow: PropTypes.bool,
1344
- isAnimated: PropTypes.bool,
1345
- isOpen: PropTypes.bool,
1346
- focusInset: PropTypes.bool
1347
- };
1348
- ColorpickerDialog.defaultProps = {
1349
- placement: 'bottom-start',
1350
- isAnimated: true,
1351
- zIndex: 1000,
1352
- hasArrow: false
1353
- };
1354
- ColorpickerDialog.displayName = 'ColorpickerDialog';
1355
-
1356
- const ColorSwatch = forwardRef((_ref, ref) => {
1357
- let {
1358
- name,
1359
- colors,
1360
- isCheckboxGroup,
1361
- defaultSelectedColIndex,
1362
- defaultSelectedRowIndex,
1363
- selectedColIndex,
1364
- selectedRowIndex,
1365
- onSelect,
1366
- ...props
1367
- } = _ref;
1368
- const theme = useContext(ThemeContext) || DEFAULT_THEME;
1369
- const environment = useDocument(theme);
1370
- const gridRef = useRef();
1371
- const [rowIndex, setRowIndex] = useState(selectedRowIndex === null ? undefined : selectedRowIndex);
1372
- const [colIndex, setColIndex] = useState(selectedColIndex === null ? undefined : selectedColIndex);
1373
- const isControlled = selectedColIndex !== undefined && selectedRowIndex !== undefined;
1374
- const {
1375
- getGridCellProps
1376
- } = useGrid({
1377
- environment,
1378
- rtl: theme.rtl,
1379
- matrix: colors,
1380
- wrap: true,
1381
- idPrefix: useId(undefined),
1382
- defaultRowIndex: defaultSelectedRowIndex,
1383
- defaultColIndex: defaultSelectedColIndex,
1384
- rowIndex,
1385
- colIndex,
1386
- onChange: (row, col) => {
1387
- setRowIndex(row);
1388
- setColIndex(col);
1389
- }
1390
- });
1391
- return React__default.createElement(StyledColorSwatch, _extends$2({
1392
- role: "grid",
1393
- ref: mergeRefs([gridRef, ref])
1394
- }, props), React__default.createElement("tbody", null, colors.map((row, _rowIndex) => React__default.createElement("tr", {
1395
- key: row[0].value
1396
- }, row.map((color, _colIndex) => {
1397
- const {
1398
- label,
1399
- value
1400
- } = color;
1401
- const {
1402
- role,
1403
- onFocus,
1404
- ...gridCellProps
1405
- } = getGridCellProps({
1406
- colIndex: _colIndex,
1407
- rowIndex: _rowIndex
1408
- });
1409
- const checked = isControlled ? selectedRowIndex === _rowIndex && selectedColIndex === _colIndex : undefined;
1410
- const defaultChecked = isControlled ? undefined : defaultSelectedRowIndex === _rowIndex && defaultSelectedColIndex === _colIndex;
1411
- const handleChange = event => {
1412
- if (onSelect) {
1413
- if (event.target.checked) {
1414
- onSelect(_rowIndex, _colIndex);
1415
- } else {
1416
- onSelect(null, null);
1417
- }
1418
- }
1419
- if (isCheckboxGroup && event.target.checked) {
1420
- const inputs = gridRef.current?.querySelectorAll('input');
1421
- inputs?.forEach(input => {
1422
- if (input !== event.target) {
1423
- input.checked = false;
1424
- }
1425
- });
1426
- }
1427
- };
1428
- const handleBlur = event => {
1429
- event.target.parentElement?.removeAttribute('data-garden-focus-visible');
1430
- if (!(isCheckboxGroup || gridRef.current?.contains(event.relatedTarget))) {
1431
- const selectedInput = gridRef.current?.querySelector(`input[name='${event.target.name}']:checked`);
1432
- if (selectedInput !== null) {
1433
- const inputs = gridRef.current?.querySelectorAll('input');
1434
- inputs?.forEach(input => input.setAttribute('tabIndex', input.checked ? '0' : '-1'));
1435
- }
1436
- }
1437
- };
1438
- const handleFocus = composeEventHandlers(onFocus, event => event.target.parentElement?.setAttribute('data-garden-focus-visible', 'true'));
1439
- return React__default.createElement(StyledCell, {
1440
- key: value,
1441
- role: role
1442
- }, React__default.createElement(StyledColorSwatchLabel, {
1443
- backgroundColor: value
1444
- }, React__default.createElement(Tooltip, {
1445
- content: label
1446
- }, React__default.createElement(StyledColorSwatchInput, _extends$2({
1447
- "aria-label": label,
1448
- name: name,
1449
- type: isCheckboxGroup ? 'checkbox' : 'radio',
1450
- value: value,
1451
- defaultChecked: defaultChecked,
1452
- checked: checked,
1453
- onFocus: handleFocus,
1454
- onBlur: handleBlur,
1455
- onChange: handleChange
1456
- }, gridCellProps))), React__default.createElement(StyledIcon, null)));
1457
- })))));
1458
- });
1459
- ColorSwatch.displayName = 'ColorSwatch';
1460
- ColorSwatch.propTypes = {
1461
- name: PropTypes.string.isRequired,
1462
- colors: PropTypes.arrayOf(PropTypes.any).isRequired,
1463
- isCheckboxGroup: PropTypes.bool,
1464
- selectedRowIndex: PropTypes.number,
1465
- selectedColIndex: PropTypes.number,
1466
- defaultSelectedRowIndex: PropTypes.number,
1467
- defaultSelectedColIndex: PropTypes.number,
1468
- onSelect: PropTypes.func
1469
- };
1470
-
1471
- const ColorSwatchDialog = forwardRef((_ref, ref) => {
1472
- let {
1473
- name,
1474
- colors,
1475
- isCheckboxGroup,
1476
- selectedRowIndex,
1477
- selectedColIndex,
1478
- defaultSelectedRowIndex,
1479
- defaultSelectedColIndex,
1480
- placement,
1481
- onSelect,
1482
- hasArrow,
1483
- isAnimated,
1484
- zIndex,
1485
- isOpen,
1486
- focusInset,
1487
- disabled,
1488
- buttonProps,
1489
- onDialogChange,
1490
- children,
1491
- 'aria-label': ariaLabel,
1492
- ...props
1493
- } = _ref;
1494
- const isControlled = selectedRowIndex !== undefined && selectedColIndex !== undefined;
1495
- const isDialogControlled = isOpen !== undefined && isOpen !== null;
1496
- const buttonRef = useRef(null);
1497
- const colorSwatchRef = useRef(null);
1498
- const [referenceElement, setReferenceElement] = useState(null);
1499
- const [rowIndex, setRowIndex] = useState(defaultSelectedRowIndex);
1500
- const [colIndex, setColIndex] = useState(defaultSelectedColIndex);
1501
- let backgroundColor;
1502
- if (isControlled) {
1503
- if (selectedRowIndex !== null && selectedColIndex !== null) {
1504
- backgroundColor = colors[selectedRowIndex][selectedColIndex].value;
1505
- }
1506
- } else if (rowIndex !== undefined && colIndex !== undefined) {
1507
- backgroundColor = colors[rowIndex][colIndex].value;
1508
- }
1509
- const ariaLabelText = useText(ColorSwatchDialog, {
1510
- 'aria-label': ariaLabel
1511
- }, 'aria-label', 'Color swatch');
1512
- useEffect(() => {
1513
- if (isDialogControlled) {
1514
- if (isOpen) {
1515
- setReferenceElement(buttonRef.current);
1516
- } else {
1517
- setReferenceElement(null);
1518
- }
1519
- }
1520
- }, [isOpen, isDialogControlled]);
1521
- const openDialog = () => {
1522
- setReferenceElement(buttonRef.current);
1523
- onDialogChange && onDialogChange({
1524
- isOpen: true
1525
- });
1526
- };
1527
- const closeDialog = () => {
1528
- setReferenceElement(null);
1529
- onDialogChange && onDialogChange({
1530
- isOpen: false
1531
- });
1532
- };
1533
- const onClick = composeEventHandlers(props.onClick, () => {
1534
- if (referenceElement) {
1535
- closeDialog();
1536
- } else {
1537
- openDialog();
1538
- }
1539
- });
1540
- useEffect(() => {
1541
- if (referenceElement && colorSwatchRef.current) {
1542
- const input = colorSwatchRef.current.querySelector('[tabindex="0"]');
1543
- input?.focus();
1544
- }
1545
- }, [referenceElement]);
1546
- const handleSelect = (row, col) => {
1547
- if (isControlled === false) {
1548
- if (row === null || col === null) {
1549
- setRowIndex(undefined);
1550
- setColIndex(undefined);
1551
- } else {
1552
- setRowIndex(row);
1553
- setColIndex(col);
1554
- }
1555
- }
1556
- onSelect && onSelect(row, col);
1557
- };
1558
- return React__default.createElement(React__default.Fragment, null, children ? ( cloneElement(Children.only(children), {
1559
- onClick,
1560
- ref: buttonRef
1561
- })) : React__default.createElement(StyledButton, _extends$2({
1562
- disabled: disabled,
1563
- focusInset: focusInset,
1564
- ref: buttonRef,
1565
- onClick: onClick
1566
- }, buttonProps), React__default.createElement(StyledButtonPreview, {
1567
- backgroundColor: backgroundColor
1568
- }), React__default.createElement(Button.EndIcon, {
1569
- isRotated: referenceElement !== null
1570
- }, React__default.createElement(SvgChevronDownStroke, null))), React__default.createElement(StyledTooltipModal, _extends$2({
1571
- ref: ref,
1572
- zIndex: zIndex,
1573
- hasArrow: hasArrow,
1574
- focusOnMount: false,
1575
- placement: placement,
1576
- isAnimated: isAnimated,
1577
- referenceElement: referenceElement,
1578
- onClose: closeDialog,
1579
- "aria-label": ariaLabelText
1580
- }, props), React__default.createElement(StyledTooltipBody, null, React__default.createElement(ColorSwatch, {
1581
- name: name,
1582
- colors: colors,
1583
- ref: colorSwatchRef,
1584
- isCheckboxGroup: isCheckboxGroup,
1585
- selectedRowIndex: selectedRowIndex,
1586
- selectedColIndex: selectedColIndex,
1587
- defaultSelectedRowIndex: rowIndex,
1588
- defaultSelectedColIndex: colIndex,
1589
- onSelect: handleSelect
1590
- }))));
1591
- });
1592
- ColorSwatchDialog.propTypes = {
1593
- ...ColorSwatch.propTypes,
1594
- placement: PropTypes.oneOf(PLACEMENT),
1595
- onDialogChange: PropTypes.func,
1596
- disabled: PropTypes.bool,
1597
- buttonProps: PropTypes.object,
1598
- zIndex: PropTypes.number,
1599
- hasArrow: PropTypes.bool,
1600
- isAnimated: PropTypes.bool,
1601
- focusInset: PropTypes.bool,
1602
- isOpen: PropTypes.bool
1603
- };
1604
- ColorSwatchDialog.defaultProps = {
1605
- placement: 'bottom-start',
1606
- isAnimated: true,
1607
- zIndex: 1000,
1608
- hasArrow: false
1609
- };
1610
- ColorSwatchDialog.displayName = 'ColorSwatchDialog';
1611
-
1612
- export { ColorSwatch, ColorSwatchDialog, Colorpicker, ColorpickerDialog };