-
Notifications
You must be signed in to change notification settings - Fork 2
/
SaturationPicker.tsx
274 lines (245 loc) · 7.41 KB
/
SaturationPicker.tsx
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Color
*/
import "./SaturationPicker.scss";
import classnames from "classnames";
import * as React from "react";
import { Key } from "ts-key-enum";
import { ColorDef, HSVColor } from "@itwin/core-common";
import type { CommonProps } from "@itwin/core-react";
import { useTranslation } from "../useTranslation";
function calculateChange(
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>,
hsv: HSVColor,
container: HTMLDivElement
): HSVColor | undefined {
const {
width: containerWidth,
height: containerHeight,
top: containerTop,
left: containerLeft,
} = container.getBoundingClientRect();
let x: number | undefined;
if ("pageX" in e) {
x = e.pageX;
} else {
// istanbul ignore next
if (undefined !== e.touches && e.touches.length) x = e.touches[0].pageX;
}
// istanbul ignore if
if (undefined === x) return hsv;
let y = 0;
if ("pageY" in e) {
y = e.pageY;
} else {
// istanbul ignore next
if (undefined !== e.touches && e.touches.length) y = e.touches[0].pageY;
}
// istanbul ignore if
if (undefined === y) return hsv;
let left = x - (containerLeft + window.scrollX);
let top = y - (containerTop + window.scrollY);
// istanbul ignore next
if (left < 0) {
left = 0;
} else if (left > containerWidth) {
left = containerWidth;
} else if (top < 0) {
top = 0;
} else if (top > containerHeight) {
top = containerHeight;
}
let saturation = Math.round((left * 100) / containerWidth);
let value = Math.round(-((top * 100) / containerHeight) + 100);
// istanbul ignore if
if (saturation < 0) saturation = 0;
// istanbul ignore if
if (saturation > 100) saturation = 100;
// istanbul ignore if
if (value < 0) value = 0;
// istanbul ignore if
if (value > 100) value = 100;
return hsv.clone(hsv.h, saturation, value);
}
/** Properties for the [[SaturationPicker]] React component
* @beta
* @deprecated in 4.11.x. Props of deprecated component {@link SaturationPicker}.
*/
export interface SaturationPickerProps
extends React.HTMLAttributes<HTMLDivElement>,
CommonProps {
/** function to run when user selects location in saturation region */
onSaturationChange?: ((saturation: HSVColor) => void) | undefined;
/** HSV Color Value */
hsv: HSVColor;
}
/** SaturationPicker component used to set the saturation value.
* @beta
* @deprecated in 4.11.x. Use {@link https://itwinui.bentley.com/docs/colorpicker iTwinUI color picker} instead.
*/
export function SaturationPicker({
onSaturationChange,
hsv,
className,
style,
}: // eslint-disable-next-line deprecation/deprecation
SaturationPickerProps) {
const { translate } = useTranslation();
const container = React.useRef<HTMLDivElement>(null);
const isDragging = React.useRef(false);
const onChange = React.useCallback(
(
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>
) => {
// istanbul ignore else
if (container.current) {
const newHsvColor = calculateChange(e, hsv, container.current);
// istanbul ignore else
if (newHsvColor) {
// istanbul ignore else
if (onSaturationChange) onSaturationChange(newHsvColor);
}
}
},
[hsv, onSaturationChange]
);
const onDragging = React.useCallback(
(
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>
) => {
// istanbul ignore else
if (isDragging.current) {
onChange(e);
}
},
[onChange]
);
const onMouseUp = React.useCallback(() => {
// istanbul ignore else
if (isDragging.current) {
isDragging.current = false;
}
}, []);
const onTouchEnd = React.useCallback(
(event: React.TouchEvent<HTMLDivElement>) => {
// istanbul ignore else
if (isDragging.current) {
onChange(event);
isDragging.current = false;
}
},
[onChange]
);
const onMouseDown = React.useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
// istanbul ignore else
if (e.target !== e.currentTarget) {
isDragging.current = true;
document.addEventListener("mouseup", onMouseUp, {
capture: true,
once: true,
});
}
onChange(e);
// istanbul ignore else
if (container.current) container.current.focus();
e.preventDefault();
},
[onChange, onMouseUp]
);
const onTouchStart = React.useCallback(
(e: React.TouchEvent<HTMLDivElement>) => {
// istanbul ignore else
if (e.target !== e.currentTarget) isDragging.current = true;
onChange(e);
// istanbul ignore else
if (container.current) container.current.focus();
},
[onChange]
);
const onKeyDown = React.useCallback(
(evt: React.KeyboardEvent<HTMLDivElement>) => {
const h = hsv.h;
let { s, v } = { ...hsv };
if (evt.key === Key.ArrowLeft.valueOf()) {
s -= evt.ctrlKey ? 10 : 1;
} else if (evt.key === Key.ArrowDown.valueOf()) {
v -= evt.ctrlKey ? 10 : 1;
} else if (evt.key === Key.ArrowRight.valueOf()) {
s += evt.ctrlKey ? 10 : 1;
} else if (evt.key === Key.ArrowUp.valueOf()) {
v += evt.ctrlKey ? 10 : 1;
} else if (evt.key === Key.PageDown.valueOf()) {
v = 0;
} else if (evt.key === Key.PageUp.valueOf()) {
v = 100;
} else if (evt.key === Key.Home.valueOf()) {
s = 0;
} else {
// istanbul ignore else
if (evt.key === Key.End.valueOf()) {
s = 100;
}
}
// istanbul ignore if
if (s < 0) s = 0;
// istanbul ignore if
if (s > 100) s = 100;
// istanbul ignore if
if (v < 0) v = 0;
// istanbul ignore if
if (v > 100) v = 100;
const newColor = new HSVColor(h, s, v);
// istanbul ignore else
if (onSaturationChange) onSaturationChange(newColor);
evt.preventDefault();
},
[hsv, onSaturationChange]
);
const pointerStyle: React.CSSProperties = React.useMemo(
() => ({
left: `${hsv.s}%`,
top: `${-hsv.v + 100}%`,
backgroundColor: `${ColorDef.fromHSV(hsv).toRgbString()}`,
}),
[hsv]
);
const colorStyle: React.CSSProperties = React.useMemo(
() => ({ backgroundColor: `hsl(${hsv.h} ,100%, 50%)` }),
[hsv]
);
return (
<div
className={classnames("components-saturation-container", className)}
style={style}
data-testid="saturation-container"
>
<div
data-testid="saturation-region"
role="slider"
aria-label={translate("color.saturation")}
aria-valuenow={hsv.s}
style={colorStyle}
className="components-saturation-region"
ref={container}
onMouseDown={onMouseDown}
onMouseMove={onDragging}
onTouchMove={onDragging}
onTouchStart={onTouchStart}
onTouchEnd={onTouchEnd}
tabIndex={0}
onKeyDown={onKeyDown}
>
<div
style={pointerStyle}
className="components-saturation-pointer"
data-testid="saturation-pointer"
/>
</div>
</div>
);
}