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

Commit

Permalink
fix: improve gesture performance
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jan 1, 2020
1 parent 9356598 commit 59803f5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
20 changes: 17 additions & 3 deletions packages/stack/src/views/Stack/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ export default class Card extends React.Component<Props> {
if (
this.getAnimateToValue(this.props) !== this.getAnimateToValue(prevProps)
) {
// We need to trigger the animation when route was closed
// Thr route might have been closed by a `POP` action or by a gesture
// When route was closed due to a gesture, the animation would've happened already
// It's still important to trigger the animation so that `onClose` is called
// If `onClose` is not called, cleanup step won't be performed for gestures
this.animate({ closing });
}
}
Expand Down Expand Up @@ -132,7 +137,7 @@ export default class Card extends React.Component<Props> {
closing,
velocity,
}: {
closing: boolean | undefined;
closing?: boolean;
velocity?: number;
}) => {
const {
Expand All @@ -143,6 +148,11 @@ export default class Card extends React.Component<Props> {
onTransitionStart,
} = this.props;

const toValue = this.getAnimateToValue({
...this.props,
closing,
});

const spec = closing ? transitionSpec.close : transitionSpec.open;

const animation =
Expand All @@ -153,7 +163,7 @@ export default class Card extends React.Component<Props> {
...spec.config,
velocity,
useNativeDriver: true,
toValue: this.getAnimateToValue(this.props),
toValue,
}).start(({ finished }) => {
if (finished) {
if (closing) {
Expand All @@ -169,7 +179,11 @@ export default class Card extends React.Component<Props> {
closing,
layout,
gestureDirection,
}: Props) => {
}: {
closing?: boolean;
layout: Layout;
gestureDirection: GestureDirection;
}) => {
if (!closing) {
return 0;
}
Expand Down
3 changes: 0 additions & 3 deletions packages/stack/src/views/Stack/CardContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type Props = TransitionPreset & {
renderScene: (props: { route: Route<string> }) => React.ReactNode;
onOpenRoute: (props: { route: Route<string> }) => void;
onCloseRoute: (props: { route: Route<string> }) => void;
onGoBack: (props: { route: Route<string> }) => void;
onTransitionStart?: (
props: { route: Route<string> },
closing: boolean
Expand Down Expand Up @@ -73,7 +72,6 @@ export default function CardContainer({
index,
layout,
onCloseRoute,
onGoBack,
onOpenRoute,
onPageChangeCancel,
onPageChangeConfirm,
Expand Down Expand Up @@ -109,7 +107,6 @@ export default function CardContainer({
}

onTransitionStart?.({ route: scene.route }, closing);
closing && onGoBack({ route: scene.route });
};

const insets = {
Expand Down
3 changes: 0 additions & 3 deletions packages/stack/src/views/Stack/CardStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ type Props = {
routes: Route<string>[];
openingRouteKeys: string[];
closingRouteKeys: string[];
onGoBack: (props: { route: Route<string> }) => void;
onOpenRoute: (props: { route: Route<string> }) => void;
onCloseRoute: (props: { route: Route<string> }) => void;
getPreviousRoute: (props: {
Expand Down Expand Up @@ -360,7 +359,6 @@ export default class CardStack extends React.Component<Props, State> {
closingRouteKeys,
onOpenRoute,
onCloseRoute,
onGoBack,
getPreviousRoute,
getGesturesEnabled,
renderHeader,
Expand Down Expand Up @@ -527,7 +525,6 @@ export default class CardStack extends React.Component<Props, State> {
onCloseRoute={onCloseRoute}
onTransitionStart={onTransitionStart}
onTransitionEnd={onTransitionEnd}
onGoBack={onGoBack}
gestureEnabled={index !== 0 && getGesturesEnabled({ route })}
gestureVelocityImpact={gestureVelocityImpact}
gestureDirection={gestureDirection}
Expand Down
47 changes: 26 additions & 21 deletions packages/stack/src/views/Stack/StackView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,6 @@ class StackView extends React.Component<Props, State> {
return <HeaderContainer {...props} />;
};

private handleGoBack = ({ route }: { route: Route<string> }) => {
const { state, navigation } = this.props;

// This event will trigger when a gesture ends
// We need to perform the transition before removing the route completely
navigation.dispatch({
...StackActions.pop(),
source: route.key,
target: state.key,
});
};

private handleOpenRoute = ({ route }: { route: Route<string> }) => {
this.setState(state => ({
routes: state.replacingRouteKeys.length
Expand All @@ -290,15 +278,33 @@ class StackView extends React.Component<Props, State> {
};

private handleCloseRoute = ({ route }: { route: Route<string> }) => {
// This event will trigger when the animation for closing the route ends
// In this case, we need to clean up any state tracking the route and pop it immediately
const { state, navigation } = this.props;

// @ts-ignore
this.setState(state => ({
routes: state.routes.filter(r => r.key !== route.key),
openingRouteKeys: state.openingRouteKeys.filter(key => key !== route.key),
closingRouteKeys: state.closingRouteKeys.filter(key => key !== route.key),
}));
if (state.routes.find(r => r.key === route.key)) {
// If a route exists in state, trigger a pop
// This will happen in when the route was closed from the card component
// e.g. When the close animation triggered from a gesture ends
// For the cleanup, the card needs to call this function again from its componentDidUpdate
navigation.dispatch({
...StackActions.pop(),
source: route.key,
target: state.key,
});
} else {
// Otherwise, the animation was triggered due to a route removal
// In this case, we need to clean up any state tracking the route and pop it immediately

// @ts-ignore
this.setState(state => ({
routes: state.routes.filter(r => r.key !== route.key),
openingRouteKeys: state.openingRouteKeys.filter(
key => key !== route.key
),
closingRouteKeys: state.closingRouteKeys.filter(
key => key !== route.key
),
}));
}
};

private handleTransitionStart = (
Expand Down Expand Up @@ -355,7 +361,6 @@ class StackView extends React.Component<Props, State> {
routes={routes}
openingRouteKeys={openingRouteKeys}
closingRouteKeys={closingRouteKeys}
onGoBack={this.handleGoBack}
onOpenRoute={this.handleOpenRoute}
onCloseRoute={this.handleCloseRoute}
onTransitionStart={this.handleTransitionStart}
Expand Down

0 comments on commit 59803f5

Please sign in to comment.