@react-aria/color 3.0.0-beta.3 → 3.0.0-beta.30

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,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
+
@@ -20,11 +20,13 @@ import {
20
20
  useState
21
21
  } from 'react';
22
22
  import {mergeProps, useId} from '@react-aria/utils';
23
+ import {privateValidationStateProp} from '@react-stately/form';
23
24
  import {useFocusWithin, useScrollWheel} from '@react-aria/interactions';
24
25
  import {useFormattedTextField} from '@react-aria/textfield';
25
26
  import {useSpinButton} from '@react-aria/spinbutton';
27
+ import {ValidationResult} from '@react-types/shared';
26
28
 
27
- interface ColorFieldAria {
29
+ export interface ColorFieldAria extends ValidationResult {
28
30
  /** Props for the label element. */
29
31
  labelProps: LabelHTMLAttributes<HTMLLabelElement>,
30
32
  /** Props for the input element. */
@@ -43,17 +45,19 @@ export function useColorField(
43
45
  let {
44
46
  isDisabled,
45
47
  isReadOnly,
46
- isRequired
48
+ isRequired,
49
+ isWheelDisabled,
50
+ validationBehavior = 'aria'
47
51
  } = props;
48
52
 
49
53
  let {
50
54
  colorValue,
51
55
  inputValue,
52
- commit,
53
56
  increment,
54
57
  decrement,
55
58
  incrementToMax,
56
- decrementToMin
59
+ decrementToMin,
60
+ commit
57
61
  } = state;
58
62
 
59
63
  let inputId = useId();
@@ -85,34 +89,46 @@ export function useColorField(
85
89
  } else if (e.deltaY < 0) {
86
90
  decrement();
87
91
  }
88
- }, [isReadOnly, isDisabled, decrement, increment]);
92
+ }, [decrement, increment]);
89
93
  // If the input isn't supposed to receive input, disable scrolling.
90
- let scrollingDisabled = isDisabled || isReadOnly || !focusWithin;
94
+ let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;
91
95
  useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, ref);
92
96
 
93
97
  let onChange = value => {
94
- state.setInputValue(value);
98
+ if (state.validate(value)) {
99
+ state.setInputValue(value);
100
+ }
95
101
  };
96
102
 
97
- let {labelProps, inputProps} = useFormattedTextField(
103
+ let {inputProps, ...otherProps} = useFormattedTextField(
98
104
  mergeProps(props, {
99
105
  id: inputId,
100
106
  value: inputValue,
107
+ defaultValue: undefined,
108
+ validate: undefined,
109
+ [privateValidationStateProp]: state,
101
110
  type: 'text',
102
111
  autoComplete: 'off',
103
112
  onChange
104
113
  }), state, ref);
105
114
 
115
+ inputProps = mergeProps(inputProps, spinButtonProps, focusWithinProps, {
116
+ role: 'textbox',
117
+ 'aria-valuemax': null,
118
+ 'aria-valuemin': null,
119
+ 'aria-valuenow': null,
120
+ 'aria-valuetext': null,
121
+ autoCorrect: 'off',
122
+ spellCheck: 'false',
123
+ onBlur: commit
124
+ });
125
+
126
+ if (validationBehavior === 'native') {
127
+ inputProps['aria-required'] = undefined;
128
+ }
129
+
106
130
  return {
107
- labelProps,
108
- inputProps: mergeProps(inputProps, spinButtonProps, focusWithinProps, {
109
- role: 'textbox',
110
- 'aria-valuemax': null,
111
- 'aria-valuemin': null,
112
- 'aria-valuenow': null,
113
- 'aria-valuetext': null,
114
- autoCorrect: 'off',
115
- onBlur: commit
116
- })
131
+ inputProps,
132
+ ...otherProps
117
133
  };
118
134
  }
@@ -12,37 +12,38 @@
12
12
 
13
13
  import {AriaColorSliderProps} from '@react-types/color';
14
14
  import {ColorSliderState} from '@react-stately/color';
15
- import {HTMLAttributes, RefObject} from 'react';
15
+ import {DOMAttributes} from '@react-types/shared';
16
+ import {InputHTMLAttributes, RefObject} from 'react';
16
17
  import {mergeProps} from '@react-aria/utils';
