@lanaco/lnc-react-ui
Version:
React component library
110 lines (109 loc) • 4.03 kB
JavaScript
import { _ as I, a as V } from "./tslib.es6-DQ-SzcmG.js";
import * as a from "react";
import { useRef as h, useState as B, useCallback as D, useMemo as N, useContext as K, cloneElement as U, Children as q, isValidElement as T } from "react";
import { u as _, a as O, P as Y, S as j, i as G } from "./motion-Ckekul_K.js";
function H() {
var e = h(!1), o = I(B(0), 2), t = o[0], r = o[1];
return _(function() {
return e.current = !0;
}), D(function() {
!e.current && r(t + 1);
}, [t]);
}
var S = 0;
function J() {
var e = S;
return S++, e;
}
var w = function(e) {
var o = e.children, t = e.initial, r = e.isPresent, i = e.onExitComplete, C = e.custom, x = e.presenceAffectsLayout, u = O(Q), m = O(J), y = N(
function() {
return {
id: m,
initial: t,
isPresent: r,
custom: C,
onExitComplete: function(s) {
u.set(s, !0);
var d = !0;
u.forEach(function(c) {
c || (d = !1);
}), d && (i == null || i());
},
register: function(s) {
return u.set(s, !1), function() {
return u.delete(s);
};
}
};
},
/**
* If the presence of a child affects the layout of the components around it,
* we want to make a new context value to ensure they get re-rendered
* so they can detect that layout change.
*/
x ? void 0 : [r]
);
return N(function() {
u.forEach(function(s, d) {
return u.set(d, !1);
});
}, [r]), a.useEffect(function() {
!r && !u.size && (i == null || i());
}, [r]), a.createElement(Y.Provider, { value: y }, o);
};
function Q() {
return /* @__PURE__ */ new Map();
}
function p(e) {
return e.key || "";
}
function W(e, o) {
var t = process.env.NODE_ENV !== "production" ? /* @__PURE__ */ new Set() : null;
e.forEach(function(r) {
var i = p(r);
process.env.NODE_ENV !== "production" && t && (t.has(i) && console.warn('Children of AnimatePresence require unique keys. "' + i + '" is a duplicate.'), t.add(i)), o.set(i, r);
});
}
function X(e) {
var o = [];
return q.forEach(e, function(t) {
T(t) && o.push(t);
}), o;
}
var ne = function(e) {
var o = e.children, t = e.custom, r = e.initial, i = r === void 0 ? !0 : r, C = e.onExitComplete, x = e.exitBeforeEnter, u = e.presenceAffectsLayout, m = u === void 0 ? !0 : u, y = H(), s = K(j);
G(s) && (y = s.forceUpdate);
var d = h(!0), c = X(o), v = h(c), P = h(/* @__PURE__ */ new Map()).current, f = h(/* @__PURE__ */ new Set()).current;
if (W(c, P), d.current)
return d.current = !1, a.createElement(a.Fragment, null, c.map(function(n) {
return a.createElement(w, { key: p(n), isPresent: !0, initial: i ? void 0 : !1, presenceAffectsLayout: m }, n);
}));
for (var l = V([], I(c)), g = v.current.map(p), R = c.map(p), b = g.length, A = 0; A < b; A++) {
var L = g[A];
R.indexOf(L) === -1 ? f.add(L) : f.delete(L);
}
return x && f.size && (l = []), f.forEach(function(n) {
if (R.indexOf(n) === -1) {
var E = P.get(n);
if (E) {
var k = g.indexOf(n), z = function() {
P.delete(n), f.delete(n);
var F = v.current.findIndex(function(M) {
return M.key === n;
});
v.current.splice(F, 1), f.size || (v.current = c, y(), C && C());
};
l.splice(k, 0, a.createElement(w, { key: p(E), isPresent: !1, onExitComplete: z, custom: t, presenceAffectsLayout: m }, E));
}
}
}), l = l.map(function(n) {
var E = n.key;
return f.has(E) ? n : a.createElement(w, { key: p(n), isPresent: !0, presenceAffectsLayout: m }, n);
}), v.current = l, process.env.NODE_ENV !== "production" && x && l.length > 1 && console.warn("You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour."), a.createElement(a.Fragment, null, f.size ? l : l.map(function(n) {
return U(n);
}));
};
export {
ne as A
};