@react-aria/color 3.0.0-beta.8 → 3.0.0-beta.9

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,393 @@
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} 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 {useKeyboard, useMove} from '@react-aria/interactions';
20
+ import {useLocale, useMessageFormatter} from '@react-aria/i18n';
21
+ import {useVisuallyHidden} from '@react-aria/visually-hidden';
22
+
23
+ interface ColorAreaAria {
24
+ /** Props for the color area container element. */
25
+ colorAreaProps: HTMLAttributes<HTMLElement>,
26
+ /** Props for the color area gradient foreground element. */
27
+ gradientProps: HTMLAttributes<HTMLElement>,
28
+ /** Props for the thumb element. */
29
+ thumbProps: HTMLAttributes<HTMLElement>,
30
+ /** Props for the visually hidden horizontal range input element. */
31
+ xInputProps: InputHTMLAttributes<HTMLInputElement>,
32
+ /** Props for the visually hidden vertical range input element. */
33
+ yInputProps: InputHTMLAttributes<HTMLInputElement>
34
+ }
35
+
36
+ interface ColorAreaAriaProps extends AriaColorAreaProps {
37
+ /** A ref to the input that represents the x axis of the color area. */
38
+ inputXRef: RefObject<HTMLElement>,
39
+ /** A ref to the input that represents the y axis of the color area. */
40
+ inputYRef: RefObject<HTMLElement>,
41
+ /** A ref to the color area containing element. */
42
+ containerRef: RefObject<HTMLElement>
43
+ }
44
+
45
+ /**
46
+ * Provides the behavior and accessibility implementation for a color wheel component.
47
+ * Color wheels allow users to adjust the hue of an HSL or HSB color value on a circular track.
48
+ */
49
+ export function useColorArea(props: ColorAreaAriaProps, state: ColorAreaState): ColorAreaAria {
50
+ let {
51
+ isDisabled,
52
+ inputXRef,
53
+ inputYRef,
54
+ containerRef
55
+ } = props;
56
+ let formatMessage = useMessageFormatter(intlMessages);
57
+
58
+ let {addGlobalListener, removeGlobalListener} = useGlobalListeners();
59
+
60
+ let {direction, locale} = useLocale();
61
+
62
+ let focusedInputRef = useRef<HTMLElement>(null);
63
+
64
+ let focusInput = useCallback((inputRef:RefObject<HTMLElement> = inputXRef) => {
65
+ if (inputRef.current) {
66
+ focusWithoutScrolling(inputRef.current);
67
+ }
68
+ }, [inputXRef]);
69
+
70
+ let stateRef = useRef<ColorAreaState>(null);
71
+ stateRef.current = state;
72
+ let {xChannel, yChannel} = stateRef.current.channels;
73
+ let xChannelStep = stateRef.current.xChannelStep;
74
+ let yChannelStep = stateRef.current.xChannelStep;
75
+
76
+ let currentPosition = useRef<{x: number, y: number}>(null);
77
+
78
+ let {keyboardProps} = useKeyboard({
79
+ onKeyDown(e) {
80
+ // these are the cases that useMove doesn't handle
81
+ if (!/^(PageUp|PageDown|Home|End)$/.test(e.key)) {
82
+ e.continuePropagation();
83
+ return;
84
+ }
85
+ // same handling as useMove, don't need to stop propagation, useKeyboard will do that for us
86
+ e.preventDefault();
87
+ // remember to set this and unset it so that onChangeEnd is fired
88
+ stateRef.current.setDragging(true);
89
+ switch (e.key) {
90
+ case 'PageUp':
91
+ stateRef.current.incrementY(stateRef.current.yChannelPageStep);
92
+ focusedInputRef.current = inputYRef.current;
93
+ break;
94
+ case 'PageDown':
95
+ stateRef.current.decrementY(stateRef.current.yChannelPageStep);
96
+ focusedInputRef.current = inputYRef.current;
97
+ break;
98
+ case 'Home':
99
+ direction === 'rtl' ? stateRef.current.incrementX(stateRef.current.xChannelPageStep) : stateRef.current.decrementX(stateRef.current.xChannelPageStep);
100
+ focusedInputRef.current = inputXRef.current;
101
+ break;
102
+ case 'End':
103
+ direction === 'rtl' ? stateRef.current.decrementX(stateRef.current.xChannelPageStep) : stateRef.current.incrementX(stateRef.current.xChannelPageStep);
104
+ focusedInputRef.current = inputXRef.current;
105
+ break;
106
+ }
107
+ stateRef.current.setDragging(false);
108
+ if (focusedInputRef.current) {
109
+ focusInput(focusedInputRef.current ? focusedInputRef : inputXRef);
110
+ focusedInputRef.current = undefined;
111
+ }
112
+ }
113
+ });
114
+
115
+ let moveHandler = {
116
+ onMoveStart() {
117
+ currentPosition.current = null;
118
+ stateRef.current.setDragging(true);
119
+ },
120
+ onMove({deltaX, deltaY, pointerType, shiftKey}) {
121
+ let {
122
+ incrementX,
123
+ decrementX,
124
+ incrementY,
125
+ decrementY,
126
+ xChannelPageStep,
127
+ xChannelStep,
128
+ yChannelPageStep,
129
+ yChannelStep,
130
+ getThumbPosition,
131
+ setColorFromPoint
132
+ } = stateRef.current;
133
+ if (currentPosition.current == null) {
134
+ currentPosition.current = getThumbPosition();
135
+ }
136
+ let {width, height} = containerRef.current.getBoundingClientRect();
137
+ if (pointerType === 'keyboard') {
138
+ let deltaXValue = shiftKey && xChannelPageStep > xChannelStep ? xChannelPageStep : xChannelStep;
139
+ let deltaYValue = shiftKey && yChannelPageStep > yChannelStep ? yChannelPageStep : yChannelStep;
140
+ if ((deltaX > 0 && direction === 'ltr') || (deltaX < 0 && direction === 'rtl')) {
141
+ incrementX(deltaXValue);
142
+ } else if ((deltaX < 0 && direction === 'ltr') || (deltaX > 0 && direction === 'rtl')) {
143
+ decrementX(deltaXValue);
144
+ } else if (deltaY > 0) {
145
+ decrementY(deltaYValue);
146
+ } else if (deltaY < 0) {
147
+ incrementY(deltaYValue);
148
+ }
149
+ // set the focused input based on which axis has the greater delta
150
+ focusedInputRef.current = (deltaX !== 0 || deltaY !== 0) && Math.abs(deltaY) > Math.abs(deltaX) ? inputYRef.current : inputXRef.current;
151
+ } else {
152
+ currentPosition.current.x += (direction === 'rtl' ? -1 : 1) * deltaX / width ;
153
+ currentPosition.current.y += deltaY / height;
154
+ setColorFromPoint(currentPosition.current.x, currentPosition.current.y);
155
+ }
156
+ },
157
+ onMoveEnd() {
158
+ isOnColorArea.current = undefined;
159
+ stateRef.current.setDragging(false);
160
+ focusInput(focusedInputRef.current ? focusedInputRef : inputXRef);
161
+ focusedInputRef.current = undefined;
162
+ }
163
+ };
164
+ let {moveProps: movePropsThumb} = useMove(moveHandler);
165
+
166
+ let currentPointer = useRef<number | null | undefined>(undefined);
167
+ let isOnColorArea = useRef<boolean>(false);
168
+ let {moveProps: movePropsContainer} = useMove({
169
+ onMoveStart() {
170
+ if (isOnColorArea.current) {
171
+ moveHandler.onMoveStart();
172
+ }
173
+ },
174
+ onMove(e) {
175
+ if (isOnColorArea.current) {
176
+ moveHandler.onMove(e);
177
+ }
178
+ },
179
+ onMoveEnd() {
180
+ if (isOnColorArea.current) {
181
+ moveHandler.onMoveEnd();
182
+ }
183
+ }
184
+ });
185
+
186
+ let onThumbDown = (id: number | null) => {
187
+ if (!state.isDragging) {
188
+ currentPointer.current = id;
189
+ focusInput();
190
+ state.setDragging(true);
191
+ if (typeof PointerEvent !== 'undefined') {
192
+ addGlobalListener(window, 'pointerup', onThumbUp, false);
193
+ } else {
194
+ addGlobalListener(window, 'mouseup', onThumbUp, false);
195
+ addGlobalListener(window, 'touchend', onThumbUp, false);
196
+ }
197
+ }
198
+ };
199
+
200
+ let onThumbUp = (e) => {
201
+ let id = e.pointerId ?? e.changedTouches?.[0].identifier;
202
+ if (id === currentPointer.current) {
203
+ focusInput();
204
+ state.setDragging(false);
205
+ currentPointer.current = undefined;
206
+ isOnColorArea.current = false;
207
+
208
+ if (typeof PointerEvent !== 'undefined') {
209
+ removeGlobalListener(window, 'pointerup', onThumbUp, false);
210
+ } else {
211
+ removeGlobalListener(window, 'mouseup', onThumbUp, false);
212
+ removeGlobalListener(window, 'touchend', onThumbUp, false);
213
+ }
214
+ }
215
+ };
216
+
217
+ let onColorAreaDown = (colorArea: Element, id: number | null, clientX: number, clientY: number) => {
218
+ let rect = colorArea.getBoundingClientRect();
219
+ let {width, height} = rect;
220
+ let x = (clientX - rect.x) / width;
221
+ let y = (clientY - rect.y) / height;
222
+ if (direction === 'rtl') {
223
+ x = 1 - x;
224
+ }
225
+ if (x >= 0 && x <= 1 && y >= 0 && y <= 1 && !state.isDragging && currentPointer.current === undefined) {
226
+ isOnColorArea.current = true;
227
+ currentPointer.current = id;
228
+ state.setColorFromPoint(x, y);
229
+
230
+ focusInput();
231
+ state.setDragging(true);
232
+
233
+ if (typeof PointerEvent !== 'undefined') {
234
+ addGlobalListener(window, 'pointerup', onColorAreaUp, false);
235
+ } else {
236
+ addGlobalListener(window, 'mouseup', onColorAreaUp, false);
237
+ addGlobalListener(window, 'touchend', onColorAreaUp, false);
238
+ }
239
+ }
240
+ };
241
+
242
+ let onColorAreaUp = (e) => {
243
+ let id = e.pointerId ?? e.changedTouches?.[0].identifier;
244
+ if (isOnColorArea.current && id === currentPointer.current) {
245
+ isOnColorArea.current = false;
246
+ currentPointer.current = undefined;
247
+ state.setDragging(false);
248
+ focusInput();
249
+
250
+ if (typeof PointerEvent !== 'undefined') {
251
+ removeGlobalListener(window, 'pointerup', onColorAreaUp, false);
252
+ } else {
253
+ removeGlobalListener(window, 'mouseup', onColorAreaUp, false);
254
+ removeGlobalListener(window, 'touchend', onColorAreaUp, false);
255
+ }
256
+ }
257
+ };
258
+
259
+ let colorAreaInteractions = isDisabled ? {} : mergeProps({
260
+ ...(typeof PointerEvent !== 'undefined' ? {
261
+ onPointerDown: (e: React.PointerEvent) => {
262
+ if (e.pointerType === 'mouse' && (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey)) {
263
+ return;
264
+ }
265
+ onColorAreaDown(e.currentTarget, e.pointerId, e.clientX, e.clientY);
266
+ }} : {
267
+ onMouseDown: (e: React.MouseEvent) => {
268
+ if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) {
269
+ return;
270
+ }
271
+ onColorAreaDown(e.currentTarget, undefined, e.clientX, e.clientY);
272
+ },
273
+ onTouchStart: (e: React.TouchEvent) => {
274
+ onColorAreaDown(e.currentTarget, e.changedTouches[0].identifier, e.changedTouches[0].clientX, e.changedTouches[0].clientY);
275
+ }
276
+ })
277
+ }, movePropsContainer);
278
+
279
+ let thumbInteractions = isDisabled ? {} : mergeProps({
280
+ ...(typeof PointerEvent !== 'undefined' ? {
281
+ onPointerDown: (e: React.PointerEvent) => {
282
+ if (e.pointerType === 'mouse' && (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey)) {
283
+ return;
284
+ }
285
+ onThumbDown(e.pointerId);
286
+ }} : {
287
+ onMouseDown: (e: React.MouseEvent) => {
288
+ if (e.button !== 0 || e.altKey || e.ctrlKey || e.metaKey) {
289
+ return;
290
+ }
291
+ onThumbDown(undefined);
292
+ },
293
+ onTouchStart: (e: React.TouchEvent) => {
294
+ onThumbDown(e.changedTouches[0].identifier);
295
+ }
296
+ })
297
+ }, keyboardProps, movePropsThumb);
298
+
299
+ let isMobile = isIOS() || isAndroid();
300
+
301
+ let xInputLabellingProps = useLabels({
302
+ ...props,
303
+ 'aria-label': isMobile ? state.value.getChannelName(xChannel, locale) : formatMessage('x/y', {x: state.value.getChannelName(xChannel, locale), y: state.value.getChannelName(yChannel, locale)})
304
+ });
305
+
306
+ let yInputLabellingProps = useLabels({
307
+ ...props,
308
+ 'aria-label': isMobile ? state.value.getChannelName(yChannel, locale) : formatMessage('x/y', {x: state.value.getChannelName(xChannel, locale), y: state.value.getChannelName(yChannel, locale)})
309
+ });
310
+
311
+ let colorAriaLabellingProps = useLabels(props);
312
+
313
+ let getValueTitle = () => [
314
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName('red', locale), value: state.value.formatChannelValue('red', locale)}),
315
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName('green', locale), value: state.value.formatChannelValue('green', locale)}),
316
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName('blue', locale), value: state.value.formatChannelValue('blue', locale)})
317
+ ].join(', ');
318
+
319
+ let ariaRoleDescription = isMobile ? null : formatMessage('twoDimensionalSlider');
320
+
321
+ let {visuallyHiddenProps} = useVisuallyHidden({style: {
322
+ opacity: '0.0001',
323
+ width: '100%',
324
+ height: '100%',
325
+ pointerEvents: 'none'
326
+ }});
327
+
328
+ return {
329
+ colorAreaProps: {
330
+ ...colorAriaLabellingProps,
331
+ ...colorAreaInteractions,
332
+ role: 'group'
333
+ },
334
+ gradientProps: {
335
+ role: 'presentation'
336
+ },
337
+ thumbProps: {
338
+ ...thumbInteractions,
339
+ role: 'presentation'
340
+ },
341
+ xInputProps: {
342
+ ...xInputLabellingProps,
343
+ ...visuallyHiddenProps,
344
+ type: 'range',
345
+ min: state.value.getChannelRange(xChannel).minValue,
346
+ max: state.value.getChannelRange(xChannel).maxValue,
347
+ step: xChannelStep,
348
+ 'aria-roledescription': ariaRoleDescription,
349
+ 'aria-valuetext': (
350
+ isMobile ?
351
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(xChannel, locale), value: state.value.formatChannelValue(xChannel, locale)})
352
+ :
353
+ [
354
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(xChannel, locale), value: state.value.formatChannelValue(xChannel, locale)}),
355
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(yChannel, locale), value: state.value.formatChannelValue(yChannel, locale)})
356
+ ].join(', ')
357
+ ),
358
+ title: getValueTitle(),
359
+ disabled: isDisabled,
360
+ value: state.value.getChannelValue(xChannel),
361
+ tabIndex: 0,
362
+ onChange: (e: ChangeEvent<HTMLInputElement>) => {
363
+ state.setXValue(parseFloat(e.target.value));
364
+ }
365
+ },
366
+ yInputProps: {
367
+ ...yInputLabellingProps,
368
+ ...visuallyHiddenProps,
369
+ type: 'range',
370
+ min: state.value.getChannelRange(yChannel).minValue,
371
+ max: state.value.getChannelRange(yChannel).maxValue,
372
+ step: yChannelStep,
373
+ 'aria-roledescription': ariaRoleDescription,
374
+ 'aria-valuetext': (
375
+ isMobile ?
376
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(yChannel, locale), value: state.value.formatChannelValue(yChannel, locale)})
377
+ :
378
+ [
379
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(yChannel, locale), value: state.value.formatChannelValue(yChannel, locale)}),
380
+ formatMessage('colorNameAndValue', {name: state.value.getChannelName(xChannel, locale), value: state.value.formatChannelValue(xChannel, locale)})
381
+ ].join(', ')
382
+ ),
383
+ 'aria-orientation': 'vertical',
384
+ title: getValueTitle(),
385
+ disabled: isDisabled,
386
+ value: state.value.getChannelValue(yChannel),
387
+ tabIndex: -1,
388
+ onChange: (e: ChangeEvent<HTMLInputElement>) => {
389
+ state.setYValue(parseFloat(e.target.value));
390
+ }
391
+ }
392
+ };
393
+ }
@@ -85,7 +85,7 @@ export function useColorField(
85
85
  } else if (e.deltaY < 0) {
86
86
  decrement();
87
87
  }
