Icon.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var _excluded = ["class", "component", "viewBox", "spin", "rotate", "tabindex", "onClick"];
  2. import { createVNode as _createVNode } from "vue";
  3. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(arguments[i]) : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
  6. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  7. import { svgBaseProps, warning } from '../utils';
  8. import { useInjectIconContext } from './Context';
  9. import { InsertStyles } from './InsertStyle';
  10. var Icon = function Icon(props, context) {
  11. var _classString;
  12. var attrs = context.attrs,
  13. slots = context.slots;
  14. var _props$attrs = _objectSpread({}, props, attrs),
  15. cls = _props$attrs["class"],
  16. Component = _props$attrs.component,
  17. viewBox = _props$attrs.viewBox,
  18. spin = _props$attrs.spin,
  19. rotate = _props$attrs.rotate,
  20. tabindex = _props$attrs.tabindex,
  21. onClick = _props$attrs.onClick,
  22. restProps = _objectWithoutProperties(_props$attrs, _excluded);
  23. var _useInjectIconContext = useInjectIconContext(),
  24. prefixCls = _useInjectIconContext.prefixCls,
  25. rootClassName = _useInjectIconContext.rootClassName;
  26. var children = slots["default"] && slots["default"]();
  27. var hasChildren = children && children.length;
  28. var slotsComponent = slots.component;
  29. warning(Boolean(Component || hasChildren || slotsComponent), 'Should have `component` prop/slot or `children`.');
  30. var classString = (_classString = {}, _defineProperty(_classString, rootClassName.value, !!rootClassName.value), _defineProperty(_classString, prefixCls.value, true), _classString);
  31. var svgClassString = _defineProperty({}, "".concat(prefixCls.value, "-spin"), spin === '' || !!spin);
  32. var svgStyle = rotate ? {
  33. msTransform: "rotate(".concat(rotate, "deg)"),
  34. transform: "rotate(".concat(rotate, "deg)")
  35. } : undefined;
  36. var innerSvgProps = _objectSpread({}, svgBaseProps, {
  37. viewBox: viewBox,
  38. "class": svgClassString,
  39. style: svgStyle
  40. });
  41. if (!viewBox) {
  42. delete innerSvgProps.viewBox;
  43. }
  44. var renderInnerNode = function renderInnerNode() {
  45. if (Component) {
  46. return _createVNode(Component, innerSvgProps, {
  47. "default": function _default() {
  48. return [children];
  49. }
  50. });
  51. }
  52. if (slotsComponent) {
  53. return slotsComponent(innerSvgProps);
  54. }
  55. if (hasChildren) {
  56. warning(Boolean(viewBox) || children.length === 1 && children[0] && children[0].type === 'use', 'Make sure that you provide correct `viewBox`' + ' prop (default `0 0 1024 1024`) to the icon.');
  57. return _createVNode("svg", _objectSpread({}, innerSvgProps, {
  58. "viewBox": viewBox
  59. }), [children]);
  60. }
  61. return null;
  62. };
  63. var iconTabIndex = tabindex;
  64. if (iconTabIndex === undefined && onClick) {
  65. iconTabIndex = -1;
  66. restProps.tabindex = iconTabIndex;
  67. }
  68. return _createVNode("span", _objectSpread({
  69. "role": "img"
  70. }, restProps, {
  71. "onClick": onClick,
  72. "class": [classString, cls]
  73. }), [renderInnerNode(), _createVNode(InsertStyles, null, null)]);
  74. };
  75. Icon.props = {
  76. spin: Boolean,
  77. rotate: Number,
  78. viewBox: String,
  79. ariaLabel: String
  80. };
  81. Icon.inheritAttrs = false;
  82. Icon.displayName = 'Icon';
  83. export default Icon;