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

Commit

Permalink
fix: use a separate shadow view for the cards
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Aug 18, 2019
1 parent 2abce43 commit d2397d5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function forHorizontalIOS({
})
: 0;

const opacity = interpolate(current, {
const overlayOpacity = interpolate(current, {
inputRange: [0, 1],
outputRange: [0, 0.07],
});
Expand All @@ -33,16 +33,15 @@ export function forHorizontalIOS({

return {
cardStyle: {
backgroundColor: '#eee',
transform: [
// Translation for the animation of the current card
{ translateX: translateFocused },
// Translation for the animation of the card on top of this
{ translateX: translateUnfocused },
],
shadowOpacity,
},
overlayStyle: { opacity },
overlayStyle: { opacity: overlayOpacity },
shadowStyle: { shadowOpacity },
};
}

Expand All @@ -60,7 +59,6 @@ export function forVerticalIOS({

return {
cardStyle: {
backgroundColor: '#eee',
transform: [
// Translation for the animation of the current card
{ translateY },
Expand Down
3 changes: 3 additions & 0 deletions packages/stack/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export type NavigationConfig = TransitionPreset & {
mode: 'card' | 'modal';
headerMode: HeaderMode;
headerBackTitleVisible?: boolean;
cardShadowEnabled?: boolean;
cardOverlayEnabled?: boolean;
transparentCard?: boolean;
};

Expand Down Expand Up @@ -170,6 +172,7 @@ export type CardInterpolatedStyle = {
containerStyle?: any;
cardStyle?: any;
overlayStyle?: any;
shadowStyle?: any;
};

export type CardStyleInterpolator = (
Expand Down
54 changes: 37 additions & 17 deletions packages/stack/src/views/Stack/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Props = ViewProps & {
onGestureCanceled?: () => void;
onGestureEnd?: () => void;
children: React.ReactNode;
overlayEnabled: boolean;
shadowEnabled: boolean;
gesturesEnabled: boolean;
gestureResponseDistance?: {
vertical?: number;
Expand Down Expand Up @@ -85,6 +87,8 @@ const {

export default class Card extends React.Component<Props> {
static defaultProps = {
overlayEnabled: true,
shadowEnabled: true,
gesturesEnabled: true,
};

Expand Down Expand Up @@ -414,6 +418,8 @@ export default class Card extends React.Component<Props> {
current,
next,
direction,
overlayEnabled,
shadowEnabled,
gesturesEnabled,
children,
styleInterpolator,
Expand All @@ -424,6 +430,7 @@ export default class Card extends React.Component<Props> {
containerStyle,
cardStyle,
overlayStyle,
shadowStyle,
} = this.getInterpolatedStyle(styleInterpolator, current, next, layout);

const handleGestureEvent =
Expand All @@ -435,7 +442,7 @@ export default class Card extends React.Component<Props> {
<StackGestureContext.Provider value={this.gestureRef}>
<View pointerEvents="box-none" {...rest}>
<Animated.Code exec={this.exec} />
{overlayStyle ? (
{overlayEnabled && overlayStyle ? (
<Animated.View
pointerEvents="none"
style={[styles.overlay, overlayStyle]}
Expand All @@ -452,14 +459,21 @@ export default class Card extends React.Component<Props> {
onHandlerStateChange={handleGestureEvent}
{...this.gestureActivationCriteria()}
>
<Animated.View
style={[
styles.card,
cardStyle,
transparent ? styles.transparent : null,
]}
>
{children}
<Animated.View style={[StyleSheet.absoluteFill, cardStyle]}>
{shadowEnabled && !transparent ? (
<Animated.View
style={[styles.shadow, shadowStyle]}
pointerEvents="none"
/>
) : null}
<View
style={[
StyleSheet.absoluteFill,
transparent ? styles.transparent : styles.opaque,
]}
>
{children}
</View>
</Animated.View>
</PanGestureHandler>
</Animated.View>
Expand All @@ -474,20 +488,26 @@ const styles = StyleSheet.create({
flex: 1,
overflow: 'hidden',
},
card: {
overlay: {
...StyleSheet.absoluteFillObject,
backgroundColor: '#000',
},
shadow: {
top: 0,
left: 0,
bottom: 0,
width: 3,
position: 'absolute',
backgroundColor: '#fff',
shadowOffset: { width: -1, height: 1 },
shadowRadius: 5,
shadowColor: '#000',
backgroundColor: 'white',
elevation: 2,
},
overlay: {
...StyleSheet.absoluteFillObject,
backgroundColor: '#000',
shadowOpacity: 1,
},
transparent: {
backgroundColor: 'transparent',
shadowOpacity: 0,
},
opaque: {
backgroundColor: '#eee',
},
});
13 changes: 12 additions & 1 deletion packages/stack/src/views/Stack/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Props = {
transparentCard?: boolean;
headerMode: HeaderMode;
direction: GestureDirection;
cardOverlayEnabled?: boolean;
cardShadowEnabled?: boolean;
onTransitionStart?: (
curr: { index: number },
prev: { index: number }
Expand Down Expand Up @@ -159,6 +161,8 @@ export default class Stack extends React.Component<Props, State> {
const {
descriptors,
navigation,
cardOverlayEnabled,
cardShadowEnabled,
routes,
closingRoutes,
onOpenRoute,
Expand Down Expand Up @@ -205,6 +209,8 @@ export default class Stack extends React.Component<Props, State> {
closing={closingRoutes.includes(route.key)}
onOpen={() => onOpenRoute({ route })}
onClose={() => onCloseRoute({ route })}
overlayEnabled={cardOverlayEnabled}
shadowEnabled={cardShadowEnabled}
gesturesEnabled={getGesturesEnabled({ route })}
onTransitionStart={({ closing }) => {
onTransitionStart &&
Expand Down Expand Up @@ -244,6 +250,7 @@ export default class Stack extends React.Component<Props, State> {
scenes={[scenes[index - 1], scenes[index]]}
navigation={navigation}
styleInterpolator={headerStyleInterpolator}
style={styles.header}
/>
) : null}
{renderScene({ route })}
Expand All @@ -259,7 +266,7 @@ export default class Stack extends React.Component<Props, State> {
navigation={navigation}
onLayout={this.handleFloatingHeaderLayout}
styleInterpolator={headerStyleInterpolator}
style={styles.header}
style={[styles.header, styles.floating]}
/>
) : null}
</React.Fragment>
Expand All @@ -273,6 +280,10 @@ const styles = StyleSheet.create({
overflow: 'hidden',
},
header: {
// This is needed to show elevation shadow
zIndex: 1,
},
floating: {
position: 'absolute',
top: 0,
left: 0,
Expand Down

0 comments on commit d2397d5

Please sign in to comment.