Skip to content

Commit

Permalink
fix(Icon): use data-namespace instread of id
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed May 9, 2020
1 parent 84831ac commit 849ece2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion docs/icon/index.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
| size | To set the icon size<br><br>**option**:<br>'xxs', 'xs', 'small', 'medium', 'large', 'xl', 'xxl', 'xxxl', 'inherit' | Enum | 'medium' |
| type | Specify which icon to display | String | - |

<!-- api-extra-start -->

### Icon.createFromIconfontCN

If you want to use svg icon, use `Icon.createFromIconfontCN`. There is cache processing by default, or you can manually cache by setting `id` (note that if there is DOM element with the same `id` on the page, icon will not load the current remote icon resource)
If you want to use svg icon, use `Icon.createFromIconfontCN`.

```js
import { Icon } from '@alifd/next';
Expand All @@ -36,3 +38,4 @@ ReactDOM.render(
</div>
, mountNode);
```
<!-- api-extra-end -->
24 changes: 24 additions & 0 deletions docs/icon/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@
| ---- | --------------------------------------------------------------------------------- | ----------- | -------- |
| size | 指定图标大小<br><br/>**可选值**<br/> xxs, xs, small, medium, large, xl, xxl, xxxl, inherit | Enum/Number | 'medium' |
| type | 指定显示哪种图标 | String | - |

<!-- api-extra-start -->

### Icon.createFromIconfontCN

通过自定义 iconfont源来使用使用svg格式的图片,默认有缓存处理

```js
import { Icon } from '@alifd/next';

const CustomIcon = Icon.createFromIconfontCN({
scriptUrl: '//at.alicdn.com/t/font_1464085_egnk4s8yv2f.js',
});

// 同 Icon 基础元素一样,有相同的 size 设定
ReactDOM.render(
<div>
<CustomIcon type="icon-store" size="small"/>
<CustomIcon type="icon-gift"/>
<CustomIcon type="icon-pic" size="large"/>
</div>
, mountNode);
```
<!-- api-extra-end -->
10 changes: 3 additions & 7 deletions src/icon/icon-font.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import Icon from './index';
const customCache = new Set();

/** Icon.createFromIconfontCN
* @description 通过自定义 iconfont源来使用使用svg格式的图片,默认有缓存处理,也可以通过设置 id 手动进行缓存(注意若页面上已经有同名 id 的 dom 元素,Icon将不再加载当前远程icon资源)
* @description 通过自定义 iconfont源来使用使用svg格式的图片
* @order 1
*/
export default function createFromIconfontCN(options = {}) {
const { scriptUrl, extraCommonProps = {}, id } = options;
const { scriptUrl, extraCommonProps = {} } = options;
let hasExist = customCache.has(scriptUrl);

if ('id' in options && document.getElementById(id)) {
if (document.querySelector(`script[data-namespace="${scriptUrl}"]`)) {
hasExist = true;
}

Expand All @@ -34,10 +34,6 @@ export default function createFromIconfontCN(options = {}) {
const script = document.createElement('script');
script.setAttribute('src', scriptUrl);
script.setAttribute('data-namespace', scriptUrl);
if ('id' in options) {
script.setAttribute('id', id);
}

customCache.add(scriptUrl);
document.body.appendChild(script);
}
Expand Down
1 change: 0 additions & 1 deletion types/icon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface iconOptions {
/**
* 如果页面上已经有同 id 的标签,那么不会再加载这个图标库
*/
id?: string;
scriptUrl: string;
}

Expand Down

0 comments on commit 849ece2

Please sign in to comment.