IconFont.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
  2. import Icon from './index';
  3. import { mergeProps } from '../_util/props-util';
  4. var customCache = new Set();
  5. export default function create(options) {
  6. var scriptUrl = options.scriptUrl,
  7. _options$extraCommonP = options.extraCommonProps,
  8. extraCommonProps = _options$extraCommonP === undefined ? {} : _options$extraCommonP;
  9. /**
  10. * DOM API required.
  11. * Make sure in browser environment.
  12. * The Custom Icon will create a <script/>
  13. * that loads SVG symbols and insert the SVG Element into the document body.
  14. */
  15. if (typeof document !== 'undefined' && typeof window !== 'undefined' && typeof document.createElement === 'function' && typeof scriptUrl === 'string' && scriptUrl.length && !customCache.has(scriptUrl)) {
  16. var script = document.createElement('script');
  17. script.setAttribute('src', scriptUrl);
  18. script.setAttribute('data-namespace', scriptUrl);
  19. customCache.add(scriptUrl);
  20. document.body.appendChild(script);
  21. }
  22. var Iconfont = {
  23. functional: true,
  24. name: 'AIconfont',
  25. props: Icon.props,
  26. render: function render(h, context) {
  27. var props = context.props,
  28. slots = context.slots,
  29. listeners = context.listeners,
  30. data = context.data;
  31. var type = props.type,
  32. restProps = _objectWithoutProperties(props, ['type']);
  33. var slotsMap = slots();
  34. var children = slotsMap['default'];
  35. // component > children > type
  36. var content = null;
  37. if (type) {
  38. content = h('use', { attrs: { 'xlink:href': '#' + type } });
  39. }
  40. if (children) {
  41. content = children;
  42. }
  43. var iconProps = mergeProps(extraCommonProps, data, { props: restProps, on: listeners });
  44. return h(
  45. Icon,
  46. iconProps,
  47. [content]
  48. );
  49. }
  50. };
  51. return Iconfont;
  52. }