You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.4 KiB

---
order: 1
title:
zh-CN: 自定义图标
en-US: Custom Icon
---
## zh-CN
6 years ago
利用 `Icon` 组件封装一个可复用的自定义图标。可以将 `svg` 图标的路径信息作为 `children` 传入至组件,也可以进一步通过 `component` 属性传入一个组件来渲染最终的图标,以满足特定的需求。
## en-US
Todo, Please replace me.
````jsx
import { Icon } from 'antd';
6 years ago
import HeartSvg from './assets/heart.svg';
import AntDesignSvg from './assets/ant-design.svg';
// use webpack loader `@svgr/webpack`,
// which convert `*.svg` file into react component.
6 years ago
const HeartIcon = props => (
<Icon component={HeartSvg} viewBox="0 0 1024 1024" {...props} />
);
6 years ago
6 years ago
const AntDesignIcon = props => (
<Icon component={AntDesignSvg} viewBox="0 0 200 200" {...props} />
);
const SvgDefinitions = () => (
<svg className="svg-common-definitions">
<defs>
<linearGradient id="gradient-demo">
<stop offset="20%" stopColor="#39F" />
<stop offset="90%" stopColor="#F3F" />
</linearGradient>
</defs>
</svg>
);
const MaterialHomeIcon = props => (
<Icon viewBox="0 0 24 24" {...props}>
{/* you should pass SVG paths */}
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
</Icon>
);
const MaterialColorfulHomeIcon = props => (
<MaterialHomeIcon
{...props}
component={svgProps => (
<svg {...svgProps}>
<defs>
<linearGradient id="gradient">
<stop offset="30%" stopColor="#8360c3" />
<stop offset="70%" stopColor="#2ebf91" />
</linearGradient>
</defs>
{React.Children.map(
svgProps.children,
child => React.cloneElement(
child,
child.type === 'path' ? { fill: 'url(#gradient)' } : {}
)
6 years ago
)}
</svg>
)}
/>
);
ReactDOM.render(
<div className="custom-icons-list">
<HeartIcon style={{ color: 'hotpink' }} />
<SvgDefinitions />
<HeartIcon
style={{ fontSize: '18px' }}
svgStyle={{ fill: 'url(#gradient-demo)' }}
/>
6 years ago
<MaterialHomeIcon style={{ color: '#8360c3' }} />
<MaterialColorfulHomeIcon style={{ fontSize: '18px' }} />
<AntDesignIcon style={{ fontSize: '32px' }} />
</div>,
mountNode
);
````
```css
6 years ago
.custom-icons-list > .anticon {
margin-right: 6px;
}
6 years ago
.custom-icons-list > .svg-common-definitions {
position: absolute;
width: 0;
height: 0;
}
```