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.

60 lines
1.6 KiB

6 years ago
import Icon, { IconProps } from './index';
import * as React from 'react';
const customCache = new Set<string>();
export interface CustomIconOptions {
namespace?: string;
prefix?: string;
scriptUrl?: string;
extraCommonProps?: { [key: string]: any };
}
6 years ago
export default function create(options: CustomIconOptions = {}): React.SFC<IconProps> {
const { namespace, prefix = '', scriptUrl, extraCommonProps = {} } = options;
6 years ago
/**
* 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
6 years ago
&& typeof namespace === 'string' && namespace.length
&& !customCache.has(namespace)
) {
const script = document.createElement('script');
script.setAttribute('src', `https:${scriptUrl}`);
6 years ago
script.setAttribute('data-namespace', namespace);
customCache.add(namespace);
document.body.appendChild(script);
}
const Iconfont: React.SFC<IconProps> = (props) => {
6 years ago
const { type } = props;
6 years ago
// component > children > type
let content = null;
if (props.type) {
content = <use xlinkHref={`#${prefix}${type}`} />;
}
6 years ago
if (props.children) {
content = props.children;
}
6 years ago
return (
<Icon
{...props}
{...extraCommonProps}
>
{content}
</Icon>
);
};
Iconfont.displayName = 'Iconfont';
return Iconfont;
}