react-touch-drag-slider
Version:
Touch and drag slider carousel component for React
817 lines (813 loc) • 26.5 kB
JavaScript
import Ye, { useRef as I, useState as lr, useCallback as $e, useEffect as Ie, useLayoutEffect as dr } from "react";
var le = { 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 Le;
function vr() {
return Le || (Le = 1, process.env.NODE_ENV !== "production" && function() {
var m = Ye, p = Symbol.for("react.element"), y = Symbol.for("react.portal"), w = Symbol.for("react.fragment"), P = Symbol.for("react.strict_mode"), D = Symbol.for("react.profiler"), S = Symbol.for("react.provider"), E = Symbol.for("react.context"), l = Symbol.for("react.forward_ref"), T = Symbol.for("react.suspense"), h = Symbol.for("react.suspense_list"), b = Symbol.for("react.memo"), O = Symbol.for("react.lazy"), s = Symbol.for("react.offscreen"), d = Symbol.iterator, B = "@@iterator";
function k(e) {
if (e === null || typeof e != "object")
return null;
var r = d && e[d] || e[B];
return typeof r == "function" ? r : null;
}
var _ = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
function v(e) {
{
for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++)
t[n - 1] = arguments[n];
re("error", e, t);
}
}
function re(e, r, t) {
{
var n = _.ReactDebugCurrentFrame, o = n.getStackAddendum();
o !== "" && (r += "%s", t = t.concat([o]));
var u = t.map(function(i) {
return String(i);
});
u.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, u);
}
}
var te = !1, z = !1, G = !1, J = !1, g = !1, C;
C = Symbol.for("react.module.reference");
function j(e) {
return !!(typeof e == "string" || typeof e == "function" || e === w || e === D || g || e === P || e === T || e === h || J || e === s || te || z || G || typeof e == "object" && e !== null && (e.$$typeof === O || e.$$typeof === b || e.$$typeof === S || e.$$typeof === E || e.$$typeof === l || // This needs to include all possible module reference object
// types supported by any Flight configuration anywhere since
// we don't know which Flight build this will end up being used
// with.
e.$$typeof === C || e.getModuleId !== void 0));
}
function L(e, r, t) {
var n = e.displayName;
if (n)
return n;
var o = r.displayName || r.name || "";
return o !== "" ? t + "(" + o + ")" : t;
}
function de(e) {
return e.displayName || "Context";
}
function A(e) {
if (e == null)
return null;
if (typeof e.tag == "number" && v("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 w:
return "Fragment";
case y:
return "Portal";
case D:
return "Profiler";
case P:
return "StrictMode";
case T:
return "Suspense";
case h:
return "SuspenseList";
}
if (typeof e == "object")
switch (e.$$typeof) {
case E:
var r = e;
return de(r) + ".Consumer";
case S:
var t = e;
return de(t._context) + ".Provider";
case l:
return L(e, e.render, "ForwardRef");
case b:
var n = e.displayName || null;
return n !== null ? n : A(e.type) || "Memo";
case O: {
var o = e, u = o._payload, i = o._init;
try {
return A(i(u));
} catch {
return null;
}
}
}
return null;
}
var W = Object.assign, M = 0, ve, pe, he, ge, me, ye, Ee;
function be() {
}
be.__reactDisabledLog = !0;
function Ne() {
{
if (M === 0) {
ve = console.log, pe = console.info, he = console.warn, ge = console.error, me = console.group, ye = console.groupCollapsed, Ee = console.groupEnd;
var e = {
configurable: !0,
enumerable: !0,
value: be,
writable: !0
};
Object.defineProperties(console, {
info: e,
log: e,
warn: e,
error: e,
group: e,
groupCollapsed: e,
groupEnd: e
});
}
M++;
}
}
function Me() {
{
if (M--, M === 0) {
var e = {
configurable: !0,
enumerable: !0,
writable: !0
};
Object.defineProperties(console, {
log: W({}, e, {
value: ve
}),
info: W({}, e, {
value: pe
}),
warn: W({}, e, {
value: he
}),
error: W({}, e, {
value: ge
}),
group: W({}, e, {
value: me
}),
groupCollapsed: W({}, e, {
value: ye
}),
groupEnd: W({}, e, {
value: Ee
})
});
}
M < 0 && v("disabledDepth fell below zero. This is a bug in React. Please file an issue.");
}
}
var ne = _.ReactCurrentDispatcher, ae;
function H(e, r, t) {
{
if (ae === void 0)
try {
throw Error();
} catch (o) {
var n = o.stack.trim().match(/\n( *(at )?)/);
ae = n && n[1] || "";
}
return `
` + ae + e;
}
}
var ie = !1, X;
{
var Ue = typeof WeakMap == "function" ? WeakMap : Map;
X = new Ue();
}
function Re(e, r) {
if (!e || ie)
return "";
{
var t = X.get(e);
if (t !== void 0)
return t;
}
var n;
ie = !0;
var o = Error.prepareStackTrace;
Error.prepareStackTrace = void 0;
var u;
u = ne.current, ne.current = null, Ne();
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 (F) {
n = F;
}
Reflect.construct(e, [], i);
} else {
try {
i.call();
} catch (F) {
n = F;
}
e.call(i.prototype);
}
} else {
try {
throw Error();
} catch (F) {
n = F;
}
e();
}
} catch (F) {
if (F && n && typeof F.stack == "string") {
for (var a = F.stack.split(`
`), R = n.stack.split(`
`), c = a.length - 1, f = R.length - 1; c >= 1 && f >= 0 && a[c] !== R[f]; )
f--;
for (; c >= 1 && f >= 0; c--, f--)
if (a[c] !== R[f]) {
if (c !== 1 || f !== 1)
do
if (c--, f--, f < 0 || a[c] !== R[f]) {
var x = `
` + a[c].replace(" at new ", " at ");
return e.displayName && x.includes("<anonymous>") && (x = x.replace("<anonymous>", e.displayName)), typeof e == "function" && X.set(e, x), x;
}
while (c >= 1 && f >= 0);
break;
}
}
} finally {
ie = !1, ne.current = u, Me(), Error.prepareStackTrace = o;
}
var N = e ? e.displayName || e.name : "", Fe = N ? H(N) : "";
return typeof e == "function" && X.set(e, Fe), Fe;
}
function Ve(e, r, t) {
return Re(e, !1);
}
function qe(e) {
var r = e.prototype;
return !!(r && r.isReactComponent);
}
function K(e, r, t) {
if (e == null)
return "";
if (typeof e == "function")
return Re(e, qe(e));
if (typeof e == "string")
return H(e);
switch (e) {
case T:
return H("Suspense");
case h:
return H("SuspenseList");
}
if (typeof e == "object")
switch (e.$$typeof) {
case l:
return Ve(e.render);
case b:
return K(e.type, r, t);
case O: {
var n = e, o = n._payload, u = n._init;
try {
return K(u(o), r, t);
} catch {
}
}
}
return "";
}
var Z = Object.prototype.hasOwnProperty, _e = {}, we = _.ReactDebugCurrentFrame;
function Q(e) {
if (e) {
var r = e._owner, t = K(e.type, e._source, r ? r.type : null);
we.setExtraStackFrame(t);
} else
we.setExtraStackFrame(null);
}
function Be(e, r, t, n, o) {
{
var u = Function.call.bind(Z);
for (var i in e)
if (u(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 (c) {
a = c;
}
a && !(a instanceof Error) && (Q(o), v("%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), Q(null)), a instanceof Error && !(a.message in _e) && (_e[a.message] = !0, Q(o), v("Failed %s type: %s", t, a.message), Q(null));
}
}
}
var ze = Array.isArray;
function oe(e) {
return ze(e);
}
function Ge(e) {
{
var r = typeof Symbol == "function" && Symbol.toStringTag, t = r && e[Symbol.toStringTag] || e.constructor.name || "Object";
return t;
}
}
function Je(e) {
try {
return Pe(e), !1;
} catch {
return !0;
}
}
function Pe(e) {
return "" + e;
}
function Te(e) {
if (Je(e))
return v("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", Ge(e)), Pe(e);
}
var U = _.ReactCurrentOwner, He = {
key: !0,
ref: !0,
__self: !0,
__source: !0
}, xe, Oe, ue;
ue = {};
function Xe(e) {
if (Z.call(e, "ref")) {
var r = Object.getOwnPropertyDescriptor(e, "ref").get;
if (r && r.isReactWarning)
return !1;
}
return e.ref !== void 0;
}
function Ke(e) {
if (Z.call(e, "key")) {
var r = Object.getOwnPropertyDescriptor(e, "key").get;
if (r && r.isReactWarning)
return !1;
}
return e.key !== void 0;
}
function Ze(e, r) {
if (typeof e.ref == "string" && U.current && r && U.current.stateNode !== r) {
var t = A(U.current.type);
ue[t] || (v('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', A(U.current.type), e.ref), ue[t] = !0);
}
}
function Qe(e, r) {
{
var t = function() {
xe || (xe = !0, v("%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 er(e, r) {
{
var t = function() {
Oe || (Oe = !0, v("%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 rr = function(e, r, t, n, o, u, i) {
var a = {
// This tag allows us to uniquely identify this as a React Element
$$typeof: p,
// Built-in properties that belong on the element
type: e,
key: r,
ref: t,
props: i,
// Record the component responsible for creating this element.
_owner: u
};
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 tr(e, r, t, n, o) {
{
var u, i = {}, a = null, R = null;
t !== void 0 && (Te(t), a = "" + t), Ke(r) && (Te(r.key), a = "" + r.key), Xe(r) && (R = r.ref, Ze(r, o));
for (u in r)
Z.call(r, u) && !He.hasOwnProperty(u) && (i[u] = r[u]);
if (e && e.defaultProps) {
var c = e.defaultProps;
for (u in c)
i[u] === void 0 && (i[u] = c[u]);
}
if (a || R) {
var f = typeof e == "function" ? e.displayName || e.name || "Unknown" : e;
a && Qe(i, f), R && er(i, f);
}
return rr(e, a, R, o, n, U.current, i);
}
}
var se = _.ReactCurrentOwner, Ce = _.ReactDebugCurrentFrame;
function Y(e) {
if (e) {
var r = e._owner, t = K(e.type, e._source, r ? r.type : null);
Ce.setExtraStackFrame(t);
} else
Ce.setExtraStackFrame(null);
}
var ce;
ce = !1;
function fe(e) {
return typeof e == "object" && e !== null && e.$$typeof === p;
}
function je() {
{
if (se.current) {
var e = A(se.current.type);
if (e)
return `
Check the render method of \`` + e + "`.";
}
return "";
}
}
function nr(e) {
{
if (e !== void 0) {
var r = e.fileName.replace(/^.*[\\\/]/, ""), t = e.lineNumber;
return `
Check your code at ` + r + ":" + t + ".";
}
return "";
}
}
var Se = {};
function ar(e) {
{
var r = je();
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 ke(e, r) {
{
if (!e._store || e._store.validated || e.key != null)
return;
e._store.validated = !0;
var t = ar(r);
if (Se[t])
return;
Se[t] = !0;
var n = "";
e && e._owner && e._owner !== se.current && (n = " It was passed a child from " + A(e._owner.type) + "."), Y(e), v('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), Y(null);
}
}
function De(e, r) {
{
if (typeof e != "object")
return;
if (oe(e))
for (var t = 0; t < e.length; t++) {
var n = e[t];
fe(n) && ke(n, r);
}
else if (fe(e))
e._store && (e._store.validated = !0);
else if (e) {
var o = k(e);
if (typeof o == "function" && o !== e.entries)
for (var u = o.call(e), i; !(i = u.next()).done; )
fe(i.value) && ke(i.value, r);
}
}
}
function ir(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 === l || // Note: Memo only checks outer props here.
// Inner props are checked in the reconciler.
r.$$typeof === b))
t = r.propTypes;
else
return;
if (t) {
var n = A(r);
Be(t, e.props, "prop", n, e);
} else if (r.PropTypes !== void 0 && !ce) {
ce = !0;
var o = A(r);
v("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", o || "Unknown");
}
typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && v("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.");
}
}
function or(e) {
{
for (var r = Object.keys(e.props), t = 0; t < r.length; t++) {
var n = r[t];
if (n !== "children" && n !== "key") {
Y(e), v("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), Y(null);
break;
}
}
e.ref !== null && (Y(e), v("Invalid attribute `ref` supplied to `React.Fragment`."), Y(null));
}
}
function Ae(e, r, t, n, o, u) {
{
var i = j(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 = nr(o);
R ? a += R : a += je();
var c;
e === null ? c = "null" : oe(e) ? c = "array" : e !== void 0 && e.$$typeof === p ? (c = "<" + (A(e.type) || "Unknown") + " />", a = " Did you accidentally export a JSX literal instead of a component?") : c = typeof e, v("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", c, a);
}
var f = tr(e, r, t, o, u);
if (f == null)
return f;
if (i) {
var x = r.children;
if (x !== void 0)
if (n)
if (oe(x)) {
for (var N = 0; N < x.length; N++)
De(x[N], e);
Object.freeze && Object.freeze(x);
} else
v("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
De(x, e);
}
return e === w ? or(f) : ir(f), f;
}
}
function ur(e, r, t) {
return Ae(e, r, t, !0);
}
function sr(e, r, t) {
return Ae(e, r, t, !1);
}
var cr = sr, fr = ur;
V.Fragment = w, V.jsx = cr, V.jsxs = fr;
}()), V;
}
var q = {};
/**
* @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 We;
function pr() {
if (We)
return q;
We = 1;
var m = Ye, p = Symbol.for("react.element"), y = Symbol.for("react.fragment"), w = Object.prototype.hasOwnProperty, P = m.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, D = { key: !0, ref: !0, __self: !0, __source: !0 };
function S(E, l, T) {
var h, b = {}, O = null, s = null;
T !== void 0 && (O = "" + T), l.key !== void 0 && (O = "" + l.key), l.ref !== void 0 && (s = l.ref);
for (h in l)
w.call(l, h) && !D.hasOwnProperty(h) && (b[h] = l[h]);
if (E && E.defaultProps)
for (h in l = E.defaultProps, l)
b[h] === void 0 && (b[h] = l[h]);
return { $$typeof: p, type: E, key: O, ref: s, props: b, _owner: P.current };
}
return q.Fragment = y, q.jsx = S, q.jsxs = S, q;
}
process.env.NODE_ENV === "production" ? le.exports = pr() : le.exports = vr();
var $ = le.exports;
const hr = String.raw;
function gr({
child: m,
sliderWidth: p,
sliderHeight: y,
scaleOnDrag: w = !1
}) {
const P = I(null), D = () => {
w && P.current && (P.current.style.transform = "scale(0.9)");
}, S = () => {
w && P.current && (P.current.style.transform = "scale(1)");
};
return /* @__PURE__ */ $.jsxs($.Fragment, { children: [
/* @__PURE__ */ $.jsx("style", { children: hr`
.rtds-single-slide-styles img {
max-width: 100%;
max-height: 100%;
}
` }),
/* @__PURE__ */ $.jsx(
"div",
{
ref: P,
style: {
width: `${p}px`,
height: `${y}px`,
transition: "transform 0.2s ease-out"
},
className: "rtds-single-slide-styles",
children: /* @__PURE__ */ $.jsx(
"div",
{
style: {
padding: "1rem",
height: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
userSelect: "none"
},
onPointerDown: D,
onPointerUp: S,
onPointerLeave: S,
onDragStart: (E) => (E.preventDefault(), E.stopPropagation(), !1),
children: m
}
)
}
)
] });
}
function ee(m) {
const p = m.clientWidth, y = m.clientHeight;
return { width: p, height: y };
}
if (import.meta.vitest) {
const { it: m, expect: p } = import.meta.vitest, y = document.createElement("div");
m("Gets an elements dimensions", () => {
p(ee(y)).toStrictEqual({
width: 0,
height: 0
});
});
}
function yr({
children: m,
onSlideComplete: p,
onSlideStart: y,
activeIndex: w = null,
threshHold: P = 100,
transition: D = 0.3,
scaleOnDrag: S = !1
}) {
const [E, l] = lr({ width: 0, height: 0 }), T = I(!1), h = I(0), b = I(0), O = I(0), s = I(0), d = I(null), B = I(null), k = $e(
(g = E.width) => {
b.current = s.current * -g, O.current = b.current, J();
},
[E.width]
), _ = $e(() => {
d.current && (d.current.style.transition = `transform ${D}s ease-out`);
}, [D]), v = () => {
d.current && (d.current.style.transition = "none");
};
Ie(() => {
w !== s.current && (_(), s.current = w, k());
}, [w, k, _]), dr(() => {
d.current && (v(), l(ee(d.current)), k(ee(d.current).width));
}, [k]), Ie(() => {
const g = () => {
if (v(), d.current) {
const { width: j, height: L } = ee(d.current);
l({ width: j, height: L }), k(j);
}
}, C = ({ key: j }) => {
const L = ["ArrowRight", "ArrowLeft"].includes(j);
L && _(), L && y && y(s.current), j === "ArrowRight" && s.current < m.length - 1 && (s.current += 1), j === "ArrowLeft" && s.current > 0 && (s.current -= 1), L && p && p(s.current), k();
};
return window.addEventListener("resize", g), window.addEventListener("keydown", C), () => {
window.removeEventListener("resize", g), window.removeEventListener("keydown", C);
};
}, [
m.length,
k,
p,
y,
_
]);
function re(g) {
return function(C) {
_(), s.current = g, h.current = C.pageX, T.current = !0, B.current = requestAnimationFrame(G), d.current && (d.current.style.cursor = "grabbing"), y && y(s.current);
};
}
function te(g) {
if (T.current) {
const C = g.pageX;
b.current = O.current + C - h.current;
}
}
function z() {
_(), cancelAnimationFrame(B.current), T.current = !1;
const g = b.current - O.current;
g < -P && s.current < m.length - 1 && (s.current += 1), g > P && s.current > 0 && (s.current -= 1), _(), k(), d.current.style.cursor = "grab", p && p(s.current);
}
function G() {
J(), T.current && requestAnimationFrame(G);
}
function J() {
d.current && (d.current.style.transform = `translateX(${b.current}px)`);
}
return /* @__PURE__ */ $.jsx(
"div",
{
style: {
overflow: "hidden",
width: "100%",
height: "100%",
maxHeight: "100vh"
},
children: /* @__PURE__ */ $.jsx(
"div",
{
"data-testid": "slider",
ref: d,
style: {
all: "initial",
width: "100%",
height: "100%",
maxHeight: "100vh",
display: "inline-flex",
willChange: "transform, scale",
cursor: "grab"
},
children: m.map((g, C) => /* @__PURE__ */ $.jsx(
"div",
{
onPointerDown: re(C),
onPointerMove: te,
onPointerUp: z,
onPointerLeave: () => {
T.current && z();
},
onContextMenu: (j) => {
j.preventDefault(), j.stopPropagation();
},
className: "slide-outer",
style: {
touchAction: "none"
},
children: /* @__PURE__ */ $.jsx(
gr,
{
child: g,
sliderWidth: E.width,
sliderHeight: E.height,
scaleOnDrag: S
}
)
},
g.key
))
}
)
}
);
}
export {
yr as default
};