1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.AnimatePresence = AnimatePresence;
|
7 | var _tools = require("@legendapp/tools");
|
8 | var _react = require("@legendapp/tools/react");
|
9 | var _react2 = _interopRequireWildcard(require("react"));
|
10 | function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
11 | function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
12 | function exitableByKey(children) {
|
13 | const map = new Map();
|
14 | _react2.Children.forEach(children, child => {
|
15 | var _child$props;
|
16 | if (child.key && (_child$props = child.props) !== null && _child$props !== void 0 && _child$props.exit && (0, _tools.isString)(child.key)) {
|
17 | map.set(child.key, child);
|
18 | }
|
19 | });
|
20 | return map;
|
21 | }
|
22 | function AnimatePresence(_ref) {
|
23 | let {
|
24 | children
|
25 | } = _ref;
|
26 | const fr = (0, _react.useForceRender)();
|
27 | const childArr = _react2.Children.toArray(children);
|
28 | const childrenPrevious = (0, _react.usePrevious)(childArr);
|
29 |
|
30 |
|
31 | const childrenByKey = exitableByKey(childArr);
|
32 | const childrenByKeyPrevious = (0, _react.usePrevious)(childrenByKey);
|
33 |
|
34 |
|
35 | const exiting = (0, _react2.useRef)(new Map());
|
36 | if (childrenByKeyPrevious) {
|
37 | childrenByKeyPrevious.forEach((prevChild, key) => {
|
38 | if (!childrenByKey.get(key)) {
|
39 | exiting.current.set(key, prevChild);
|
40 | }
|
41 | });
|
42 | }
|
43 |
|
44 |
|
45 | let childrenToRender = [...childArr];
|
46 | exiting.current.forEach((child, key) => {
|
47 | if (childrenByKey.get(key)) {
|
48 | exiting.current.delete(key);
|
49 | } else {
|
50 | const index = childrenPrevious.indexOf(child);
|
51 | childrenToRender.splice(index, 0, child);
|
52 | }
|
53 | });
|
54 | return _react2.default.createElement(_react2.default.Fragment, null, childrenToRender.map(child => {
|
55 | if (child && child.props.exit) {
|
56 | const key = child.key;
|
57 | const animKeys = Object.keys(child.props.exit);
|
58 |
|
59 | return key && exiting.current.get(key) && animKeys ? (0, _react2.cloneElement)(child, {
|
60 | animate: child.props.exit,
|
61 | onAnimationComplete: animKey => {
|
62 | if (exiting.current.has(key)) {
|
63 | (0, _tools.arrayRemove)(animKeys, animKey);
|
64 | if (animKeys.length === 0) {
|
65 | exiting.current.delete(key);
|
66 | fr();
|
67 | }
|
68 | }
|
69 | }
|
70 | }) : child;
|
71 | }
|
72 | return child;
|
73 | }));
|
74 | }
|
75 |
|
\ | No newline at end of file |