touch.mjs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Composables
  2. import { useToggleScope } from "../../composables/toggleScope.mjs";
  3. import { useVelocity } from "../../composables/touch.mjs"; // Utilities
  4. import { computed, onBeforeUnmount, onMounted, onScopeDispose, shallowRef, watchEffect } from 'vue';
  5. // Types
  6. export function useTouch(_ref) {
  7. let {
  8. el,
  9. isActive,
  10. isTemporary,
  11. width,
  12. touchless,
  13. position
  14. } = _ref;
  15. onMounted(() => {
  16. window.addEventListener('touchstart', onTouchstart, {
  17. passive: true
  18. });
  19. window.addEventListener('touchmove', onTouchmove, {
  20. passive: false
  21. });
  22. window.addEventListener('touchend', onTouchend, {
  23. passive: true
  24. });
  25. });
  26. onBeforeUnmount(() => {
  27. window.removeEventListener('touchstart', onTouchstart);
  28. window.removeEventListener('touchmove', onTouchmove);
  29. window.removeEventListener('touchend', onTouchend);
  30. });
  31. const isHorizontal = computed(() => ['left', 'right'].includes(position.value));
  32. const {
  33. addMovement,
  34. endTouch,
  35. getVelocity
  36. } = useVelocity();
  37. let maybeDragging = false;
  38. const isDragging = shallowRef(false);
  39. const dragProgress = shallowRef(0);
  40. const offset = shallowRef(0);
  41. let start;
  42. function getOffset(pos, active) {
  43. return (position.value === 'left' ? pos : position.value === 'right' ? document.documentElement.clientWidth - pos : position.value === 'top' ? pos : position.value === 'bottom' ? document.documentElement.clientHeight - pos : oops()) - (active ? width.value : 0);
  44. }
  45. function getProgress(pos) {
  46. let limit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  47. const progress = position.value === 'left' ? (pos - offset.value) / width.value : position.value === 'right' ? (document.documentElement.clientWidth - pos - offset.value) / width.value : position.value === 'top' ? (pos - offset.value) / width.value : position.value === 'bottom' ? (document.documentElement.clientHeight - pos - offset.value) / width.value : oops();
  48. return limit ? Math.max(0, Math.min(1, progress)) : progress;
  49. }
  50. function onTouchstart(e) {
  51. if (touchless.value) return;
  52. const touchX = e.changedTouches[0].clientX;
  53. const touchY = e.changedTouches[0].clientY;
  54. const touchZone = 25;
  55. const inTouchZone = position.value === 'left' ? touchX < touchZone : position.value === 'right' ? touchX > document.documentElement.clientWidth - touchZone : position.value === 'top' ? touchY < touchZone : position.value === 'bottom' ? touchY > document.documentElement.clientHeight - touchZone : oops();
  56. const inElement = isActive.value && (position.value === 'left' ? touchX < width.value : position.value === 'right' ? touchX > document.documentElement.clientWidth - width.value : position.value === 'top' ? touchY < width.value : position.value === 'bottom' ? touchY > document.documentElement.clientHeight - width.value : oops());
  57. if (inTouchZone || inElement || isActive.value && isTemporary.value) {
  58. start = [touchX, touchY];
  59. offset.value = getOffset(isHorizontal.value ? touchX : touchY, isActive.value);
  60. dragProgress.value = getProgress(isHorizontal.value ? touchX : touchY);
  61. maybeDragging = offset.value > -20 && offset.value < 80;
  62. endTouch(e);
  63. addMovement(e);
  64. }
  65. }
  66. function onTouchmove(e) {
  67. const touchX = e.changedTouches[0].clientX;
  68. const touchY = e.changedTouches[0].clientY;
  69. if (maybeDragging) {
  70. if (!e.cancelable) {
  71. maybeDragging = false;
  72. return;
  73. }
  74. const dx = Math.abs(touchX - start[0]);
  75. const dy = Math.abs(touchY - start[1]);
  76. const thresholdMet = isHorizontal.value ? dx > dy && dx > 3 : dy > dx && dy > 3;
  77. if (thresholdMet) {
  78. isDragging.value = true;
  79. maybeDragging = false;
  80. } else if ((isHorizontal.value ? dy : dx) > 3) {
  81. maybeDragging = false;
  82. }
  83. }
  84. if (!isDragging.value) return;
  85. e.preventDefault();
  86. addMovement(e);
  87. const progress = getProgress(isHorizontal.value ? touchX : touchY, false);
  88. dragProgress.value = Math.max(0, Math.min(1, progress));
  89. if (progress > 1) {
  90. offset.value = getOffset(isHorizontal.value ? touchX : touchY, true);
  91. } else if (progress < 0) {
  92. offset.value = getOffset(isHorizontal.value ? touchX : touchY, false);
  93. }
  94. }
  95. function onTouchend(e) {
  96. maybeDragging = false;
  97. if (!isDragging.value) return;
  98. addMovement(e);
  99. isDragging.value = false;
  100. const velocity = getVelocity(e.changedTouches[0].identifier);
  101. const vx = Math.abs(velocity.x);
  102. const vy = Math.abs(velocity.y);
  103. const thresholdMet = isHorizontal.value ? vx > vy && vx > 400 : vy > vx && vy > 3;
  104. if (thresholdMet) {
  105. isActive.value = velocity.direction === ({
  106. left: 'right',
  107. right: 'left',
  108. top: 'down',
  109. bottom: 'up'
  110. }[position.value] || oops());
  111. } else {
  112. isActive.value = dragProgress.value > 0.5;
  113. }
  114. }
  115. const dragStyles = computed(() => {
  116. return isDragging.value ? {
  117. transform: position.value === 'left' ? `translateX(calc(-100% + ${dragProgress.value * width.value}px))` : position.value === 'right' ? `translateX(calc(100% - ${dragProgress.value * width.value}px))` : position.value === 'top' ? `translateY(calc(-100% + ${dragProgress.value * width.value}px))` : position.value === 'bottom' ? `translateY(calc(100% - ${dragProgress.value * width.value}px))` : oops(),
  118. transition: 'none'
  119. } : undefined;
  120. });
  121. useToggleScope(isDragging, () => {
  122. const transform = el.value?.style.transform ?? null;
  123. const transition = el.value?.style.transition ?? null;
  124. watchEffect(() => {
  125. el.value?.style.setProperty('transform', dragStyles.value?.transform || 'none');
  126. el.value?.style.setProperty('transition', dragStyles.value?.transition || null);
  127. });
  128. onScopeDispose(() => {
  129. el.value?.style.setProperty('transform', transform);
  130. el.value?.style.setProperty('transition', transition);
  131. });
  132. });
  133. return {
  134. isDragging,
  135. dragProgress,
  136. dragStyles
  137. };
  138. }
  139. function oops() {
  140. throw new Error();
  141. }
  142. //# sourceMappingURL=touch.mjs.map