wave.js 6.2 KB

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