Skip to content
This repository was archived by the owner on Feb 25, 2020. It is now read-only.

Commit a32e43a

Browse files
osdnksatya164
authored andcommitted
feat: add on transition end callback (#153)
1 parent f43800b commit a32e43a

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

src/types.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ export type NavigationStackOptions = HeaderOptions &
108108
horizontal?: number;
109109
};
110110
disableKeyboardHandling?: boolean;
111+
onTransitionStart?: () => void;
112+
onTransitionEnd?: () => void;
111113
};
112114

113115
export type NavigationConfig = {

src/views/Stack/Stack.tsx

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ type Props = {
4646
renderScene: (props: { route: Route }) => React.ReactNode;
4747
headerMode: HeaderMode;
4848
onTransitionStart?: (
49-
curr: { index: number },
50-
prev: { index: number }
49+
current: { index: number },
50+
previous: { index: number }
5151
) => void;
5252
onGestureBegin?: () => void;
5353
onGestureCanceled?: () => void;
@@ -60,7 +60,7 @@ type State = {
6060
scenes: HeaderScene<Route>[];
6161
progress: ProgressValues;
6262
layout: Layout;
63-
floaingHeaderHeight: number;
63+
floatingHeaderHeight: number;
6464
};
6565

6666
const dimensions = Dimensions.get('window');
@@ -171,7 +171,7 @@ export default class Stack extends React.Component<Props, State> {
171171
// This is not a great heuristic here. We don't know synchronously
172172
// on mount what the header height is so we have just used the most
173173
// common cases here.
174-
floaingHeaderHeight: getDefaultHeaderHeight(layout),
174+
floatingHeaderHeight: getDefaultHeaderHeight(layout),
175175
};
176176

177177
private handleLayout = (e: LayoutChangeEvent) => {
@@ -192,11 +192,32 @@ export default class Stack extends React.Component<Props, State> {
192192
private handleFloatingHeaderLayout = (e: LayoutChangeEvent) => {
193193
const { height } = e.nativeEvent.layout;
194194

195-
if (height !== this.state.floaingHeaderHeight) {
196-
this.setState({ floaingHeaderHeight: height });
195+
if (height !== this.state.floatingHeaderHeight) {
196+
this.setState({ floatingHeaderHeight: height });
197197
}
198198
};
199199

200+
private handleTransitionStart = ({
201+
route,
202+
current,
203+
previous,
204+
}: {
205+
route: Route;
206+
current: { index: number };
207+
previous: { index: number };
208+
}) => {
209+
const { onTransitionStart, descriptors } = this.props;
210+
const options = descriptors[route.key].options;
211+
212+
onTransitionStart && onTransitionStart(current, previous);
213+
options.onTransitionStart && options.onTransitionStart();
214+
};
215+
216+
private handleTransitionEnd = ({ route }: { route: Route }) => {
217+
const options = this.props.descriptors[route.key].options;
218+
options.onTransitionEnd && options.onTransitionEnd();
219+
};
220+
200221
render() {
201222
const {
202223
mode,
@@ -212,13 +233,12 @@ export default class Stack extends React.Component<Props, State> {
212233
renderHeader,
213234
renderScene,
214235
headerMode,
215-
onTransitionStart,
216236
onGestureBegin,
217237
onGestureCanceled,
218238
onGestureEnd,
219239
} = this.props;
220240

221-
const { scenes, layout, progress, floaingHeaderHeight } = this.state;
241+
const { scenes, layout, progress, floatingHeaderHeight } = this.state;
222242

223243
const focusedRoute = navigation.state.routes[navigation.state.index];
224244
const focusedOptions = descriptors[focusedRoute.key].options;
@@ -300,7 +320,7 @@ export default class Stack extends React.Component<Props, State> {
300320
onGestureCanceled={onGestureCanceled}
301321
onGestureEnd={onGestureEnd}
302322
gestureResponseDistance={gestureResponseDistance}
303-
floaingHeaderHeight={floaingHeaderHeight}
323+
floatingHeaderHeight={floatingHeaderHeight}
304324
hasCustomHeader={header === null}
305325
getPreviousRoute={getPreviousRoute}
306326
headerMode={headerMode}
@@ -309,7 +329,8 @@ export default class Stack extends React.Component<Props, State> {
309329
renderScene={renderScene}
310330
onOpenRoute={onOpenRoute}
311331
onCloseRoute={onCloseRoute}
312-
onTransitionStart={onTransitionStart}
332+
onTransitionStart={this.handleTransitionStart}
333+
onTransitionEnd={this.handleTransitionEnd}
313334
onGoBack={onGoBack}
314335
direction={direction}
315336
transitionSpec={transitionSpec}

src/views/Stack/StackItem.tsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ type Props = TransitionPreset & {
3333
onOpenRoute: (props: { route: Route }) => void;
3434
onCloseRoute: (props: { route: Route }) => void;
3535
onGoBack: (props: { route: Route }) => void;
36-
onTransitionStart?: (
37-
curr: { index: number },
38-
prev: { index: number }
39-
) => void;
36+
onTransitionStart?: (props: {
37+
route: Route;
38+
current: { index: number };
39+
previous: { index: number };
40+
}) => void;
41+
onTransitionEnd?: (props: { route: Route }) => void;
4042
onGestureBegin?: () => void;
4143
onGestureCanceled?: () => void;
4244
onGestureEnd?: () => void;
@@ -46,22 +48,34 @@ type Props = TransitionPreset & {
4648
};
4749
headerMode: HeaderMode;
4850
headerTransparent?: boolean;
49-
floaingHeaderHeight: number;
51+
floatingHeaderHeight: number;
5052
hasCustomHeader: boolean;
5153
};
5254

5355
export default class StackItem extends React.PureComponent<Props> {
54-
private handleOpen = () =>
55-
this.props.onOpenRoute({ route: this.props.scene.route });
56+
private handleOpen = () => {
57+
const { scene, onTransitionEnd, onOpenRoute } = this.props;
5658

57-
private handleClose = () =>
58-
this.props.onCloseRoute({ route: this.props.scene.route });
59+
onTransitionEnd && onTransitionEnd({ route: scene.route });
60+
onOpenRoute({ route: scene.route });
61+
};
62+
63+
private handleClose = () => {
64+
const { scene, onTransitionEnd, onCloseRoute } = this.props;
65+
66+
onTransitionEnd && onTransitionEnd({ route: scene.route });
67+
onCloseRoute({ route: scene.route });
68+
};
5969

6070
private handleTransitionStart = ({ closing }: { closing: boolean }) => {
6171
const { index, scene, onTransitionStart, onGoBack } = this.props;
6272

6373
onTransitionStart &&
64-
onTransitionStart({ index: closing ? index - 1 : index }, { index });
74+
onTransitionStart({
75+
route: scene.route,
76+
previous: { index: closing ? index - 1 : index },
77+
current: { index },
78+
});
6579

6680
closing && onGoBack({ route: scene.route });
6781
};
@@ -86,7 +100,7 @@ export default class StackItem extends React.PureComponent<Props> {
86100
onGestureCanceled,
87101
onGestureEnd,
88102
gestureResponseDistance,
89-
floaingHeaderHeight,
103+
floatingHeaderHeight,
90104
hasCustomHeader,
91105
getPreviousRoute,
92106
headerMode,
@@ -126,7 +140,7 @@ export default class StackItem extends React.PureComponent<Props> {
126140
pointerEvents="box-none"
127141
containerStyle={
128142
headerMode === 'float' && !headerTransparent && !hasCustomHeader
129-
? { marginTop: floaingHeaderHeight }
143+
? { marginTop: floatingHeaderHeight }
130144
: null
131145
}
132146
contentStyle={cardStyle}

src/views/Stack/StackView.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type Props = {
1919
descriptors: Descriptors;
2020
navigationConfig: NavigationConfig;
2121
onTransitionStart?: (
22-
curr: { index: number },
23-
prev: { index: number }
22+
current: { index: number },
23+
previous: { index: number }
2424
) => void;
2525
onGestureBegin?: () => void;
2626
onGestureCanceled?: () => void;

0 commit comments

Comments
 (0)