ant-design/components/icon/utils.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-12-07 16:17:45 +08:00
import { ThemeType } from './index';
import warning from '../_util/warning';
2018-08-30 22:53:24 +08:00
2018-08-15 17:21:02 +08:00
// 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 = {
2018-12-07 16:17:45 +08:00
width: '1em',
height: '1em',
fill: 'currentColor',
['aria-hidden']: 'true',
focusable: 'false',
2018-08-15 17:21:02 +08:00
};
2018-08-30 22:53:24 +08:00
const fillTester = /-fill$/;
const outlineTester = /-o$/;
const twoToneTester = /-twotone$/;
export function getThemeFromTypeName(type: string): ThemeType | null {
let result: ThemeType | null = null;
if (fillTester.test(type)) {
2018-12-07 16:17:45 +08:00
result = 'filled';
2018-08-30 22:53:24 +08:00
} else if (outlineTester.test(type)) {
2018-12-07 16:17:45 +08:00
result = 'outlined';
2018-08-30 22:53:24 +08:00
} else if (twoToneTester.test(type)) {
2018-12-07 16:17:45 +08:00
result = 'twoTone';
2018-08-30 22:53:24 +08:00
}
return result;
}
2018-09-02 19:48:57 +08:00
export function removeTypeTheme(type: string) {
2018-09-06 21:42:06 +08:00
return type
2018-12-07 16:17:45 +08:00
.replace(fillTester, '')
.replace(outlineTester, '')
.replace(twoToneTester, '');
2018-09-02 19:48:57 +08:00
}
2018-08-30 22:53:24 +08:00
export function withThemeSuffix(type: string, theme: ThemeType) {
let result = type;
2018-12-07 16:17:45 +08:00
if (theme === 'filled') {
result += '-fill';
} else if (theme === 'outlined') {
result += '-o';
} else if (theme === 'twoTone') {
result += '-twotone';
2018-09-02 19:48:57 +08:00
} else {
warning(false, `This icon '${type}' has unknown theme '${theme}'`);
2018-08-30 22:53:24 +08:00
}
return result;
}
// For alias or compatibility
export function alias(type: string) {
switch (type) {
case 'cross':
return 'close';
default:
}
return type;
}