@react-aria/color 3.1.4 → 3.2.0

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.
Files changed (227) hide show
  1. package/dist/import.mjs +6 -8
  2. package/dist/main.js +11 -13
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +6 -8
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +10 -0
  7. package/package.json +15 -23
  8. package/src/index.ts +11 -13
  9. package/dist/ar-AE.main.js +0 -11
  10. package/dist/ar-AE.main.js.map +0 -1
  11. package/dist/ar-AE.mjs +0 -13
  12. package/dist/ar-AE.module.js +0 -13
  13. package/dist/ar-AE.module.js.map +0 -1
  14. package/dist/bg-BG.main.js +0 -11
  15. package/dist/bg-BG.main.js.map +0 -1
  16. package/dist/bg-BG.mjs +0 -13
  17. package/dist/bg-BG.module.js +0 -13
  18. package/dist/bg-BG.module.js.map +0 -1
  19. package/dist/cs-CZ.main.js +0 -11
  20. package/dist/cs-CZ.main.js.map +0 -1
  21. package/dist/cs-CZ.mjs +0 -13
  22. package/dist/cs-CZ.module.js +0 -13
  23. package/dist/cs-CZ.module.js.map +0 -1
  24. package/dist/da-DK.main.js +0 -11
  25. package/dist/da-DK.main.js.map +0 -1
  26. package/dist/da-DK.mjs +0 -13
  27. package/dist/da-DK.module.js +0 -13
  28. package/dist/da-DK.module.js.map +0 -1
  29. package/dist/de-DE.main.js +0 -11
  30. package/dist/de-DE.main.js.map +0 -1
  31. package/dist/de-DE.mjs +0 -13
  32. package/dist/de-DE.module.js +0 -13
  33. package/dist/de-DE.module.js.map +0 -1
  34. package/dist/el-GR.main.js +0 -11
  35. package/dist/el-GR.main.js.map +0 -1
  36. package/dist/el-GR.mjs +0 -13
  37. package/dist/el-GR.module.js +0 -13
  38. package/dist/el-GR.module.js.map +0 -1
  39. package/dist/en-US.main.js +0 -11
  40. package/dist/en-US.main.js.map +0 -1
  41. package/dist/en-US.mjs +0 -13
  42. package/dist/en-US.module.js +0 -13
  43. package/dist/en-US.module.js.map +0 -1
  44. package/dist/es-ES.main.js +0 -11
  45. package/dist/es-ES.main.js.map +0 -1
  46. package/dist/es-ES.mjs +0 -13
  47. package/dist/es-ES.module.js +0 -13
  48. package/dist/es-ES.module.js.map +0 -1
  49. package/dist/et-EE.main.js +0 -11
  50. package/dist/et-EE.main.js.map +0 -1
  51. package/dist/et-EE.mjs +0 -13
  52. package/dist/et-EE.module.js +0 -13
  53. package/dist/et-EE.module.js.map +0 -1
  54. package/dist/fi-FI.main.js +0 -11
  55. package/dist/fi-FI.main.js.map +0 -1
  56. package/dist/fi-FI.mjs +0 -13
  57. package/dist/fi-FI.module.js +0 -13
  58. package/dist/fi-FI.module.js.map +0 -1
  59. package/dist/fr-FR.main.js +0 -11
  60. package/dist/fr-FR.main.js.map +0 -1
  61. package/dist/fr-FR.mjs +0 -13
  62. package/dist/fr-FR.module.js +0 -13
  63. package/dist/fr-FR.module.js.map +0 -1
  64. package/dist/he-IL.main.js +0 -11
  65. package/dist/he-IL.main.js.map +0 -1
  66. package/dist/he-IL.mjs +0 -13
  67. package/dist/he-IL.module.js +0 -13
  68. package/dist/he-IL.module.js.map +0 -1
  69. package/dist/hr-HR.main.js +0 -11
  70. package/dist/hr-HR.main.js.map +0 -1
  71. package/dist/hr-HR.mjs +0 -13
  72. package/dist/hr-HR.module.js +0 -13
  73. package/dist/hr-HR.module.js.map +0 -1
  74. package/dist/hu-HU.main.js +0 -11
  75. package/dist/hu-HU.main.js.map +0 -1
  76. package/dist/hu-HU.mjs +0 -13
  77. package/dist/hu-HU.module.js +0 -13
  78. package/dist/hu-HU.module.js.map +0 -1
  79. package/dist/intlStrings.main.js +0 -108
  80. package/dist/intlStrings.main.js.map +0 -1
  81. package/dist/intlStrings.mjs +0 -110
  82. package/dist/intlStrings.module.js +0 -110
  83. package/dist/intlStrings.module.js.map +0 -1
  84. package/dist/it-IT.main.js +0 -11
  85. package/dist/it-IT.main.js.map +0 -1
  86. package/dist/it-IT.mjs +0 -13
  87. package/dist/it-IT.module.js +0 -13
  88. package/dist/it-IT.module.js.map +0 -1
  89. package/dist/ja-JP.main.js +0 -11
  90. package/dist/ja-JP.main.js.map +0 -1
  91. package/dist/ja-JP.mjs +0 -13
  92. package/dist/ja-JP.module.js +0 -13
  93. package/dist/ja-JP.module.js.map +0 -1
  94. package/dist/ko-KR.main.js +0 -11
  95. package/dist/ko-KR.main.js.map +0 -1
  96. package/dist/ko-KR.mjs +0 -13
  97. package/dist/ko-KR.module.js +0 -13
  98. package/dist/ko-KR.module.js.map +0 -1
  99. package/dist/lt-LT.main.js +0 -11
  100. package/dist/lt-LT.main.js.map +0 -1
  101. package/dist/lt-LT.mjs +0 -13
  102. package/dist/lt-LT.module.js +0 -13
  103. package/dist/lt-LT.module.js.map +0 -1
  104. package/dist/lv-LV.main.js +0 -11
  105. package/dist/lv-LV.main.js.map +0 -1
  106. package/dist/lv-LV.mjs +0 -13
  107. package/dist/lv-LV.module.js +0 -13
  108. package/dist/lv-LV.module.js.map +0 -1
  109. package/dist/nb-NO.main.js +0 -11
  110. package/dist/nb-NO.main.js.map +0 -1
  111. package/dist/nb-NO.mjs +0 -13
  112. package/dist/nb-NO.module.js +0 -13
  113. package/dist/nb-NO.module.js.map +0 -1
  114. package/dist/nl-NL.main.js +0 -11
  115. package/dist/nl-NL.main.js.map +0 -1
  116. package/dist/nl-NL.mjs +0 -13
  117. package/dist/nl-NL.module.js +0 -13
  118. package/dist/nl-NL.module.js.map +0 -1
  119. package/dist/pl-PL.main.js +0 -11
  120. package/dist/pl-PL.main.js.map +0 -1
  121. package/dist/pl-PL.mjs +0 -13
  122. package/dist/pl-PL.module.js +0 -13
  123. package/dist/pl-PL.module.js.map +0 -1
  124. package/dist/pt-BR.main.js +0 -11
  125. package/dist/pt-BR.main.js.map +0 -1
  126. package/dist/pt-BR.mjs +0 -13
  127. package/dist/pt-BR.module.js +0 -13
  128. package/dist/pt-BR.module.js.map +0 -1
  129. package/dist/pt-PT.main.js +0 -11
  130. package/dist/pt-PT.main.js.map +0 -1
  131. package/dist/pt-PT.mjs +0 -13
  132. package/dist/pt-PT.module.js +0 -13
  133. package/dist/pt-PT.module.js.map +0 -1
  134. package/dist/ro-RO.main.js +0 -11
  135. package/dist/ro-RO.main.js.map +0 -1
  136. package/dist/ro-RO.mjs +0 -13
  137. package/dist/ro-RO.module.js +0 -13
  138. package/dist/ro-RO.module.js.map +0 -1
  139. package/dist/ru-RU.main.js +0 -11
  140. package/dist/ru-RU.main.js.map +0 -1
  141. package/dist/ru-RU.mjs +0 -13
  142. package/dist/ru-RU.module.js +0 -13
  143. package/dist/ru-RU.module.js.map +0 -1
  144. package/dist/sk-SK.main.js +0 -11
  145. package/dist/sk-SK.main.js.map +0 -1
  146. package/dist/sk-SK.mjs +0 -13
  147. package/dist/sk-SK.module.js +0 -13
  148. package/dist/sk-SK.module.js.map +0 -1
  149. package/dist/sl-SI.main.js +0 -11
  150. package/dist/sl-SI.main.js.map +0 -1
  151. package/dist/sl-SI.mjs +0 -13
  152. package/dist/sl-SI.module.js +0 -13
  153. package/dist/sl-SI.module.js.map +0 -1
  154. package/dist/sr-SP.main.js +0 -11
  155. package/dist/sr-SP.main.js.map +0 -1
  156. package/dist/sr-SP.mjs +0 -13
  157. package/dist/sr-SP.module.js +0 -13
  158. package/dist/sr-SP.module.js.map +0 -1
  159. package/dist/sv-SE.main.js +0 -11
  160. package/dist/sv-SE.main.js.map +0 -1
  161. package/dist/sv-SE.mjs +0 -13
  162. package/dist/sv-SE.module.js +0 -13
  163. package/dist/sv-SE.module.js.map +0 -1
  164. package/dist/tr-TR.main.js +0 -11
  165. package/dist/tr-TR.main.js.map +0 -1
  166. package/dist/tr-TR.mjs +0 -13
  167. package/dist/tr-TR.module.js +0 -13
  168. package/dist/tr-TR.module.js.map +0 -1
  169. package/dist/types.d.ts +0 -119
  170. package/dist/types.d.ts.map +0 -1
  171. package/dist/uk-UA.main.js +0 -11
  172. package/dist/uk-UA.main.js.map +0 -1
  173. package/dist/uk-UA.mjs +0 -13
  174. package/dist/uk-UA.module.js +0 -13
  175. package/dist/uk-UA.module.js.map +0 -1
  176. package/dist/useColorArea.main.js +0 -400
  177. package/dist/useColorArea.main.js.map +0 -1
  178. package/dist/useColorArea.mjs +0 -395
  179. package/dist/useColorArea.module.js +0 -395
  180. package/dist/useColorArea.module.js.map +0 -1
  181. package/dist/useColorAreaGradient.main.js +0 -123
  182. package/dist/useColorAreaGradient.main.js.map +0 -1
  183. package/dist/useColorAreaGradient.mjs +0 -118
  184. package/dist/useColorAreaGradient.module.js +0 -118
  185. package/dist/useColorAreaGradient.module.js.map +0 -1
  186. package/dist/useColorChannelField.main.js +0 -36
  187. package/dist/useColorChannelField.main.js.map +0 -1
  188. package/dist/useColorChannelField.mjs +0 -31
  189. package/dist/useColorChannelField.module.js +0 -31
  190. package/dist/useColorChannelField.module.js.map +0 -1
  191. package/dist/useColorField.main.js +0 -101
  192. package/dist/useColorField.main.js.map +0 -1
  193. package/dist/useColorField.mjs +0 -96
  194. package/dist/useColorField.module.js +0 -96
  195. package/dist/useColorField.module.js.map +0 -1
  196. package/dist/useColorSlider.main.js +0 -133
  197. package/dist/useColorSlider.main.js.map +0 -1
  198. package/dist/useColorSlider.mjs +0 -128
  199. package/dist/useColorSlider.module.js +0 -128
  200. package/dist/useColorSlider.module.js.map +0 -1
  201. package/dist/useColorSwatch.main.js +0 -67
  202. package/dist/useColorSwatch.main.js.map +0 -1
  203. package/dist/useColorSwatch.mjs +0 -62
  204. package/dist/useColorSwatch.module.js +0 -62
  205. package/dist/useColorSwatch.module.js.map +0 -1
  206. package/dist/useColorWheel.main.js +0 -275
  207. package/dist/useColorWheel.main.js.map +0 -1
  208. package/dist/useColorWheel.mjs +0 -270
  209. package/dist/useColorWheel.module.js +0 -270
  210. package/dist/useColorWheel.module.js.map +0 -1
  211. package/dist/zh-CN.main.js +0 -11
  212. package/dist/zh-CN.main.js.map +0 -1
  213. package/dist/zh-CN.mjs +0 -13
  214. package/dist/zh-CN.module.js +0 -13
  215. package/dist/zh-CN.module.js.map +0 -1
  216. package/dist/zh-TW.main.js +0 -11
  217. package/dist/zh-TW.main.js.map +0 -1
  218. package/dist/zh-TW.mjs +0 -13
  219. package/dist/zh-TW.module.js +0 -13
  220. package/dist/zh-TW.module.js.map +0 -1
  221. package/src/useColorArea.ts +0 -467
  222. package/src/useColorAreaGradient.ts +0 -139
  223. package/src/useColorChannelField.ts +0 -36
  224. package/src/useColorField.ts +0 -142
  225. package/src/useColorSlider.ts +0 -151
  226. package/src/useColorSwatch.ts +0 -74
  227. package/src/useColorWheel.ts +0 -345
