-
Notifications
You must be signed in to change notification settings - Fork 209
/
MenuItem.tsx
201 lines (189 loc) · 4.87 KB
/
MenuItem.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
import * as React from 'react';
import styled from '@emotion/styled';
import {
colors,
commonColors,
iconColors,
typeColors,
spacing,
} from '@workday/canvas-kit-react-core';
import type from '@workday/canvas-kit-labs-react-core';
import {CanvasSystemIcon} from '@workday/design-assets-types';
import {SystemIcon, SystemIconProps} from '@workday/canvas-kit-react-icon';
export interface MenuItemProps extends React.LiHTMLAttributes<HTMLLIElement> {
onClick?: (event: React.SyntheticEvent) => void;
children?: React.ReactNode;
id?: string;
icon?: CanvasSystemIcon;
secondaryIcon?: CanvasSystemIcon;
hasDivider?: boolean;
isDisabled?: boolean;
isFocused?: boolean;
role: string;
shouldClose?: boolean;
}
const Item = styled('li')<Pick<MenuItemProps, 'isDisabled' | 'isFocused'>>(
{
...type.body2,
padding: `${spacing.xxs} ${spacing.s}`,
height: spacing.xl,
boxSizing: 'border-box',
cursor: 'pointer',
color: colors.blackPepper300,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
transition: 'background-color 80ms, color 80ms',
'&:focus': {
outline: 'none',
},
},
({isFocused, isDisabled}) => {
if (!isFocused && !isDisabled) {
return {
backgroundColor: 'inherit',
'&:hover': {
backgroundColor: commonColors.hoverBackground,
color: colors.blackPepper300,
'.wd-icon-fill': {
fill: iconColors.hover,
},
},
};
} else if (isDisabled && !isFocused) {
return {
color: colors.licorice100,
cursor: 'default',
'&:hover .wd-icon-fill': {
fill: iconColors.disabled,
},
};
} else {
// Is focused or focused and disabled
return {
backgroundColor: isDisabled ? colors.blueberry200 : commonColors.focusBackground,
color: typeColors.inverse,
'span .wd-icon-fill': {
fill: iconColors.inverse,
},
[`[data-whatinput='mouse'] &,
[data-whatinput='touch'] &,
[data-whatinput='pointer'] &`]: {
backgroundColor: 'inherit',
color: colors.blackPepper300,
'&:hover': {
backgroundColor: commonColors.hoverBackground,
'.wd-icon-fill': {
fill: iconColors.hover,
},
},
'.wd-icon-fill': {
fill: iconColors.standard,
},
},
};
}
}
);
const LabelContainer = styled('span')({
flex: '1',
overflow: 'hidden',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
});
const Divider = styled('hr')({
display: 'block',
height: 1,
border: 0,
borderTop: `1px solid ${colors.soap400}`,
margin: `${spacing.xxs} 0`,
});
const iconSize = 24;
const iconPadding = 16;
const StyledSystemIcon = styled(SystemIcon)({
flex: `${iconSize + iconPadding}px 0`, // gives padding between LabelContainer, no matter the direction
});
const SecondaryStyledSystemIcon = styled(StyledSystemIcon)({
display: `flex`,
justifyContent: `flex-end`,
});
let iconProps: SystemIconProps | null = null;
let secondaryIconProps: SystemIconProps | null = null;
const setIconProps = (
icon?: CanvasSystemIcon,
isDisabled?: boolean,
isFocused?: boolean
): SystemIconProps | null => {
if (!icon) {
return null;
}
let props: SystemIconProps = {
icon: icon,
size: iconSize,
};
if (isDisabled) {
props = {
...props,
fillHover: iconColors.disabled,
fill: iconColors.disabled,
};
}
if (isFocused) {
props = {
...props,
fillHover: iconColors.inverse,
};
}
return props;
};
export default class MenuItem extends React.Component<MenuItemProps> {
static defaultProps = {
shouldClose: true,
role: 'menuItem',
};
render(): React.ReactNode {
const {
onClick,
children,
id,
icon,
secondaryIcon,
hasDivider,
isDisabled,
isFocused,
role,
...elemProps
} = this.props;
iconProps = setIconProps(icon, isDisabled, isFocused);
secondaryIconProps = setIconProps(secondaryIcon, isDisabled, isFocused);
return (
<>
{hasDivider && <Divider />}
<Item
tabIndex={-1}
id={id}
role={role}
onClick={this.handleClick}
aria-disabled={!!isDisabled}
isDisabled={!!isDisabled}
isFocused={!!isFocused}
{...elemProps}
>
{icon && iconProps && <StyledSystemIcon {...iconProps} />}
<LabelContainer>{children}</LabelContainer>
{secondaryIcon && secondaryIconProps && (
<SecondaryStyledSystemIcon {...secondaryIconProps} />
)}
</Item>
</>
);
}
private handleClick = (event: React.SyntheticEvent): void => {
if (this.props.isDisabled) {
return;
}
if (this.props.onClick) {
this.props.onClick(event);
}
};
}