@ant-design/icons
Version:
[](https://npmjs.org/package/@ant-design/icons) [](https://npmjs.org/package/@ant-design/icons)
69 lines (67 loc) • 2.52 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import * as React from 'react';
import Icon from "./Icon";
const customCache = new Set();
function isValidCustomScriptUrl(scriptUrl) {
return Boolean(typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl));
}
function createScriptUrlElements(scriptUrls, index = 0) {
const currentScriptUrl = scriptUrls[index];
if (isValidCustomScriptUrl(currentScriptUrl)) {
const script = document.createElement('script');
script.setAttribute('src', currentScriptUrl);
script.setAttribute('data-namespace', currentScriptUrl);
if (scriptUrls.length > index + 1) {
script.onload = () => {
createScriptUrlElements(scriptUrls, index + 1);
};
script.onerror = () => {
createScriptUrlElements(scriptUrls, index + 1);
};
}
customCache.add(currentScriptUrl);
document.body.appendChild(script);
}
}
export default function create(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 (scriptUrl && typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function') {
if (Array.isArray(scriptUrl)) {
// 因为iconfont资源会把svg插入before,所以前加载相同type会覆盖后加载,为了数组覆盖顺序,倒叙插入
createScriptUrlElements(scriptUrl.reverse());
} else {
createScriptUrlElements([scriptUrl]);
}
}
const Iconfont = /*#__PURE__*/React.forwardRef((props, ref) => {
const {
type,
children,
...restProps
} = props;
// children > type
let content = null;
if (props.type) {
content = /*#__PURE__*/React.createElement("use", {
xlinkHref: `#${type}`
});
}
if (children) {
content = children;
}
return /*#__PURE__*/React.createElement(Icon, _extends({}, extraCommonProps, restProps, {
ref: ref
}), content);
});
Iconfont.displayName = 'Iconfont';
return Iconfont;
}