forked from maisano/react-router-transition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (40 loc) · 1.32 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
import { createElement as h } from 'react'
import TM from 'react-motion/lib/TransitionMotion'
import sp from 'react-motion/lib/spring';
import { object, string, func, bool } from 'prop-types'
const e = s => return Object.keys(s).reduce((a, b) => { a[b] = typeof s[b] === 'number' ? sp(s[b]) : s[b]; return a; }, {});
const al = p => p.atLeave; const aa = p => p.atActive; const ae = p => p.atEnter;
const RT = p => {
const base = { data: p.children, key: p.pathname }
const defaultStyles =
!p.runOnMount ? null :
!p.children ? [] :
[{...base, style: ae(p) }];
const styles =
!p.children ? [] :
[{...base, style: e(aa(p), p.opts) }]
const willEnter = () => ae(p);
const willLeave = () => e(al(p), p.opts);
const tm = { willEnter, willLeave, defaultStyles, styles };
const route = ({ key, style, data }) => {
const p = {...{key}, {style: p.mapStyles(style)}}
return h("div", p, data)
}
const routes = routes => h("div", null, routes.map(route))
return h(TM, tm, routes)
}
RT.defaultProps = {
runOnMount: true,
mapStyles: val => val,
opts: { stiffness: 120, damping: 26 }
};
RT.propTypes = {
atEnter: object.isRequired,
atActive: object.isRequired,
atLeave: object.isRequired,
pathname: string.isRequired,
mapStyles: func,
runOnMount: bool,
opts: object
};
export default RT;