Skip to content

Commit d6206f7

Browse files
authored
Adding support for semantic color for icons (adobe#1182)
1 parent a44c769 commit d6206f7

File tree

5 files changed

+77
-16
lines changed

5 files changed

+77
-16
lines changed

lib/varsToTypeScript.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ const customPropertyRegExp = /^--[A-z][\w-]*$/;
1717
const patterns = [
1818
[/^--spectrum-global-dimension-((?:static-)?size-.*)$/, 'DimensionValue'],
1919
[/^--spectrum-alias-(?!.*text)(.*-(?:height|width))$/, 'DimensionValue'],
20-
[/^--spectrum-global-color-(?!.*opacity)(.*)$/, 'ColorValue'],
20+
[/^--spectrum-global-color-(?!.*opacity|status|version)(.*)$/, 'ColorValue'],
2121
[/^--spectrum-semantic-(.*?)-color-default$/, 'ColorValue'],
2222
[/^--spectrum-semantic-(.*?)-color-border$/, 'BorderColorValue'],
2323
[/^--spectrum-alias-border-color-(.*)$/, 'BorderColorValue'],
2424
[/^--spectrum-alias-background-color-(?!.*overlay|quickactions)(.*)$/, 'BackgroundColorValue'],
25+
[/^--spectrum-semantic-(.*?)-color-icon$/, 'IconColorValue'],
2526
[/^--spectrum-alias-border-size-(.*)$/, 'BorderSizeValue'],
2627
[/^--spectrum-alias-border-radius-(.*)$/, 'BorderRadiusValue']
2728
];
@@ -86,4 +87,4 @@ for (let type in types) {
8687
ts = ts.trim() + ';\n\n';
8788
}
8889

89-
fs.writeFileSync(`${__dirname}/../packages/@react-types/shared/src/dna.ts`, ts.trim() + '\n');
90+
fs.writeFileSync(`${__dirname}/../packages/@react-types/shared/src/dna.d.ts`, ts.trim() + '\n');

packages/@react-spectrum/icon/docs/workflow-icons.mdx

+28-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import workflowIconPackageData from '@spectrum-icons/workflow/package.json';
1818
```jsx import
1919
// import {Icon} from '@react-spectrum/icon';
2020
// import IconTable from './IconTable';
21+
import {Flex} from '@react-spectrum/layout';
2122
```
2223

2324
---
@@ -39,7 +40,7 @@ Workflow icons are graphical metaphors or symbols that can be used to compliment
3940
```tsx example
4041
import Airplane from '@spectrum-icons/workflow/Airplane';
4142

42-
<Airplane />
43+
<Airplane aria-label="Airplane" />
4344
```
4445

4546
## Sizing
@@ -51,13 +52,32 @@ but if you use icons standalone, you can use the `size` prop to control the sizi
5152
```tsx example
5253
import Beaker from '@spectrum-icons/workflow/Beaker';
5354

54-
<Beaker size="XXS" />
55-
<Beaker size="XS" />
56-
<Beaker size="S" />
57-
<Beaker size="M" />
58-
<Beaker size="L" />
59-
<Beaker size="XL" />
60-
<Beaker size="XXL" />
55+
<Flex gap="size-100">
56+
<Beaker aria-label="XXS Beaker" size="XXS" />
57+
<Beaker aria-label="XS Beaker" size="XS" />
58+
<Beaker aria-label="S Beaker" size="S" />
59+
<Beaker aria-label="M Beaker" size="M" />
60+
<Beaker aria-label="L Beaker" size="L" />
61+
<Beaker aria-label="XL Beaker" size="XL" />
62+
<Beaker aria-label="XXL Beaker" size="XXL" />
63+
</Flex>
64+
```
65+
66+
## Coloring
67+
68+
Icons support four Spectrum semantic colors: `negative`, `notice`, `positive`, and `informative`. Icons within React Spectrum components typically
69+
have their colors styled appropriately, but you can use the `color` prop to adjust the color of any standalone icons.
70+
71+
```tsx example
72+
import Alert from '@spectrum-icons/workflow/Alert';
73+
74+
<Flex gap="size-100">
75+
<Alert aria-label="Default Alert" />
76+
<Alert aria-label="Negative Alert" color="negative" />
77+
<Alert aria-label="Notification Alert" color="notice" />
78+
<Alert aria-label="Positive Alert" color="positive" />
79+
<Alert aria-label="Informative Alert" color="informative" />
80+
</Flex>
6181
```
6282

6383
## Labeling

packages/@react-spectrum/icon/src/Icon.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {AriaLabelingProps, DOMProps, StyleProps} from '@react-types/shared';
14-
import {classNames, useSlotProps, useStyleProps} from '@react-spectrum/utils';
13+
import {AriaLabelingProps, DOMProps, IconColorValue, StyleProps} from '@react-types/shared';
14+
import {classNames, iconStyleProps, useSlotProps, useStyleProps} from '@react-spectrum/utils';
1515
import {filterDOMProps} from '@react-aria/utils';
1616
import React, {ReactElement} from 'react';
1717
import styles from '@adobe/spectrum-css-temp/components/icon/vars.css';
@@ -38,7 +38,11 @@ interface IconProps extends DOMProps, AriaLabelingProps, StyleProps {
3838
/**
3939
* Indicates whether the element is exposed to an accessibility API.
4040
*/
41-
'aria-hidden'?: boolean | 'false' | 'true'
41+
'aria-hidden'?: boolean | 'false' | 'true',
42+
/**
43+
* Color of the Icon.
44+
*/
45+
color?: IconColorValue
4246
}
4347

4448
export type IconPropsWithoutChildren = Omit<IconProps, 'children'>;
@@ -55,7 +59,7 @@ export function Icon(props: IconProps) {
5559
'aria-hidden': ariaHidden,
5660
...otherProps
5761
} = props;
58-
let {styleProps} = useStyleProps(otherProps);
62+
let {styleProps} = useStyleProps(otherProps, iconStyleProps);
5963

6064
let provider = useProvider();
6165
let scale = 'M';

packages/@react-spectrum/utils/src/styleProps.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {BackgroundColorValue, BorderColorValue, BorderRadiusValue, BorderSizeValue, ColorValue, DimensionValue, Direction, StyleProps, ViewStyleProps} from '@react-types/shared';
13+
import {BackgroundColorValue, BorderColorValue, BorderRadiusValue, BorderSizeValue, ColorValue, DimensionValue, Direction, IconColorValue, StyleProps, ViewStyleProps} from '@react-types/shared';
1414
import {CSSProperties, HTMLAttributes} from 'react';
1515
import {useLocale} from '@react-aria/i18n';
1616

@@ -110,6 +110,11 @@ const borderStyleProps = {
110110
borderBottomWidth: 'borderBottomStyle'
111111
};
112112

113+
export const iconStyleProps: StyleHandlers = {
114+
...baseStyleProps,
115+
color: ['color', iconColorValue]
116+
};
117+
113118
function rtl(ltr: string, rtl: string) {
114119
return (direction: Direction) => (
115120
direction === 'rtl' ? rtl : ltr
@@ -134,6 +139,10 @@ function colorValue(value: ColorValue, type: ColorType = 'default') {
134139
return `var(--spectrum-global-color-${value}, var(--spectrum-semantic-${value}-color-${type}))`;
135140
}
136141

142+
function iconColorValue(value: IconColorValue) {
143+
return `var(--spectrum-semantic-${value}-color-icon)`;
144+
}
145+
137146
function backgroundColorValue(value: BackgroundColorValue) {
138147
return `var(--spectrum-alias-background-color-${value}, ${colorValue(value as ColorValue, 'background')})`;
139148
}

packages/@react-types/shared/src/dna.d.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
// This file is generated by lib/varsToTypeScript.js! DO NOT EDIT.
14+
1315
/** See the [Styling docs](styling.html#dimension-values) for a visualization of these values. */
1416
export type DimensionValue =
1517
| 'size-0'
@@ -63,7 +65,9 @@ export type DimensionValue =
6365
| 'static-size-100'
6466
| 'static-size-115'
6567
| 'static-size-125'
68+
| 'static-size-130'
6669
| 'static-size-150'
70+
| 'static-size-160'
6771
| 'static-size-175'
6872
| 'static-size-200'
6973
| 'static-size-225'
@@ -168,10 +172,13 @@ export type ColorValue =
168172
| 'static-gray-700'
169173
| 'static-gray-800'
170174
| 'static-gray-900'
175+
| 'static-blue-200'
176+
| 'static-blue-300'
171177
| 'static-blue-400'
172178
| 'static-blue-500'
173179
| 'static-blue-600'
174180
| 'static-blue-700'
181+
| 'static-blue-800'
175182
| 'static-red-400'
176183
| 'static-red-500'
177184
| 'static-red-600'
@@ -184,18 +191,25 @@ export type ColorValue =
184191
| 'static-green-500'
185192
| 'static-green-600'
186193
| 'static-green-700'
194+
| 'static-celery-200'
195+
| 'static-celery-300'
187196
| 'static-celery-400'
188197
| 'static-celery-500'
189198
| 'static-celery-600'
190199
| 'static-celery-700'
200+
| 'static-chartreuse-300'
191201
| 'static-chartreuse-400'
192202
| 'static-chartreuse-500'
193203
| 'static-chartreuse-600'
194204
| 'static-chartreuse-700'
205+
| 'static-yellow-200'
206+
| 'static-yellow-300'
195207
| 'static-yellow-400'
196208
| 'static-yellow-500'
197209
| 'static-yellow-600'
198210
| 'static-yellow-700'
211+
| 'static-magenta-200'
212+
| 'static-magenta-300'
199213
| 'static-magenta-400'
200214
| 'static-magenta-500'
201215
| 'static-magenta-600'
@@ -208,10 +222,15 @@ export type ColorValue =
208222
| 'static-purple-500'
209223
| 'static-purple-600'
210224
| 'static-purple-700'
225+
| 'static-purple-800'
226+
| 'static-indigo-200'
227+
| 'static-indigo-300'
211228
| 'static-indigo-400'
212229
| 'static-indigo-500'
213230
| 'static-indigo-600'
214231
| 'static-indigo-700'
232+
| 'static-seafoam-200'
233+
| 'static-seafoam-300'
215234
| 'static-seafoam-400'
216235
| 'static-seafoam-500'
217236
| 'static-seafoam-600'
@@ -228,8 +247,10 @@ export type BorderColorValue =
228247
| 'positive'
229248
| 'informative'
230249
| 'hover'
231-
| 'focus'
232250
| 'down'
251+
| 'focus'
252+
| 'mouse-focus'
253+
| 'disabled'
233254
| 'extralight'
234255
| 'light'
235256
| 'mid'
@@ -245,6 +266,12 @@ export type BackgroundColorValue =
245266
| 'label-gray'
246267
| ColorValue;
247268

269+
export type IconColorValue =
270+
| 'negative'
271+
| 'notice'
272+
| 'positive'
273+
| 'informative';
274+
248275
export type BorderSizeValue =
249276
| 'thin'
250277
| 'thick'

0 commit comments

Comments
 (0)