@@ -1,142 +0,0 @@
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 {AriaColorFieldProps} from '@react-types/color';
14
- import {ColorFieldState} from '@react-stately/color';
15
- import {DOMAttributes, ValidationResult} from '@react-types/shared';
16
- import {
17
- InputHTMLAttributes,
18
- LabelHTMLAttributes,
19
- RefObject,
20
- useCallback,
21
- useState
22
- } from 'react';
23
- import {mergeProps, useFormReset, useId} from '@react-aria/utils';
24
- import {privateValidationStateProp} from '@react-stately/form';
25
- import {useFocusWithin, useScrollWheel} from '@react-aria/interactions';
26
- import {useFormattedTextField} from '@react-aria/textfield';
27
- import {useSpinButton} from '@react-aria/spinbutton';
28
-
29
- export interface ColorFieldAria extends ValidationResult {
30
- /** Props for the label element. */
31
- labelProps: LabelHTMLAttributes<HTMLLabelElement>,
32
- /** Props for the input element. */
33
- inputProps: InputHTMLAttributes<HTMLInputElement>,
34
- /** Props for the text field's description element, if any. */
35
- descriptionProps: DOMAttributes,
36
- /** Props for the text field's error message element, if any. */
37
- errorMessageProps: DOMAttributes
38
- }
39
-
40
- /**
41
- * Provides the behavior and accessibility implementation for a color field component.
42
- * Color fields allow users to enter and adjust a hex color value.
43
- */
44
- export function useColorField(
45
- props: AriaColorFieldProps,
46
- state: ColorFieldState,
47
- ref: RefObject<HTMLInputElement | null>
48
- ): ColorFieldAria {
49
- let {
50
- isDisabled,
51
- isReadOnly,
52
- isRequired,
53
- isWheelDisabled,
54
- validationBehavior = 'aria'
55
- } = props;
56
-
57
- let {
58
- colorValue,
59
- inputValue,
60
- increment,
61
- decrement,
62
- incrementToMax,
63
- decrementToMin,
64
- commit
65
- } = state;
66
-
67
- let inputId = useId();
68
- let {spinButtonProps} = useSpinButton(
69
- {
70
- isDisabled,
71
- isReadOnly,
72
- isRequired,
73
- maxValue: 0xFFFFFF,
74
- minValue: 0,
75
- onIncrement: increment,
76
- onIncrementToMax: incrementToMax,
77
- onDecrement: decrement,
78
- onDecrementToMin: decrementToMin,
79
- value: colorValue ? colorValue.toHexInt() : undefined,
80
- textValue: colorValue ? colorValue.toString('hex') : undefined
81
- }
82
- );
83
-
84
- let [focusWithin, setFocusWithin] = useState(false);
85
- let {focusWithinProps} = useFocusWithin({isDisabled, onFocusWithinChange: setFocusWithin});
86
-
87
- let onWheel = useCallback((e) => {
88
- if (Math.abs(e.deltaY) <= Math.abs(e.deltaX)) {
89
- return;
90
- }
91
- if (e.deltaY > 0) {
92
- increment();
93
- } else if (e.deltaY < 0) {
94
- decrement();
95
- }
96
- }, [decrement, increment]);
97
- // If the input isn't supposed to receive input, disable scrolling.
98
- let scrollingDisabled = isWheelDisabled || isDisabled || isReadOnly || !focusWithin;
99
- useScrollWheel({onScroll: onWheel, isDisabled: scrollingDisabled}, ref);
100
-
101
- let onChange = value => {
102
- if (state.validate(value)) {
103
- state.setInputValue(value);
104
- }
105
- };
106
-
107
- let {inputProps, ...otherProps} = useFormattedTextField({
108
- ...props,
109
- id: inputId,
110
- value: inputValue,
111
- // Intentionally invalid value that will be ignored by onChange during form reset
112
- // This is handled separately below.
113
- defaultValue: '!',
114
- validate: undefined,
115
- [privateValidationStateProp]: state,
116
- type: 'text',
117
- autoComplete: 'off',
118
- onChange
119
- }, state, ref);
120
-
121
- useFormReset(ref, state.defaultColorValue, state.setColorValue);
122
-
123
- inputProps = mergeProps(inputProps, spinButtonProps, focusWithinProps, {
124
- role: 'textbox',
125
- 'aria-valuemax': null,
126
- 'aria-valuemin': null,
127
- 'aria-valuenow': null,
128
- 'aria-valuetext': null,
129
- autoCorrect: 'off',
130
- spellCheck: 'false',
131
- onBlur: commit
132
- });
133
-
134
- if (validationBehavior === 'native') {
135
- inputProps['aria-required'] = undefined;
136
- }
137
-
138
- return {
139
- inputProps,
140
- ...otherProps
141
- };
142
- }
@@ -1,151 +0,0 @@
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 {AriaColorSliderProps} from '@react-types/color';
14
- import {ColorSliderState} from '@react-stately/color';
15
- import {DOMAttributes, RefObject} from '@react-types/shared';
16
- import {InputHTMLAttributes} from 'react';
17
- import {mergeProps} from '@react-aria/utils';
18
- import {useLocale} from '@react-aria/i18n';
19
- import {useSlider, useSliderThumb} from '@react-aria/slider';
20
- import {useVisuallyHidden} from '@react-aria/visually-hidden';
21
-
22
- export interface AriaColorSliderOptions extends AriaColorSliderProps {
23
- /** A ref for the track element. */
24
- trackRef: RefObject<Element | null>,
25
- /** A ref for the input element. */
26
- inputRef: RefObject<HTMLInputElement | null>
27
- }
28
-
29
- export interface ColorSliderAria {
30
- /** Props for the label element. */
31
- labelProps: DOMAttributes,
32
- /** Props for the track element. */
33
- trackProps: DOMAttributes,
34
- /** Props for the thumb element. */
35
- thumbProps: DOMAttributes,
36
- /** Props for the visually hidden range input element. */
37
- inputProps: InputHTMLAttributes<HTMLInputElement>,
38
- /** Props for the output element, displaying the value of the color slider. */
39
- outputProps: DOMAttributes
40
- }
41
-
42
- /**
43
- * Provides the behavior and accessibility implementation for a color slider component.
44
- * Color sliders allow users to adjust an individual channel of a color value.
45
- */
46
- export function useColorSlider(props: AriaColorSliderOptions, state: ColorSliderState): ColorSliderAria {
47
- let {trackRef, inputRef, orientation, channel, 'aria-label': ariaLabel, name, form} = props;
48
-
49
- let {locale, direction} = useLocale();
50
-
51
- // Provide a default aria-label if there is no other label provided.
52
- if (!props.label && !ariaLabel && !props['aria-labelledby']) {
53
- ariaLabel = state.value.getChannelName(channel, locale);
54
- }
55
-
56
- // @ts-ignore - ignore unused incompatible props
57
- let {groupProps, trackProps, labelProps, outputProps} = useSlider({...props, 'aria-label': ariaLabel}, state, trackRef);
58
- let {inputProps, thumbProps} = useSliderThumb({
59
- index: 0,
60
- orientation,
61
- isDisabled: props.isDisabled,
62
- name,
63
- form,
64
- trackRef,
65
- inputRef
66
- }, state);
67
-
68
- let value = state.getDisplayColor();
69
- let generateBackground = () => {
70
- let to: string;
71
- if (orientation === 'vertical') {
72
- to = 'top';
73
- } else if (direction === 'ltr') {
74
- to = 'right';
75
- } else {
76
- to = 'left';
77
- }
78
- switch (channel) {
79
- case 'hue': {
80
- let stops = [0, 60, 120, 180, 240, 300, 360].map(hue => value.withChannelValue('hue', hue).toString('css')).join(', ');
81
- return `linear-gradient(to ${to}, ${stops})`;
82
- }
83
- case 'lightness': {
84
- // We have to add an extra color stop in the middle so that the hue shows up at all.
85
- // Otherwise it will always just be black to white.
86
- let min = state.getThumbMinValue(0);
87
- let max = state.getThumbMaxValue(0);
88
- let start = value.withChannelValue(channel, min).toString('css');
89
- let middle = value.withChannelValue(channel, (max - min) / 2).toString('css');
90
- let end = value.withChannelValue(channel, max).toString('css');
91
- return `linear-gradient(to ${to}, ${start}, ${middle}, ${end})`;
92
- }
93
- case 'saturation':
94
- case 'brightness':
95
- case 'red':
96
- case 'green':
97
- case 'blue':
98
- case 'alpha': {
99
- let start = value.withChannelValue(channel, state.getThumbMinValue(0)).toString('css');
100
- let end = value.withChannelValue(channel, state.getThumbMaxValue(0)).toString('css');
101
- return `linear-gradient(to ${to}, ${start}, ${end})`;
102
- }
103
- default:
104
- throw new Error('Unknown color channel: ' + channel);
105
- }
106
- };
107
-
108
- let forcedColorAdjustNoneStyle = {forcedColorAdjust: 'none'};
109
-
110
- if (channel === 'hue') {
111
- inputProps['aria-valuetext'] += `, ${value.getHueName(locale)}`;
112
- } else if (channel !== 'alpha') {
113
- inputProps['aria-valuetext'] += `, ${value.getColorName(locale)}`;
114
- }
115
-
116
- let {visuallyHiddenProps} = useVisuallyHidden({
117
- style: {
118
- opacity: '0.0001',
119
- width: '100%',
120
- height: '100%',
121
- pointerEvents: 'none'
122
- }
123
- });
124
-
125
- return {
126
- trackProps: {
127
- ...mergeProps(groupProps, trackProps),
128
- style: {
129
- ...trackProps.style,
130
- ...forcedColorAdjustNoneStyle,
131
- background: generateBackground()
132
- }
133
- },
134
- inputProps: {
135
- ...inputProps,
136
- style: {
137
- ...inputProps.style,
138
- ...visuallyHiddenProps.style
139
- }
140
- },
141
- thumbProps: {
142
- ...thumbProps,
143
- style: {
144
- ...thumbProps.style,
145
- ...forcedColorAdjustNoneStyle
146
- }
147
- },
148
- labelProps,
149
- outputProps
150
- };
151
- }
@@ -1,74 +0,0 @@
1
- /*
2
- * Copyright 2024 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 {AriaLabelingProps, DOMProps} from '@react-types/shared';
14
- import {Color} from '@react-types/color';
15
- import {filterDOMProps, useId} from '@react-aria/utils';
16
- import {HTMLAttributes, useMemo} from 'react';
17
- // @ts-ignore
18
- import intlMessages from '../intl/*.json';
19
- import {parseColor} from '@react-stately/color';
20
- import {useLocale, useLocalizedStringFormatter} from '@react-aria/i18n';
21
-
22
- export interface AriaColorSwatchProps extends AriaLabelingProps, DOMProps {
23
- /** The color value to display in the swatch. */
24
- color?: string | Color,
25
- /**
26
- * A localized accessible name for the color.
27
- * By default, a description is generated from the color value,
28
- * but this can be overridden if you have a more specific color
29
- * name (e.g. Pantone colors).
30
- */
31
- colorName?: string
32
- }
33
-
34
- export interface ColorSwatchAria {
35
- /** Props for the color swatch element. */
36
- colorSwatchProps: HTMLAttributes<HTMLElement>,
37
- /** The parsed color value of the swatch. */
38
- color: Color
39
- }
40
-
41
- /**
42
- * Provides the accessibility implementation for a color swatch component.
43
- * A color swatch displays a preview of a selected color.
44
- */
45
- export function useColorSwatch(props: AriaColorSwatchProps): ColorSwatchAria {
46
- let {color: value, colorName} = props;
47
- let nonNullValue = value || '#fff0';
48
- let color = useMemo(() => typeof nonNullValue === 'string' ? parseColor(nonNullValue) : nonNullValue, [nonNullValue]);
49
- let {locale} = useLocale();
50
- let DOMProps = filterDOMProps(props, {labelable: true});
51
- let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/color');
52
- let id = useId(props.id);
53
-
54
- if (!colorName) {
55
- colorName = color.getChannelValue('alpha') === 0 ? stringFormatter.format('transparent') : color.getColorName(locale);
56
- }
57
-
58
- return {
59
- colorSwatchProps: {
60
- ...DOMProps,
61
- role: 'img',
62
- 'aria-roledescription': stringFormatter.format('colorSwatch'),
63
- 'aria-label': [colorName, props['aria-label'] || ''].filter(Boolean).join(', '),
64
- 'aria-labelledby': props['aria-labelledby'] ? `${id} ${props['aria-labelledby']}` : undefined,
65
- id,
66
- style: {
67
- backgroundColor: color.toString('css'),
68
- // @ts-ignore
69
- forcedColorAdjust: 'none'
70
- }
71
- },
72
- color: color || null
73
- };
74
- }