ant-design/components/icon/utils.ts

79 lines
2.0 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',
2019-08-14 18:21:24 +08:00
'aria-hidden': true,
2018-12-07 16:17:45 +08:00
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, 'Icon', `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) {
let newType = type;
switch (type) {
case 'cross':
newType = 'close';
break;
// https://github.com/ant-design/ant-design/issues/13007
case 'interation':
newType = 'interaction';
break;
// https://github.com/ant-design/ant-design/issues/16810
case 'canlendar':
newType = 'calendar';
break;
// https://github.com/ant-design/ant-design/issues/17448
case 'colum-height':
newType = 'column-height';
break;
default:
}
warning(
newType === type,
'Icon',
2019-07-07 22:08:46 +08:00
`Icon '${type}' was a typo and is now deprecated, please use '${newType}' instead.`,
);
return newType;
}