index.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import "./index.scss";
  2. import { isObject } from "@pureadmin/utils";
  3. import type { Directive, DirectiveBinding } from "vue";
  4. export interface RippleOptions {
  5. /** 自定义`ripple`颜色,支持`tailwindcss` */
  6. class?: string;
  7. /** 是否从中心扩散 */
  8. center?: boolean;
  9. circle?: boolean;
  10. }
  11. export interface RippleDirectiveBinding
  12. extends Omit<DirectiveBinding, "modifiers" | "value"> {
  13. value?: boolean | { class: string };
  14. modifiers: {
  15. center?: boolean;
  16. circle?: boolean;
  17. };
  18. }
  19. function transform(el: HTMLElement, value: string) {
  20. el.style.transform = value;
  21. el.style.webkitTransform = value;
  22. }
  23. const calculate = (
  24. e: PointerEvent,
  25. el: HTMLElement,
  26. value: RippleOptions = {}
  27. ) => {
  28. const offset = el.getBoundingClientRect();
  29. // 获取点击位置距离 el 的垂直和水平距离
  30. const localX = e.clientX - offset.left;
  31. const localY = e.clientY - offset.top;
  32. let radius = 0;
  33. let scale = 0.3;
  34. // 计算点击位置到 el 顶点最远距离,即为圆的最大半径(勾股定理)
  35. if (el._ripple?.circle) {
  36. scale = 0.15;
  37. radius = el.clientWidth / 2;
  38. radius = value.center
  39. ? radius
  40. : radius + Math.sqrt((localX - radius) ** 2 + (localY - radius) ** 2) / 4;
  41. } else {
  42. radius = Math.sqrt(el.clientWidth ** 2 + el.clientHeight ** 2) / 2;
  43. }
  44. // 中心点坐标
  45. const centerX = `${(el.clientWidth - radius * 2) / 2}px`;
  46. const centerY = `${(el.clientHeight - radius * 2) / 2}px`;
  47. // 点击位置坐标
  48. const x = value.center ? centerX : `${localX - radius}px`;
  49. const y = value.center ? centerY : `${localY - radius}px`;
  50. return { radius, scale, x, y, centerX, centerY };
  51. };
  52. const ripples = {
  53. show(e: PointerEvent, el: HTMLElement, value: RippleOptions = {}) {
  54. if (!el?._ripple?.enabled) {
  55. return;
  56. }
  57. // 创建 ripple 元素和 ripple 父元素
  58. const container = document.createElement("span");
  59. const animation = document.createElement("span");
  60. container.appendChild(animation);
  61. container.className = "v-ripple__container";
  62. if (value.class) {
  63. container.className += ` ${value.class}`;
  64. }
  65. const { radius, scale, x, y, centerX, centerY } = calculate(e, el, value);
  66. // ripple 圆大小
  67. const size = `${radius * 2}px`;
  68. animation.className = "v-ripple__animation";
  69. animation.style.width = size;
  70. animation.style.height = size;
  71. el.appendChild(container);
  72. // 获取目标元素样式表
  73. const computed = window.getComputedStyle(el);
  74. // 防止 position 被覆盖导致 ripple 位置有问题
  75. if (computed && computed.position === "static") {
  76. el.style.position = "relative";
  77. el.dataset.previousPosition = "static";
  78. }
  79. animation.classList.add("v-ripple__animation--enter");
  80. animation.classList.add("v-ripple__animation--visible");
  81. transform(
  82. animation,
  83. `translate(${x}, ${y}) scale3d(${scale},${scale},${scale})`
  84. );
  85. animation.dataset.activated = String(performance.now());
  86. setTimeout(() => {
  87. animation.classList.remove("v-ripple__animation--enter");
  88. animation.classList.add("v-ripple__animation--in");
  89. transform(animation, `translate(${centerX}, ${centerY}) scale3d(1,1,1)`);
  90. }, 0);
  91. },
  92. hide(el: HTMLElement | null) {
  93. if (!el?._ripple?.enabled) return;
  94. const ripples = el.getElementsByClassName("v-ripple__animation");
  95. if (ripples.length === 0) return;
  96. const animation = ripples[ripples.length - 1] as HTMLElement;
  97. if (animation.dataset.isHiding) return;
  98. else animation.dataset.isHiding = "true";
  99. const diff = performance.now() - Number(animation.dataset.activated);
  100. const delay = Math.max(250 - diff, 0);
  101. setTimeout(() => {
  102. animation.classList.remove("v-ripple__animation--in");
  103. animation.classList.add("v-ripple__animation--out");
  104. setTimeout(() => {
  105. const ripples = el.getElementsByClassName("v-ripple__animation");
  106. if (ripples.length === 1 && el.dataset.previousPosition) {
  107. el.style.position = el.dataset.previousPosition;
  108. delete el.dataset.previousPosition;
  109. }
  110. if (animation.parentNode?.parentNode === el)
  111. el.removeChild(animation.parentNode);
  112. }, 300);
  113. }, delay);
  114. }
  115. };
  116. function isRippleEnabled(value: any): value is true {
  117. return typeof value === "undefined" || !!value;
  118. }
  119. function rippleShow(e: PointerEvent) {
  120. const value: RippleOptions = {};
  121. const element = e.currentTarget as HTMLElement | undefined;
  122. if (!element?._ripple || element._ripple.touched) return;
  123. value.center = element._ripple.centered;
  124. if (element._ripple.class) {
  125. value.class = element._ripple.class;
  126. }
  127. ripples.show(e, element, value);
  128. }
  129. function rippleHide(e: Event) {
  130. const element = e.currentTarget as HTMLElement | null;
  131. if (!element?._ripple) return;
  132. window.setTimeout(() => {
  133. if (element._ripple) {
  134. element._ripple.touched = false;
  135. }
  136. });
  137. ripples.hide(element);
  138. }
  139. function updateRipple(
  140. el: HTMLElement,
  141. binding: RippleDirectiveBinding,
  142. wasEnabled: boolean
  143. ) {
  144. const { value, modifiers } = binding;
  145. const enabled = isRippleEnabled(value);
  146. if (!enabled) {
  147. ripples.hide(el);
  148. }
  149. el._ripple = el._ripple ?? {};
  150. el._ripple.enabled = enabled;
  151. el._ripple.centered = modifiers.center;
  152. el._ripple.circle = modifiers.circle;
  153. if (isObject(value) && value.class) {
  154. el._ripple.class = value.class;
  155. }
  156. if (enabled && !wasEnabled) {
  157. el.addEventListener("pointerdown", rippleShow);
  158. el.addEventListener("pointerup", rippleHide);
  159. } else if (!enabled && wasEnabled) {
  160. removeListeners(el);
  161. }
  162. }
  163. function removeListeners(el: HTMLElement) {
  164. el.removeEventListener("pointerdown", rippleShow);
  165. el.removeEventListener("pointerup", rippleHide);
  166. }
  167. function mounted(el: HTMLElement, binding: RippleDirectiveBinding) {
  168. updateRipple(el, binding, false);
  169. }
  170. function unmounted(el: HTMLElement) {
  171. delete el._ripple;
  172. removeListeners(el);
  173. }
  174. function updated(el: HTMLElement, binding: RippleDirectiveBinding) {
  175. if (binding.value === binding.oldValue) {
  176. return;
  177. }
  178. const wasEnabled = isRippleEnabled(binding.oldValue);
  179. updateRipple(el, binding, wasEnabled);
  180. }
  181. export const Ripple: Directive = {
  182. mounted,
  183. unmounted,
  184. updated
  185. };