forked from maisano/react-router-transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (161 loc) · 6.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var _excluded = ["render", "component", "path", "exact", "strict", "sensitive", "children"],
_excluded2 = ["children"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { cloneElement, createElement, useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import { Route, Switch, matchPath, useLocation } from 'react-router-dom';
import TransitionMotion from '@serprex/react-motion/lib/TransitionMotion';
import spring from '@serprex/react-motion/lib/spring'; // Helpers
function ensureSpring(styles) {
var obj = {};
for (var key in styles) {
var value = styles[key];
if (typeof value === 'number') {
obj[key] = spring(value);
} else {
obj[key] = value;
}
}
return obj;
}
function identity(v) {
return v;
}
function noop() {} // Components
function RouteTransition(_ref) {
var children = _ref.children,
className = _ref.className,
atEnter = _ref.atEnter,
atActive = _ref.atActive,
atLeave = _ref.atLeave,
_ref$wrapperComponent = _ref.wrapperComponent,
wrapperComponent = _ref$wrapperComponent === void 0 ? 'div' : _ref$wrapperComponent,
_ref$didLeave = _ref.didLeave,
didLeave = _ref$didLeave === void 0 ? noop : _ref$didLeave,
_ref$mapStyles = _ref.mapStyles,
mapStyles = _ref$mapStyles === void 0 ? identity : _ref$mapStyles,
_ref$runOnMount = _ref.runOnMount,
runOnMount = _ref$runOnMount === void 0 ? false : _ref$runOnMount;
var defaultStyles = runOnMount === false ? null : children == undefined ? [] : [{
key: children.key,
data: children,
style: atEnter
}];
var styles = children == undefined ? [] : [{
key: children.key,
data: children,
style: ensureSpring(atActive)
}];
return /*#__PURE__*/React.createElement(TransitionMotion, {
defaultStyles: defaultStyles,
styles: styles,
willEnter: function willEnter() {
return atEnter;
},
willLeave: function willLeave() {
return ensureSpring(atLeave);
},
didLeave: didLeave
}, function (interpolatedStyles) {
return /*#__PURE__*/React.createElement("div", {
className: className
}, interpolatedStyles.map(function (config) {
var props = {
style: mapStyles(config.style),
key: config.key
};
return wrapperComponent !== false ? /*#__PURE__*/createElement(wrapperComponent, props, config.data) : /*#__PURE__*/cloneElement(config.data, props);
}));
});
}
RouteTransition.propTypes = {
className: PropTypes.string,
wrapperComponent: PropTypes.oneOfType([PropTypes.bool, PropTypes.element, PropTypes.string, PropTypes.func]),
atEnter: PropTypes.object.isRequired,
atActive: PropTypes.object.isRequired,
atLeave: PropTypes.object.isRequired,
didLeave: PropTypes.func,
mapStyles: PropTypes.func,
runOnMount: PropTypes.bool
}; // AnimatedRoute
// The key-getter for RouteTransition. It's either on or off.
function getKey(_ref2, path, exact) {
var pathname = _ref2.pathname;
return matchPath(pathname, {
exact: exact,
path: path
}) ? 'match' : 'no-match';
}
function AnimatedRoute(_ref3) {
var render = _ref3.render,
component = _ref3.component,
path = _ref3.path,
exact = _ref3.exact,
strict = _ref3.strict,
sensitive = _ref3.sensitive,
children = _ref3.children,
routeTransitionProps = _objectWithoutProperties(_ref3, _excluded);
var location = useLocation();
return /*#__PURE__*/React.createElement(RouteTransition, routeTransitionProps, /*#__PURE__*/React.createElement(Route, {
key: getKey(location, path, exact),
path: path,
exact: exact,
strict: strict,
sensitive: sensitive,
location: location,
component: component,
render: render,
children: children
}));
} // AnimatedSwitch
var NO_MATCH = {
key: 'no-match'
}; // Not every location object has a `key` property (e.g. HashHistory).
function getLocationKey(location) {
return typeof location.key === 'string' ? location.key : '';
} // Some superfluous work, but something we need to do in order
// to persist matches/allow for nesting/etc.
function getMatchedRoute(children, _ref4) {
var pathname = _ref4.pathname;
var childrenArray = React.Children.toArray(children);
for (var i = 0; i < childrenArray.length; i++) {
var child = childrenArray[i];
var matches = null;
try {
matches = matchPath(pathname, {
exact: child.props.exact,
path: child.props.path
});
} catch (e) {
matches = null;
}
if (matches) {
return child;
}
}
return NO_MATCH;
}
var counter = 0;
function AnimatedSwitch(_ref5) {
var children = _ref5.children,
routeTransitionProps = _objectWithoutProperties(_ref5, _excluded2);
var location = useLocation();
var match = useRef(null);
var key = useRef(null);
var nextMatch = getMatchedRoute(children, location);
if (match.current === null) {
// Persist a reference to the most recent match
match.current = nextMatch;
key.current = getLocationKey(location);
} else if (match.current.key !== nextMatch.key) {
// Update the key given to Switch anytime the matched route changes
match.current = nextMatch;
key.current = getLocationKey(location) + ++counter;
}
return /*#__PURE__*/React.createElement(RouteTransition, routeTransitionProps, /*#__PURE__*/React.createElement(Switch, {
key: key.current,
location: location
}, children));
}
export { ensureSpring, spring, RouteTransition, AnimatedRoute, AnimatedSwitch };