summaryrefslogtreecommitdiffhomepage
path: root/app/transitions.js
blob: 56b1d74cfbd8e41c3571e5580e1cd9470c560dab (plain)
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
// @flow
import TransitionRule from './lib/transition-rule';
import type { TransitionFork, TransitionDescriptor } from './lib/transition-rule';

export type TransitionGroupProps = {
  name: string,
  duration: number,
};

type TransitionMap = {
  [name: string]: TransitionFork,
};

/**
 * Transition descriptors
 */
const transitions: TransitionMap = {
  slide: {
    forward: {
      name: 'slide-up',
      duration: 450,
    },
    backward: {
      name: 'slide-down',
      duration: 450,
    },
  },
  push: {
    forward: {
      name: 'push',
      duration: 450,
    },
    backward: {
      name: 'pop',
      duration: 450,
    },
  },
};

/**
 * Transition rules
 * (null) is used to indicate any route.
 */
const transitionRules = [
  r('/settings', '/settings/account', transitions.push),
  r('/settings', '/settings/preferences', transitions.push),
  r('/settings', '/settings/advanced', transitions.push),
  r('/settings', '/settings/support', transitions.push),
  r(null, '/settings', transitions.slide),
  r(null, '/select-location', transitions.slide),
];

/**
 * Calculate TransitionGroup props.
 *
 * @param {string} [fromRoute] - source route
 * @param {string} toRoute     - target route
 */
export function getTransitionProps(fromRoute: ?string, toRoute: string): TransitionGroupProps {
  // ignore initial transition and transition between the same routes
  if (!fromRoute || fromRoute === toRoute) {
    return noTransitionProps();
  }

  for (const rule of transitionRules) {
    const match = rule.match(fromRoute, toRoute);
    if (match) {
      return toTransitionGroupProps(match.descriptor);
    }
  }

  return noTransitionProps();
}

/**
 * Integrate TransitionDescriptor into TransitionGroupProps
 * @param {TransitionDescriptor} descriptor
 */
function toTransitionGroupProps(descriptor: TransitionDescriptor): TransitionGroupProps {
  const { name, duration } = descriptor;
  return {
    name: name,
    duration: duration,
  };
}

/**
 * Returns default props with no animation
 */
function noTransitionProps(): TransitionGroupProps {
  return {
    name: '',
    duration: 0,
  };
}

/**
 * Shortcut to create TransitionRule
 */
function r(from: ?string, to: string, fork: TransitionFork): TransitionRule {
  return new TransitionRule(from, to, fork);
}