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

Commit ff63ea8

Browse files
authored
Merge pull request #2 from fram-x/Feature/AddSupportForRenderFunctions
Add support for configurable render container and screen functions
2 parents f573d9d + 5653b78 commit ff63ea8

2 files changed

Lines changed: 45 additions & 22 deletions

File tree

src/Transitioner.js

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,31 @@ const defaultCreateTransition = transition => {
3737

3838
const defaultRunTransition = () => {};
3939

40+
const defaultRenderScreen = (
41+
ScreenComponent, transition, transitions, transitioningFromState,
42+
transitioningToState, transitionRouteKey, navigation, ref, behindScreenStyles,
43+
) => (
44+
<Animated.View
45+
style={[{ ...StyleSheet.absoluteFillObject }, behindScreenStyles]}
46+
pointerEvents={'auto'}
47+
>
48+
<ScreenComponent
49+
transition={transition}
50+
transitions={transitions}
51+
transitioningFromState={transitioningFromState}
52+
transitioningToState={transitioningToState}
53+
transitionRouteKey={transitionRouteKey}
54+
navigation={navigation}
55+
transitionRef={ref}
56+
/>
57+
</Animated.View>
58+
);
59+
60+
const defaultRenderContainer = (transitionRouteKey, transitions, navigation,
61+
transitioningFromState, transitioningToState, transitionRefs, children) => (
62+
<React.Fragment>{children}</React.Fragment>
63+
);
64+
4065
const getStateForNavChange = (props, state) => {
4166
// by this point we know the nav state has changed and it is safe to provide a new state. static
4267
// getDerivedStateFromProps will never interrupt a transition (when there is state.transitionRouteKey),
@@ -208,6 +233,7 @@ export class Transitioner extends React.Component {
208233
navState,
209234
descriptors,
210235
} = this.state;
236+
const { navigation } = this.props;
211237
const mainRouteKeys = navState.routes.map(r => r.key);
212238
let routeKeys = mainRouteKeys;
213239

@@ -219,9 +245,15 @@ export class Transitioner extends React.Component {
219245
}
220246
}
221247

248+
// Use render container function from last route descriptor
249+
const renderContainerFunc = descriptors[transitionRouteKey].options.renderContainer
250+
|| defaultRenderContainer;
251+
222252
return (
223253
<TransitionContext.Provider value={this._transitionContext}>
224-
{routeKeys.map((key, index) => {
254+
{renderContainerFunc(transitionRouteKey, transitions, navigation,
255+
transitioningFromState, transitionRouteKey ? navigation.state : null,
256+
routeKeys.map((key, index) => {
225257
const ref =
226258
this._transitionRefs[key] ||
227259
(this._transitionRefs[key] = React.createRef());
@@ -246,34 +278,25 @@ export class Transitioner extends React.Component {
246278
return options.getBehindTransitionAnimatedStyle(aboveTransition);
247279
}
248280
);
281+
249282
let transition = transitions[key];
250283
if (behindScreenStyles.length === 0) {
251284
// bizarre react-native bug that refuses to clear Animated.View styles unless you do something like this..
252285
// to reproduce the problem, set a getBehindTransitionAnimatedStyle that puts opacity at 0.5
253286
behindScreenStyles = [{ opacity: 1 }];
254287
}
288+
289+
const renderFunc = descriptor.options.renderScreen || defaultRenderScreen;
290+
255291
return (
256-
<Animated.View
257-
style={[{ ...StyleSheet.absoluteFillObject }, behindScreenStyles]}
258-
pointerEvents={'auto'}
259-
key={key}
260-
>
261-
<NavigationProvider value={descriptor.navigation}>
262-
<C
263-
transition={transition}
264-
transitions={transitions}
265-
transitioningFromState={transitioningFromState}
266-
transitioningToState={
267-
transitionRouteKey ? this.props.navigation.state : null
268-
}
269-
transitionRouteKey={transitionRouteKey}
270-
navigation={descriptor.navigation}
271-
transitionRef={ref}
272-
/>
273-
</NavigationProvider>
274-
</Animated.View>
292+
<NavigationProvider key={key} value={descriptor.navigation}>
293+
{renderFunc(C, transition, transitions, transitioningFromState,
294+
transitionRouteKey ? navigation.state : null,
295+
transitionRouteKey, descriptor.navigation, ref,
296+
behindScreenStyles, key)}
297+
</NavigationProvider>
275298
);
276-
})}
299+
}))}
277300
</TransitionContext.Provider>
278301
);
279302
}

src/createStackTransitionNavigator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import Transitioner from './Transitioner';
44
export default function createStackTransitionNavigator(routeConfigs, options) {
55
const router = StackRouter(routeConfigs, options);
66

7-
return createNavigator(Transitioner, router);
7+
return createNavigator(Transitioner, router, options);
88
}

0 commit comments

Comments
 (0)