wave.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _vue = require("vue");
  8. var _Event = _interopRequireDefault(require("./css-animation/Event"));
  9. var _raf = _interopRequireDefault(require("./raf"));
  10. var _propsUtil = require("./props-util");
  11. var _useConfigInject2 = _interopRequireDefault(require("./hooks/useConfigInject"));
  12. var styleForPesudo;
  13. // Where el is the DOM element you'd like to test for visibility
  14. function isHidden(element) {
  15. if (process.env.NODE_ENV === 'test') {
  16. return false;
  17. }
  18. return !element || element.offsetParent === null;
  19. }
  20. function isNotGrey(color) {
  21. // eslint-disable-next-line no-useless-escape
  22. var match = (color || '').match(/rgba?\((\d*), (\d*), (\d*)(, [\.\d]*)?\)/);
  23. if (match && match[1] && match[2] && match[3]) {
  24. return !(match[1] === match[2] && match[2] === match[3]);
  25. }
  26. return true;
  27. }
  28. var _default = (0, _vue.defineComponent)({
  29. compatConfig: {
  30. MODE: 3
  31. },
  32. name: 'Wave',
  33. props: {
  34. insertExtraNode: Boolean,
  35. disabled: Boolean
  36. },
  37. setup: function setup(props, _ref) {
  38. var slots = _ref.slots,
  39. expose = _ref.expose;
  40. var instance = (0, _vue.getCurrentInstance)();
  41. var _useConfigInject = (0, _useConfigInject2.default)('', props),
  42. csp = _useConfigInject.csp,
  43. prefixCls = _useConfigInject.prefixCls;
  44. expose({
  45. csp: csp
  46. });
  47. var eventIns = null;
  48. var clickWaveTimeoutId = null;
  49. var animationStartId = null;
  50. var animationStart = false;
  51. var extraNode = null;
  52. var isUnmounted = false;
  53. var onTransitionStart = function onTransitionStart(e) {
  54. if (isUnmounted) return;
  55. var node = (0, _propsUtil.findDOMNode)(instance);
  56. if (!e || e.target !== node) {
  57. return;
  58. }
  59. if (!animationStart) {
  60. resetEffect(node);
  61. }
  62. };
  63. var onTransitionEnd = function onTransitionEnd(e) {
  64. if (!e || e.animationName !== 'fadeEffect') {
  65. return;
  66. }
  67. resetEffect(e.target);
  68. };
  69. var getAttributeName = function getAttributeName() {
  70. var insertExtraNode = props.insertExtraNode;
  71. return insertExtraNode ? "".concat(prefixCls.value, "-click-animating") : "".concat(prefixCls.value, "-click-animating-without-extra-node");
  72. };
  73. var onClick = function onClick(node, waveColor) {
  74. var insertExtraNode = props.insertExtraNode,
  75. disabled = props.disabled;
  76. if (disabled || !node || isHidden(node) || node.className.indexOf('-leave') >= 0) {
  77. return;
  78. }
  79. extraNode = document.createElement('div');
  80. extraNode.className = "".concat(prefixCls.value, "-click-animating-node");
  81. var attributeName = getAttributeName();
  82. node.removeAttribute(attributeName);
  83. node.setAttribute(attributeName, 'true');
  84. // Not white or transparent or grey
  85. styleForPesudo = styleForPesudo || document.createElement('style');
  86. if (waveColor && waveColor !== '#ffffff' && waveColor !== 'rgb(255, 255, 255)' && isNotGrey(waveColor) && !/rgba\(\d*, \d*, \d*, 0\)/.test(waveColor) &&
  87. // any transparent rgba color
  88. waveColor !== 'transparent') {
  89. var _csp$value;
  90. // Add nonce if CSP exist
  91. if ((_csp$value = csp.value) !== null && _csp$value !== void 0 && _csp$value.nonce) {
  92. styleForPesudo.nonce = csp.value.nonce;
  93. }
  94. extraNode.style.borderColor = waveColor;
  95. styleForPesudo.innerHTML = "\n [".concat(prefixCls.value, "-click-animating-without-extra-node='true']::after, .").concat(prefixCls.value, "-click-animating-node {\n --antd-wave-shadow-color: ").concat(waveColor, ";\n }");
  96. if (!document.body.contains(styleForPesudo)) {
  97. document.body.appendChild(styleForPesudo);
  98. }
  99. }
  100. if (insertExtraNode) {
  101. node.appendChild(extraNode);
  102. }
  103. _Event.default.addStartEventListener(node, onTransitionStart);
  104. _Event.default.addEndEventListener(node, onTransitionEnd);
  105. };
  106. var resetEffect = function resetEffect(node) {
  107. if (!node || node === extraNode || !(node instanceof Element)) {
  108. return;
  109. }
  110. var insertExtraNode = props.insertExtraNode;
  111. var attributeName = getAttributeName();
  112. node.setAttribute(attributeName, 'false'); // edge has bug on `removeAttribute` #14466
  113. if (styleForPesudo) {
  114. styleForPesudo.innerHTML = '';
  115. }
  116. if (insertExtraNode && extraNode && node.contains(extraNode)) {
  117. node.removeChild(extraNode);
  118. }
  119. _Event.default.removeStartEventListener(node, onTransitionStart);
  120. _Event.default.removeEndEventListener(node, onTransitionEnd);
  121. };
  122. var bindAnimationEvent = function bindAnimationEvent(node) {
  123. if (!node || !node.getAttribute || node.getAttribute('disabled') || node.className.indexOf('disabled') >= 0) {
  124. return;
  125. }
  126. var newClick = function newClick(e) {
  127. // Fix radio button click twice
  128. if (e.target.tagName === 'INPUT' || isHidden(e.target)) {
  129. return;
  130. }
  131. resetEffect(node);
  132. // Get wave color from target
  133. var waveColor = getComputedStyle(node).getPropertyValue('border-top-color') ||
  134. // Firefox Compatible
  135. getComputedStyle(node).getPropertyValue('border-color') || getComputedStyle(node).getPropertyValue('background-color');
  136. clickWaveTimeoutId = setTimeout(function () {
  137. return onClick(node, waveColor);
  138. }, 0);
  139. _raf.default.cancel(animationStartId);
  140. animationStart = true;
  141. // Render to trigger transition event cost 3 frames. Let's delay 10 frames to reset this.
  142. animationStartId = (0, _raf.default)(function () {
  143. animationStart = false;
  144. }, 10);
  145. };
  146. node.addEventListener('click', newClick, true);
  147. return {
  148. cancel: function cancel() {
  149. node.removeEventListener('click', newClick, true);
  150. }
  151. };
  152. };
  153. (0, _vue.onMounted)(function () {
  154. (0, _vue.nextTick)(function () {
  155. var node = (0, _propsUtil.findDOMNode)(instance);
  156. if (node.nodeType !== 1) {
  157. return;
  158. }
  159. eventIns = bindAnimationEvent(node);
  160. });
  161. });
  162. (0, _vue.onBeforeUnmount)(function () {
  163. if (eventIns) {
  164. eventIns.cancel();
  165. }
  166. clearTimeout(clickWaveTimeoutId);
  167. isUnmounted = true;
  168. });
  169. return function () {
  170. var _slots$default;
  171. return (_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots)[0];
  172. };
  173. }
  174. });
  175. exports.default = _default;