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

Commit

Permalink
feat: add on transition end callback (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
osdnk authored and satya164 committed Aug 18, 2019
1 parent fb9dbf9 commit 51b1069
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
2 changes: 2 additions & 0 deletions packages/stack/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ export type NavigationStackOptions = HeaderOptions &
horizontal?: number;
};
disableKeyboardHandling?: boolean;
onTransitionStart?: () => void;
onTransitionEnd?: () => void;
};

export type NavigationConfig = {
Expand Down
41 changes: 31 additions & 10 deletions packages/stack/src/views/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type Props = {
renderScene: (props: { route: Route }) => React.ReactNode;
headerMode: HeaderMode;
onTransitionStart?: (
curr: { index: number },
prev: { index: number }
current: { index: number },
previous: { index: number }
) => void;
onGestureBegin?: () => void;
onGestureCanceled?: () => void;
Expand All @@ -60,7 +60,7 @@ type State = {
scenes: HeaderScene<Route>[];
progress: ProgressValues;
layout: Layout;
floaingHeaderHeight: number;
floatingHeaderHeight: number;
};

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

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

if (height !== this.state.floaingHeaderHeight) {
this.setState({ floaingHeaderHeight: height });
if (height !== this.state.floatingHeaderHeight) {
this.setState({ floatingHeaderHeight: height });
}
};

private handleTransitionStart = ({
route,
current,
previous,
}: {
route: Route;
current: { index: number };
previous: { index: number };
}) => {
const { onTransitionStart, descriptors } = this.props;
const options = descriptors[route.key].options;

onTransitionStart && onTransitionStart(current, previous);
options.onTransitionStart && options.onTransitionStart();
};

private handleTransitionEnd = ({ route }: { route: Route }) => {
const options = this.props.descriptors[route.key].options;
options.onTransitionEnd && options.onTransitionEnd();
};

render() {
const {
mode,
Expand All @@ -212,13 +233,12 @@ export default class Stack extends React.Component<Props, State> {
renderHeader,
renderScene,
headerMode,
onTransitionStart,
onGestureBegin,
onGestureCanceled,
onGestureEnd,
} = this.props;

const { scenes, layout, progress, floaingHeaderHeight } = this.state;
const { scenes, layout, progress, floatingHeaderHeight } = this.state;

const focusedRoute = navigation.state.routes[navigation.state.index];
const focusedOptions = descriptors[focusedRoute.key].options;
Expand Down Expand Up @@ -300,7 +320,7 @@ export default class Stack extends React.Component<Props, State> {
onGestureCanceled={onGestureCanceled}
onGestureEnd={onGestureEnd}
gestureResponseDistance={gestureResponseDistance}
floaingHeaderHeight={floaingHeaderHeight}
floatingHeaderHeight={floatingHeaderHeight}
hasCustomHeader={header === null}
getPreviousRoute={getPreviousRoute}
headerMode={headerMode}
Expand All @@ -309,7 +329,8 @@ export default class Stack extends React.Component<Props, State> {
renderScene={renderScene}
onOpenRoute={onOpenRoute}
onCloseRoute={onCloseRoute}
onTransitionStart={onTransitionStart}
onTransitionStart={this.handleTransitionStart}
onTransitionEnd={this.handleTransitionEnd}
onGoBack={onGoBack}
direction={direction}
transitionSpec={transitionSpec}
Expand Down
38 changes: 26 additions & 12 deletions packages/stack/src/views/Stack/StackItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ type Props = TransitionPreset & {
onOpenRoute: (props: { route: Route }) => void;
onCloseRoute: (props: { route: Route }) => void;
onGoBack: (props: { route: Route }) => void;
onTransitionStart?: (
curr: { index: number },
prev: { index: number }
) => void;
onTransitionStart?: (props: {
route: Route;
current: { index: number };
previous: { index: number };
}) => void;
onTransitionEnd?: (props: { route: Route }) => void;
onGestureBegin?: () => void;
onGestureCanceled?: () => void;
onGestureEnd?: () => void;
Expand All @@ -46,22 +48,34 @@ type Props = TransitionPreset & {
};
headerMode: HeaderMode;
headerTransparent?: boolean;
floaingHeaderHeight: number;
floatingHeaderHeight: number;
hasCustomHeader: boolean;
};

export default class StackItem extends React.PureComponent<Props> {
private handleOpen = () =>
this.props.onOpenRoute({ route: this.props.scene.route });
private handleOpen = () => {
const { scene, onTransitionEnd, onOpenRoute } = this.props;

private handleClose = () =>
this.props.onCloseRoute({ route: this.props.scene.route });
onTransitionEnd && onTransitionEnd({ route: scene.route });
onOpenRoute({ route: scene.route });
};

private handleClose = () => {
const { scene, onTransitionEnd, onCloseRoute } = this.props;

onTransitionEnd && onTransitionEnd({ route: scene.route });
onCloseRoute({ route: scene.route });
};

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

onTransitionStart &&
onTransitionStart({ index: closing ? index - 1 : index }, { index });
onTransitionStart({
route: scene.route,
previous: { index: closing ? index - 1 : index },
current: { index },
});

closing && onGoBack({ route: scene.route });
};
Expand All @@ -86,7 +100,7 @@ export default class StackItem extends React.PureComponent<Props> {
onGestureCanceled,
onGestureEnd,
gestureResponseDistance,
floaingHeaderHeight,
floatingHeaderHeight,
hasCustomHeader,
getPreviousRoute,
headerMode,
Expand Down Expand Up @@ -126,7 +140,7 @@ export default class StackItem extends React.PureComponent<Props> {
pointerEvents="box-none"
containerStyle={
headerMode === 'float' && !headerTransparent && !hasCustomHeader
? { marginTop: floaingHeaderHeight }
? { marginTop: floatingHeaderHeight }
: null
}
contentStyle={cardStyle}
Expand Down
4 changes: 2 additions & 2 deletions packages/stack/src/views/Stack/StackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Props = {
descriptors: Descriptors;
navigationConfig: NavigationConfig;
onTransitionStart?: (
curr: { index: number },
prev: { index: number }
current: { index: number },
previous: { index: number }
) => void;
onGestureBegin?: () => void;
onGestureCanceled?: () => void;
Expand Down

0 comments on commit 51b1069

Please sign in to comment.