-
Notifications
You must be signed in to change notification settings - Fork 590
/
Copy pathIcon.tsx
143 lines (118 loc) · 3.15 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
// Seems this is used for iconFont
import * as React from 'react';
import classNames from 'classnames';
import { useComposeRef } from 'rc-util/lib/ref'
import Context from './Context';
import { svgBaseProps, warning, useInsertStyles } from '../utils';
export interface IconBaseProps extends React.HTMLProps<HTMLSpanElement> {
spin?: boolean;
rotate?: number;
}
export interface CustomIconComponentProps {
width: string | number;
height: string | number;
fill?: string;
viewBox?: string;
className?: string;
color?: string;
style?: React.CSSProperties;
}
export interface IconComponentProps extends IconBaseProps {
viewBox?: string;
component?: React.ComponentType<CustomIconComponentProps | React.SVGProps<SVGSVGElement>> | React.ForwardRefExoticComponent<CustomIconComponentProps>;
ariaLabel?: React.AriaAttributes['aria-label'];
}
const Icon: React.ForwardRefExoticComponent<
Omit<IconComponentProps, 'ref'> & React.RefAttributes<HTMLSpanElement>
> = React.forwardRef<HTMLSpanElement, IconComponentProps>((props, ref) => {
const {
// affect outter <i>...</i>
className,
// affect inner <svg>...</svg>
component: Component,
color,
viewBox,
spin,
rotate,
tabIndex,
onClick,
// children
children,
...restProps
} = props;
const iconRef = React.useRef<HTMLElement>();
const mergedRef = useComposeRef(iconRef, ref);
warning(
Boolean(Component || children),
'Should have `component` prop or `children`.',
);
useInsertStyles(iconRef);
const { prefixCls = 'anticon', rootClassName } = React.useContext(Context);
const classString = classNames(
rootClassName,
prefixCls,
{
[`${prefixCls}-spin`]: !!spin && !!Component,
},
className,
);
const svgClassString = classNames({
[`${prefixCls}-spin`]: !!spin,
});
const svgStyle = rotate
? {
msTransform: `rotate(${rotate}deg)`,
transform: `rotate(${rotate}deg)`,
}
: undefined;
const innerSvgProps: CustomIconComponentProps = {
...svgBaseProps,
color,
className: svgClassString,
style: svgStyle,
viewBox,
};
if (!viewBox) {
delete innerSvgProps.viewBox;
}
// component > children
const renderInnerNode = () => {
if (Component) {
return <Component {...innerSvgProps}>{children}</Component>;
}
if (children) {
warning(
Boolean(viewBox) ||
(React.Children.count(children) === 1 &&
React.isValidElement(children) &&
React.Children.only(children).type === 'use'),
'Make sure that you provide correct `viewBox`' +
' prop (default `0 0 1024 1024`) to the icon.',
);
return (
<svg {...innerSvgProps} viewBox={viewBox}>
{children}
</svg>
);
}
return null;
};
let iconTabIndex = tabIndex;
if (iconTabIndex === undefined && onClick) {
iconTabIndex = -1;
}
return (
<span
role="img"
{...restProps}
ref={mergedRef}
tabIndex={iconTabIndex}
onClick={onClick}
className={classString}
>
{renderInnerNode()}
</span>
);
});
Icon.displayName = 'AntdIcon';
export default Icon;