@react-stately/color 3.0.0-beta.7 → 3.0.0-beta.8

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.
package/src/Color.ts CHANGED
@@ -29,6 +29,14 @@ export function parseColor(value: string): IColor {
29
29
  throw new Error('Invalid color value: ' + value);
30
30
  }
31
31
 
32
+ export function normalizeColor(v: string | IColor) {
33
+ if (typeof v === 'string') {
34
+ return parseColor(v);
35
+ } else {
36
+ return v;
37
+ }
38
+ }
39
+
32
40
  abstract class Color implements IColor {
33
41
  abstract toFormat(format: ColorFormat): IColor;
34
42
  abstract toString(format: ColorFormat | 'css'): string;
@@ -229,9 +237,9 @@ class RGBColor extends Color {
229
237
  case 'red':
230
238
  case 'green':
231
239
  case 'blue':
232
- return {minValue: 0, maxValue: 255, step: 1};
240
+ return {minValue: 0x0, maxValue: 0xFF, step: 0x1, pageSize: 0x11};
233
241
  case 'alpha':
234
- return {minValue: 0, maxValue: 1, step: 0.01};
242
+ return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
235
243
  default:
236
244
  throw new Error('Unknown color channel: ' + channel);
237
245
  }
@@ -356,12 +364,12 @@ class HSBColor extends Color {
356
364
  getChannelRange(channel: ColorChannel): ColorChannelRange {
357
365
  switch (channel) {
358
366
  case 'hue':
359
- return {minValue: 0, maxValue: 360, step: 1};
367
+ return {minValue: 0, maxValue: 360, step: 1, pageSize: 15};
360
368
  case 'saturation':
361
369
  case 'brightness':
362
- return {minValue: 0, maxValue: 100, step: 1};
370
+ return {minValue: 0, maxValue: 100, step: 1, pageSize: 10};
363
371
  case 'alpha':
364
- return {minValue: 0, maxValue: 1, step: 0.01};
372
+ return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
365
373
  default:
366
374
  throw new Error('Unknown color channel: ' + channel);
367
375
  }
@@ -491,12 +499,12 @@ class HSLColor extends Color {
491
499
  getChannelRange(channel: ColorChannel): ColorChannelRange {
492
500
  switch (channel) {
493
501
  case 'hue':
494
- return {minValue: 0, maxValue: 360, step: 1};
502
+ return {minValue: 0, maxValue: 360, step: 1, pageSize: 15};
495
503
  case 'saturation':
496
504
  case 'lightness':
497
- return {minValue: 0, maxValue: 100, step: 1};
505
+ return {minValue: 0, maxValue: 100, step: 1, pageSize: 10};
498
506
  case 'alpha':
499
- return {minValue: 0, maxValue: 1, step: 0.01};
507
+ return {minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1};
500
508
  default:
501
509
  throw new Error('Unknown color channel: ' + channel);
502
510
  }
package/src/index.ts CHANGED
@@ -10,7 +10,8 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export * from './Color';
13
+ export {parseColor} from './Color';
14
+ export * from './useColorAreaState';
14
15
  export * from './useColorSliderState';
15
16
  export * from './useColorWheelState';
16
17
  export * from './useColorFieldState';
@@ -0,0 +1,217 @@
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 {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';
14
+ import {Color, ColorAreaProps, ColorChannel} from '@react-types/color';
15
+ import {normalizeColor, parseColor} from './Color';
16
+ import {useMemo, useRef, useState} from 'react';
17
+
18
+ export interface ColorAreaState {
19
+ /** The current color value displayed by the color area. */
20
+ readonly value: Color,
21
+ /** Sets the current color value. If a string is passed, it will be parsed to a Color. */
22
+ setValue(value: string | Color): void,
23
+
24
+ /** The current value of the horizontal axis channel displayed by the color area. */
25
+ xValue: number,
26
+ /** Sets the value for the horizontal axis channel displayed by the color area, and triggers `onChange`. */
27
+ setXValue(value: number): void,
28
+
29
+ /** The current value of the vertical axis channel displayed by the color area. */
30
+ yValue: number,
31
+ /** Sets the value for the vertical axis channel displayed by the color area, and triggers `onChange`. */
32
+ setYValue(value: number): void,
33
+
34
+ /** Sets the x and y channels of the current color value based on a percentage of the width and height of the color area, and triggers `onChange`. */
35
+ setColorFromPoint(x: number, y: number): void,
36
+ /** Returns the coordinates of the thumb relative to the upper left corner of the color area as a percentage. */
37
+ getThumbPosition(): {x: number, y: number},
38
+
39
+ /** Increments the value of the horizontal axis channel by the channel step or page amount. */
40
+ incrementX(stepSize?: number): void,
41
+ /** Decrements the value of the horizontal axis channel by the channel step or page amount. */
42
+ decrementX(stepSize?: number): void,
43
+
44
+ /** Increments the value of the vertical axis channel by the channel step or page amount. */
45
+ incrementY(stepSize?: number): void,
46
+ /** Decrements the value of the vertical axis channel by the channel step or page amount. */
47
+ decrementY(stepSize?: number): void,
48
+
49
+ /** Whether the color area is currently being dragged. */
50
+ readonly isDragging: boolean,
51
+ /** Sets whether the color area is being dragged. */
52
+ setDragging(value: boolean): void,
53
+
54
+ /** Returns the xChannel, yChannel and zChannel names based on the color value. */
55
+ channels: {xChannel: ColorChannel, yChannel: ColorChannel, zChannel: ColorChannel},
56
+ xChannelStep: number,
57
+ yChannelStep: number,
58
+ xChannelPageStep: number,
59
+ yChannelPageStep: number,
60
+
61
+ /** Returns the color that should be displayed in the color area thumb instead of `value`. */
62
+ getDisplayColor(): Color
63
+ }
64
+
65
+ const DEFAULT_COLOR = parseColor('#ffffff');
66
+ const RGBSet: Set<ColorChannel> = new Set(['red', 'green', 'blue']);
67
+ let difference = <T>(a: Set<T>, b: Set<T>): Set<T> => new Set([...a].filter(x => !b.has(x)));
68
+
69
+ /**
70
+ * Provides state management for a color area component.
71
+ * Color area allows users to adjust two channels of an HSL, HSB or RGB color value against a two-dimensional gradient background.
72
+ */
73
+ export function useColorAreaState(props: ColorAreaProps): ColorAreaState {
74
+ let {
75
+ value,
76
+ defaultValue,
77
+ xChannel,
78
+ yChannel,
79
+ onChange,
80
+ onChangeEnd
81
+ } = props;
82
+
83
+ if (!value && !defaultValue) {
84
+ defaultValue = DEFAULT_COLOR;
85
+ }
86
+
87
+ let [color, setColor] = useControlledState(value && normalizeColor(value), defaultValue && normalizeColor(defaultValue), onChange);
88
+ let valueRef = useRef(color);
89
+ valueRef.current = color;
90
+
91
+ let channels = useMemo(() => {
92
+ if (!xChannel) {
93
+ switch (yChannel) {
94
+ case 'red':
95
+ case 'green':
96
+ xChannel = 'blue';
97
+ break;
98
+ case 'blue':
99
+ xChannel = 'red';
100
+ break;
101
+ default:
102
+ xChannel = 'blue';
103
+ yChannel = 'green';
104
+ }
105
+ } else if (!yChannel) {
106
+ switch (xChannel) {
107
+ case 'red':
108
+ yChannel = 'green';
109
+ break;
110
+ case 'blue':
111
+ yChannel = 'red';
112
+ break;
113
+ default:
114
+ xChannel = 'blue';
115
+ yChannel = 'green';
116
+ }
117
+ }
118
+ let xyChannels: Set<ColorChannel> = new Set([xChannel, yChannel]);
119
+ let zChannel = difference(RGBSet, xyChannels).values().next().value;
120
+
121
+ return {xChannel, yChannel, zChannel};
122
+ }, [xChannel, yChannel]);
123
+
124
+ let xChannelRange = color.getChannelRange(channels.xChannel);
125
+ let yChannelRange = color.getChannelRange(channels.yChannel);
126
+ let {minValue: minValueX, maxValue: maxValueX, step: stepX, pageSize: pageSizeX} = xChannelRange;
127
+ let {minValue: minValueY, maxValue: maxValueY, step: stepY, pageSize: pageSizeY} = yChannelRange;
128
+
129
+ let [isDragging, setDragging] = useState(false);
130
+ let isDraggingRef = useRef(false).current;
131
+
132
+ let xValue = color.getChannelValue(channels.xChannel);
133
+ let yValue = color.getChannelValue(channels.yChannel);
134
+ let setXValue = (v: number) => {
135
+ if (v === xValue) {
136
+ return;
137
+ }
138
+ valueRef.current = color.withChannelValue(channels.xChannel, v);
139
+ setColor(valueRef.current);
140
+ };
141
+ let setYValue = (v: number) => {
142
+ if (v === yValue) {
143
+ return;
144
+ }
145
+ valueRef.current = color.withChannelValue(channels.yChannel, v);
146
+ setColor(valueRef.current);
147
+ };
148
+
149
+ return {
150
+ channels,
151
+ xChannelStep: stepX,
152
+ yChannelStep: stepY,
153
+ xChannelPageStep: pageSizeX,
154
+ yChannelPageStep: pageSizeY,
155
+ value: color,
156
+ setValue(value) {
157
+ let c = normalizeColor(value);
158
+ valueRef.current = c;
159
+ setColor(c);
160
+ },
161
+ xValue,
162
+ setXValue,
163
+ yValue,
164
+ setYValue,
165
+ setColorFromPoint(x: number, y: number) {
166
+ let {minValue: minValueX, maxValue: maxValueX} = color.getChannelRange(channels.xChannel);
167
+ let {minValue: minValueY, maxValue: maxValueY} = color.getChannelRange(channels.yChannel);
168
+ let newXValue = minValueX + clamp(x, 0, 1) * (maxValueX - minValueX);
169
+ let newYValue = minValueY + (1 - clamp(y, 0, 1)) * (maxValueY - minValueY);
170
+ let newColor:Color;
171
+ if (newXValue !== xValue) {
172
+ // Round new value to multiple of step, clamp value between min and max
173
+ newXValue = snapValueToStep(newXValue, minValueX, maxValueX, stepX);
174
+ newColor = color.withChannelValue(channels.xChannel, newXValue);
175
+ }
176
+ if (newYValue !== yValue) {
177
+ // Round new value to multiple of step, clamp value between min and max
178
+ newYValue = snapValueToStep(newYValue, minValueY, maxValueY, stepY);
179
+ newColor = (newColor || color).withChannelValue(channels.yChannel, newYValue);
180
+ }
181
+ if (newColor) {
182
+ setColor(newColor);
183
+ }
184
+ },
185
+ getThumbPosition() {
186
+ let x = (xValue - minValueX) / (maxValueX - minValueX);
187
+ let y = 1 - (yValue - minValueY) / (maxValueY - minValueY);
188
+ return {x, y};
189
+ },
190
+ incrementX(stepSize) {
191
+ setXValue(xValue + stepSize > maxValueX ? maxValueX : snapValueToStep(xValue + stepSize, minValueX, maxValueX, stepX));
192
+ },
193
+ incrementY(stepSize) {
194
+ setYValue(yValue + stepSize > maxValueY ? maxValueY : snapValueToStep(yValue + stepSize, minValueY, maxValueY, stepY));
195
+ },
196
+ decrementX(stepSize) {
197
+ setXValue(snapValueToStep(xValue - stepSize, minValueX, maxValueX, stepX));
198
+ },
199
+ decrementY(stepSize) {
200
+ setYValue(snapValueToStep(yValue - stepSize, minValueY, maxValueY, stepY));
201
+ },
202
+ setDragging(isDragging) {
203
+ let wasDragging = isDraggingRef;
204
+ isDraggingRef = isDragging;
205
+
206
+ if (onChangeEnd && !isDragging && wasDragging) {
207
+ onChangeEnd(valueRef.current);
208
+ }
209
+
210
+ setDragging(isDragging);
211
+ },
212
+ isDragging,
213
+ getDisplayColor() {
214
+ return color.withChannelValue('alpha', 1);
215
+ }
216
+ };
217
+ }
@@ -14,7 +14,7 @@ import {Color, ColorFieldProps} from '@react-types/color';
14
14
  import {parseColor} from './Color';
15
15
  import {useColor} from './useColor';
16
16
  import {useControlledState} from '@react-stately/utils';
17
- import {useEffect, useMemo, useRef, useState} from 'react';
17
+ import {useMemo, useRef, useState} from 'react';
18
18
 
19
19
  export interface ColorFieldState {
20
20
  /**
@@ -63,11 +63,11 @@ export function useColorFieldState(
63
63
  props: ColorFieldProps
64
64
  ): ColorFieldState {
65
65
  let {
66
- step = 1,
67
66
  value,
68
67
  defaultValue,
69
68
  onChange
70
69
  } = props;
70
+ let {step} = MIN_COLOR.getChannelRange('red');
71
71
 
72
72
  let initialValue = useColor(value);
73
73
  let initialDefaultValue = useColor(defaultValue);
@@ -85,9 +85,12 @@ export function useColorFieldState(
85
85
  }
86
86
  };
87
87
 
88
- useEffect(() => {
88
+ let prevValue = useRef(colorValue);
89
+ if (prevValue.current !== colorValue) {
89
90
  setInputValue(colorValue ? colorValue.toString('hex') : '');
90
- }, [colorValue, setInputValue]);
91
+ prevValue.current = colorValue;
92
+ }
93
+
91
94
 
92
95
  let parsedValue = useMemo(() => {
93
96
  let color;
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import {Color, ColorSliderProps} from '@react-types/color';
14
- import {parseColor} from './Color';
14
+ import {normalizeColor, parseColor} from './Color';
15
15
  import {SliderState, useSliderState} from '@react-stately/slider';
16
16
  import {useControlledState} from '@react-stately/utils';
17
17
 
@@ -30,14 +30,6 @@ interface ColorSliderStateOptions extends ColorSliderProps {
30
30
  locale: string
31
31
  }
32
32
 
33
- function normalizeColor(v: string | Color) {
34
- if (typeof v === 'string') {
35
- return parseColor(v);
36
- } else {
37
- return v;
38
- }
39
- }
40
-
41
33
  /**
42
34
  * Provides state management for a color slider component.
43
35
  * Color sliders allow users to adjust an individual channel of a color value.
@@ -59,13 +51,14 @@ export function useColorSliderState(props: ColorSliderStateOptions): ColorSlider
59
51
  setColor(color.withChannelValue(channel, v));
60
52
  },
61
53
  onChangeEnd([v]) {
62
- // onChange will have already been called with the right value, this is just to trigger onChangEnd
54
+ // onChange will have already been called with the right value, this is just to trigger onChangeEnd
63
55
  if (props.onChangeEnd) {
64
56
  props.onChangeEnd(color.withChannelValue(channel, v));
65
57
  }
66
58
  }
67
59
  });
68
60
 
61
+ let {step, pageSize} = color.getChannelRange(channel);
69
62
  return {
70
63
  ...sliderState,
71
64
  value: color,
@@ -92,6 +85,8 @@ export function useColorSliderState(props: ColorSliderStateOptions): ColorSlider
92
85
  },
93
86
  getThumbValueLabel() {
94
87
  return color.formatChannelValue(channel, locale);
95
- }
88
+ },
89
+ step,
90
+ pageSize
96
91
  };
97
92
  }
@@ -11,7 +11,7 @@
11
11
  */
12
12
 
13
13
  import {Color, ColorWheelProps} from '@react-types/color';
14
- import {parseColor} from './Color';
14
+ import {normalizeColor, parseColor} from './Color';
15
15
  import {useControlledState} from '@react-stately/utils';
16
16
  import {useRef, useState} from 'react';
17
17
 
@@ -32,24 +32,18 @@ export interface ColorWheelState {
32
32
  getThumbPosition(radius: number): {x: number, y: number},
33
33
 
34
34
  /** Increments the hue by the given amount (defaults to 1). */
35
- increment(minStepSize?: number): void,
35
+ increment(stepSize?: number): void,
36
36
  /** Decrements the hue by the given amount (defaults to 1). */
37
- decrement(minStepSize?: number): void,
37
+ decrement(stepSize?: number): void,
38
38
 
39
39
  /** Whether the color wheel is currently being dragged. */
40
40
  readonly isDragging: boolean,
41
41
  /** Sets whether the color wheel is being dragged. */
42
42
  setDragging(value: boolean): void,
43
43
  /** Returns the color that should be displayed in the color wheel instead of `value`. */
44
- getDisplayColor(): Color
45
- }
46
-
47
- function normalizeColor(v: string | Color) {
48
- if (typeof v === 'string') {
49
- return parseColor(v);
50
- } else {
51
- return v;
52
- }
44
+ getDisplayColor(): Color,
45
+ step: number,
46
+ pageStep: number
53
47
  }
54
48
 
55
49
  const DEFAULT_COLOR = parseColor('hsl(0, 100%, 50%)');
@@ -97,7 +91,7 @@ function cartesianToAngle(x: number, y: number, radius: number): number {
97
91
  * Color wheels allow users to adjust the hue of an HSL or HSB color value on a circular track.
98
92
  */
99
93
  export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
100
- let {defaultValue, onChange, onChangeEnd, step = 1} = props;
94
+ let {defaultValue, onChange, onChangeEnd} = props;
101
95
 
102
96
  if (!props.value && !defaultValue) {
103
97
  defaultValue = DEFAULT_COLOR;
@@ -107,6 +101,8 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
107
101
  let valueRef = useRef(value);
108
102
  valueRef.current = value;
109
103
 
104
+ let channelRange = value.getChannelRange('hue');
105
+ let {minValue: minValueX, maxValue: maxValueX, step: step, pageSize: pageStep} = channelRange;
110
106
  let [isDragging, setDragging] = useState(false);
111
107
  let isDraggingRef = useRef(false).current;
112
108
 
@@ -126,6 +122,8 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
126
122
 
127
123
  return {
128
124
  value,
125
+ step,
126
+ pageStep,
129
127
  setValue(v) {
130
128
  let color = normalizeColor(v);
131
129
  valueRef.current = color;
@@ -139,22 +137,23 @@ export function useColorWheelState(props: ColorWheelProps): ColorWheelState {
139
137
  getThumbPosition(radius) {
140
138
  return angleToCartesian(value.getChannelValue('hue'), radius);
141
139
  },
142
- increment(minStepSize: number = 0) {
143
- let newValue = hue + Math.max(minStepSize, step);
144
- if (newValue > 360) {
140
+ increment(stepSize = 1) {
141
+ let s = Math.max(stepSize, step);
142
+ let newValue = hue + s;
143
+ if (newValue >= maxValueX) {
145
144
  // Make sure you can always get back to 0.
146
- newValue = 0;
145
+ newValue = minValueX;
147
146
  }
148
- setHue(newValue);
147
+ setHue(roundToStep(mod(newValue, 360), s));
149
148
  },
150
- decrement(minStepSize: number = 0) {
151
- let s = Math.max(minStepSize, step);
149
+ decrement(stepSize = 1) {
150
+ let s = Math.max(stepSize, step);
152
151
  if (hue === 0) {
153
152
  // We can't just subtract step because this might be the case:
154
153
  // |(previous step) - 0| < step size
155
154
  setHue(roundDown(360 / s) * s);
156
155
  } else {
157
- setHue(hue - s);
156
+ setHue(roundToStep(mod(hue - s, 360), s));
158
157
  }
159
158
  },
160
159
  setDragging(isDragging) {