Add options to opt-in/out of card overlay and shadow

This commit is contained in:
Brent Vatne 2018-10-11 17:04:23 -07:00
parent 8b7723d329
commit f3f46d0db4
5 changed files with 48 additions and 20 deletions

View File

@ -68,5 +68,9 @@ export default createStackNavigator(
}, },
{ {
initialRouteName: 'List', initialRouteName: 'List',
// these are the defaults
cardShadowEnabled: true,
cardOverlayEnabled: false,
} }
); );

View File

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { NativeModules } from 'react-native'; import { Platform, NativeModules } from 'react-native';
import { StackActions } from 'react-navigation'; import { StackActions } from 'react-navigation';
import StackViewLayout from './StackViewLayout'; import StackViewLayout from './StackViewLayout';
@ -9,13 +9,16 @@ import TransitionConfigs from './StackViewTransitionConfigs';
const NativeAnimatedModule = const NativeAnimatedModule =
NativeModules && NativeModules.NativeAnimatedModule; NativeModules && NativeModules.NativeAnimatedModule;
class StackView extends React.Component { // NOTE(brentvatne): this was previously in defaultProps, but that is deceiving
static defaultProps = { // because the entire object will be clobbered by navigationConfig that is
navigationConfig: { // passed in.
const DefaultNavigationConfig = {
mode: 'card', mode: 'card',
}, cardShadowEnabled: true,
}; cardcardOverlayEnabled: false,
};
class StackView extends React.Component {
render() { render() {
return ( return (
<Transitioner <Transitioner
@ -68,11 +71,27 @@ class StackView extends React.Component {
}; };
}; };
_getShadowEnabled = () => {
const { navigationConfig } = this.props;
return navigationConfig && navigationConfig.hasOwnProperty('cardShadowEnabled')
? navigationConfig.cardShadowEnabled
: DefaultNavigationConfig.cardShadowEnabled;
};
_getCardOverlayEnabled = () => {
const { navigationConfig } = this.props;
return navigationConfig && navigationConfig.hasOwnProperty('cardOverlayEnabled')
? navigationConfig.cardOverlayEnabled
: DefaultNavigationConfig.cardOverlayEnabled;
};
_render = (transitionProps, lastTransitionProps) => { _render = (transitionProps, lastTransitionProps) => {
const { screenProps, navigationConfig } = this.props; const { screenProps, navigationConfig } = this.props;
return ( return (
<StackViewLayout <StackViewLayout
{...navigationConfig} {...navigationConfig}
shadowEnabled={this._getShadowEnabled()}
cardOverlayEnabled={this._getCardOverlayEnabled()}
onGestureBegin={this.props.onGestureBegin} onGestureBegin={this.props.onGestureBegin}
onGestureCanceled={this.props.onGestureCanceled} onGestureCanceled={this.props.onGestureCanceled}
onGestureEnd={this.props.onGestureEnd} onGestureEnd={this.props.onGestureEnd}

View File

@ -714,6 +714,8 @@ class StackViewLayout extends React.Component {
screenInterpolator && screenInterpolator &&
screenInterpolator({ screenInterpolator({
...this.props.transitionProps, ...this.props.transitionProps,
shadowEnabled: this.props.shadowEnabled,
cardOverlayEnabled: this.props.cardOverlayEnabled,
position: this._getPosition(), position: this._getPosition(),
scene, scene,
}); });

View File

@ -59,19 +59,21 @@ function forHorizontal(props) {
extrapolate: 'clamp', extrapolate: 'clamp',
}); });
// TODO: add flag to disable shadow const shadowOpacity = props.shadowEnabled
const shadowOpacity = position.interpolate({ ? position.interpolate({
inputRange: [first, index, last], inputRange: [first, index, last],
outputRange: [0, 0.7, 0], outputRange: [0, 0.7, 0],
extrapolate: 'clamp', extrapolate: 'clamp',
}); })
: null;
// TODO: disable overlay by default, add flag to enable let overlayOpacity = props.cardOverlayEnabled
let overlayOpacity = position.interpolate({ ? position.interpolate({
inputRange: [index, last - 0.5, last, last + EPS], inputRange: [index, last - 0.5, last, last + EPS],
outputRange: [0, 0.07, 0.07, 0], outputRange: [0, 0.07, 0.07, 0],
extrapolate: 'clamp', extrapolate: 'clamp',
}); })
: null;
return { return {
transform: [{ translateX }], transform: [{ translateX }],

View File

@ -243,7 +243,7 @@ class Transitioner extends React.Component {
} }
function buildTransitionProps(props, state) { function buildTransitionProps(props, state) {
const { navigation } = props; const { navigation, options } = props;
const { layout, position, scenes } = state; const { layout, position, scenes } = state;
@ -257,6 +257,7 @@ function buildTransitionProps(props, state) {
position, position,
scenes, scenes,
scene, scene,
options,
index: scene.index, index: scene.index,
}; };
} }