@react-aria/color 3.0.0-beta.0 → 3.0.0-beta.11

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.
@@ -0,0 +1,447 @@
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {AriaColorAreaProps, ColorChannel} from '@react-types/color';
14
+ import {ColorAreaState} from '@react-stately/color';
15
+ import {focusWithoutScrolling, isAndroid, isIOS, mergeProps, useGlobalListeners, useLabels} from '@react-aria/utils';
16
+ // @ts-ignore
17
+ import intlMessages from '../intl/*.json';
18
+ import React, {ChangeEvent, HTMLAttributes, InputHTMLAttributes, RefObject, useCallback, useRef} from 'react';
19
+ import {useColorAreaGradient} from './useColorAreaGradient';
20
+ import {useFocus, useFocusWithin, useKeyboard, useMove} from '@react-aria/interactions';
21
+ import {useLocale, useMessageFormatter} from '@react-aria/i18n';
22
+ import {useVisuallyHidden} from '@react-aria/visually-hidden';
23
+
24
+ interface ColorAreaAria {
25
+ /** Props for the color area container element. */
26
+ colorAreaProps: HTMLAttributes<HTMLElement>,
27
+ /** Props for the color area gradient foreground element. */
28
+ gradientProps: HTMLAttributes<HTMLElement>,
29
+ /** Props for the thumb element. */
30
+ thumbProps: HTMLAttributes<HTMLElement>,
31
+ /** Props for the visually hidden horizontal range input element. */
32
+ xInputProps: InputHTMLAttributes<HTMLInputElement>,
33
+ /** Props for the visually hidden vertical range input element. */
34
+ yInputProps: InputHTMLAttributes<HTMLInputElement>
35
+ }
36
+
37
+ interface ColorAreaAriaProps extends AriaColorAreaProps {
38
+ /** A ref to the input that represents the x axis of the color area. */
39
+ inputXRef: RefObject<HTMLElement>,
40
+ /** A ref to the input that represents the y axis of the color area. */
41
+ inputYRef: RefObject<HTMLElement>,
42
+ /** A ref to the color area containing element. */
43
+ containerRef: RefObject<HTMLElement>
44
+ }
45
+
46
+ /**
47
+ * Provides the behavior and accessibility implementation for a color wheel component.
48
+ * Color wheels allow users to adjust the hue of an HSL or HSB color value on a circular track.
49
+ */
50
+ export function useColorArea(props: ColorAreaAriaProps, state: ColorAreaState): ColorAreaAria {
51
+ let {
52
+ isDisabled,
53
+ inputXRef,
54
+ inputYRef,
55
+ containerRef,
56
+ 'aria-label': ariaLabel
57
+ } = props;
58
+ let formatMessage = useMessageFormatter(intlMessages);
59
+
60
+ let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
61
+
62
+ let {direction, locale} = useLocale();
63
+
64
+ let focusedInputRef = useRef<HTMLElement>(null);
65
+
66
+ let focusInput = useCallback((inputRef:RefObject<HTMLElement> = inputXRef) => {
67
+ if (inputRef.current) {
68
+ focusWithoutScrolling(inputRef.current);
69
+ }
70
+ }, [inputXRef]);
71
+
72
+ let stateRef = useRef<ColorAreaState>(null);
73
+ stateRef.current = state;
74
+ let {xChannel, yChannel, zChannel} = stateRef.current.channels;
75
+ let xChannelStep = stateRef.current.xChannelStep;
76
+ let yChannelStep = stateRef.current.yChannelStep;
77
+
78
+ let currentPosition = useRef<{x: number, y: number}>(null);
79
+
80
+ let {keyboardProps} = useKeyboard({
81
+ onKeyDown(e) {
82
+ // these are the cases that useMove doesn't handle
83
+ if (!/^(PageUp|PageDown|Home|End)$/.test(e.key)) {
84
+ e.continuePropagation();
85
+ return;
86
+ }
87
+ // same handling as useMove, don't need to stop propagation, useKeyboard will do that for us
88
+ e.preventDefault();
89
+ // remember to set this and unset it so that onChangeEnd is fired
90
+ stateRef.current.setDragging(true);
91
+ valueChangedViaKeyboard.current = true;
92
+ switch (e.key) {
93
+ case 'PageUp':
94
+ stateRef.current.incrementY(stateRef.current.yChannelPageStep);
95
+ focusedInputRef.current = inputYRef.current;
96
+ break;
97
+ case 'PageDown':
98
+ stateRef.current.decrementY(stateRef.current.yChannelPageStep);
99
+ focusedInputRef.current = inputYRef.current;
100
+ break;
101
+ case 'Home':
102
+ direction === 'rtl' ? stateRef.current.incrementX(stateRef.current.xChannelPageStep) : stateRef.current.decrementX(stateRef.current.xChannelPageStep);
103
+ focusedInputRef.current = inputXRef.current;
104
+ break;
105
+ case 'End':
106
+ direction === 'rtl' ? stateRef.current.decrementX(stateRef.current.xChannelPageStep) : stateRef.current.incrementX(stateRef.current.xChannelPageStep);
107
+ focusedInputRef.current = inputXRef.current;
108
+ break;
109
+ }
110
+ stateRef.current.setDragging(false);
111
+ if (focusedInputRef.current) {
112
+ focusInput(focusedInputRef.current ? focusedInputRef : inputXRef);
113
+ }
114
+ }
115
+ });
116
+
117
+ let moveHandler = {
118
+ onMoveStart() {
119
+ currentPosition.current = null;
120
+ stateRef.current.setDragging(true);
121
+ },
122
+ onMove({deltaX, deltaY, pointerType, shiftKey}) {
123
+ let {
124
+ incrementX,
125
+ decrementX,
126
+ incrementY,
127
+ decrementY,
128
+ xChannelPageStep,
129
+ xChannelStep,
130
+ yChannelPageStep,
131
+ yChannelStep,
132
+ getThumbPosition,
133
+ setColorFromPoint
134
+ } = stateRef.current;
135
+ if (currentPosition.current == null) {
136
+ currentPosition.current = getThumbPosition();
137
+ }
138
+ let {width, height} = containerRef.current.getBoundingClientRect();
139
+ let valueChanged = deltaX !== 0 || deltaY !== 0;
140
+ if (pointerType === 'keyboard') {
141
+ let deltaXValue = shiftKey && xChannelPageStep > xChannelStep ? xChannelPageStep : xChannelStep;
142
+ let deltaYValue = shiftKey && yChannelPageStep > yChannelStep ? yChannelPageStep : yChannelStep;
143
+ if ((deltaX > 0 && direction === 'ltr') || (deltaX < 0 && direction === 'rtl')) {
144
+ incrementX(deltaXValue);
145
+ } else if ((deltaX < 0 && direction === 'ltr') || (deltaX > 0 && direction === 'rtl')) {
146
+ decrementX(deltaXValue);
147
+ } else if (deltaY > 0) {
148
+ decrementY(deltaYValue);
149
+ } else if (deltaY < 0) {
150
+ incrementY(deltaYValue);
151
+ }
152
+ valueChangedViaKeyboard.current = valueChanged;
153
+ // set the focused input based on which axis has the greater delta
154
+ focusedInputRef.current = valueChanged && Math.abs(deltaY) > Math.abs(deltaX) ? inputYRef.current : inputXRef.current;
155
+ } else {
156
+ currentPosition.current.x += (direction === 'rtl' ? -1 : 1) * deltaX / width ;
157
+ currentPosition.current.y += deltaY / height;
158
+ setColorFromPoint(currentPosition.current.x, currentPosition.current.y);
159
+ }
160
+ },
161
+ onMoveEnd() {
162
+ isOnColorArea.current = undefined;
163
+ stateRef.current.setDragging(false);
164
+ focusInput(focusedInputRef.current ? focusedInputRef : inputXRef);
165
+ }
166
+ };
167
+ let {moveProps: movePropsThumb} = useMove(moveHandler);
168
+
169
+ let valueChangedViaKeyboard = useRef<boolean>(false);
170
+ let {focusWithinProps} = useFocusWithin({
171
+ onFocusWithinChange: (focusWithin:boolean) => {
172
+ if (!focusWithin) {
173
+ valueChangedViaKeyboard.current = false;
174
+ focusedInputRef.current === undefined;
175
+ }
176
+ }
177
+ });
178
+
179
+ let currentPointer = useRef<number | null | undefined>(undefined);
180
+ let isOnColorArea = useRef<boolean>(false);
181
+ let {moveProps: movePropsContainer} = useMove({
182
+ onMoveStart() {
183
+ if (isOnColorArea.current) {
184
+ moveHandler.onMoveStart();
185
+ }
186
+ },
187
+ onMove(e) {
188
+ if (isOnColorArea.current) {
189
+ moveHandler.onMove(e);
190
+ }
191
+ },
192
+ onMoveEnd() {
193
+ if (isOnColorArea.current) {
194
+ moveHandler.onMoveEnd();
195
+ }
196
+ }
197
+ });
198
+
199
+ let onThumbDown = (id: number | null) => {
200
+ if (!state.isDragging) {
201
+ currentPointer.current = id;
202
+ valueChangedViaKeyboard.current = false;
203
+ focusInput();
204
+ state.setDragging(true);
205
+ if (typeof PointerEvent !== 'undefined') {
206
+ addGlobalListener(window, 'pointerup', onThumbUp, false);
207
+ } else {
208
+ addGlobalListener(window, 'mouseup', onThumbUp, false);
209
+ addGlobalListener(window, 'touchend', onThumbUp, false);
210
+ }
211
+ }
212
+ };
213
+
214
+ let onThumbUp = (e) => {
215
+ let id = e.pointerId ?? e.changedTouches?.[0].identifier;
216
+ if (id === currentPointer.current) {
217
+ valueChangedViaKeyboard.current = false;
218
+ focusInput();
219
+ state.setDragging(false);
220
+ currentPointer.current = undefined;
221
+ isOnColorArea.current = false;
222
+
223
+ if (typeof PointerEvent !== 'undefined') {
224
+ removeGlobalListener(window, 'pointerup', onThumbUp, false);
225
+ } else {
226
+ removeGlobalListener(window, 'mouseup', onThumbUp, false);
227
+ removeGlobalListener(window, 'touchend', onThumbUp, false);
228
+ }
229
+ }
230
+ };
231
+
232
+ let onColorAreaDown = (colorArea: Element, id: number | null, clientX: number, clientY: number) => {
233
+ let rect = colorArea.getBoundingClientRect();
234
+ let {width, height} = rect;
235
+ let x = (clientX - rect.x) / width;
236
+ let y = (clientY - rect.y) / height;
237
+ if (direction === 'rtl') {
238
+ x = 1 - x;
239
+ }
240
+ if (x >= 0 && x <= 1 && y >= 0 && y <= 1 && !state.isDragging && currentPointer.current === undefined) {
241
+ isOnColorArea.current = true;
242
+ valueChangedViaKeyboard.current = false;
243
+ currentPointer.current = id;
244
+ state.setColorFromPoint(x, y);
245
+
246
+ focusInput();
247
+ state.setDragging(true);
248
+
249
+ if (typeof PointerEvent !== 'undefined') {
250
+ addGlobalListener(window, 'pointerup', onColorAreaUp, false);
251
+ } else {
252
+ addGlobalListener(window, 'mouseup', onColorAreaUp, false);
253
+ addGlobalListener(window, 'touchend', onColorAreaUp, false);
254
+ }
255
+ }
256
+ };
257
+
258
+ let onColorAreaUp = (e) => {
259
+ let id = e.pointerId ?? e.changedTouches?.[0].identifier;
260
+ if (isOnColorArea.current && id === currentPointer.current) {
261
+ isOnColorArea.current = false;
262
+ valueChangedViaKeyboard.current = false;
263
+ currentPointer.current = undefined;
264
+ state.setDragging(false);
265
+ focusInput();
266
+
267
+ if (typeof PointerEvent !== 'undefined') {
268
+ removeGlobalListener(window, 'pointerup', onColorAreaUp, false);
269
+ } else {
270
+ removeGlobalListener(window, 'mouseup', onColorAreaUp, false);
271
+ removeGlobalListener(window, 'touchend', onColorAreaUp, false);
272
+ }
273
+ }
274
+ };
275
+
276
+ let colorAreaInteractions = isDisabled ? {} : mergeProps({
277
+ ...(typeof PointerEvent !== 'undefined' ? {
278
+ onPointerDown: (e: React.PointerEvent) => {
279
+ if (e.pointerType === 'mouse' && (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey)) {
280
+ return;
281
+ }
282
+ onColorAreaDown(e.currentTarget, e.pointerId, e.clientX, e.clientY);
283
+ }} : {
284
+ onMouseDown: (e: React.MouseEvent) => {
285
+ if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) {
286
+ return;
287
+ }
288
+ onColorAreaDown(e.currentTarget, undefined, e.clientX, e.clientY);
289
+ },
290
+ onTouchStart: (e: React.TouchEvent) => {
291
+ onColorAreaDown(e.currentTarget, e.changedTouches[0].identifier, e.changedTouches[0].clientX, e.changedTouches[0].clientY);
292
+ }
293
+ })
294
+ }, movePropsContainer);
295
+
296
+ let thumbInteractions = isDisabled ? {} : mergeProps({
297
+ ...(typeof PointerEvent !== 'undefined' ? {
298
+ onPointerDown: (e: React.PointerEvent) => {
299
+ if (e.pointerType === 'mouse' && (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey)) {
300
+ return;
301
+ }
302
+ onThumbDown(e.pointerId);
303
+ }} : {
304
+ onMouseDown: (e: React.MouseEvent) => {
305
+ if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) {
306
+ return;
307
+ }
308
+ onThumbDown(undefined);
309
+ },
310
+ onTouchStart: (e: React.TouchEvent) => {
311
+ onThumbDown(e.changedTouches[0].identifier);
312
+ }
313
+ })
314
+ }, focusWithinProps, keyboardProps, movePropsThumb);
315
+
316
+ let {focusProps: xInputFocusProps} = useFocus({
317
+ onFocus: () => {
318
+ focusedInputRef.current = inputXRef.current;
319
+ }
320
+ });
321
+
322
+ let {focusProps: yInputFocusProps} = useFocus({
323
+ onFocus: () => {
324
+ focusedInputRef.current = inputYRef.current;
325
+ }
326
+ });
327
+
328
+ let isMobile = isIOS() || isAndroid();
329
+
330
+ function getAriaValueTextForChannel(channel:ColorChannel) {
331
+ return (
332
+ valueChangedViaKeyboard.current ?
333
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(channel, locale), value: state.value.formatChannelValue(channel, locale)})
334
+ :
335
+ [
336
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(channel, locale), value: state.value.formatChannelValue(channel, locale)}),
337
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(channel === yChannel ? xChannel : yChannel, locale), value: state.value.formatChannelValue(channel === yChannel ? xChannel : yChannel, locale)})
338
+ ].join(', ')
339
+ );
340
+ }
341
+
342
+ let colorPickerLabel = formatMessage('colorPicker');
343
+
344
+ let xInputLabellingProps = useLabels({
345
+ ...props,
346
+ 'aria-label': ariaLabel ? formatMessage('colorInputLabel', {label: ariaLabel, channelLabel: colorPickerLabel}) : colorPickerLabel
347
+ });
348
+
349
+ let yInputLabellingProps = useLabels({
350
+ ...props,
351
+ 'aria-label': ariaLabel ? formatMessage('colorInputLabel', {label: ariaLabel, channelLabel: colorPickerLabel}) : colorPickerLabel
352
+ });
353
+
354
+ let colorAriaLabellingProps = useLabels(
355
+ {
356
+ ...props,
357
+ 'aria-label': ariaLabel ? `${ariaLabel} ${colorPickerLabel}` : undefined
358
+ },
359
+ isMobile ? colorPickerLabel : undefined
360
+ );
361
+
362
+ let ariaRoleDescription = formatMessage('twoDimensionalSlider');
363
+
364
+ let {visuallyHiddenProps} = useVisuallyHidden({style: {
365
+ opacity: '0.0001',
366
+ width: '100%',
367
+ height: '100%',
368
+ pointerEvents: 'none'
369
+ }});
370
+
371
+ let {
372
+ colorAreaStyleProps,
373
+ gradientStyleProps,
374
+ thumbStyleProps
375
+ } = useColorAreaGradient({
376
+ direction,
377
+ state,
378
+ xChannel,
379
+ zChannel,
380
+ isDisabled: props.isDisabled
381
+ });
382
+
383
+ return {
384
+ colorAreaProps: {
385
+ ...colorAriaLabellingProps,
386
+ ...colorAreaInteractions,
387
+ ...colorAreaStyleProps,
388
+ role: 'group'
389
+ },
390
+ gradientProps: {
391
+ ...gradientStyleProps,
392
+ role: 'presentation'
393
+ },
394
+ thumbProps: {
395
+ ...thumbInteractions,
396
+ ...thumbStyleProps,
397
+ role: 'presentation'
398
+ },
399
+ xInputProps: {
400
+ ...xInputLabellingProps,
401
+ ...visuallyHiddenProps,
402
+ ...xInputFocusProps,
403
+ type: 'range',
404
+ min: state.value.getChannelRange(xChannel).minValue,
405
+ max: state.value.getChannelRange(xChannel).maxValue,
406
+ step: xChannelStep,
407
+ 'aria-roledescription': ariaRoleDescription,
408
+ 'aria-valuetext': getAriaValueTextForChannel(xChannel),
409
+ disabled: isDisabled,
410
+ value: state.value.getChannelValue(xChannel),
411
+ tabIndex: (isMobile || !focusedInputRef.current || focusedInputRef.current === inputXRef.current ? undefined : -1),
412
+ /*
413
+ So that only a single "2d slider" control shows up when listing form elements for screen readers,
414
+ add aria-hidden="true" to the unfocused control when the value has not changed via the keyboard,
415
+ but remove aria-hidden to reveal the input for each channel when the value has changed with the keyboard.
416
+ */
417
+ 'aria-hidden': (!isMobile && focusedInputRef.current === inputYRef.current && !valueChangedViaKeyboard.current ? 'true' : undefined),
418
+ onChange: (e: ChangeEvent<HTMLInputElement>) => {
419
+ state.setXValue(parseFloat(e.target.value));
420
+ }
421
+ },
422
+ yInputProps: {
423
+ ...yInputLabellingProps,
424
+ ...visuallyHiddenProps,
425
+ ...yInputFocusProps,
426
+ type: 'range',
427
+ min: state.value.getChannelRange(yChannel).minValue,
428
+ max: state.value.getChannelRange(yChannel).maxValue,
429
+ step: yChannelStep,
430
+ 'aria-roledescription': ariaRoleDescription,
431
+ 'aria-valuetext': getAriaValueTextForChannel(yChannel),
432
+ 'aria-orientation': 'vertical',
433
+ disabled: isDisabled,
434
+ value: state.value.getChannelValue(yChannel),
435
+ tabIndex: (isMobile || focusedInputRef.current === inputYRef.current ? undefined : -1),
436
+ /*
437
+ So that only a single "2d slider" control shows up when listing form elements for screen readers,
438
+ add aria-hidden="true" to the unfocused input when the value has not changed via the keyboard,
439
+ but remove aria-hidden to reveal the input for each channel when the value has changed with the keyboard.
440
+ */
441
+ 'aria-hidden': (isMobile || focusedInputRef.current === inputYRef.current || valueChangedViaKeyboard.current ? undefined : 'true'),
442
+ onChange: (e: ChangeEvent<HTMLInputElement>) => {
443
+ state.setYValue(parseFloat(e.target.value));
444
+ }
445
+ }
446
+ };
447
+ }
@@ -0,0 +1,249 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {CSSProperties, useMemo} from 'react';
14
+
15
+ const generateRGB_R = (orientation, dir: boolean, zValue: number) => {
16
+ let maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
17
+ let result = {
18
+ colorAreaStyles: {
19
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,0),rgb(${zValue},255,0))`
20
+ },
21
+ gradientStyles: {
22
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,255),rgb(${zValue},255,255))`,
23
+ 'WebkitMaskImage': maskImage,
24
+ maskImage
25
+ }
26
+ };
27
+ return result;
28
+ };
29
+
30
+ const generateRGB_G = (orientation, dir: boolean, zValue: number) => {
31
+ let maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
32
+ let result = {
33
+ colorAreaStyles: {
34
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},0),rgb(255,${zValue},0))`
35
+ },
36
+ gradientStyles: {
37
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},255),rgb(255,${zValue},255))`,
38
+ 'WebkitMaskImage': maskImage,
39
+ maskImage
40
+ }
41
+ };
42
+ return result;
43
+ };
44
+
45
+ const generateRGB_B = (orientation, dir: boolean, zValue: number) => {
46
+ let maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
47
+ let result = {
48
+ colorAreaStyles: {
49
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,0,${zValue}),rgb(255,0,${zValue}))`
50
+ },
51
+ gradientStyles: {
52
+ backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,255,${zValue}),rgb(255,255,${zValue}))`,
53
+ 'WebkitMaskImage': maskImage,
54
+ maskImage
55
+ }
56
+ };
57
+ return result;
58
+ };
59
+
60
+
61
+ const generateHSL_H = (orientation, dir: boolean, zValue: number) => {
62
+ let result = {
63
+ colorAreaStyles: {},
64
+ gradientStyles: {
65
+ background: [
66
+ `linear-gradient(to ${orientation[Number(dir)]}, hsla(0,0%,0%,1) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,1) 100%)`,
67
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,50%),hsla(0,0%,50%,0))`,
68
+ `hsl(${zValue}, 100%, 50%)`
69
+ ].join(',')
70
+ }
71
+ };
72
+ return result;
73
+ };
74
+
75
+ const generateHSL_S = (orientation, dir: boolean, alphaValue: number) => {
76
+ let result = {
77
+ colorAreaStyles: {},
78
+ gradientStyles: {
79
+ background: [
80
+ `linear-gradient(to ${orientation[Number(!dir)]}, hsla(0,0%,0%,${alphaValue}) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,${alphaValue}) 100%)`,
81
+ `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
82
+ 'hsl(0, 0%, 50%)'
83
+ ].join(',')
84
+ }
85
+ };
86
+ return result;
87
+ };
88
+
89
+ const generateHSL_L = (orientation, dir: boolean, zValue: number) => {
90
+ let result = {
91
+ colorAreaStyles: {},
92
+ gradientStyles: {
93
+ backgroundImage: [
94
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,${zValue}%),hsla(0,0%,${zValue}%,0))`,
95
+ `linear-gradient(to ${orientation[Number(dir)]},hsl(0,100%,${zValue}%),hsl(60,100%,${zValue}%),hsl(120,100%,${zValue}%),hsl(180,100%,${zValue}%),hsl(240,100%,${zValue}%),hsl(300,100%,${zValue}%),hsl(360,100%,${zValue}%))`
96
+ ].join(',')
97
+ }
98
+ };
99
+ return result;
100
+ };
101
+
102
+
103
+ const generateHSB_H = (orientation, dir: boolean, zValue: number) => {
104
+ let result = {
105
+ colorAreaStyles: {},
106
+ gradientStyles: {
107
+ background: [
108
+ `linear-gradient(to ${orientation[Number(dir)]},hsl(0,0%,0%),hsla(0,0%,0%,0))`,
109
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,100%),hsla(0,0%,100%,0))`,
110
+ `hsl(${zValue}, 100%, 50%)`
111
+ ].join(',')
112
+ }
113
+ };
114
+ return result;
115
+ };
116
+
117
+ const generateHSB_S = (orientation, dir: boolean, alphaValue: number) => {
118
+ let result = {
119
+ colorAreaStyles: {},
120
+ gradientStyles: {
121
+ background: [
122
+ `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,0%,${alphaValue}),hsla(0,0%,0%,0))`,
123
+ `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
124
+ `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,0%),hsl(0,0%,100%))`
125
+ ].join(',')
126
+ }
127
+ };
128
+ return result;
129
+ };
130
+
131
+ const generateHSB_B = (orientation, dir: boolean, alphaValue: number) => {
132
+ let result = {
133
+ colorAreaStyles: {},
134
+ gradientStyles: {
135
+ background: [
136
+ `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,100%,${alphaValue}),hsla(0,0%,100%,0))`,
137
+ `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
138
+ '#000'
139
+ ].join(',')
140
+ }
141
+ };
142
+ return result;
143
+ };
144
+
145
+
146
+ interface Gradients {
147
+ colorAreaStyleProps: {
148
+ style: CSSProperties
149
+ },
150
+ gradientStyleProps: {
151
+ style: CSSProperties
152
+ },
153
+ thumbStyleProps: {
154
+ style: CSSProperties
155
+ }
156
+ }
157
+
158
+ export function useColorAreaGradient({direction, state, zChannel, xChannel, isDisabled}): Gradients {
159
+ let returnVal = useMemo<Gradients>(() => {
160
+ let orientation = ['top', direction === 'rtl' ? 'left' : 'right'];
161
+ let dir = false;
162
+ let background = {colorAreaStyles: {}, gradientStyles: {}};
163
+ let zValue = state.value.getChannelValue(zChannel);
164
+ let {minValue: zMin, maxValue: zMax} = state.value.getChannelRange(zChannel);
165
+ let alphaValue = (zValue - zMin) / (zMax - zMin);
166
+ let isHSL = state.value.getColorSpace() === 'hsl';
167
+ if (!isDisabled) {
168
+ switch (zChannel) {
169
+ case 'red': {
170
+ dir = xChannel === 'green';
171
+ background = generateRGB_R(orientation, dir, zValue);
172
+ break;
173
+ }
174
+ case 'green': {
175
+ dir = xChannel === 'red';
176
+ background = generateRGB_G(orientation, dir, zValue);
177
+ break;
178
+ }
179
+ case 'blue': {
180
+ dir = xChannel === 'red';
181
+ background = generateRGB_B(orientation, dir, zValue);
182
+ break;
183
+ }
184
+ case 'hue': {
185
+ dir = xChannel !== 'saturation';
186
+ if (isHSL) {
187
+ background = generateHSL_H(orientation, dir, zValue);
188
+ } else {
189
+ background = generateHSB_H(orientation, dir, zValue);
190
+ }
191
+ break;
192
+ }
193
+ case 'saturation': {
194
+ dir = xChannel === 'hue';
195
+ if (isHSL) {
196
+ background = generateHSL_S(orientation, dir, alphaValue);
197
+ } else {
198
+ background = generateHSB_S(orientation, dir, alphaValue);
199
+ }
200
+ break;
201
+ }
202
+ case 'brightness': {
203
+ dir = xChannel === 'hue';
204
+ background = generateHSB_B(orientation, dir, alphaValue);
205
+ break;
206
+ }
207
+ case 'lightness': {
208
+ dir = xChannel === 'hue';
209
+ background = generateHSL_L(orientation, dir, zValue);
210
+ break;
211
+ }
212
+ }
213
+ }
214
+
215
+ let {x, y} = state.getThumbPosition();
216
+
217
+ if (direction === 'rtl') {
218
+ x = 1 - x;
219
+ }
220
+
221
+ return {
222
+ colorAreaStyleProps: {
223
+ style: {
224
+ position: 'relative',
225
+ touchAction: 'none',
226
+ ...background.colorAreaStyles
227
+ }
228
+ },
229
+ gradientStyleProps: {
230
+ style: {
231
+ touchAction: 'none',
232
+ ...background.gradientStyles
233
+ }
234
+ },
235
+ thumbStyleProps: {
236
+ style: {
237
+ position: 'absolute',
238
+ left: `${x * 100}%`,
239
+ top: `${y * 100}%`,
240
+ transform: 'translate(0%, 0%)',
241
+ touchAction: 'none'
242
+ }
243
+ }
244
+ };
245
+ }, [direction, state, zChannel, xChannel, isDisabled]);
246
+
247
+ return returnVal;
248
+ }
249
+