forked from elastic/eui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icon.tsx
342 lines (297 loc) Β· 9.26 KB
/
icon.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, {
PureComponent,
ImgHTMLAttributes,
ComponentType,
SVGAttributes,
CSSProperties,
} from 'react';
import classNames from 'classnames';
import { CommonProps, keysOf } from '../common';
import { typeToPathMap } from './icon_map';
import { icon as empty } from './assets/empty';
import { enqueueStateChange } from '../../services/react';
import {
htmlIdGenerator,
withEuiTheme,
WithEuiThemeProps,
} from '../../services';
export { COLORS } from './named_colors';
import { isNamedColor, NamedColor } from './named_colors';
import { euiIconStyles } from './icon.styles';
const getIsAppIcon = (iconType: IconType) => {
if (typeof iconType !== 'string') return false;
if (iconType === 'dataVisualizer') return true; // Special case
if (iconType.indexOf('data:') === 0) return false; // Inline data URIs should be short-circuited for performance
return iconType.endsWith('App') || iconType.endsWith('Job');
};
export const TYPES = keysOf(typeToPathMap);
export type EuiIconType = keyof typeof typeToPathMap;
export type IconType = EuiIconType | string | ComponentType;
// We accept arbitrary color strings, which are impossible to type.
export type IconColor = string | NamedColor;
export const SIZES = ['original', 's', 'm', 'l', 'xl', 'xxl'] as const;
export type IconSize = (typeof SIZES)[number];
export type EuiIconProps = CommonProps &
Omit<SVGAttributes<SVGElement>, 'type' | 'color' | 'size'> & {
/**
* `Enum` is any of the named icons listed in the docs, `string` is usually a URL to an SVG file, and `elementType` is any React SVG component
*/
type: IconType;
/**
* One of EUI's color palette or a valid CSS color value https://developer.mozilla.org/en-US/docs/Web/CSS/color_value.
* Note that coloring only works if your SVG is removed of fill attributes.
*/
color?: IconColor;
/**
* Note that every size other than `original` assumes the provided SVG sits on a square viewbox.
*/
size?: IconSize;
/**
* Descriptive title for naming the icon based on its use
*/
title?: string;
/**
* A unique identifier for the title element
*/
titleId?: string;
/**
* Its value should be one or more element IDs
*/
'aria-labelledby'?: string;
/**
* Callback when the icon has been loaded & rendered
*/
onIconLoad?: () => void;
};
interface State {
icon: undefined | ComponentType | string;
iconTitle: undefined | string;
isLoading: boolean;
neededLoading: boolean; // controls the fade-in animation, cached icons are immediately rendered
}
function isEuiIconType(x: EuiIconProps['type']): x is EuiIconType {
return typeof x === 'string' && typeToPathMap.hasOwnProperty(x);
}
function getInitialIcon(icon: EuiIconProps['type']) {
if (icon == null) {
return undefined;
}
if (isEuiIconType(icon)) {
if (iconComponentCache.hasOwnProperty(icon)) {
return iconComponentCache[icon];
}
return undefined;
}
return icon;
}
const generateId = htmlIdGenerator();
let iconComponentCache: { [iconType: string]: ComponentType } = {};
export const clearIconComponentCache = (iconType?: EuiIconType) => {
if (iconType != null) {
delete iconComponentCache[iconType];
} else {
iconComponentCache = {};
}
};
export const appendIconComponentCache = (iconTypeToIconComponentMap: {
[iconType: string]: ComponentType;
}) => {
for (const iconType in iconTypeToIconComponentMap) {
if (iconTypeToIconComponentMap.hasOwnProperty(iconType)) {
iconComponentCache[iconType] = iconTypeToIconComponentMap[iconType];
}
}
};
export class EuiIconClass extends PureComponent<
EuiIconProps & WithEuiThemeProps,
State
> {
isMounted = false;
constructor(props: EuiIconProps & WithEuiThemeProps) {
super(props);
const { type } = props;
const initialIcon = getInitialIcon(type);
this.state = {
icon: initialIcon,
iconTitle: undefined,
isLoading: false,
neededLoading: false,
};
}
componentDidMount() {
this.isMounted = true;
const { type } = this.props;
if (isEuiIconType(type) && this.state.icon == null) {
this.setState({
neededLoading: true,
isLoading: true,
});
this.loadIconComponent(type);
} else {
this.onIconLoad();
}
}
componentDidUpdate(prevProps: EuiIconProps) {
const { type } = this.props;
if (type !== prevProps.type) {
if (isEuiIconType(type)) {
this.setState({
neededLoading: iconComponentCache.hasOwnProperty(type),
isLoading: true,
});
this.loadIconComponent(type);
} else {
this.setState({
icon: type,
neededLoading: true,
isLoading: false,
});
}
}
}
componentWillUnmount() {
this.isMounted = false;
}
loadIconComponent = (iconType: EuiIconType) => {
if (iconComponentCache.hasOwnProperty(iconType)) {
// exists in cache
this.setState({
isLoading: false,
neededLoading: false,
icon: iconComponentCache[iconType],
});
this.onIconLoad();
return;
}
import(
/* webpackChunkName: "icon.[request]" */
// It's important that we don't use a template string here, it
// stops webpack from building a dynamic require context.
// eslint-disable-next-line prefer-template
'./assets/' + typeToPathMap[iconType]
).then(({ icon }) => {
iconComponentCache[iconType] = icon;
enqueueStateChange(() => {
if (this.isMounted && this.props.type === iconType) {
this.setState(
{
icon,
iconTitle: iconType,
isLoading: false,
},
this.onIconLoad
);
}
});
});
};
onIconLoad = () => {
const { onIconLoad } = this.props;
if (onIconLoad) {
onIconLoad();
}
};
render() {
const {
type,
size = 'm',
color,
className,
tabIndex,
title,
onIconLoad,
theme,
style,
...rest
} = this.props;
const { isLoading, neededLoading, iconTitle } = this.state;
const isLoaded = !isLoading && neededLoading;
const isCustomColor = color && !isNamedColor(color);
const optionalCustomStyles: CSSProperties | undefined = isCustomColor
? {
color: color,
...style,
}
: style;
// These icons are a little special and get some extra CSS flexibility
const isAppIcon = getIsAppIcon(type);
// App color styles are only applied if no color is passed or if color="default" is passed
const appIconHasColor = color && color !== 'default';
// The Elastic logo should be an outline in text and ghost mode
const isElasticLogoOutline =
type === 'logoElastic' && (color === 'ghost' || color === 'text');
const classes = classNames('euiIcon', className);
// Emotion styles
const styles = euiIconStyles(theme);
const cssStyles = [
styles.euiIcon,
styles[size],
color && isNamedColor(color) && styles[color as NamedColor],
isCustomColor && styles.customColor,
isElasticLogoOutline && styles.logoElasticOutline,
isAppIcon && !appIconHasColor && styles.app,
isLoading && styles.isLoading,
!isLoading && neededLoading && styles.isLoaded,
];
const icon = this.state.icon || empty;
if (typeof icon === 'string') {
return (
<img
alt={title ? title : ''}
src={icon}
className={classes}
css={cssStyles}
style={style}
tabIndex={tabIndex}
{...(rest as ImgHTMLAttributes<HTMLImageElement>)}
/>
);
} else {
const Svg = icon;
// If it's an empty icon, or if there is no aria-label, aria-labelledby, or title it gets aria-hidden true
const isAriaHidden =
icon === empty ||
!(
this.props['aria-label'] ||
this.props['aria-labelledby'] ||
this.props.title
);
const hideIconEmpty = isAriaHidden && { 'aria-hidden': true };
let titleId: any;
// If no aria-label or aria-labelledby is provided but there's a title, a titleId is generated
// The svg aria-labelledby attribute gets this titleId
// The svg title element gets this titleId as an id
if (
!this.props['aria-label'] &&
!this.props['aria-labelledby'] &&
title
) {
titleId = { titleId: generateId() };
}
return (
<Svg
className={classes}
style={optionalCustomStyles}
css={cssStyles}
tabIndex={tabIndex}
role="img"
title={title}
data-icon-type={iconTitle}
data-is-loaded={isLoaded || undefined}
data-is-loading={isLoading || undefined}
{...titleId}
{...rest}
{...hideIconEmpty}
/>
);
}
}
}
export const EuiIcon = withEuiTheme<EuiIconProps>(EuiIconClass);