@react-aria/color 3.0.0-beta.2 → 3.0.0-beta.20

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