@carbon/utilities
Version:
Utilities and helpers to drive consistency across software products using the Carbon Design System
2 lines (1 loc) • 1.05 kB
JavaScript
const b=(t,u,d,w)=>{let n=null,l=null,v=0;const M=400;let o=!1,s=null,i=null;const r=e=>{n=e.touches[0].clientX},a=e=>{l=e.touches[0].clientX},m=()=>{if(n!==null&&l!==null){const e=n-l;Math.abs(e)>50&&(e>0?u():d())}n=null,l=null},c=e=>{o=!0,s=e.clientX},h=e=>{o&&(i=e.clientX)},E=()=>{if(o&&s!==null&&i!==null){const e=s-i;Math.abs(e)>50&&(e>0?u():d())}o=!1,s=null,i=null},L=e=>{const f=Date.now();if(Math.abs(e.deltaX)>Math.abs(e.deltaY)&&Math.abs(e.deltaX)>20){if(e.preventDefault(),f-v<M)return;e.deltaX>0?u():d(),v=f}};w&&(t.removeEventListener("touchstart",r),t.removeEventListener("touchmove",a),t.removeEventListener("touchend",m),t.removeEventListener("mousedown",c),t.removeEventListener("mousemove",h),t.removeEventListener("mouseup",E),t.removeEventListener("wheel",L)),t.addEventListener("touchstart",r),t.addEventListener("touchmove",a),t.addEventListener("touchend",m),t.addEventListener("mousedown",c),t.addEventListener("mousemove",h),t.addEventListener("mouseup",E),t.addEventListener("wheel",L)};export{b as registerSwipeEvents};