react-touch-drag-slider
Version:
Touch and drag slider carousel component for React
33 lines (29 loc) • 17.1 kB
JavaScript
(function(u,W){typeof exports=="object"&&typeof module<"u"?module.exports=W(require("react")):typeof define=="function"&&define.amd?define(["react"],W):(u=typeof globalThis<"u"?globalThis:u||self,u["react-touch-drag-slider"]=W(u.React))})(this,function(u){"use strict";var W={exports:{}},V={};/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var de;function We(){return de||(de=1,process.env.NODE_ENV!=="production"&&function(){var b=u,h=Symbol.for("react.element"),_=Symbol.for("react.portal"),T=Symbol.for("react.fragment"),P=Symbol.for("react.strict_mode"),F=Symbol.for("react.profiler"),k=Symbol.for("react.provider"),y=Symbol.for("react.context"),d=Symbol.for("react.forward_ref"),x=Symbol.for("react.suspense"),g=Symbol.for("react.suspense_list"),E=Symbol.for("react.memo"),C=Symbol.for("react.lazy"),c=Symbol.for("react.offscreen"),v=Symbol.iterator,J="@@iterator";function D(e){if(e===null||typeof e!="object")return null;var r=v&&e[v]||e[J];return typeof r=="function"?r:null}var w=b.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function p(e){{for(var r=arguments.length,t=new Array(r>1?r-1:0),n=1;n<r;n++)t[n-1]=arguments[n];te("error",e,t)}}function te(e,r,t){{var n=w.ReactDebugCurrentFrame,o=n.getStackAddendum();o!==""&&(r+="%s",t=t.concat([o]));var s=t.map(function(i){return String(i)});s.unshift("Warning: "+r),Function.prototype.apply.call(console[e],console,s)}}var ne=!1,H=!1,X=!1,K=!1,m=!1,j;j=Symbol.for("react.module.reference");function S(e){return!!(typeof e=="string"||typeof e=="function"||e===T||e===F||m||e===P||e===x||e===g||K||e===c||ne||H||X||typeof e=="object"&&e!==null&&(e.$$typeof===C||e.$$typeof===E||e.$$typeof===k||e.$$typeof===y||e.$$typeof===d||e.$$typeof===j||e.getModuleId!==void 0))}function Y(e,r,t){var n=e.displayName;if(n)return n;var o=r.displayName||r.name||"";return o!==""?t+"("+o+")":t}function pe(e){return e.displayName||"Context"}function I(e){if(e==null)return null;if(typeof e.tag=="number"&&p("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),typeof e=="function")return e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case T:return"Fragment";case _:return"Portal";case F:return"Profiler";case P:return"StrictMode";case x:return"Suspense";case g:return"SuspenseList"}if(typeof e=="object")switch(e.$$typeof){case y:var r=e;return pe(r)+".Consumer";case k:var t=e;return pe(t._context)+".Provider";case d:return Y(e,e.render,"ForwardRef");case E:var n=e.displayName||null;return n!==null?n:I(e.type)||"Memo";case C:{var o=e,s=o._payload,i=o._init;try{return I(i(s))}catch{return null}}}return null}var N=Object.assign,z=0,he,ge,me,ye,Ee,Re,be;function _e(){}_e.__reactDisabledLog=!0;function Ve(){{if(z===0){he=console.log,ge=console.info,me=console.warn,ye=console.error,Ee=console.group,Re=console.groupCollapsed,be=console.groupEnd;var e={configurable:!0,enumerable:!0,value:_e,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}z++}}function Be(){{if(z--,z===0){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:N({},e,{value:he}),info:N({},e,{value:ge}),warn:N({},e,{value:me}),error:N({},e,{value:ye}),group:N({},e,{value:Ee}),groupCollapsed:N({},e,{value:Re}),groupEnd:N({},e,{value:be})})}z<0&&p("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}}var ae=w.ReactCurrentDispatcher,ie;function Z(e,r,t){{if(ie===void 0)try{throw Error()}catch(o){var n=o.stack.trim().match(/\n( *(at )?)/);ie=n&&n[1]||""}return`
`+ie+e}}var oe=!1,Q;{var ze=typeof WeakMap=="function"?WeakMap:Map;Q=new ze}function we(e,r){if(!e||oe)return"";{var t=Q.get(e);if(t!==void 0)return t}var n;oe=!0;var o=Error.prepareStackTrace;Error.prepareStackTrace=void 0;var s;s=ae.current,ae.current=null,Ve();try{if(r){var i=function(){throw Error()};if(Object.defineProperty(i.prototype,"props",{set:function(){throw Error()}}),typeof Reflect=="object"&&Reflect.construct){try{Reflect.construct(i,[])}catch(L){n=L}Reflect.construct(e,[],i)}else{try{i.call()}catch(L){n=L}e.call(i.prototype)}}else{try{throw Error()}catch(L){n=L}e()}}catch(L){if(L&&n&&typeof L.stack=="string"){for(var a=L.stack.split(`
`),R=n.stack.split(`
`),f=a.length-1,l=R.length-1;f>=1&&l>=0&&a[f]!==R[l];)l--;for(;f>=1&&l>=0;f--,l--)if(a[f]!==R[l]){if(f!==1||l!==1)do if(f--,l--,l<0||a[f]!==R[l]){var O=`
`+a[f].replace(" at new "," at ");return e.displayName&&O.includes("<anonymous>")&&(O=O.replace("<anonymous>",e.displayName)),typeof e=="function"&&Q.set(e,O),O}while(f>=1&&l>=0);break}}}finally{oe=!1,ae.current=s,Be(),Error.prepareStackTrace=o}var U=e?e.displayName||e.name:"",Le=U?Z(U):"";return typeof e=="function"&&Q.set(e,Le),Le}function Ge(e,r,t){return we(e,!1)}function Je(e){var r=e.prototype;return!!(r&&r.isReactComponent)}function $(e,r,t){if(e==null)return"";if(typeof e=="function")return we(e,Je(e));if(typeof e=="string")return Z(e);switch(e){case x:return Z("Suspense");case g:return Z("SuspenseList")}if(typeof e=="object")switch(e.$$typeof){case d:return Ge(e.render);case E:return $(e.type,r,t);case C:{var n=e,o=n._payload,s=n._init;try{return $(s(o),r,t)}catch{}}}return""}var q=Object.prototype.hasOwnProperty,Te={},Pe=w.ReactDebugCurrentFrame;function ee(e){if(e){var r=e._owner,t=$(e.type,e._source,r?r.type:null);Pe.setExtraStackFrame(t)}else Pe.setExtraStackFrame(null)}function He(e,r,t,n,o){{var s=Function.call.bind(q);for(var i in e)if(s(e,i)){var a=void 0;try{if(typeof e[i]!="function"){var R=Error((n||"React class")+": "+t+" type `"+i+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[i]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw R.name="Invariant Violation",R}a=e[i](r,i,n,t,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(f){a=f}a&&!(a instanceof Error)&&(ee(o),p("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",n||"React class",t,i,typeof a),ee(null)),a instanceof Error&&!(a.message in Te)&&(Te[a.message]=!0,ee(o),p("Failed %s type: %s",t,a.message),ee(null))}}}var Xe=Array.isArray;function se(e){return Xe(e)}function Ke(e){{var r=typeof Symbol=="function"&&Symbol.toStringTag,t=r&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t}}function Ze(e){try{return xe(e),!1}catch{return!0}}function xe(e){return""+e}function Oe(e){if(Ze(e))return p("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",Ke(e)),xe(e)}var G=w.ReactCurrentOwner,Qe={key:!0,ref:!0,__self:!0,__source:!0},Ce,je,ue;ue={};function $e(e){if(q.call(e,"ref")){var r=Object.getOwnPropertyDescriptor(e,"ref").get;if(r&&r.isReactWarning)return!1}return e.ref!==void 0}function qe(e){if(q.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function er(e,r){if(typeof e.ref=="string"&&G.current&&r&&G.current.stateNode!==r){var t=I(G.current.type);ue[t]||(p('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref',I(G.current.type),e.ref),ue[t]=!0)}}function rr(e,r){{var t=function(){Ce||(Ce=!0,p("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}}function tr(e,r){{var t=function(){je||(je=!0,p("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",r))};t.isReactWarning=!0,Object.defineProperty(e,"ref",{get:t,configurable:!0})}}var nr=function(e,r,t,n,o,s,i){var a={$$typeof:h,type:e,key:r,ref:t,props:i,_owner:s};return a._store={},Object.defineProperty(a._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(a,"_self",{configurable:!1,enumerable:!1,writable:!1,value:n}),Object.defineProperty(a,"_source",{configurable:!1,enumerable:!1,writable:!1,value:o}),Object.freeze&&(Object.freeze(a.props),Object.freeze(a)),a};function ar(e,r,t,n,o){{var s,i={},a=null,R=null;t!==void 0&&(Oe(t),a=""+t),qe(r)&&(Oe(r.key),a=""+r.key),$e(r)&&(R=r.ref,er(r,o));for(s in r)q.call(r,s)&&!Qe.hasOwnProperty(s)&&(i[s]=r[s]);if(e&&e.defaultProps){var f=e.defaultProps;for(s in f)i[s]===void 0&&(i[s]=f[s])}if(a||R){var l=typeof e=="function"?e.displayName||e.name||"Unknown":e;a&&rr(i,l),R&&tr(i,l)}return nr(e,a,R,o,n,G.current,i)}}var ce=w.ReactCurrentOwner,Se=w.ReactDebugCurrentFrame;function M(e){if(e){var r=e._owner,t=$(e.type,e._source,r?r.type:null);Se.setExtraStackFrame(t)}else Se.setExtraStackFrame(null)}var fe;fe=!1;function le(e){return typeof e=="object"&&e!==null&&e.$$typeof===h}function ke(){{if(ce.current){var e=I(ce.current.type);if(e)return`
Check the render method of \``+e+"`."}return""}}function ir(e){{if(e!==void 0){var r=e.fileName.replace(/^.*[\\\/]/,""),t=e.lineNumber;return`
Check your code at `+r+":"+t+"."}return""}}var De={};function or(e){{var r=ke();if(!r){var t=typeof e=="string"?e:e.displayName||e.name;t&&(r=`
Check the top-level render call using <`+t+">.")}return r}}function Ae(e,r){{if(!e._store||e._store.validated||e.key!=null)return;e._store.validated=!0;var t=or(r);if(De[t])return;De[t]=!0;var n="";e&&e._owner&&e._owner!==ce.current&&(n=" It was passed a child from "+I(e._owner.type)+"."),M(e),p('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',t,n),M(null)}}function Fe(e,r){{if(typeof e!="object")return;if(se(e))for(var t=0;t<e.length;t++){var n=e[t];le(n)&&Ae(n,r)}else if(le(e))e._store&&(e._store.validated=!0);else if(e){var o=D(e);if(typeof o=="function"&&o!==e.entries)for(var s=o.call(e),i;!(i=s.next()).done;)le(i.value)&&Ae(i.value,r)}}}function sr(e){{var r=e.type;if(r==null||typeof r=="string")return;var t;if(typeof r=="function")t=r.propTypes;else if(typeof r=="object"&&(r.$$typeof===d||r.$$typeof===E))t=r.propTypes;else return;if(t){var n=I(r);He(t,e.props,"prop",n,e)}else if(r.PropTypes!==void 0&&!fe){fe=!0;var o=I(r);p("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",o||"Unknown")}typeof r.getDefaultProps=="function"&&!r.getDefaultProps.isReactClassApproved&&p("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function ur(e){{for(var r=Object.keys(e.props),t=0;t<r.length;t++){var n=r[t];if(n!=="children"&&n!=="key"){M(e),p("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",n),M(null);break}}e.ref!==null&&(M(e),p("Invalid attribute `ref` supplied to `React.Fragment`."),M(null))}}function Ie(e,r,t,n,o,s){{var i=S(e);if(!i){var a="";(e===void 0||typeof e=="object"&&e!==null&&Object.keys(e).length===0)&&(a+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var R=ir(o);R?a+=R:a+=ke();var f;e===null?f="null":se(e)?f="array":e!==void 0&&e.$$typeof===h?(f="<"+(I(e.type)||"Unknown")+" />",a=" Did you accidentally export a JSX literal instead of a component?"):f=typeof e,p("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",f,a)}var l=ar(e,r,t,o,s);if(l==null)return l;if(i){var O=r.children;if(O!==void 0)if(n)if(se(O)){for(var U=0;U<O.length;U++)Fe(O[U],e);Object.freeze&&Object.freeze(O)}else p("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else Fe(O,e)}return e===T?ur(l):sr(l),l}}function cr(e,r,t){return Ie(e,r,t,!0)}function fr(e,r,t){return Ie(e,r,t,!1)}var lr=fr,dr=cr;V.Fragment=T,V.jsx=lr,V.jsxs=dr}()),V}var B={};/**
* @license React
* react-jsx-runtime.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var ve;function Ye(){if(ve)return B;ve=1;var b=u,h=Symbol.for("react.element"),_=Symbol.for("react.fragment"),T=Object.prototype.hasOwnProperty,P=b.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,F={key:!0,ref:!0,__self:!0,__source:!0};function k(y,d,x){var g,E={},C=null,c=null;x!==void 0&&(C=""+x),d.key!==void 0&&(C=""+d.key),d.ref!==void 0&&(c=d.ref);for(g in d)T.call(d,g)&&!F.hasOwnProperty(g)&&(E[g]=d[g]);if(y&&y.defaultProps)for(g in d=y.defaultProps,d)E[g]===void 0&&(E[g]=d[g]);return{$$typeof:h,type:y,key:C,ref:c,props:E,_owner:P.current}}return B.Fragment=_,B.jsx=k,B.jsxs=k,B}process.env.NODE_ENV==="production"?W.exports=Ye():W.exports=We();var A=W.exports;const Ne=String.raw;function Me({child:b,sliderWidth:h,sliderHeight:_,scaleOnDrag:T=!1}){const P=u.useRef(null),F=()=>{T&&P.current&&(P.current.style.transform="scale(0.9)")},k=()=>{T&&P.current&&(P.current.style.transform="scale(1)")};return A.jsxs(A.Fragment,{children:[A.jsx("style",{children:Ne`
.rtds-single-slide-styles img {
max-width: 100%;
max-height: 100%;
}
`}),A.jsx("div",{ref:P,style:{width:`${h}px`,height:`${_}px`,transition:"transform 0.2s ease-out"},className:"rtds-single-slide-styles",children:A.jsx("div",{style:{padding:"1rem",height:"100%",display:"flex",alignItems:"center",justifyContent:"center",userSelect:"none"},onPointerDown:F,onPointerUp:k,onPointerLeave:k,onDragStart:y=>(y.preventDefault(),y.stopPropagation(),!1),children:b})})]})}function re(b){const h=b.clientWidth,_=b.clientHeight;return{width:h,height:_}}function Ue({children:b,onSlideComplete:h,onSlideStart:_,activeIndex:T=null,threshHold:P=100,transition:F=.3,scaleOnDrag:k=!1}){const[y,d]=u.useState({width:0,height:0}),x=u.useRef(!1),g=u.useRef(0),E=u.useRef(0),C=u.useRef(0),c=u.useRef(0),v=u.useRef(null),J=u.useRef(null),D=u.useCallback((m=y.width)=>{E.current=c.current*-m,C.current=E.current,K()},[y.width]),w=u.useCallback(()=>{v.current&&(v.current.style.transition=`transform ${F}s ease-out`)},[F]),p=()=>{v.current&&(v.current.style.transition="none")};u.useEffect(()=>{T!==c.current&&(w(),c.current=T,D())},[T,D,w]),u.useLayoutEffect(()=>{v.current&&(p(),d(re(v.current)),D(re(v.current).width))},[D]),u.useEffect(()=>{const m=()=>{if(p(),v.current){const{width:S,height:Y}=re(v.current);d({width:S,height:Y}),D(S)}},j=({key:S})=>{const Y=["ArrowRight","ArrowLeft"].includes(S);Y&&w(),Y&&_&&_(c.current),S==="ArrowRight"&&c.current<b.length-1&&(c.current+=1),S==="ArrowLeft"&&c.current>0&&(c.current-=1),Y&&h&&h(c.current),D()};return window.addEventListener("resize",m),window.addEventListener("keydown",j),()=>{window.removeEventListener("resize",m),window.removeEventListener("keydown",j)}},[b.length,D,h,_,w]);function te(m){return function(j){w(),c.current=m,g.current=j.pageX,x.current=!0,J.current=requestAnimationFrame(X),v.current&&(v.current.style.cursor="grabbing"),_&&_(c.current)}}function ne(m){if(x.current){const j=m.pageX;E.current=C.current+j-g.current}}function H(){w(),cancelAnimationFrame(J.current),x.current=!1;const m=E.current-C.current;m<-P&&c.current<b.length-1&&(c.current+=1),m>P&&c.current>0&&(c.current-=1),w(),D(),v.current.style.cursor="grab",h&&h(c.current)}function X(){K(),x.current&&requestAnimationFrame(X)}function K(){v.current&&(v.current.style.transform=`translateX(${E.current}px)`)}return A.jsx("div",{style:{overflow:"hidden",width:"100%",height:"100%",maxHeight:"100vh"},children:A.jsx("div",{"data-testid":"slider",ref:v,style:{all:"initial",width:"100%",height:"100%",maxHeight:"100vh",display:"inline-flex",willChange:"transform, scale",cursor:"grab"},children:b.map((m,j)=>A.jsx("div",{onPointerDown:te(j),onPointerMove:ne,onPointerUp:H,onPointerLeave:()=>{x.current&&H()},onContextMenu:S=>{S.preventDefault(),S.stopPropagation()},className:"slide-outer",style:{touchAction:"none"},children:A.jsx(Me,{child:m,sliderWidth:y.width,sliderHeight:y.height,scaleOnDrag:k})},m.key))})})}return Ue});