@zendeskgarden/react-colorpickers 8.75.1 → 8.76.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/elements/ColorSwatch/index.js +102 -0
- package/dist/esm/elements/ColorSwatchDialog/index.js +198 -0
- package/dist/esm/elements/Colorpicker/ColorWell.js +109 -0
- package/dist/esm/elements/Colorpicker/index.js +236 -0
- package/dist/esm/elements/Colorpicker/reducer.js +275 -0
- package/dist/esm/elements/ColorpickerDialog/index.js +156 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/node_modules/@zendeskgarden/svg-icons/src/12/check-sm-fill.svg.js +29 -0
- package/dist/esm/node_modules/@zendeskgarden/svg-icons/src/16/chevron-down-stroke.svg.js +25 -0
- package/dist/esm/styled/ColorSwatch/StyledCell.js +22 -0
- package/dist/esm/styled/ColorSwatch/StyledColorSwatch.js +23 -0
- package/dist/esm/styled/ColorSwatch/StyledIcon.js +48 -0
- package/dist/esm/styled/ColorSwatch/StyledSwatchButton.js +27 -0
- package/dist/esm/styled/Colorpicker/StyledAlphaRange.js +37 -0
- package/dist/esm/styled/Colorpicker/StyledColorPicker.js +25 -0
- package/dist/esm/styled/Colorpicker/StyledColorWell.js +36 -0
- package/dist/esm/styled/Colorpicker/StyledColorWellThumb.js +40 -0
- package/dist/esm/styled/Colorpicker/StyledHexField.js +24 -0
- package/dist/esm/styled/Colorpicker/StyledHueRange.js +23 -0
- package/dist/esm/styled/Colorpicker/StyledInput.js +24 -0
- package/dist/esm/styled/Colorpicker/StyledInputGroup.js +22 -0
- package/dist/esm/styled/Colorpicker/StyledLabel.js +23 -0
- package/dist/esm/styled/Colorpicker/StyledPreview.js +37 -0
- package/dist/esm/styled/Colorpicker/StyledRGBAField.js +32 -0
- package/dist/esm/styled/Colorpicker/StyledSliderGroup.js +22 -0
- package/dist/esm/styled/Colorpicker/StyledSliders.js +23 -0
- package/dist/esm/styled/ColorpickerDialog/StyledButton.js +24 -0
- package/dist/esm/styled/ColorpickerDialog/StyledButtonPreview.js +48 -0
- package/dist/esm/styled/ColorpickerDialog/StyledTooltipBody.js +23 -0
- package/dist/esm/styled/ColorpickerDialog/StyledTooltipModal.js +23 -0
- package/dist/esm/styled/common/StyledRange.js +151 -0
- package/dist/esm/styled/common/checkeredBackground.js +28 -0
- package/dist/esm/utils/conversion.js +34 -0
- package/dist/esm/utils/saturation.js +64 -0
- package/dist/esm/utils/validation.js +12 -0
- package/dist/index.cjs.js +33 -49
- package/package.json +9 -9
- package/dist/index.esm.js +0 -1583
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
import React__default, { forwardRef, useContext } from 'react';
|
|
8
|
+
import PropTypes from 'prop-types';
|
|
9
|
+
import { ThemeContext } from 'styled-components';
|
|
10
|
+
import { Tooltip } from '@zendeskgarden/react-tooltips';
|
|
11
|
+
import { useGrid } from '@zendeskgarden/container-grid';
|
|
12
|
+
import { useId } from '@zendeskgarden/container-utilities';
|
|
13
|
+
import SvgCheckSmFill from '../../node_modules/@zendeskgarden/svg-icons/src/12/check-sm-fill.svg.js';
|
|
14
|
+
import '../../styled/Colorpicker/StyledColorPicker.js';
|
|
15
|
+
import '../../styled/Colorpicker/StyledHueRange.js';
|
|
16
|
+
import '../../styled/Colorpicker/StyledAlphaRange.js';
|
|
17
|
+
import '../../styled/Colorpicker/StyledPreview.js';
|
|
18
|
+
import '../../styled/Colorpicker/StyledColorWell.js';
|
|
19
|
+
import '../../styled/Colorpicker/StyledColorWellThumb.js';
|
|
20
|
+
import '../../styled/Colorpicker/StyledSliderGroup.js';
|
|
21
|
+
import '../../styled/Colorpicker/StyledHexField.js';
|
|
22
|
+
import '../../styled/Colorpicker/StyledLabel.js';
|
|
23
|
+
import '../../styled/Colorpicker/StyledInput.js';
|
|
24
|
+
import '../../styled/Colorpicker/StyledInputGroup.js';
|
|
25
|
+
import '../../styled/Colorpicker/StyledRGBAField.js';
|
|
26
|
+
import '../../styled/Colorpicker/StyledSliders.js';
|
|
27
|
+
import '../../styled/ColorpickerDialog/StyledButton.js';
|
|
28
|
+
import '../../styled/ColorpickerDialog/StyledButtonPreview.js';
|
|
29
|
+
import '../../styled/ColorpickerDialog/StyledTooltipModal.js';
|
|
30
|
+
import '../../styled/ColorpickerDialog/StyledTooltipBody.js';
|
|
31
|
+
import { StyledSwatchButton } from '../../styled/ColorSwatch/StyledSwatchButton.js';
|
|
32
|
+
import { StyledColorSwatch } from '../../styled/ColorSwatch/StyledColorSwatch.js';
|
|
33
|
+
import { StyledIcon } from '../../styled/ColorSwatch/StyledIcon.js';
|
|
34
|
+
import { StyledCell } from '../../styled/ColorSwatch/StyledCell.js';
|
|
35
|
+
|
|
36
|
+
const ColorSwatch = forwardRef((_ref, ref) => {
|
|
37
|
+
let {
|
|
38
|
+
colors,
|
|
39
|
+
...props
|
|
40
|
+
} = _ref;
|
|
41
|
+
const {
|
|
42
|
+
rtl
|
|
43
|
+
} = useContext(ThemeContext);
|
|
44
|
+
const {
|
|
45
|
+
getGridCellProps
|
|
46
|
+
} = useGrid({
|
|
47
|
+
rtl,
|
|
48
|
+
matrix: colors,
|
|
49
|
+
selection: true,
|
|
50
|
+
wrap: true,
|
|
51
|
+
idPrefix: useId(undefined),
|
|
52
|
+
...props
|
|
53
|
+
});
|
|
54
|
+
return React__default.createElement(StyledColorSwatch, {
|
|
55
|
+
ref: ref
|
|
56
|
+
}, React__default.createElement("tbody", null, colors.map((row, rowIdx) => React__default.createElement("tr", {
|
|
57
|
+
key: row[0].value
|
|
58
|
+
}, row.map((color, colIdx) => {
|
|
59
|
+
const {
|
|
60
|
+
label,
|
|
61
|
+
value
|
|
62
|
+
} = color;
|
|
63
|
+
const {
|
|
64
|
+
'aria-selected': ariaSelected,
|
|
65
|
+
...other
|
|
66
|
+
} = getGridCellProps({
|
|
67
|
+
colIdx,
|
|
68
|
+
rowIdx,
|
|
69
|
+
type: 'button',
|
|
70
|
+
role: undefined
|
|
71
|
+
});
|
|
72
|
+
return React__default.createElement(StyledCell, {
|
|
73
|
+
key: value,
|
|
74
|
+
"aria-selected": ariaSelected
|
|
75
|
+
}, React__default.createElement(Tooltip, {
|
|
76
|
+
content: label
|
|
77
|
+
}, React__default.createElement(StyledSwatchButton, Object.assign({
|
|
78
|
+
backgroundColor: value,
|
|
79
|
+
"aria-pressed": ariaSelected,
|
|
80
|
+
"aria-label": label
|
|
81
|
+
}, other), React__default.createElement(StyledIcon, {
|
|
82
|
+
color: value,
|
|
83
|
+
selected: ariaSelected
|
|
84
|
+
}, React__default.createElement(SvgCheckSmFill, null)))));
|
|
85
|
+
})))));
|
|
86
|
+
});
|
|
87
|
+
ColorSwatch.displayName = 'ColorSwatch';
|
|
88
|
+
ColorSwatch.propTypes = {
|
|
89
|
+
colors: PropTypes.arrayOf(PropTypes.any).isRequired,
|
|
90
|
+
rowIndex: PropTypes.number,
|
|
91
|
+
colIndex: PropTypes.number,
|
|
92
|
+
selectedRowIndex: PropTypes.number,
|
|
93
|
+
selectedColIndex: PropTypes.number,
|
|
94
|
+
defaultRowIndex: PropTypes.number,
|
|
95
|
+
defaultColIndex: PropTypes.number,
|
|
96
|
+
defaultSelectedRowIndex: PropTypes.number,
|
|
97
|
+
defaultSelectedColIndex: PropTypes.number,
|
|
98
|
+
onChange: PropTypes.func,
|
|
99
|
+
onSelect: PropTypes.func
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { ColorSwatch };
|
|
@@ -0,0 +1,198 @@
|
|
|
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
|
+
import React__default, { forwardRef, useRef, useState, useEffect, cloneElement, Children } from 'react';
|
|
8
|
+
import PropTypes from 'prop-types';
|
|
9
|
+
import { Button } from '@zendeskgarden/react-buttons';
|
|
10
|
+
import { PLACEMENT } from '@zendeskgarden/react-modals';
|
|
11
|
+
import { useText } from '@zendeskgarden/react-theming';
|
|
12
|
+
import { composeEventHandlers } from '@zendeskgarden/container-utilities';
|
|
13
|
+
import SvgChevronDownStroke from '../../node_modules/@zendeskgarden/svg-icons/src/16/chevron-down-stroke.svg.js';
|
|
14
|
+
import { ColorSwatch } from '../ColorSwatch/index.js';
|
|
15
|
+
import '../../styled/Colorpicker/StyledColorPicker.js';
|
|
16
|
+
import '../../styled/Colorpicker/StyledHueRange.js';
|
|
17
|
+
import '../../styled/Colorpicker/StyledAlphaRange.js';
|
|
18
|
+
import '../../styled/Colorpicker/StyledPreview.js';
|
|
19
|
+
import '../../styled/Colorpicker/StyledColorWell.js';
|
|
20
|
+
import '../../styled/Colorpicker/StyledColorWellThumb.js';
|
|
21
|
+
import '../../styled/Colorpicker/StyledSliderGroup.js';
|
|
22
|
+
import '../../styled/Colorpicker/StyledHexField.js';
|
|
23
|
+
import '../../styled/Colorpicker/StyledLabel.js';
|
|
24
|
+
import '../../styled/Colorpicker/StyledInput.js';
|
|
25
|
+
import '../../styled/Colorpicker/StyledInputGroup.js';
|
|
26
|
+
import '../../styled/Colorpicker/StyledRGBAField.js';
|
|
27
|
+
import '../../styled/Colorpicker/StyledSliders.js';
|
|
28
|
+
import { StyledButton } from '../../styled/ColorpickerDialog/StyledButton.js';
|
|
29
|
+
import { StyledButtonPreview } from '../../styled/ColorpickerDialog/StyledButtonPreview.js';
|
|
30
|
+
import { StyledTooltipModal } from '../../styled/ColorpickerDialog/StyledTooltipModal.js';
|
|
31
|
+
import { StyledTooltipBody } from '../../styled/ColorpickerDialog/StyledTooltipBody.js';
|
|
32
|
+
import '../../styled/ColorSwatch/StyledSwatchButton.js';
|
|
33
|
+
import '../../styled/ColorSwatch/StyledColorSwatch.js';
|
|
34
|
+
import '../../styled/ColorSwatch/StyledIcon.js';
|
|
35
|
+
import '../../styled/ColorSwatch/StyledCell.js';
|
|
36
|
+
|
|
37
|
+
const ColorSwatchDialog = forwardRef((_ref, ref) => {
|
|
38
|
+
let {
|
|
39
|
+
colors,
|
|
40
|
+
rowIndex,
|
|
41
|
+
colIndex,
|
|
42
|
+
selectedRowIndex,
|
|
43
|
+
selectedColIndex,
|
|
44
|
+
defaultRowIndex,
|
|
45
|
+
defaultColIndex,
|
|
46
|
+
defaultSelectedRowIndex,
|
|
47
|
+
defaultSelectedColIndex,
|
|
48
|
+
placement,
|
|
49
|
+
onChange,
|
|
50
|
+
onSelect,
|
|
51
|
+
hasArrow,
|
|
52
|
+
isAnimated,
|
|
53
|
+
popperModifiers,
|
|
54
|
+
zIndex,
|
|
55
|
+
isOpen,
|
|
56
|
+
focusInset,
|
|
57
|
+
disabled,
|
|
58
|
+
buttonProps,
|
|
59
|
+
onDialogChange,
|
|
60
|
+
children,
|
|
61
|
+
'aria-label': ariaLabel,
|
|
62
|
+
...props
|
|
63
|
+
} = _ref;
|
|
64
|
+
const controlledFocus = rowIndex !== null && colIndex !== null && rowIndex !== undefined && colIndex !== undefined;
|
|
65
|
+
const controlledSelect = selectedRowIndex !== null && selectedColIndex !== null && selectedRowIndex !== undefined && selectedColIndex !== undefined;
|
|
66
|
+
const isControlled = controlledFocus || controlledSelect;
|
|
67
|
+
const isDialogControlled = isOpen !== undefined && isOpen !== null;
|
|
68
|
+
const buttonRef = useRef(null);
|
|
69
|
+
const colorSwatchRef = useRef(null);
|
|
70
|
+
const [referenceElement, setReferenceElement] = useState();
|
|
71
|
+
const [uncontrolledSelectedRowIndex, setUncontrolledSelectedRowIndex] = useState(defaultSelectedRowIndex || 0);
|
|
72
|
+
const [uncontrolledSelectedColIndex, setUncontrolledSelectedColIndex] = useState(defaultSelectedColIndex || 0);
|
|
73
|
+
const [uncontrolledRowIndex, setUncontrolledRowIndex] = useState(defaultRowIndex || 0);
|
|
74
|
+
const [uncontrolledColIndex, setUncontrolledColIndex] = useState(defaultColIndex || 0);
|
|
75
|
+
const ariaLabelText = useText(ColorSwatchDialog, {
|
|
76
|
+
'aria-label': ariaLabel
|
|
77
|
+
}, 'aria-label', 'Color swatch');
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (isDialogControlled) {
|
|
80
|
+
if (isOpen) {
|
|
81
|
+
setReferenceElement(buttonRef.current);
|
|
82
|
+
} else {
|
|
83
|
+
setReferenceElement(null);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}, [isOpen, isDialogControlled]);
|
|
87
|
+
let uncontrolledSelectedColor;
|
|
88
|
+
let controlledSelectedColor;
|
|
89
|
+
if (uncontrolledSelectedRowIndex > -1 && uncontrolledSelectedColIndex > -1) {
|
|
90
|
+
uncontrolledSelectedColor = colors[uncontrolledSelectedRowIndex][uncontrolledSelectedColIndex];
|
|
91
|
+
}
|
|
92
|
+
if (selectedRowIndex !== undefined && selectedColIndex !== undefined && selectedRowIndex > -1 && selectedColIndex > -1) {
|
|
93
|
+
controlledSelectedColor = colors[selectedRowIndex][selectedColIndex];
|
|
94
|
+
}
|
|
95
|
+
const openDialog = () => {
|
|
96
|
+
setReferenceElement(buttonRef.current);
|
|
97
|
+
onDialogChange && onDialogChange({
|
|
98
|
+
isOpen: true
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const closeDialog = () => {
|
|
102
|
+
setUncontrolledRowIndex(uncontrolledSelectedRowIndex);
|
|
103
|
+
setUncontrolledColIndex(uncontrolledSelectedColIndex);
|
|
104
|
+
setReferenceElement(null);
|
|
105
|
+
onDialogChange && onDialogChange({
|
|
106
|
+
isOpen: false
|
|
107
|
+
});
|
|
108
|
+
};
|
|
109
|
+
const onClick = composeEventHandlers(props.onClick, () => {
|
|
110
|
+
if (referenceElement) {
|
|
111
|
+
closeDialog();
|
|
112
|
+
} else {
|
|
113
|
+
openDialog();
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (referenceElement && colorSwatchRef.current) {
|
|
118
|
+
const focusableButton = colorSwatchRef.current.querySelector('[tabindex="0"]');
|
|
119
|
+
const selectedCell = colorSwatchRef.current.querySelector('[aria-selected="true"]');
|
|
120
|
+
if (selectedCell) {
|
|
121
|
+
selectedCell.children[0].focus();
|
|
122
|
+
} else {
|
|
123
|
+
focusableButton?.focus();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}, [referenceElement]);
|
|
127
|
+
return React__default.createElement(React__default.Fragment, null, children ? ( cloneElement(Children.only(children), {
|
|
128
|
+
onClick,
|
|
129
|
+
ref: buttonRef
|
|
130
|
+
})) : React__default.createElement(StyledButton, Object.assign({
|
|
131
|
+
disabled: disabled,
|
|
132
|
+
focusInset: focusInset,
|
|
133
|
+
ref: buttonRef,
|
|
134
|
+
onClick: onClick
|
|
135
|
+
}, buttonProps), React__default.createElement(StyledButtonPreview, {
|
|
136
|
+
backgroundColor: isControlled ? controlledSelectedColor?.value : uncontrolledSelectedColor?.value
|
|
137
|
+
}), React__default.createElement(Button.EndIcon, {
|
|
138
|
+
isRotated: referenceElement != null
|
|
139
|
+
}, React__default.createElement(SvgChevronDownStroke, null))), React__default.createElement(StyledTooltipModal, Object.assign({
|
|
140
|
+
ref: ref,
|
|
141
|
+
zIndex: zIndex,
|
|
142
|
+
hasArrow: hasArrow,
|
|
143
|
+
focusOnMount: false,
|
|
144
|
+
placement: placement,
|
|
145
|
+
isAnimated: isAnimated,
|
|
146
|
+
popperModifiers: popperModifiers,
|
|
147
|
+
referenceElement: referenceElement,
|
|
148
|
+
onClose: closeDialog,
|
|
149
|
+
"aria-label": ariaLabelText
|
|
150
|
+
}, props), React__default.createElement(StyledTooltipBody, null, React__default.createElement(ColorSwatch, {
|
|
151
|
+
colors: colors,
|
|
152
|
+
ref: colorSwatchRef,
|
|
153
|
+
rowIndex: rowIndex,
|
|
154
|
+
colIndex: colIndex,
|
|
155
|
+
selectedRowIndex: selectedRowIndex,
|
|
156
|
+
selectedColIndex: selectedColIndex,
|
|
157
|
+
defaultRowIndex: uncontrolledRowIndex,
|
|
158
|
+
defaultColIndex: uncontrolledColIndex,
|
|
159
|
+
defaultSelectedRowIndex: uncontrolledSelectedRowIndex,
|
|
160
|
+
defaultSelectedColIndex: uncontrolledSelectedColIndex,
|
|
161
|
+
onChange: (rowIdx, colIdx) => {
|
|
162
|
+
if (isControlled === false) {
|
|
163
|
+
setUncontrolledRowIndex(rowIdx);
|
|
164
|
+
setUncontrolledColIndex(colIdx);
|
|
165
|
+
}
|
|
166
|
+
onChange && onChange(rowIdx, colIdx);
|
|
167
|
+
},
|
|
168
|
+
onSelect: (rowIdx, colIdx) => {
|
|
169
|
+
if (isControlled === false) {
|
|
170
|
+
setUncontrolledSelectedRowIndex(rowIdx);
|
|
171
|
+
setUncontrolledSelectedColIndex(colIdx);
|
|
172
|
+
}
|
|
173
|
+
onSelect && onSelect(rowIdx, colIdx);
|
|
174
|
+
}
|
|
175
|
+
}))));
|
|
176
|
+
});
|
|
177
|
+
ColorSwatchDialog.propTypes = {
|
|
178
|
+
...ColorSwatch.propTypes,
|
|
179
|
+
placement: PropTypes.oneOf(PLACEMENT),
|
|
180
|
+
onDialogChange: PropTypes.func,
|
|
181
|
+
disabled: PropTypes.bool,
|
|
182
|
+
buttonProps: PropTypes.object,
|
|
183
|
+
popperModifiers: PropTypes.any,
|
|
184
|
+
zIndex: PropTypes.number,
|
|
185
|
+
hasArrow: PropTypes.bool,
|
|
186
|
+
isAnimated: PropTypes.bool,
|
|
187
|
+
focusInset: PropTypes.bool,
|
|
188
|
+
isOpen: PropTypes.bool
|
|
189
|
+
};
|
|
190
|
+
ColorSwatchDialog.defaultProps = {
|
|
191
|
+
placement: 'bottom-start',
|
|
192
|
+
isAnimated: true,
|
|
193
|
+
zIndex: 1000,
|
|
194
|
+
hasArrow: false
|
|
195
|
+
};
|
|
196
|
+
ColorSwatchDialog.displayName = 'ColorSwatchDialog';
|
|
197
|
+
|
|
198
|
+
export { ColorSwatchDialog };
|
|
@@ -0,0 +1,109 @@
|
|
|
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
|
+
import React__default, { useContext, useRef, useState, useEffect, useMemo, useCallback } from 'react';
|
|
8
|
+
import throttle from 'lodash.throttle';
|
|
9
|
+
import { ThemeContext } from 'styled-components';
|
|
10
|
+
import { hslToHsv } from '../../utils/conversion.js';
|
|
11
|
+
import { getThumbPosition, getNextHsv } from '../../utils/saturation.js';
|
|
12
|
+
import '../../styled/Colorpicker/StyledColorPicker.js';
|
|
13
|
+
import '../../styled/Colorpicker/StyledHueRange.js';
|
|
14
|
+
import '../../styled/Colorpicker/StyledAlphaRange.js';
|
|
15
|
+
import '../../styled/Colorpicker/StyledPreview.js';
|
|
16
|
+
import { StyledColorWell } from '../../styled/Colorpicker/StyledColorWell.js';
|
|
17
|
+
import { StyledColorWellThumb } from '../../styled/Colorpicker/StyledColorWellThumb.js';
|
|
18
|
+
import '../../styled/Colorpicker/StyledSliderGroup.js';
|
|
19
|
+
import '../../styled/Colorpicker/StyledHexField.js';
|
|
20
|
+
import '../../styled/Colorpicker/StyledLabel.js';
|
|
21
|
+
import '../../styled/Colorpicker/StyledInput.js';
|
|
22
|
+
import '../../styled/Colorpicker/StyledInputGroup.js';
|
|
23
|
+
import '../../styled/Colorpicker/StyledRGBAField.js';
|
|
24
|
+
import '../../styled/Colorpicker/StyledSliders.js';
|
|
25
|
+
import '../../styled/ColorpickerDialog/StyledButton.js';
|
|
26
|
+
import '../../styled/ColorpickerDialog/StyledButtonPreview.js';
|
|
27
|
+
import '../../styled/ColorpickerDialog/StyledTooltipModal.js';
|
|
28
|
+
import '../../styled/ColorpickerDialog/StyledTooltipBody.js';
|
|
29
|
+
import '../../styled/ColorSwatch/StyledSwatchButton.js';
|
|
30
|
+
import '../../styled/ColorSwatch/StyledColorSwatch.js';
|
|
31
|
+
import '../../styled/ColorSwatch/StyledIcon.js';
|
|
32
|
+
import '../../styled/ColorSwatch/StyledCell.js';
|
|
33
|
+
|
|
34
|
+
const ColorWell = React__default.memo(_ref => {
|
|
35
|
+
let {
|
|
36
|
+
hue,
|
|
37
|
+
saturation,
|
|
38
|
+
lightness,
|
|
39
|
+
onChange
|
|
40
|
+
} = _ref;
|
|
41
|
+
const {
|
|
42
|
+
rtl
|
|
43
|
+
} = useContext(ThemeContext);
|
|
44
|
+
const containerRef = useRef(null);
|
|
45
|
+
const hsv = hslToHsv(hue, saturation, lightness);
|
|
46
|
+
const mouseActiveRef = useRef(false);
|
|
47
|
+
const [x, setX] = useState(0);
|
|
48
|
+
const [y, setY] = useState(0);
|
|
49
|
+
const {
|
|
50
|
+
topFromMouse,
|
|
51
|
+
leftFromMouse
|
|
52
|
+
} = getThumbPosition(x, y, rtl, containerRef);
|
|
53
|
+
const [topPosition, setTopPosition] = useState(0);
|
|
54
|
+
const [leftPosition, setLeftPosition] = useState(0);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
setTopPosition(100 - hsv.v);
|
|
57
|
+
setLeftPosition(rtl ? 100 - hsv.s : hsv.s);
|
|
58
|
+
}, [hsv.s, hsv.v, rtl]);
|
|
59
|
+
const throttledChange = useMemo(() => {
|
|
60
|
+
return throttle(e => {
|
|
61
|
+
if (containerRef.current) {
|
|
62
|
+
const nextHsv = getNextHsv(e, hue, containerRef.current, rtl);
|
|
63
|
+
onChange && onChange(nextHsv, e);
|
|
64
|
+
}
|
|
65
|
+
}, 50);
|
|
66
|
+
}, [hue, rtl, onChange]);
|
|
67
|
+
const handleMouseMove = useCallback(e => {
|
|
68
|
+
mouseActiveRef.current = true;
|
|
69
|
+
setX(e.pageX);
|
|
70
|
+
setY(e.pageY);
|
|
71
|
+
throttledChange(e);
|
|
72
|
+
}, [throttledChange]);
|
|
73
|
+
const handleMouseUp = useCallback(() => {
|
|
74
|
+
mouseActiveRef.current = true;
|
|
75
|
+
setTimeout(() => {
|
|
76
|
+
mouseActiveRef.current = false;
|
|
77
|
+
});
|
|
78
|
+
throttledChange.cancel();
|
|
79
|
+
window.removeEventListener('mousemove', handleMouseMove);
|
|
80
|
+
window.removeEventListener('mouseup', handleMouseUp);
|
|
81
|
+
}, [throttledChange, handleMouseMove]);
|
|
82
|
+
const handleMouseDown = useCallback(e => {
|
|
83
|
+
mouseActiveRef.current = true;
|
|
84
|
+
e.persist();
|
|
85
|
+
handleMouseMove(e);
|
|
86
|
+
throttledChange(e);
|
|
87
|
+
window.addEventListener('mousemove', handleMouseMove);
|
|
88
|
+
window.addEventListener('mouseup', handleMouseUp);
|
|
89
|
+
}, [throttledChange, handleMouseMove, handleMouseUp]);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
return () => {
|
|
92
|
+
throttledChange.cancel();
|
|
93
|
+
};
|
|
94
|
+
}, [throttledChange]);
|
|
95
|
+
return (
|
|
96
|
+
React__default.createElement(StyledColorWell, {
|
|
97
|
+
hue: hue,
|
|
98
|
+
ref: containerRef,
|
|
99
|
+
role: "presentation",
|
|
100
|
+
onMouseDown: handleMouseDown
|
|
101
|
+
}, React__default.createElement(StyledColorWellThumb, {
|
|
102
|
+
top: mouseActiveRef.current ? topFromMouse : topPosition,
|
|
103
|
+
left: mouseActiveRef.current ? leftFromMouse : leftPosition
|
|
104
|
+
}))
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
ColorWell.displayName = 'ColorWell';
|
|
108
|
+
|
|
109
|
+
export { ColorWell };
|
|
@@ -0,0 +1,236 @@
|
|
|
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
|
+
import React__default, { forwardRef, useReducer, useRef, useMemo, useEffect, useCallback } from 'react';
|
|
8
|
+
import PropTypes from 'prop-types';
|
|
9
|
+
import { Field, Label } from '@zendeskgarden/react-forms';
|
|
10
|
+
import { ColorWell } from './ColorWell.js';
|
|
11
|
+
import { StyledColorPicker } from '../../styled/Colorpicker/StyledColorPicker.js';
|
|
12
|
+
import { StyledHueRange } from '../../styled/Colorpicker/StyledHueRange.js';
|
|
13
|
+
import { StyledAlphaRange } from '../../styled/Colorpicker/StyledAlphaRange.js';
|
|
14
|
+
import { StyledPreview } from '../../styled/Colorpicker/StyledPreview.js';
|
|
15
|
+
import '../../styled/Colorpicker/StyledColorWell.js';
|
|
16
|
+
import '../../styled/Colorpicker/StyledColorWellThumb.js';
|
|
17
|
+
import { StyledSliderGroup } from '../../styled/Colorpicker/StyledSliderGroup.js';
|
|
18
|
+
import { StyledHexField } from '../../styled/Colorpicker/StyledHexField.js';
|
|
19
|
+
import { StyledLabel } from '../../styled/Colorpicker/StyledLabel.js';
|
|
20
|
+
import { StyledInput } from '../../styled/Colorpicker/StyledInput.js';
|
|
21
|
+
import { StyledInputGroup } from '../../styled/Colorpicker/StyledInputGroup.js';
|
|
22
|
+
import { StyledRGBAField } from '../../styled/Colorpicker/StyledRGBAField.js';
|
|
23
|
+
import { StyledSliders } from '../../styled/Colorpicker/StyledSliders.js';
|
|
24
|
+
import '../../styled/ColorpickerDialog/StyledButton.js';
|
|
25
|
+
import '../../styled/ColorpickerDialog/StyledButtonPreview.js';
|
|
26
|
+
import '../../styled/ColorpickerDialog/StyledTooltipModal.js';
|
|
27
|
+
import '../../styled/ColorpickerDialog/StyledTooltipBody.js';
|
|
28
|
+
import '../../styled/ColorSwatch/StyledSwatchButton.js';
|
|
29
|
+
import '../../styled/ColorSwatch/StyledColorSwatch.js';
|
|
30
|
+
import '../../styled/ColorSwatch/StyledIcon.js';
|
|
31
|
+
import '../../styled/ColorSwatch/StyledCell.js';
|
|
32
|
+
import { reducer, getInitialState, convertStringToColor, areColorsEqual } from './reducer.js';
|
|
33
|
+
|
|
34
|
+
const Colorpicker = forwardRef((_ref, ref) => {
|
|
35
|
+
let {
|
|
36
|
+
color,
|
|
37
|
+
defaultColor,
|
|
38
|
+
isOpaque,
|
|
39
|
+
labels = {},
|
|
40
|
+
autofocus,
|
|
41
|
+
onChange,
|
|
42
|
+
...props
|
|
43
|
+
} = _ref;
|
|
44
|
+
const [state, dispatch] = useReducer(reducer, getInitialState(color || defaultColor));
|
|
45
|
+
const previousComputedColorRef = useRef(state.color);
|
|
46
|
+
const previousStateColorRef = useRef(state.color);
|
|
47
|
+
const computedColor = useMemo(() => {
|
|
48
|
+
let retVal = state.color;
|
|
49
|
+
if (color) {
|
|
50
|
+
if (typeof color === 'string') {
|
|
51
|
+
const convertedColor = convertStringToColor(color);
|
|
52
|
+
if (convertedColor) {
|
|
53
|
+
retVal = convertedColor;
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
retVal = color;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (isOpaque) {
|
|
60
|
+
retVal.alpha = 100;
|
|
61
|
+
}
|
|
62
|
+
if (areColorsEqual(retVal, previousComputedColorRef.current)) {
|
|
63
|
+
return previousComputedColorRef.current;
|
|
64
|
+
}
|
|
65
|
+
return retVal;
|
|
66
|
+
}, [color, isOpaque, state.color]);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (!areColorsEqual(previousStateColorRef.current, state.color) && !areColorsEqual(color, state.color)) {
|
|
69
|
+
onChange && onChange(state.color);
|
|
70
|
+
}
|
|
71
|
+
previousStateColorRef.current = state.color;
|
|
72
|
+
}, [color, onChange, state.color]);
|
|
73
|
+
if (previousComputedColorRef.current !== computedColor) {
|
|
74
|
+
dispatch({
|
|
75
|
+
type: 'RESET_COLOR',
|
|
76
|
+
payload: computedColor
|
|
77
|
+
});
|
|
78
|
+
previousComputedColorRef.current = computedColor;
|
|
79
|
+
}
|
|
80
|
+
const handleColorWellChange = useCallback(hsv => {
|
|
81
|
+
dispatch({
|
|
82
|
+
type: 'SATURATION_CHANGE',
|
|
83
|
+
payload: hsv
|
|
84
|
+
});
|
|
85
|
+
}, []);
|
|
86
|
+
const handleHueChange = useCallback(e => {
|
|
87
|
+
dispatch({
|
|
88
|
+
type: 'HUE_CHANGE',
|
|
89
|
+
payload: e.target.value
|
|
90
|
+
});
|
|
91
|
+
}, []);
|
|
92
|
+
const handleAlphaSliderChange = useCallback(e => {
|
|
93
|
+
dispatch({
|
|
94
|
+
type: 'ALPHA_SLIDER_CHANGE',
|
|
95
|
+
payload: e.target.value
|
|
96
|
+
});
|
|
97
|
+
}, []);
|
|
98
|
+
const handleHexChange = useCallback(e => {
|
|
99
|
+
dispatch({
|
|
100
|
+
type: 'HEX_CHANGE',
|
|
101
|
+
payload: e.target.value
|
|
102
|
+
});
|
|
103
|
+
}, []);
|
|
104
|
+
const handleRedChange = useCallback(e => {
|
|
105
|
+
dispatch({
|
|
106
|
+
type: 'RED_CHANGE',
|
|
107
|
+
payload: e.target.value
|
|
108
|
+
});
|
|
109
|
+
}, []);
|
|
110
|
+
const handleGreenChange = useCallback(e => {
|
|
111
|
+
dispatch({
|
|
112
|
+
type: 'GREEN_CHANGE',
|
|
113
|
+
payload: e.target.value
|
|
114
|
+
});
|
|
115
|
+
}, []);
|
|
116
|
+
const handleBlueChange = useCallback(e => {
|
|
117
|
+
dispatch({
|
|
118
|
+
type: 'BLUE_CHANGE',
|
|
119
|
+
payload: e.target.value
|
|
120
|
+
});
|
|
121
|
+
}, []);
|
|
122
|
+
const handleAlphaChange = useCallback(e => {
|
|
123
|
+
dispatch({
|
|
124
|
+
type: 'ALPHA_CHANGE',
|
|
125
|
+
payload: e.target.value
|
|
126
|
+
});
|
|
127
|
+
}, []);
|
|
128
|
+
const handleBlur = useCallback(() => {
|
|
129
|
+
dispatch({
|
|
130
|
+
type: 'RESET_COLOR',
|
|
131
|
+
payload: computedColor
|
|
132
|
+
});
|
|
133
|
+
}, [computedColor]);
|
|
134
|
+
return React__default.createElement(StyledColorPicker, Object.assign({
|
|
135
|
+
ref: ref,
|
|
136
|
+
isOpaque: isOpaque
|
|
137
|
+
}, props), React__default.createElement(ColorWell, {
|
|
138
|
+
hue: computedColor.hue,
|
|
139
|
+
saturation: computedColor.saturation,
|
|
140
|
+
lightness: computedColor.lightness,
|
|
141
|
+
onChange: handleColorWellChange
|
|
142
|
+
}), React__default.createElement(StyledSliderGroup, null, React__default.createElement(StyledPreview, {
|
|
143
|
+
red: computedColor.red,
|
|
144
|
+
green: computedColor.green,
|
|
145
|
+
blue: computedColor.blue,
|
|
146
|
+
alpha: computedColor.alpha,
|
|
147
|
+
isOpaque: isOpaque
|
|
148
|
+
}), React__default.createElement(StyledSliders, {
|
|
149
|
+
isOpaque: isOpaque
|
|
150
|
+
}, React__default.createElement(Field, null, React__default.createElement(Label, {
|
|
151
|
+
hidden: true
|
|
152
|
+
}, labels.hueSlider || 'Hue slider'), React__default.createElement(StyledHueRange, {
|
|
153
|
+
step: 1,
|
|
154
|
+
max: 360,
|
|
155
|
+
value: computedColor.hue,
|
|
156
|
+
isOpaque: isOpaque,
|
|
157
|
+
onChange: handleHueChange
|
|
158
|
+
})), !isOpaque && React__default.createElement(Field, null, React__default.createElement(Label, {
|
|
159
|
+
hidden: true
|
|
160
|
+
}, labels.alphaSlider || 'Alpha slider'), React__default.createElement(StyledAlphaRange, {
|
|
161
|
+
max: 1,
|
|
162
|
+
step: 0.01,
|
|
163
|
+
value: computedColor.alpha / 100,
|
|
164
|
+
onChange: handleAlphaSliderChange,
|
|
165
|
+
red: computedColor.red,
|
|
166
|
+
green: computedColor.green,
|
|
167
|
+
blue: computedColor.blue
|
|
168
|
+
})))), React__default.createElement(StyledInputGroup, null, React__default.createElement(StyledHexField, null, React__default.createElement(StyledLabel, {
|
|
169
|
+
isRegular: true
|
|
170
|
+
}, labels.hex || 'Hex'), React__default.createElement(StyledInput, {
|
|
171
|
+
isCompact: true,
|
|
172
|
+
maxLength: 7,
|
|
173
|
+
value: state.hexInput
|
|
174
|
+
,
|
|
175
|
+
autoFocus: autofocus,
|
|
176
|
+
spellCheck: false,
|
|
177
|
+
onBlur: handleBlur,
|
|
178
|
+
onChange: handleHexChange
|
|
179
|
+
})), React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
|
|
180
|
+
isRegular: true
|
|
181
|
+
}, labels.red || 'R'), React__default.createElement(StyledInput, {
|
|
182
|
+
isCompact: true,
|
|
183
|
+
type: "number",
|
|
184
|
+
min: "0",
|
|
185
|
+
max: "255",
|
|
186
|
+
maxLength: 3,
|
|
187
|
+
value: state.redInput,
|
|
188
|
+
onBlur: handleBlur,
|
|
189
|
+
onChange: handleRedChange
|
|
190
|
+
})), React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
|
|
191
|
+
isRegular: true
|
|
192
|
+
}, labels.green || 'G'), React__default.createElement(StyledInput, {
|
|
193
|
+
isCompact: true,
|
|
194
|
+
type: "number",
|
|
195
|
+
min: "0",
|
|
196
|
+
max: "255",
|
|
197
|
+
maxLength: 3,
|
|
198
|
+
value: state.greenInput,
|
|
199
|
+
onBlur: handleBlur,
|
|
200
|
+
onChange: handleGreenChange
|
|
201
|
+
})), React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
|
|
202
|
+
isRegular: true
|
|
203
|
+
}, labels.blue || 'B'), React__default.createElement(StyledInput, {
|
|
204
|
+
isCompact: true,
|
|
205
|
+
type: "number",
|
|
206
|
+
min: "0",
|
|
207
|
+
max: "255",
|
|
208
|
+
maxLength: 3,
|
|
209
|
+
value: state.blueInput,
|
|
210
|
+
onBlur: handleBlur,
|
|
211
|
+
onChange: handleBlueChange
|
|
212
|
+
})), !isOpaque && React__default.createElement(StyledRGBAField, null, React__default.createElement(StyledLabel, {
|
|
213
|
+
isRegular: true
|
|
214
|
+
}, labels.alpha || 'A'), React__default.createElement(StyledInput, {
|
|
215
|
+
isCompact: true,
|
|
216
|
+
type: "number",
|
|
217
|
+
min: "0",
|
|
218
|
+
max: "100",
|
|
219
|
+
value: state.alphaInput,
|
|
220
|
+
onBlur: handleBlur,
|
|
221
|
+
onChange: handleAlphaChange
|
|
222
|
+
}))));
|
|
223
|
+
});
|
|
224
|
+
Colorpicker.defaultProps = {
|
|
225
|
+
defaultColor: '#fff'
|
|
226
|
+
};
|
|
227
|
+
Colorpicker.displayName = 'Colorpicker';
|
|
228
|
+
Colorpicker.propTypes = {
|
|
229
|
+
color: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
|
|
230
|
+
isOpaque: PropTypes.bool,
|
|
231
|
+
onChange: PropTypes.func,
|
|
232
|
+
labels: PropTypes.object,
|
|
233
|
+
defaultColor: PropTypes.oneOfType([PropTypes.object, PropTypes.string])
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
export { Colorpicker };
|