-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathindex.tsx
273 lines (215 loc) · 6.73 KB
/
index.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
/**
* @file runtime 运行时
* @author Auto Generated by IconPark
*/
import React, {HTMLAttributes, ReactElement, createContext, useContext, useMemo} from 'react';
// 描边连接类型
export type StrokeLinejoin = 'miter' | 'round' | 'bevel';
// 描边端点类型
export type StrokeLinecap = 'butt' | 'round' | 'square';
// 主题
export type Theme = 'outline' | 'filled' | 'two-tone' | 'multi-color';
// 包裹前的图标属性
export interface ISvgIconProps {
// 当前图标的唯一Id
id: string;
// 图标尺寸大小,默认1em
size: number | string;
// 描边宽度
strokeWidth: number;
// 描边端点类型
strokeLinecap: StrokeLinecap;
// 描边连接线类型
strokeLinejoin: StrokeLinejoin;
// 换肤的颜色数组
colors: string[];
}
// 图标配置属性
export interface IIconConfig {
// 图标尺寸大小,默认1em
size: number | string;
// 描边宽度
strokeWidth: number;
// 描边端点类型
strokeLinecap: StrokeLinecap;
// 描边连接线类型
strokeLinejoin: StrokeLinejoin;
// CSS前缀
prefix: string;
// RTL是否开启
rtl: boolean;
// 默认主题
theme: Theme;
// 主题默认颜色
colors: {
outline: {
fill: string;
background: string;
};
filled: {
fill: string;
background: string;
};
twoTone: {
fill: string;
twoTone: string;
};
multiColor: {
outStrokeColor: string;
outFillColor: string;
innerStrokeColor: string;
innerFillColor: string;
};
};
// 唯一ID 生成函数
guid?: () => string,
}
// 图标基础属性
export interface IIconBase {
// 图标尺寸大小,默认1em
size?: number | string;
// 描边宽度
strokeWidth?: number;
// 描边端点类型
strokeLinecap?: StrokeLinecap;
// 描边连接线类型
strokeLinejoin?: StrokeLinejoin;
// 默认主题
theme?: Theme;
// 填充色
fill?: string | string[];
}
// 安全的类型合并
export type Intersection<T, K> = T & Omit<K, keyof T>;
// 包裹后的图标非扩展属性
export interface IIcon extends IIconBase {
spin?: boolean;
}
// 包裹后的图标属性
export type IIconProps = Intersection<IIcon, HTMLAttributes<HTMLSpanElement>>;
// 包裹前的图标
export type IconRender = (props: ISvgIconProps) => ReactElement;
// 包裹后的图标
export type Icon = (props: IIconProps) => ReactElement;
// 默认属性
export const DEFAULT_ICON_CONFIGS: IIconConfig = {
size: '1em',
strokeWidth: 4,
strokeLinecap: 'round',
strokeLinejoin: 'round',
rtl: false,
theme: 'outline',
colors: {
outline: {
fill: '#333',
background: 'transparent'
},
filled: {
fill: '#333',
background: '#FFF'
},
twoTone: {
fill: '#333',
twoTone: '#2F88FF'
},
multiColor: {
outStrokeColor: '#333',
outFillColor: '#2F88FF',
innerStrokeColor: '#FFF',
innerFillColor: '#43CCF8'
}
},
prefix: 'i'
};
function guid(): string {
return 'icon-' + (((1 + Math.random()) * 0x100000000) | 0).toString(16).substring(1);
}
// 属性转换函数
export function IconConverter(id: string, icon: IIconBase, config: IIconConfig): ISvgIconProps {
const fill = typeof icon.fill === 'string' ? [icon.fill] : icon.fill || [];
const colors: string[] = [];
const theme: Theme = icon.theme || config.theme;
switch (theme) {
case 'outline':
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push('none');
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push('none');
break;
case 'filled':
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push('#FFF');
colors.push('#FFF');
break;
case 'two-tone':
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push(typeof fill[1] === 'string' ? fill[1] : config.colors.twoTone.twoTone);
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push(typeof fill[1] === 'string' ? fill[1] : config.colors.twoTone.twoTone);
break;
case 'multi-color':
colors.push(typeof fill[0] === 'string' ? fill[0] : 'currentColor');
colors.push(typeof fill[1] === 'string' ? fill[1] : config.colors.multiColor.outFillColor);
colors.push(typeof fill[2] === 'string' ? fill[2] : config.colors.multiColor.innerStrokeColor);
colors.push(typeof fill[3] === 'string' ? fill[3] : config.colors.multiColor.innerFillColor);
break;
}
return {
size: icon.size || config.size,
strokeWidth: icon.strokeWidth || config.strokeWidth,
strokeLinecap: icon.strokeLinecap || config.strokeLinecap,
strokeLinejoin: icon.strokeLinejoin || config.strokeLinejoin,
colors,
id
};
}
// 图标配置Context
const IconContext = createContext(DEFAULT_ICON_CONFIGS);
// 图标配置Provider
export const IconProvider = IconContext.Provider;
// 图标Wrapper
export function IconWrapper(name: string, rtl: boolean, render: IconRender): Icon {
return (props: IIconProps) => {
const {
size,
strokeWidth,
strokeLinecap,
strokeLinejoin,
theme,
fill,
className,
spin,
...extra
} = props;
const ICON_CONFIGS = useContext(IconContext);
let id = guid();
if (ICON_CONFIGS.guid && typeof ICON_CONFIGS.guid == 'function') {
id = ICON_CONFIGS.guid();
}
const svgProps = IconConverter(id, {
size,
strokeWidth,
strokeLinecap,
strokeLinejoin,
theme,
fill
}, ICON_CONFIGS);
const cls: string[] = [ICON_CONFIGS.prefix + '-icon'];
cls.push(ICON_CONFIGS.prefix + '-icon' + '-' + name);
if (rtl && ICON_CONFIGS.rtl) {
cls.push(ICON_CONFIGS.prefix + '-icon-rtl');
}
if (spin) {
cls.push(ICON_CONFIGS.prefix + '-icon-spin');
}
if (className) {
cls.push(className);
}
return (
<span {...extra} className={cls.join(' ')}>
{render(svgProps)}
</span>
);
};
}