index.mjs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Styles
  2. import "./VRipple.css";
  3. // Utilities
  4. import { isObject, keyCodes } from "../../util/index.mjs"; // Types
  5. const stopSymbol = Symbol('rippleStop');
  6. const DELAY_RIPPLE = 80;
  7. function transform(el, value) {
  8. el.style.transform = value;
  9. el.style.webkitTransform = value;
  10. }
  11. function isTouchEvent(e) {
  12. return e.constructor.name === 'TouchEvent';
  13. }
  14. function isKeyboardEvent(e) {
  15. return e.constructor.name === 'KeyboardEvent';
  16. }
  17. const calculate = function (e, el) {
  18. let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  19. let localX = 0;
  20. let localY = 0;
  21. if (!isKeyboardEvent(e)) {
  22. const offset = el.getBoundingClientRect();
  23. const target = isTouchEvent(e) ? e.touches[e.touches.length - 1] : e;
  24. localX = target.clientX - offset.left;
  25. localY = target.clientY - offset.top;
  26. }
  27. let radius = 0;
  28. let scale = 0.3;
  29. if (el._ripple?.circle) {
  30. scale = 0.15;
  31. radius = el.clientWidth / 2;
  32. radius = value.center ? radius : radius + Math.sqrt((localX - radius) ** 2 + (localY - radius) ** 2) / 4;
  33. } else {
  34. radius = Math.sqrt(el.clientWidth ** 2 + el.clientHeight ** 2) / 2;
  35. }
  36. const centerX = `${(el.clientWidth - radius * 2) / 2}px`;
  37. const centerY = `${(el.clientHeight - radius * 2) / 2}px`;
  38. const x = value.center ? centerX : `${localX - radius}px`;
  39. const y = value.center ? centerY : `${localY - radius}px`;
  40. return {
  41. radius,
  42. scale,
  43. x,
  44. y,
  45. centerX,
  46. centerY
  47. };
  48. };
  49. const ripples = {
  50. /* eslint-disable max-statements */
  51. show(e, el) {
  52. let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  53. if (!el?._ripple?.enabled) {
  54. return;
  55. }
  56. const container = document.createElement('span');
  57. const animation = document.createElement('span');
  58. container.appendChild(animation);
  59. container.className = 'v-ripple__container';
  60. if (value.class) {
  61. container.className += ` ${value.class}`;
  62. }
  63. const {
  64. radius,
  65. scale,
  66. x,
  67. y,
  68. centerX,
  69. centerY
  70. } = calculate(e, el, value);
  71. const size = `${radius * 2}px`;
  72. animation.className = 'v-ripple__animation';
  73. animation.style.width = size;
  74. animation.style.height = size;
  75. el.appendChild(container);
  76. const computed = window.getComputedStyle(el);
  77. if (computed && computed.position === 'static') {
  78. el.style.position = 'relative';
  79. el.dataset.previousPosition = 'static';
  80. }
  81. animation.classList.add('v-ripple__animation--enter');
  82. animation.classList.add('v-ripple__animation--visible');
  83. transform(animation, `translate(${x}, ${y}) scale3d(${scale},${scale},${scale})`);
  84. animation.dataset.activated = String(performance.now());
  85. setTimeout(() => {
  86. animation.classList.remove('v-ripple__animation--enter');
  87. animation.classList.add('v-ripple__animation--in');
  88. transform(animation, `translate(${centerX}, ${centerY}) scale3d(1,1,1)`);
  89. }, 0);
  90. },
  91. hide(el) {
  92. if (!el?._ripple?.enabled) return;
  93. const ripples = el.getElementsByClassName('v-ripple__animation');
  94. if (ripples.length === 0) return;
  95. const animation = ripples[ripples.length - 1];
  96. if (animation.dataset.isHiding) return;else animation.dataset.isHiding = 'true';
  97. const diff = performance.now() - Number(animation.dataset.activated);
  98. const delay = Math.max(250 - diff, 0);
  99. setTimeout(() => {
  100. animation.classList.remove('v-ripple__animation--in');
  101. animation.classList.add('v-ripple__animation--out');
  102. setTimeout(() => {
  103. const ripples = el.getElementsByClassName('v-ripple__animation');
  104. if (ripples.length === 1 && el.dataset.previousPosition) {
  105. el.style.position = el.dataset.previousPosition;
  106. delete el.dataset.previousPosition;
  107. }
  108. if (animation.parentNode?.parentNode === el) el.removeChild(animation.parentNode);
  109. }, 300);
  110. }, delay);
  111. }
  112. };
  113. function isRippleEnabled(value) {
  114. return typeof value === 'undefined' || !!value;
  115. }
  116. function rippleShow(e) {
  117. const value = {};
  118. const element = e.currentTarget;
  119. if (!element?._ripple || element._ripple.touched || e[stopSymbol]) return;
  120. // Don't allow the event to trigger ripples on any other elements
  121. e[stopSymbol] = true;
  122. if (isTouchEvent(e)) {
  123. element._ripple.touched = true;
  124. element._ripple.isTouch = true;
  125. } else {
  126. // It's possible for touch events to fire
  127. // as mouse events on Android/iOS, this
  128. // will skip the event call if it has
  129. // already been registered as touch
  130. if (element._ripple.isTouch) return;
  131. }
  132. value.center = element._ripple.centered || isKeyboardEvent(e);
  133. if (element._ripple.class) {
  134. value.class = element._ripple.class;
  135. }
  136. if (isTouchEvent(e)) {
  137. // already queued that shows or hides the ripple
  138. if (element._ripple.showTimerCommit) return;
  139. element._ripple.showTimerCommit = () => {
  140. ripples.show(e, element, value);
  141. };
  142. element._ripple.showTimer = window.setTimeout(() => {
  143. if (element?._ripple?.showTimerCommit) {
  144. element._ripple.showTimerCommit();
  145. element._ripple.showTimerCommit = null;
  146. }
  147. }, DELAY_RIPPLE);
  148. } else {
  149. ripples.show(e, element, value);
  150. }
  151. }
  152. function rippleStop(e) {
  153. e[stopSymbol] = true;
  154. }
  155. function rippleHide(e) {
  156. const element = e.currentTarget;
  157. if (!element?._ripple) return;
  158. window.clearTimeout(element._ripple.showTimer);
  159. // The touch interaction occurs before the show timer is triggered.
  160. // We still want to show ripple effect.
  161. if (e.type === 'touchend' && element._ripple.showTimerCommit) {
  162. element._ripple.showTimerCommit();
  163. element._ripple.showTimerCommit = null;
  164. // re-queue ripple hiding
  165. element._ripple.showTimer = window.setTimeout(() => {
  166. rippleHide(e);
  167. });
  168. return;
  169. }
  170. window.setTimeout(() => {
  171. if (element._ripple) {
  172. element._ripple.touched = false;
  173. }
  174. });
  175. ripples.hide(element);
  176. }
  177. function rippleCancelShow(e) {
  178. const element = e.currentTarget;
  179. if (!element?._ripple) return;
  180. if (element._ripple.showTimerCommit) {
  181. element._ripple.showTimerCommit = null;
  182. }
  183. window.clearTimeout(element._ripple.showTimer);
  184. }
  185. let keyboardRipple = false;
  186. function keyboardRippleShow(e) {
  187. if (!keyboardRipple && (e.keyCode === keyCodes.enter || e.keyCode === keyCodes.space)) {
  188. keyboardRipple = true;
  189. rippleShow(e);
  190. }
  191. }
  192. function keyboardRippleHide(e) {
  193. keyboardRipple = false;
  194. rippleHide(e);
  195. }
  196. function focusRippleHide(e) {
  197. if (keyboardRipple) {
  198. keyboardRipple = false;
  199. rippleHide(e);
  200. }
  201. }
  202. function updateRipple(el, binding, wasEnabled) {
  203. const {
  204. value,
  205. modifiers
  206. } = binding;
  207. const enabled = isRippleEnabled(value);
  208. if (!enabled) {
  209. ripples.hide(el);
  210. }
  211. el._ripple = el._ripple ?? {};
  212. el._ripple.enabled = enabled;
  213. el._ripple.centered = modifiers.center;
  214. el._ripple.circle = modifiers.circle;
  215. if (isObject(value) && value.class) {
  216. el._ripple.class = value.class;
  217. }
  218. if (enabled && !wasEnabled) {
  219. if (modifiers.stop) {
  220. el.addEventListener('touchstart', rippleStop, {
  221. passive: true
  222. });
  223. el.addEventListener('mousedown', rippleStop);
  224. return;
  225. }
  226. el.addEventListener('touchstart', rippleShow, {
  227. passive: true
  228. });
  229. el.addEventListener('touchend', rippleHide, {
  230. passive: true
  231. });
  232. el.addEventListener('touchmove', rippleCancelShow, {
  233. passive: true
  234. });
  235. el.addEventListener('touchcancel', rippleHide);
  236. el.addEventListener('mousedown', rippleShow);
  237. el.addEventListener('mouseup', rippleHide);
  238. el.addEventListener('mouseleave', rippleHide);
  239. el.addEventListener('keydown', keyboardRippleShow);
  240. el.addEventListener('keyup', keyboardRippleHide);
  241. el.addEventListener('blur', focusRippleHide);
  242. // Anchor tags can be dragged, causes other hides to fail - #1537
  243. el.addEventListener('dragstart', rippleHide, {
  244. passive: true
  245. });
  246. } else if (!enabled && wasEnabled) {
  247. removeListeners(el);
  248. }
  249. }
  250. function removeListeners(el) {
  251. el.removeEventListener('mousedown', rippleShow);
  252. el.removeEventListener('touchstart', rippleShow);
  253. el.removeEventListener('touchend', rippleHide);
  254. el.removeEventListener('touchmove', rippleCancelShow);
  255. el.removeEventListener('touchcancel', rippleHide);
  256. el.removeEventListener('mouseup', rippleHide);
  257. el.removeEventListener('mouseleave', rippleHide);
  258. el.removeEventListener('keydown', keyboardRippleShow);
  259. el.removeEventListener('keyup', keyboardRippleHide);
  260. el.removeEventListener('dragstart', rippleHide);
  261. el.removeEventListener('blur', focusRippleHide);
  262. }
  263. function mounted(el, binding) {
  264. updateRipple(el, binding, false);
  265. }
  266. function unmounted(el) {
  267. delete el._ripple;
  268. removeListeners(el);
  269. }
  270. function updated(el, binding) {
  271. if (binding.value === binding.oldValue) {
  272. return;
  273. }
  274. const wasEnabled = isRippleEnabled(binding.oldValue);
  275. updateRipple(el, binding, wasEnabled);
  276. }
  277. export const Ripple = {
  278. mounted,
  279. unmounted,
  280. updated
  281. };
  282. export default Ripple;
  283. //# sourceMappingURL=index.mjs.map