-
Notifications
You must be signed in to change notification settings - Fork 577
/
utils.ts
200 lines (176 loc) · 4.67 KB
/
utils.ts
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
import { nextTick, h, getCurrentInstance } from 'vue';
import { AbstractNode, IconDefinition } from '@ant-design/icons-svg/lib/types';
import { generate as generateColor } from '@ant-design/colors';
import { useInjectIconContext } from './components/Context';
import { updateCSS, canUseDom } from './dynamicCSS';
export function warn(valid: boolean, message: string): void {
// Support uglify
if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
console.error(`Warning: ${message}`);
}
}
export function warning(valid: boolean, message: string): void {
warn(valid, `[@ant-design/icons-vue] ${message}`);
}
function camelCase(input: string) {
return input.replace(/-(.)/g, (_match, g) => g.toUpperCase());
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function isIconDefinition(target: any): target is IconDefinition {
return (
typeof target === 'object' &&
typeof target.name === 'string' &&
typeof target.theme === 'string' &&
(typeof target.icon === 'object' || typeof target.icon === 'function')
);
}
export function normalizeAttrs(attrs: Attrs = {}): Attrs {
return Object.keys(attrs).reduce((acc: Attrs, key) => {
const val = attrs[key];
switch (key) {
case 'class':
acc.className = val;
delete acc.class;
break;
default:
delete acc[key];
acc[camelCase(key)] = val;
}
return acc;
}, {});
}
export interface Attrs {
[key: string]: string;
}
export type StringKeyOf<T> = Extract<keyof T, string>;
export type EventHandlers<E> = {
[K in StringKeyOf<E>]?: E[K] extends () => any ? E[K] : (payload: E[K]) => void;
};
export function generate(
node: AbstractNode,
key: string,
rootProps?: { [key: string]: any } | false,
): any {
if (!rootProps) {
return h(
node.tag,
{ key, ...node.attrs },
(node.children || []).map((child, index) => generate(child, `${key}-${node.tag}-${index}`)),
);
}
return h(
node.tag,
{
key,
...rootProps,
...node.attrs,
},
(node.children || []).map((child, index) => generate(child, `${key}-${node.tag}-${index}`)),
);
}
export function getSecondaryColor(primaryColor: string): string {
// choose the second color
return generateColor(primaryColor)[0];
}
export function normalizeTwoToneColors(
twoToneColor: string | [string, string] | undefined,
): string[] {
if (!twoToneColor) {
return [];
}
return Array.isArray(twoToneColor) ? twoToneColor : [twoToneColor];
}
// These props make sure that the SVG behaviours like general text.
// Reference: https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4
export const svgBaseProps = {
width: '1em',
height: '1em',
fill: 'currentColor',
'aria-hidden': 'true',
focusable: 'false',
} as any;
export const iconStyles = `
.anticon {
display: inline-block;
color: inherit;
font-style: normal;
line-height: 0;
text-align: center;
text-transform: none;
vertical-align: -0.125em;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.anticon > * {
line-height: 1;
}
.anticon svg {
display: inline-block;
}
.anticon::before {
display: none;
}
.anticon .anticon-icon {
display: block;
}
.anticon[tabindex] {
cursor: pointer;
}
.anticon-spin::before,
.anticon-spin {
display: inline-block;
-webkit-animation: loadingCircle 1s infinite linear;
animation: loadingCircle 1s infinite linear;
}
@-webkit-keyframes loadingCircle {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes loadingCircle {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
`;
function getRoot(ele: Node) {
return ele && ele.getRootNode && ele.getRootNode();
}
/**
* Check if is in shadowRoot
*/
function inShadow(ele: Node) {
if (!canUseDom()) {
return false;
}
return getRoot(ele) instanceof ShadowRoot;
}
/**
* Return shadowRoot if possible
*/
function getShadowRoot(ele: Node): ShadowRoot {
return inShadow(ele) ? (getRoot(ele) as ShadowRoot) : null;
}
export const useInsertStyles = (): void => {
const { prefixCls, csp } = useInjectIconContext();
const instance = getCurrentInstance();
let mergedStyleStr = iconStyles;
if (prefixCls) {
mergedStyleStr = mergedStyleStr.replace(/anticon/g, prefixCls.value);
}
nextTick(() => {
if (!canUseDom()) {
return;
}
const ele = instance.vnode.el as any;
const shadowRoot = getShadowRoot(ele);
updateCSS(mergedStyleStr, '@ant-design-vue-icons', {
prepend: true,
csp: csp.value,
attachTo: shadowRoot,
});
});
};