Skip to content

Commit

Permalink
fix: make transparent modal work with modal presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed May 24, 2021
1 parent b590337 commit c90bff0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
41 changes: 41 additions & 0 deletions example/src/Screens/StackTransparent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import {
useCardAnimation,
} from '@react-navigation/stack';
import Article from '../Shared/Article';
import NewsFeed from '../Shared/NewsFeed';

type TransparentStackParams = {
Article: { author: string };
NewsFeed: undefined;
Dialog: undefined;
};

Expand All @@ -37,6 +39,13 @@ const ArticleScreen = ({
>
Show Dialog
</Button>
<Button
mode="contained"
onPress={() => navigation.push('NewsFeed')}
style={styles.button}
>
Push NewsFeed
</Button>
<Button
mode="outlined"
onPress={() => navigation.goBack()}
Expand All @@ -53,6 +62,32 @@ const ArticleScreen = ({
);
};

const NewsFeedScreen = ({
navigation,
}: StackScreenProps<TransparentStackParams, 'NewsFeed'>) => {
return (
<ScrollView>
<View style={styles.buttons}>
<Button
mode="contained"
onPress={() => navigation.push('Dialog')}
style={styles.button}
>
Show Dialog
</Button>
<Button
mode="outlined"
onPress={() => navigation.goBack()}
style={styles.button}
>
Go back
</Button>
</View>
<NewsFeed scrollEnabled={scrollEnabled} />
</ScrollView>
);
};

const DialogScreen = ({
navigation,
}: StackScreenProps<TransparentStackParams>) => {
Expand Down Expand Up @@ -116,6 +151,11 @@ export default function TransparentStackScreen({ navigation }: Props) {
component={ArticleScreen}
initialParams={{ author: 'Gandalf' }}
/>
<TransparentStack.Screen
name="NewsFeed"
component={NewsFeedScreen}
options={{ presentation: 'modal' }}
/>
<TransparentStack.Screen
name="Dialog"
component={DialogScreen}
Expand All @@ -131,6 +171,7 @@ export default function TransparentStackScreen({ navigation }: Props) {
const styles = StyleSheet.create({
buttons: {
flexDirection: 'row',
flexWrap: 'wrap',
padding: 8,
},
button: {
Expand Down
2 changes: 1 addition & 1 deletion packages/stack/src/views/Stack/CardContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function CardContainer({
insets={insets}
gesture={gesture}
current={scene.progress.current}
next={isNextScreenTransparent ? undefined : scene.progress.next}
next={scene.progress.next}
closing={closing}
onOpen={handleOpen}
onClose={handleClose}
Expand Down
32 changes: 21 additions & 11 deletions packages/stack/src/views/Stack/CardStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ export default class CardStack extends React.Component<Props, State> {
// This will still be broken when 2 transitions have different idle state (e.g. modal presentation),
// but majority of the transitions look alright
const optionsForTransitionConfig =
index !== self.length - 1 && nextDescriptor
index !== self.length - 1 &&
nextDescriptor &&
nextDescriptor.options.presentation !== 'transparentModal'
? nextDescriptor.options
: descriptor.options;

Expand Down Expand Up @@ -251,6 +253,8 @@ export default class CardStack extends React.Component<Props, State> {
(!(
optionsForTransitionConfig.presentation === 'modal' ||
optionsForTransitionConfig.presentation === 'transparentModal' ||
nextDescriptor?.options.presentation === 'modal' ||
nextDescriptor?.options.presentation === 'transparentModal' ||
cardStyleInterpolator === forModalPresentationIOS
) &&
Platform.OS === 'ios' &&
Expand Down Expand Up @@ -280,13 +284,15 @@ export default class CardStack extends React.Component<Props, State> {
state.layout,
descriptor
),
next: nextGesture
? getProgressFromGesture(
nextGesture,
state.layout,
nextDescriptor
)
: undefined,
next:
nextGesture &&
nextDescriptor.options.presentation !== 'transparentModal'
? getProgressFromGesture(
nextGesture,
state.layout,
nextDescriptor
)
: undefined,
previous: previousGesture
? getProgressFromGesture(
previousGesture,
Expand Down Expand Up @@ -466,9 +472,13 @@ export default class CardStack extends React.Component<Props, State> {
const { options } = scenes[i].descriptor;
const {
// By default, we don't want to detach the previous screen of the active one for modals
detachPreviousScreen = options.presentation === 'transparentModal' ||
options.cardStyleInterpolator === forModalPresentationIOS
? i !== scenes.length - 1
detachPreviousScreen = options.presentation === 'transparentModal'
? false
: options.cardStyleInterpolator === forModalPresentationIOS
? i !==
scenes
.map((scene) => scene.descriptor.options.cardStyleInterpolator)
.lastIndexOf(forModalPresentationIOS)
: true,
} = options;

Expand Down

0 comments on commit c90bff0

Please sign in to comment.