17
18
  import {useLocale} from '@react-aria/i18n';
18
19
  import {useSlider, useSliderThumb} from '@react-aria/slider';
19
20
 
20
- interface ColorSliderAriaOptions extends AriaColorSliderProps {
21
+ export interface AriaColorSliderOptions extends AriaColorSliderProps {
21
22
  /** A ref for the track element. */
22
- trackRef: RefObject<HTMLElement>,
23
+ trackRef: RefObject<Element>,
23
24
  /** A ref for the input element. */
24
25
  inputRef: RefObject<HTMLInputElement>
25
26
  }
26
27
 
27
- interface ColorSliderAria {
28
+ export interface ColorSliderAria {
28
29
  /** Props for the label element. */
29
- labelProps: HTMLAttributes<HTMLElement>,
30
+ labelProps: DOMAttributes,
30
31
  /** Props for the track element. */
31
- trackProps: HTMLAttributes<HTMLElement>,
32
+ trackProps: DOMAttributes,
32
33
  /** Props for the thumb element. */
33
- thumbProps: HTMLAttributes<HTMLElement>,
34
+ thumbProps: DOMAttributes,
34
35
  /** Props for the visually hidden range input element. */
35
- inputProps: HTMLAttributes<HTMLElement>,
36
+ inputProps: InputHTMLAttributes<HTMLInputElement>,
36
37
  /** Props for the output element, displaying the value of the color slider. */
37
- outputProps: HTMLAttributes<HTMLElement>
38
+ outputProps: DOMAttributes
38
39
  }
39
40
 
40
41
  /**
41
42
  * Provides the behavior and accessibility implementation for a color slider component.
42
43
  * Color sliders allow users to adjust an individual channel of a color value.
43
44
  */
44
- export function useColorSlider(props: ColorSliderAriaOptions, state: ColorSliderState): ColorSliderAria {
45
- let {trackRef, inputRef, orientation, channel, 'aria-label': ariaLabel} = props;
45
+ export function useColorSlider(props: AriaColorSliderOptions, state: ColorSliderState): ColorSliderAria {
46
+ let {trackRef, inputRef, orientation, channel, 'aria-label': ariaLabel, name} = props;
46
47
 
47
48
  let {locale, direction} = useLocale();
48
49
 
@@ -57,6 +58,7 @@ export function useColorSlider(props: ColorSliderAriaOptions, state: ColorSlider
57
58
  index: 0,
58
59
  orientation,
59
60
  isDisabled: props.isDisabled,
61
+ name,
60
62
  trackRef,
61
63
  inputRef
62
64
  }, state);
@@ -99,17 +101,14 @@ export function useColorSlider(props: ColorSliderAriaOptions, state: ColorSlider
99
101
  }
100
102
  };
101
103
 
102
- let thumbPosition = state.getThumbPercent(0);
103
- if (orientation === 'vertical' || direction === 'rtl') {
104
- thumbPosition = 1 - thumbPosition;
105
- }
104
+ let forcedColorAdjustNoneStyle = {forcedColorAdjust: 'none'};
106
105
 
107
106
  return {
108
107
  trackProps: {
109
108
  ...mergeProps(groupProps, trackProps),
110
109
  style: {
111
- position: 'relative',
112
- touchAction: 'none',
110
+ ...trackProps.style,
111
+ ...forcedColorAdjustNoneStyle,
113
112
  background: generateBackground()
114
113
  }
115
114
  },
@@ -117,10 +116,8 @@ export function useColorSlider(props: ColorSliderAriaOptions, state: ColorSlider
117
116
  thumbProps: {
118
117
  ...thumbProps,
119
118
  style: {
120
- touchAction: 'none',
121
- position: 'absolute',
122
- [orientation === 'vertical' ? 'top' : 'left']: `${thumbPosition * 100}%`,
123
- transform: 'translate(-50%, -50%)'
119
+ ...thumbProps.style,
120
+ ...forcedColorAdjustNoneStyle
124
121
  }
125
122
  },
126
123
  labelProps,