preact-gestures
Version:
Preact compatible HOCs for gesture detection
2 lines • 3.69 kB
JavaScript
import{h}from"preact";function _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_nonIterableRest()}function _toArray(e){return _arrayWithHoles(e)||_iterableToArray(e)||_nonIterableRest()}function _arrayWithHoles(e){if(Array.isArray(e))return e}function _iterableToArray(e){if(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e))return Array.from(e)}function _iterableToArrayLimit(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var a,c=e[Symbol.iterator]();!(n=(a=c.next()).done)&&(r.push(a.value),!t||r.length!==t);n=!0);}catch(e){o=!0,i=e}finally{try{n||null==c.return||c.return()}finally{if(o)throw i}}return r}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}var gestureTriggers=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.touchMenuHoldTime,r=void 0===t?600:t,n=e.touchDragDelay,o=void 0===n?500:n;return{onClick:{onClick:(e,t)=>e(t)},onDblclick:{onDblclick:(e,t)=>e(t)},onMenu:{onMousedown(e,t){2===t.button&&e(t)},onTouchstart(e,t){const n=t.target;let o=!1;const i=setTimeout(()=>o=!0,r),a=()=>{n.removeEventListener("touchend",c),n.removeEventListener("touchcancel",a)},c=()=>{o?e(t):clearTimeout(i),a()};n.addEventListener("touchend",c),n.addEventListener("touchcancel",a)}},onDrag:{onMousedown(e,t){if(0!==t.button)return;const r=t.target;let n=!0;const o=r=>{n&&(e(t,"start"),n=!1),e(r,"move")},i=t=>{e(t,"end"),r.removeEventListener("mousemove",o),r.removeEventListener("mouseup",i)};r.addEventListener("mousemove",o),r.addEventListener("mouseup",i)},onTouchstart(e,t){const r=t.target,n=t.changedTouches[0];let i=!1,a=!0;const c=r=>{a&&(e(t,"start"),a=!1);for(let t=0;t<r.changedTouches.length;t++){r.changedTouches[t].identifier===n.identifier&&e(r,"move")}},s=setTimeout(()=>{i=!0,r.addEventListener("touchmove",c)},o),u=t=>{i?(e(t,"end"),r.removeEventListener("touchmove",c)):clearTimeout(s),i=!1,r.removeEventListener("touchend",u),r.removeEventListener("touchcancel",u)};r.addEventListener("touchend",u),r.addEventListener("touchcancel",u)}}}};function series(e){return function(){for(var t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];return e.forEach(e=>e(...r))}}function splitCamelCase(e){return e.split(/(?=[A-Z])/g)}function safeAppend(e,t){return Array.isArray(e)?e.concat([t]):[e,t]}function mergeWith(e,t,r){let n=Object.assign({},t);return Object.entries(r).forEach(e=>{let t=_slicedToArray(e,2),r=t[0],o=t[1];n[r]=r in n?safeAppend(n[r],o):[o]}),Object.entries(n).forEach(t=>{let r=_slicedToArray(t,2),o=r[0],i=r[1];1===i.length?n[o]=i[0]:n[o]=e(i,o)}),n}function mapObj(e,t){let r={};return Object.entries(e).forEach(e=>{let n=_slicedToArray(e,2),o=n[0],i=n[1];return r[o]=t(o,i)}),r}function setPath(e,t,r){const n=t.slice(),o=n.pop();let i=e;n.forEach(e=>{"object"!=typeof i[e]&&(i[e]={}),i=i[e]}),i[o]=r}const splitHandlerName=e=>{let t=_toArray(splitCamelCase(e)),r=t[0],n=t[1],o=t.slice(2);return[r+n,(o=0===o.length?["default"]:o).join("")]};function withGestures(e,t){const r=[],n=gestureTriggers(t);return function(t){let o={},i={},a={};return Object.entries(t).forEach(e=>{let t=_slicedToArray(e,2),r=t[0],i=t[1],c=_slicedToArray(splitHandlerName(r),2),s=c[0],u=c[1];if(u=u.toLowerCase(),s in n){setPath(a,[s,u],i)}else o[r]=i}),Object.entries(a).forEach(e=>{let t=_slicedToArray(e,2),o=t[0],a=t[1],c=mapObj(n[o],(e,t)=>((e,t)=>{return e.bind(null,function(e){let n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"default";r.includes(e)||(r.push(e),n in t&&t[n](e))})})(t,a));i=mergeWith(series,i,c)}),h("span",i,h(e,o))}}export{withGestures};
//# sourceMappingURL=index.min.js.map