88
- }, [isReadOnly, isDisabled, decrement, increment]);
88
+ }, [decrement, increment]);
89
89
  // If the input isn't supposed to receive input, disable scrolling.
90
90
  let scrollingDisabled = isDisabled || isReadOnly || !focusWithin;
91
91
  useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, ref);
@@ -33,8 +33,6 @@ interface ColorWheelAria {
33
33
  inputProps: InputHTMLAttributes<HTMLInputElement>
34
34
  }
35
35
 
36
- const PAGE_MIN_STEP_SIZE = 6;
37
-
38
36
  /**
39
37
  * Provides the behavior and accessibility implementation for a color wheel component.
40
38
  * Color wheels allow users to adjust the hue of an HSL or HSB color value on a circular track.
@@ -42,7 +40,6 @@ const PAGE_MIN_STEP_SIZE = 6;
42
40
  export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState, inputRef: RefObject<HTMLElement>): ColorWheelAria {
43
41
  let {
44
42
  isDisabled,
45
- step = 1,
46
43
  innerRadius,
47
44
  outerRadius,
48
45
  'aria-label': ariaLabel
@@ -62,12 +59,38 @@ export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState
62
59
  stateRef.current = state;
63
60
 
64
61
  let currentPosition = useRef<{x: number, y: number}>(null);
62
+
63
+ let {keyboardProps} = useKeyboard({
64
+ onKeyDown(e) {
65
+ // these are the cases that useMove doesn't handle
66
+ if (!/^(PageUp|PageDown)$/.test(e.key)) {
67
+ e.continuePropagation();
68
+ return;
69
+ }
70
+ // same handling as useMove, don't need to stop propagation, useKeyboard will do that for us
71
+ e.preventDefault();
72
+ // remember to set this and unset it so that onChangeEnd is fired
73
+ stateRef.current.setDragging(true);
74
+ switch (e.key) {
75
+ case 'PageUp':
76
+ e.preventDefault();
77
+ state.increment(stateRef.current.pageStep);
78
+ break;
79
+ case 'PageDown':
80
+ e.preventDefault();
81
+ state.decrement(stateRef.current.pageStep);
82
+ break;
83
+ }
84
+ stateRef.current.setDragging(false);
85
+ }
86
+ });
87
+
65
88
  let moveHandler = {
66
89
  onMoveStart() {
67
90
  currentPosition.current = null;
68
91
  state.setDragging(true);
69
92
  },
70
- onMove({deltaX, deltaY, pointerType}) {
93
+ onMove({deltaX, deltaY, pointerType, shiftKey}) {
71
94
  if (currentPosition.current == null) {
72
95
  currentPosition.current = stateRef.current.getThumbPosition(thumbRadius);
73
96
  }
@@ -75,9 +98,9 @@ export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState
75
98
  currentPosition.current.y += deltaY;
76
99
  if (pointerType === 'keyboard') {
77
100
  if (deltaX > 0 || deltaY < 0) {
78
- state.increment();
101
+ state.increment(shiftKey ? stateRef.current.pageStep : stateRef.current.step);
79
102
  } else if (deltaX < 0 || deltaY > 0) {
80
- state.decrement();
103
+ state.decrement(shiftKey ? stateRef.current.pageStep : stateRef.current.step);
81
104
  }
82
105
  } else {
83
106
  stateRef.current.setHueFromPoint(currentPosition.current.x, currentPosition.current.y, thumbRadius);
@@ -183,21 +206,6 @@ export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState
183
206
  }
184
207
  };
185
208
 
186
- let {keyboardProps} = useKeyboard({
187
- onKeyDown(e) {
188
- switch (e.key) {
189
- case 'PageUp':
190
- e.preventDefault();
191
- state.increment(PAGE_MIN_STEP_SIZE);
192
- break;
193
- case 'PageDown':
194
- e.preventDefault();
195
- state.decrement(PAGE_MIN_STEP_SIZE);
196
- break;
197
- }
198
- }
199
- });
200
-
201
209
  let trackInteractions = isDisabled ? {} : mergeProps({
202
210
  ...(typeof PointerEvent !== 'undefined' ? {
203
211
  onPointerDown: (e: React.PointerEvent) => {
@@ -234,7 +242,7 @@ export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState
234
242
  onTouchStart: (e: React.TouchEvent) => {
235
243
  onThumbDown(e.changedTouches[0].identifier);
236
244
  }
237
- }, movePropsThumb, keyboardProps);
245
+ }, keyboardProps, movePropsThumb);
238
246
  let {x, y} = state.getThumbPosition(thumbRadius);
239
247
 
240
248
  // Provide a default aria-label if none is given
@@ -248,6 +256,7 @@ export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState
248
256
  'aria-label': ariaLabel
249
257
  });
250
258
 
259
+ let {minValue, maxValue, step} = state.value.getChannelRange('hue');
251
260
  return {
252
261
  trackProps: {
253
262
  ...trackInteractions,
@@ -291,8 +300,8 @@ export function useColorWheel(props: ColorWheelAriaProps, state: ColorWheelState
291
300
  inputLabellingProps,
292
301
  {
293
302
  type: 'range',
294
- min: '0',
295
- max: '360',
303
+ min: String(minValue),
304
+ max: String(maxValue),
296
305
  step: String(step),
297
306
  'aria-valuetext': state.value.formatChannelValue('hue', locale),
298
307
  disabled: isDisabled,