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

Commit e33892a

Browse files
chrfalchsatya164
authored andcommitted
feat: expose animation related properties in context (#278)
1 parent 07645f7 commit e33892a

File tree

7 files changed

+94
-61
lines changed

7 files changed

+94
-61
lines changed

example/src/StackAnimationConsumerStack.tsx

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import Animated from 'react-native-reanimated';
44
import {
55
createStackNavigator,
66
NavigationStackScreenProps,
7-
StackAnimationProgressContext,
8-
StackAnimationIsSwipingContext,
9-
StackAnimationIsClosingContext,
7+
StackCardAnimationContext,
108
} from 'react-navigation-stack';
119

1210
const ListScreen = (props: NavigationStackScreenProps) => (
@@ -27,12 +25,14 @@ const ListScreen = (props: NavigationStackScreenProps) => (
2725
);
2826

2927
const AnotherScreen = () => (
30-
<StackAnimationProgressContext.Consumer>
31-
{progress => {
32-
const fontSize = Animated.interpolate(progress, {
33-
inputRange: [0, 1],
34-
outputRange: [8, 32],
35-
});
28+
<StackCardAnimationContext.Consumer>
29+
{value => {
30+
const fontSize = value
31+
? Animated.interpolate(value.current.progress, {
32+
inputRange: [0, 1],
33+
outputRange: [8, 32],
34+
})
35+
: 32;
3636

3737
return (
3838
<View
@@ -43,13 +43,15 @@ const AnotherScreen = () => (
4343
backgroundColor: 'honeydew',
4444
}}
4545
>
46-
<Animated.Text style={{ fontSize, opacity: progress }}>
46+
<Animated.Text
47+
style={{ fontSize, opacity: value ? value.current.progress : 1 }}
48+
>
4749
Animates on progress
4850
</Animated.Text>
4951
</View>
5052
);
5153
}}
52-
</StackAnimationProgressContext.Consumer>
54+
</StackCardAnimationContext.Consumer>
5355
);
5456

5557
const YetAnotherScreen = () => (
@@ -61,36 +63,40 @@ const YetAnotherScreen = () => (
6163
backgroundColor: 'papayawhip',
6264
}}
6365
>
64-
<StackAnimationIsSwipingContext.Consumer>
65-
{isSwiping => (
66+
<StackCardAnimationContext.Consumer>
67+
{value => (
6668
<Animated.Text
6769
style={{
6870
fontSize: 24,
69-
opacity: Animated.interpolate(isSwiping, {
70-
inputRange: [0, 1],
71-
outputRange: [1, 0],
72-
}),
71+
opacity: value
72+
? Animated.interpolate(value.swiping, {
73+
inputRange: [0, 1],
74+
outputRange: [1, 0],
75+
})
76+
: 1,
7377
}}
7478
>
7579
Disappears when swiping
7680
</Animated.Text>
7781
)}
78-
</StackAnimationIsSwipingContext.Consumer>
79-
<StackAnimationIsClosingContext.Consumer>
80-
{isClosing => (
82+
</StackCardAnimationContext.Consumer>
83+
<StackCardAnimationContext.Consumer>
84+
{value => (
8185
<Animated.Text
8286
style={{
8387
fontSize: 24,
84-
opacity: Animated.interpolate(isClosing, {
85-
inputRange: [0, 1],
86-
outputRange: [1, 0],
87-
}),
88+
opacity: value
89+
? Animated.interpolate(value.closing, {
90+
inputRange: [0, 1],
91+
outputRange: [1, 0],
92+
})
93+
: 1,
8894
}}
8995
>
9096
Disappears only when closing
9197
</Animated.Text>
9298
)}
93-
</StackAnimationIsClosingContext.Consumer>
99+
</StackCardAnimationContext.Consumer>
94100
</View>
95101
);
96102

src/index.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,8 @@ export {
3737
*/
3838
export { default as StackGestureContext } from './utils/StackGestureContext';
3939
export {
40-
default as StackAnimationProgressContext,
41-
} from './utils/StackAnimationProgressContext';
42-
export {
43-
default as StackAnimationIsSwipingContext,
44-
} from './utils/StackAnimationIsSwipingContext';
45-
export {
46-
default as StackAnimationIsClosingContext,
47-
} from './utils/StackAnimationIsClosingContext';
40+
default as StackCardAnimationContext,
41+
} from './utils/StackCardAnimationContext';
4842

4943
/**
5044
* Types

src/utils/StackAnimationIsClosingContext.tsx

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/utils/StackAnimationIsSwipingContext.tsx

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/utils/StackAnimationProgressContext.tsx

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react';
2+
import Animated from 'react-native-reanimated';
3+
import { Layout } from '../types';
4+
5+
type StackCardAnimationContextType = {
6+
current: { progress: Animated.Node<number> };
7+
next?: { progress: Animated.Node<number> };
8+
index: number;
9+
closing: Animated.Node<0 | 1>;
10+
swiping: Animated.Node<0 | 1>;
11+
layouts: {
12+
screen: Layout;
13+
};
14+
insets: {
15+
top: number;
16+
right: number;
17+
bottom: number;
18+
left: number;
19+
};
20+
};
21+
22+
export default React.createContext<StackCardAnimationContextType | undefined>(
23+
undefined
24+
);

src/views/Stack/Card.tsx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ import {
2525
import memoize from '../../utils/memoize';
2626
import StackGestureContext from '../../utils/StackGestureContext';
2727
import PointerEventsView from './PointerEventsView';
28-
import StackAnimationProgressContext from '../../utils/StackAnimationProgressContext';
29-
import StackAnimationIsSwipingContext from '../../utils/StackAnimationIsSwipingContext';
30-
import StackAnimationIsClosingContext from '../../utils/StackAnimationIsClosingContext';
28+
import StackCardAnimationContext from '../../utils/StackCardAnimationContext';
3129

3230
type Props = ViewProps & {
3331
index: number;
@@ -775,6 +773,29 @@ export default class Card extends React.Component<Props> {
775773
this.props.insets.left
776774
);
777775

776+
// Keep track of the animation context when deps changes.
777+
private getCardAnimationContext = memoize(
778+
(
779+
index: number,
780+
current: Animated.Node<number>,
781+
next: Animated.Node<number> | undefined,
782+
isSwiping: Animated.Node<0 | 1>,
783+
isClosing: Animated.Node<0 | 1>,
784+
layout: Layout,
785+
insets: EdgeInsets
786+
) => ({
787+
index,
788+
current: { progress: current },
789+
next: next && { progress: next },
790+
closing: isClosing,
791+
swiping: isSwiping,
792+
layouts: {
793+
screen: layout,
794+
},
795+
insets,
796+
})
797+
);
798+
778799
private gestureActivationCriteria() {
779800
const { layout, gestureDirection, gestureResponseDistance } = this.props;
780801

@@ -926,17 +947,19 @@ export default class Card extends React.Component<Props> {
926947
contentStyle,
927948
]}
928949
>
929-
<StackAnimationProgressContext.Provider value={current}>
930-
<StackAnimationIsSwipingContext.Provider
931-
value={this.isSwiping}
932-
>
933-
<StackAnimationIsClosingContext.Provider
934-
value={this.isClosing}
935-
>
936-
{children}
937-
</StackAnimationIsClosingContext.Provider>
938-
</StackAnimationIsSwipingContext.Provider>
939-
</StackAnimationProgressContext.Provider>
950+
<StackCardAnimationContext.Provider
951+
value={this.getCardAnimationContext(
952+
index,
953+
current,
954+
next,
955+
this.isClosing,
956+
this.isSwiping,
957+
this.props.layout,
958+
this.props.insets
959+
)}
960+
>
961+
{children}
962+
</StackCardAnimationContext.Provider>
940963
</PointerEventsView>
941964
</Animated.View>
942965
</PanGestureHandler>

0 commit comments

Comments
 (0)