-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Icon): get svg icon from iconfont, close #1209
- Loading branch information
Showing
5 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# 使用其他源的图标 | ||
|
||
- order: 5 | ||
|
||
直接使用 iconfont 源扩展icon,且不使用定制主题包的方式,支持svg图标 | ||
|
||
:::lang=en-us | ||
# Extend icon | ||
|
||
- order: 5 | ||
|
||
Extend icon without theme package | ||
::: | ||
--- | ||
|
||
````jsx | ||
import { Icon } from '@alifd/next'; | ||
|
||
const sizes = ['xxs', 'xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl']; | ||
|
||
const CustomIcon = Icon.createFromIconfontCN({ | ||
scriptUrl: '//at.alicdn.com/t/font_1464085_egnk4s8yv2f.js', | ||
}); | ||
|
||
ReactDOM.render( | ||
<div> | ||
<div className="icon-list"> | ||
<CustomIcon type="icon-store"/> | ||
<CustomIcon type="icon-pic"/> | ||
<CustomIcon type="icon-gift"/> | ||
</div> | ||
<br/> | ||
<ul className="icon-sizes"> | ||
{sizes.map((size, index) => ( | ||
<li key={index}> | ||
<CustomIcon type="icon-pic" size={size} /> | ||
<span>{size}</span> | ||
</li>))} | ||
</ul> | ||
</div>, mountNode); | ||
```` | ||
````css | ||
.icon-list i { | ||
margin: 0 10px; | ||
} | ||
.icon-sizes { | ||
margin: 0; | ||
padding: 0; | ||
list-style: none; | ||
} | ||
|
||
.icon-sizes li { | ||
display: inline-block; | ||
width: 80px; | ||
height: 80px; | ||
} | ||
|
||
.icon-sizes i { | ||
display: block; | ||
margin: 12px auto 0 auto; | ||
text-align: center; | ||
} | ||
|
||
.icon-sizes span { | ||
display: block; | ||
margin-top: 10px; | ||
text-align: center; | ||
} | ||
```` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import ConfigProvider from '../config-provider'; | ||
import Icon from './index'; | ||
|
||
const customCache = new Set(); | ||
|
||
export default function createFromIconfontCN(options = {}) { | ||
const { scriptUrl, extraCommonProps = {} } = options; | ||
|
||
/** | ||
* DOM API required. | ||
* Make sure in browser environment. | ||
* The Custom Icon will create a <script/> | ||
* that loads SVG symbols and insert the SVG Element into the document body. | ||
*/ | ||
if ( | ||
typeof document !== 'undefined' && | ||
typeof window !== 'undefined' && | ||
typeof document.createElement === 'function' && | ||
typeof scriptUrl === 'string' && | ||
scriptUrl.length && | ||
!customCache.has(scriptUrl) | ||
) { | ||
const script = document.createElement('script'); | ||
script.setAttribute('src', scriptUrl); | ||
script.setAttribute('data-namespace', scriptUrl); | ||
customCache.add(scriptUrl); | ||
document.body.appendChild(script); | ||
} | ||
|
||
const Iconfont = props => { | ||
const { type, size, children, prefix = 'next-', ...others } = props; | ||
|
||
// component > children > type | ||
let content = null; | ||
if (props.type) { | ||
content = <use xlinkHref={`#${type}`} />; | ||
} | ||
if (children) { | ||
content = children; | ||
} | ||
|
||
const classes = cx({ | ||
[`${prefix}icon-remote`]: true, | ||
}); | ||
|
||
return ( | ||
<Icon size={size}> | ||
<svg | ||
className={classes} | ||
focusable={false} | ||
{...others} | ||
{...extraCommonProps} | ||
> | ||
{content} | ||
</svg> | ||
</Icon> | ||
); | ||
}; | ||
|
||
Iconfont.displayName = 'Iconfont'; | ||
|
||
return ConfigProvider.config(Iconfont); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters