/
IconButton.tsx
270 lines (256 loc) · 8.76 KB
/
IconButton.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import * as React from 'react';
import {styled} from '@workday/canvas-kit-labs-react-core';
import isPropValid from '@emotion/is-prop-valid';
import {IconButtonVariant, IconButtonSize} from './types';
import {iconButtonStyles} from './ButtonStyles';
import {getButtonStyle} from './utils';
import {colors} from '@workday/canvas-kit-react-core';
import {SystemIcon} from '@workday/canvas-kit-react-icon';
import {focusRing, mouseFocusBehavior} from '@workday/canvas-kit-react-common';
import {CanvasSystemIcon} from '@workday/design-assets-types';
import {ClassNames, CSSObject} from '@emotion/core';
export interface IconButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
/**
* The type of the IconButton.
* @default IconButtonVariant.Circle
*/
variant: IconButtonVariant;
/**
* The accessibility label to indicate the action triggered by clicking the IconButton.
*/
'aria-label': string;
/**
* The size of the IconButton.
* @default IconButtonSize.Medium
*/
size?: IconButtonSize;
/**
* If true, toggle the IconButton on.
* @default false
*/
toggled?: boolean;
/**
* The ref to the button that the styled component renders.
*/
buttonRef?: React.Ref<HTMLButtonElement>;
/**
* The icon of the IconButton.
*/
icon?: CanvasSystemIcon;
/**
* The function called when the IconButton toggled state changes.
*/
onToggleChange?: (toggled: boolean | undefined) => void;
}
function getFillSelector(fillColor: string): CSSObject {
return {
'&:focus span .wd-icon-fill, &:hover span .wd-icon-fill, span .wd-icon-fill': {
fill: fillColor,
},
};
}
function getBackgroundSelector(fillColor: string): CSSObject {
return {
'&:hover span .wd-icon-background, span .wd-icon-background': {
fill: fillColor,
},
};
}
function getAccentSelector(fillColor: string): CSSObject {
return {
'&:focus span .wd-icon-accent, &:hover span .wd-icon-accent, span .wd-icon-accent': {
fill: fillColor,
},
};
}
export const iconButtonIdentifier = 'wdc-ckr-icon-button';
export const IconButtonCon = styled('button', {
shouldForwardProp: prop => isPropValid(prop) && prop !== 'size',
})<IconButtonProps>(
iconButtonStyles.styles,
({variant}) => getButtonStyle(iconButtonStyles, variant),
({size, variant}) => {
switch (size) {
default:
case IconButtonSize.Medium:
return {
margin: variant === IconButtonVariant.Plain ? '-8px' : undefined,
...iconButtonStyles.variants!.sizes.medium,
};
case IconButtonSize.Small:
return {
margin: variant === IconButtonVariant.Plain ? '-6px' : undefined,
...iconButtonStyles.variants!.sizes.small,
};
}
},
({variant, toggled}) => {
if (!toggled) {
return {};
}
switch (variant) {
case IconButtonVariant.CircleFilled:
case IconButtonVariant.SquareFilled:
case IconButtonVariant.Circle:
case IconButtonVariant.Square:
default: {
return {
backgroundColor: colors.blueberry400,
...getFillSelector(colors.frenchVanilla100),
...getAccentSelector(colors.blueberry400),
...getBackgroundSelector(colors.frenchVanilla100),
'&:focus&:hover, &:focus, &:active, &:active:hover': {
...getFillSelector(colors.frenchVanilla100),
...getAccentSelector(colors.blueberry400),
...getBackgroundSelector(colors.frenchVanilla100),
backgroundColor: colors.blueberry500,
},
'&:not([disabled])': {
'&:focus, &:focus:active': {
backgroundColor: colors.blueberry500,
...(toggled ? focusRing(2, 2) : {}),
},
},
'&:hover, &:active': {
...getFillSelector(colors.frenchVanilla100),
...getAccentSelector(colors.blueberry400),
...getBackgroundSelector(colors.frenchVanilla100),
backgroundColor: colors.blueberry500,
},
'&:disabled, &:active:disabled, &:focus:disabled, &:hover:disabled': {
backgroundColor: colors.blueberry100,
...getFillSelector(colors.blueberry300),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry300),
},
...mouseFocusBehavior({
'&:focus:active': {
...getFillSelector(colors.frenchVanilla100),
...getAccentSelector(colors.blueberry400),
...getBackgroundSelector(colors.frenchVanilla100),
backgroundColor: `${colors.blueberry500} !important`,
},
}),
};
}
case IconButtonVariant.Plain:
return {
backgroundColor: 'transparent',
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
'&:focus:hover, &:focus, &:active, &:active:hover': {
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
},
'&:not([disabled]):focus': {
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
...(toggled ? focusRing(2, 0) : {}),
},
'&:disabled, &:active:disabled, &:focus:disabled, &:hover:disabled': {
...getFillSelector(colors.blueberry200),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry200),
},
};
case IconButtonVariant.Inverse:
case IconButtonVariant.InverseFilled:
return {
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
backgroundColor: colors.frenchVanilla100,
'&:focus': {
backgroundColor: colors.frenchVanilla100,
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
},
'&:focus&:hover, &:active, &:active:hover': {
backgroundColor: colors.frenchVanilla100,
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
},
'&:focus:active': {
backgroundColor: colors.frenchVanilla100,
},
'&:hover': {
backgroundColor: colors.frenchVanilla100,
},
'&:not([disabled])': {
'&:focus': {
backgroundColor: colors.frenchVanilla100,
...(toggled
? focusRing(2, 2, true, false, 'currentColor', colors.frenchVanilla100)
: {}),
},
'&:focus:active': {
backgroundColor: colors.frenchVanilla100,
},
},
'&:disabled, &:active:disabled, &:focus:disabled, &:hover:disabled': {
backgroundColor: 'rgba(255,255,255,0.75)',
...getFillSelector(colors.blueberry400),
...getAccentSelector(colors.frenchVanilla100),
...getBackgroundSelector(colors.blueberry400),
},
...mouseFocusBehavior({
'&:focus:active': {
backgroundColor: `${colors.frenchVanilla100} !important`,
},
}),
};
}
}
);
export default class IconButton extends React.Component<IconButtonProps> {
public static Variant = IconButtonVariant;
public static Size = IconButtonSize;
static defaultProps = {
variant: IconButtonVariant.Circle,
size: IconButtonSize.Medium,
} as const;
componentDidUpdate(prevProps: IconButtonProps) {
if (
prevProps.toggled !== this.props.toggled &&
typeof this.props.onToggleChange === 'function'
) {
this.props.onToggleChange(this.props.toggled);
}
}
public render() {
// onToggleChange will generate a warning if spread over a <button>
const {
buttonRef,
size,
variant,
onToggleChange,
icon,
toggled,
children,
className,
...elemProps
} = this.props;
return (
<ClassNames>
{({cx}) => (
<IconButtonCon
toggled={toggled}
ref={buttonRef}
variant={variant}
size={size}
aria-pressed={toggled}
className={cx(iconButtonIdentifier, className)}
{...elemProps}
>
{icon ? <SystemIcon icon={icon} /> : children}
</IconButtonCon>
)}
</ClassNames>
);
}
}