-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.jsx
More file actions
280 lines (252 loc) · 8.39 KB
/
index.jsx
File metadata and controls
280 lines (252 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import React, { useState, useRef } from "react";
import classnames from "classnames";
import { Down, ColorPicker as ColorPickerIcon } from "neetoicons";
import PropTypes from "prop-types";
import {
HexColorPicker,
HexColorInput,
HexAlphaColorPicker,
} from "react-colorful";
import { useTranslation } from "react-i18next";
import tinycolor from "tinycolor2";
import useEyeDropper from "use-eye-dropper";
import Button from "components/Button";
import Dropdown from "components/Dropdown";
import Typography from "components/Typography";
import { getLocale, noop } from "utils";
import useRecentlyUsedColors from "./hooks/useRecentlyUsedColors";
import Palette from "./Palette";
const TARGET_SIZES = {
large: "large",
medium: "medium",
small: "small",
};
const ColorPicker = ({
color = "",
size = TARGET_SIZES.large,
onChange = noop,
dropdownProps,
showEyeDropper = true,
showHexValue = false,
showTransparencyControl = false,
showPicker = true,
portalProps,
colorPalette,
showRecentlyUsedColors = showPicker,
}) => {
const { t, i18n } = useTranslation();
const [colorInternal, setColorInternal] = useState(color);
const [isColorSelected, setIsColorSelected] = useState(false);
const hexColorInputValue = useRef("");
const { open, isSupported } = useEyeDropper({ pickRadius: 3 });
const [recentlyUsedColors, setRecentlyUsedColors] = useRecentlyUsedColors();
const PickerComponent = showTransparencyControl
? HexAlphaColorPicker
: HexColorPicker;
const colorValue = color ?? colorInternal ?? "";
const getColor = colorValue => {
const color = tinycolor(colorValue);
if (color.isValid()) {
let hex = color.toHexString();
// return `transparent` for transparent colors.
if (color.getAlpha() === 0) hex = colorValue;
else if (showTransparencyControl) hex = color.toHex8String();
return { hex, rgb: color.toRgb() };
}
return { hex: colorValue, rgb: colorValue };
};
const onColorChange = color => {
setIsColorSelected(true);
const changeHandler = onChange ?? setColorInternal;
changeHandler(getColor(color));
};
const onColorInputChange = hex => {
// HexColorInput onChange will trigger only if the input value is a valid color
hexColorInputValue.current = hex;
if (hex.length !== (showTransparencyControl ? 9 : 7)) return;
onColorChange(hex);
hexColorInputValue.current = "";
};
const onBlur = () => {
if (!hexColorInputValue.current) return;
onColorChange(hexColorInputValue.current);
hexColorInputValue.current = "";
};
const pickColor = async () => {
try {
const colorResponse = await open();
const hex = tinycolor(colorResponse.sRGBHex).toHexString();
onColorChange(hex);
} catch {
// Ensures component is still mounted
// before calling setState
// if (!e.canceled) setError(e);
}
};
const onClose = () => {
if (!showRecentlyUsedColors || !isColorSelected) return;
const newColor = getColor(colorValue);
const recentColorsExcludingNew = recentlyUsedColors.filter(
({ hex }) => hex !== newColor.hex
);
const updatedColors = [newColor, ...recentColorsExcludingNew];
if (updatedColors.length > 14) updatedColors.pop();
setRecentlyUsedColors(updatedColors);
setIsColorSelected(false);
};
const Target = ({ size }) => (
<button
data-cy="color-picker-target"
data-testid="neeto-color-picker"
type="button"
className={classnames("neeto-ui-colorpicker__target", {
"neeto-ui-colorpicker__target-size--large": size === TARGET_SIZES.large,
"neeto-ui-colorpicker__target-size--medium":
size === TARGET_SIZES.medium,
"neeto-ui-colorpicker__target-size--small": size === TARGET_SIZES.small,
})}
>
{showHexValue && (
<span className="neeto-ui-colorpicker-target__code">{color}</span>
)}
<span className="neeto-ui-colorpicker-target__color-wrapper">
<span
className="neeto-ui-colorpicker-target__color neeto-ui-border-gray-200"
style={{ backgroundColor: colorValue }}
/>
<span className="neeto-ui-colorpicker-target__icon">
<Down size={16} />
</span>
</span>
</button>
);
return (
<Dropdown
className="neeto-ui-colorpicker__dropdown"
closeOnSelect={false}
customTarget={<Target {...{ size }} />}
label={colorValue}
position="bottom-start"
{...{ ...dropdownProps, onClose }}
dropdownProps={{ ...dropdownProps?.dropdownProps, ...portalProps }}
>
<div className="neeto-ui-colorpicker__popover">
{showPicker && (
<>
<div
className="neeto-ui-colorpicker__pointer"
data-testid="neeto-color-picker-section"
>
<PickerComponent color={colorValue} onChange={onColorChange} />
</div>
<div className="neeto-ui-flex neeto-ui-items-center neeto-ui-justify-center neeto-ui-mt-3 neeto-ui-gap-2">
{showEyeDropper && isSupported() && (
<Button
className="neeto-ui-colorpicker__eyedropper-btn"
icon={ColorPickerIcon}
size="small"
style="text"
type="button"
onClick={pickColor}
/>
)}
<div className="neeto-ui-input__wrapper">
<div
className="neeto-ui-colorpicker__input neeto-ui-input neeto-ui-input--small"
data-cy="colorpicker-editable-input"
>
<HexColorInput
{...{ onBlur }}
prefixed
alpha={!!showTransparencyControl}
color={colorValue}
data-cy="colorpicker-editable-input-textbox"
onChange={onColorInputChange}
/>
</div>
</div>
</div>
</>
)}
<div
data-testid="color-palette"
className={classnames("neeto-ui-colorpicker__palette-wrapper", {
"neeto-ui-colorpicker__palette-wrapper--hidden-picker": !showPicker,
"neeto-ui-pt-3 neeto-ui-border-t neeto-ui-border-gray-200":
showPicker,
})}
>
<Palette
{...{ color }}
colorList={colorPalette}
onChange={onColorChange}
/>
</div>
{showRecentlyUsedColors && recentlyUsedColors.length > 0 && (
<div
className="neeto-ui-colorpicker__palette-wrapper neeto-ui-border-t neeto-ui-border-gray-200 neeto-ui-pt-3"
data-testid="color-palette-recently-used"
>
<Typography
className="neeto-ui-text-gray-600 mb-2"
style="body3"
weight="medium"
>
{getLocale(i18n, t, "neetoui.colorPicker.recentlyUsed")}
</Typography>
<Palette colorList={recentlyUsedColors} onChange={onColorChange} />
</div>
)}
</div>
</Dropdown>
);
};
ColorPicker.propTypes = {
/**
* To specify the color value.
*/
color: PropTypes.string,
/**
* <div class="neeto-ui-tag neeto-ui-tag--size-small neeto-ui-tag--style-outline neeto-ui-tag--style-success mb-2">
* New
* </div>
* To set the size of the target.
*/
size: PropTypes.oneOf(Object.values(TARGET_SIZES)),
/**
* To specify the action to be triggered on changing the color.
*/
onChange: PropTypes.func,
/**
* To specify the colors shown in the palette.
*/
colorPalette: PropTypes.arrayOf(
PropTypes.shape({ hex: PropTypes.string, rgb: PropTypes.string })
),
/**
* Shows eye dropper to pick color.
*/
showEyeDropper: PropTypes.bool,
/**
* To show hex value near to the color in the dropdown.
* By default it will be enabled.
*/
showHexValue: PropTypes.bool,
/**
* To show transparency control. By default it will be hidden.
*/
showTransparencyControl: PropTypes.bool,
/**
* To show the color picker. Used to hide the picker in cases where only palette is required. By default it will be true.
*/
showPicker: PropTypes.bool,
/**
* To specify the props to be passed to the dropdown portal.
*/
portalProps: PropTypes.object,
/**
* To show the recently used colors.
*/
showRecentlyUsedColors: PropTypes.bool,
};
export default ColorPicker;