utils.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 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; }
  2. 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; }
  3. import { nextTick, h, getCurrentInstance } from 'vue';
  4. import { generate as generateColor } from '@ant-design/colors';
  5. import { useInjectIconContext } from './components/Context';
  6. import { updateCSS, canUseDom } from './dynamicCSS';
  7. export function warn(valid, message) {
  8. // Support uglify
  9. if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
  10. console.error("Warning: ".concat(message));
  11. }
  12. }
  13. export function warning(valid, message) {
  14. warn(valid, "[@ant-design/icons-vue] ".concat(message));
  15. }
  16. function camelCase(input) {
  17. return input.replace(/-(.)/g, function (_match, g) {
  18. return g.toUpperCase();
  19. });
  20. } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
  21. export function isIconDefinition(target) {
  22. return typeof target === 'object' && typeof target.name === 'string' && typeof target.theme === 'string' && (typeof target.icon === 'object' || typeof target.icon === 'function');
  23. }
  24. export function normalizeAttrs() {
  25. var attrs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  26. return Object.keys(attrs).reduce(function (acc, key) {
  27. var val = attrs[key];
  28. switch (key) {
  29. case 'class':
  30. acc.className = val;
  31. delete acc["class"];
  32. break;
  33. default:
  34. delete acc[key];
  35. acc[camelCase(key)] = val;
  36. }
  37. return acc;
  38. }, {});
  39. }
  40. export function generate(node, key, rootProps) {
  41. if (!rootProps) {
  42. return h(node.tag, _objectSpread({
  43. key: key
  44. }, node.attrs), (node.children || []).map(function (child, index) {
  45. return generate(child, "".concat(key, "-").concat(node.tag, "-").concat(index));
  46. }));
  47. }
  48. return h(node.tag, _objectSpread({
  49. key: key
  50. }, rootProps, node.attrs), (node.children || []).map(function (child, index) {
  51. return generate(child, "".concat(key, "-").concat(node.tag, "-").concat(index));
  52. }));
  53. }
  54. export function getSecondaryColor(primaryColor) {
  55. // choose the second color
  56. return generateColor(primaryColor)[0];
  57. }
  58. export function normalizeTwoToneColors(twoToneColor) {
  59. if (!twoToneColor) {
  60. return [];
  61. }
  62. return Array.isArray(twoToneColor) ? twoToneColor : [twoToneColor];
  63. } // These props make sure that the SVG behaviours like general text.
  64. // Reference: https://blog.prototypr.io/align-svg-icons-to-text-and-say-goodbye-to-font-icons-d44b3d7b26b4
  65. export var svgBaseProps = {
  66. width: '1em',
  67. height: '1em',
  68. fill: 'currentColor',
  69. 'aria-hidden': 'true',
  70. focusable: 'false'
  71. };
  72. export var iconStyles = "\n.anticon {\n display: inline-block;\n color: inherit;\n font-style: normal;\n line-height: 0;\n text-align: center;\n text-transform: none;\n vertical-align: -0.125em;\n text-rendering: optimizeLegibility;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\n.anticon > * {\n line-height: 1;\n}\n\n.anticon svg {\n display: inline-block;\n}\n\n.anticon::before {\n display: none;\n}\n\n.anticon .anticon-icon {\n display: block;\n}\n\n.anticon[tabindex] {\n cursor: pointer;\n}\n\n.anticon-spin::before,\n.anticon-spin {\n display: inline-block;\n -webkit-animation: loadingCircle 1s infinite linear;\n animation: loadingCircle 1s infinite linear;\n}\n\n@-webkit-keyframes loadingCircle {\n 100% {\n -webkit-transform: rotate(360deg);\n transform: rotate(360deg);\n }\n}\n\n@keyframes loadingCircle {\n 100% {\n -webkit-transform: rotate(360deg);\n transform: rotate(360deg);\n }\n}\n";
  73. function getRoot(ele) {
  74. return ele && ele.getRootNode && ele.getRootNode();
  75. }
  76. /**
  77. * Check if is in shadowRoot
  78. */
  79. function inShadow(ele) {
  80. if (!canUseDom()) {
  81. return false;
  82. }
  83. return getRoot(ele) instanceof ShadowRoot;
  84. }
  85. /**
  86. * Return shadowRoot if possible
  87. */
  88. function getShadowRoot(ele) {
  89. return inShadow(ele) ? getRoot(ele) : null;
  90. }
  91. export var useInsertStyles = function useInsertStyles() {
  92. var _useInjectIconContext = useInjectIconContext(),
  93. prefixCls = _useInjectIconContext.prefixCls,
  94. csp = _useInjectIconContext.csp;
  95. var instance = getCurrentInstance();
  96. var mergedStyleStr = iconStyles;
  97. if (prefixCls) {
  98. mergedStyleStr = mergedStyleStr.replace(/anticon/g, prefixCls.value);
  99. }
  100. nextTick(function () {
  101. if (!canUseDom()) {
  102. return;
  103. }
  104. var ele = instance.vnode.el;
  105. var shadowRoot = getShadowRoot(ele);
  106. updateCSS(mergedStyleStr, '@ant-design-vue-icons', {
  107. prepend: true,
  108. csp: csp.value,
  109. attachTo: shadowRoot
  110. });
  111. });
  112. };