Skip to content

Commit a2e7564

Browse files
committed
Update docs for transparent modals
1 parent b5f2137 commit a2e7564

File tree

1 file changed

+100
-34
lines changed

1 file changed

+100
-34
lines changed

versioned_docs/version-6.x/stack-navigator.md

Lines changed: 100 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,18 @@ You can also specify `{ backgroundColor: 'transparent' }` to make the previous s
9595

9696
This is shortcut option which configures several options to configure the style for rendering and transitions:
9797

98-
- `card` - Use the standard iOS and Android screen transitions. This is the default.
99-
- `modal` - This does few things:
98+
- `card`: Use the default OS animations for iOS and Android screen transitions.
99+
- `modal`: Use Modal animations. This changes a few things:
100100
- Sets `headerMode` to `screen` for the screen unless specified otherwise.
101101
- Changes the screen animation to match the platform behavior for modals.
102+
- `transparentModal`: Similar to `modal`. This changes following things:
103+
- Sets `headerMode` to `screen` for the screen unless specified otherwise.
104+
- Sets background color of the screen to transparent, so previous screen is visible
102105
- Adjusts the `detachPreviousScreen` option so that the previous screen stays rendered.
106+
- Prevents the previous screen from animating from its last position.
107+
- Changes the screen animation to a vertical slide animation.
108+
109+
See [Transparent modals](#transparent-modals) for more details on how to customize `transparentModal`.
103110

104111
#### `animationEnabled`
105112

@@ -761,43 +768,102 @@ import { TransitionPresets } from '@react-navigation/stack';
761768

762769
### Transparent modals
763770

764-
A transparent modal is like a modal dialog which overlays the screen. The previous screen still stays visible underneath. To get a transparent modal screen, it's usually easier to create a separate modal stack. In the modal stack, you will want to configure few things:
765-
766-
- Set the `presentation` prop to `modal` which sets `detachPreviousScreen` option to `false` for the last screen
767-
- Set the card background to transparent using `cardStyle`
768-
- Use a custom animation instead of the default platform animation (we'll use fade in this case)
769-
- Disable the header with `headerShown: false` (optional)
770-
- Enable the overlay with `cardOverlayEnabled: true` (optional)
771+
A transparent modal is like a modal dialog which overlays the screen. The previous screen still stays visible underneath. To get a transparent modal screen, you can specify `presentation: 'transparentModal'` in the screen's options.
771772

772773
Example:
773774

774775
```js
775-
<Stack.Navigator
776-
screenOptions={{
777-
headerShown: false,
778-
presentation: 'modal',
779-
cardStyle: { backgroundColor: 'transparent' },
780-
cardOverlayEnabled: true,
781-
cardStyleInterpolator: ({ current: { progress } }) => ({
782-
cardStyle: {
783-
opacity: progress.interpolate({
784-
inputRange: [0, 0.5, 0.9, 1],
785-
outputRange: [0, 0.25, 0.7, 1],
786-
}),
787-
},
788-
overlayStyle: {
789-
opacity: progress.interpolate({
790-
inputRange: [0, 1],
791-
outputRange: [0, 0.5],
792-
extrapolate: 'clamp',
793-
}),
794-
},
795-
}),
796-
}}
797-
>
776+
<Stack.Navigator>
798777
<Stack.Screen name="Home" component={HomeStack} />
799-
<Stack.Screen name="Modal" component={ModalScreen} />
778+
<Stack.Screen
779+
name="Modal"
780+
component={ModalScreen}
781+
options={{ presentation: 'transparentModal' }}
782+
/>
800783
</Stack.Navigator>
801784
```
802785

803-
Now, when you navigate to the `Modal` screen, the `Home` screen will still be visible underneath.
786+
Now, when you navigate to the `Modal` screen, it'll have a transparent background and the `Home` screen will be visible underneath.
787+
788+
In addition to `presentation`, you might want to optionally specify few more things to get a modal dialog like behavior:
789+
790+
- Disable the header with `headerShown: false`
791+
- Enable the overlay with `cardOverlayEnabled: true` (you can't tap the overlay to close the screen this way, see below for alternatives)
792+
793+
If you want to further customize how the dialog animates, or want to close the screen when tapping the overlay etc., you can use the `useCardAnimation` hook to customize elements inside your screen.
794+
795+
Example:
796+
797+
```js
798+
import {
799+
Animated,
800+
View,
801+
Text,
802+
Pressable,
803+
Button,
804+
StyleSheet,
805+
} from 'react-native';
806+
import { useTheme } from '@react-navigation/native';
807+
import { useCardAnimation } from '@react-navigation/stack';
808+
809+
function ModalScreen({ navigation }) {
810+
const { colors } = useTheme();
811+
const { current } = useCardAnimation();
812+
813+
return (
814+
<View
815+
style={{
816+
flex: 1,
817+
alignItems: 'center',
818+
justifyContent: 'center',
819+
}}
820+
>
821+
<Pressable
822+
style={[
823+
StyleSheet.absoluteFill,
824+
{ backgroundColor: 'rgba(0, 0, 0, 0.5)' },
825+
]}
826+
onPress={navigation.goBack}
827+
/>
828+
<Animated.View
829+
style={{
830+
padding: 16,
831+
width: '90%',
832+
maxWidth: 400,
833+
borderRadius: 3,
834+
backgroundColor: colors.card,
835+
transform: [
836+
{
837+
scale: current.progress.interpolate({
838+
inputRange: [0, 1],
839+
outputRange: [0.9, 1],
840+
extrapolate: 'clamp',
841+
}),
842+
},
843+
],
844+
}}
845+
>
846+
<Text>
847+
Mise en place is a French term that literally means “put in place.” It
848+
also refers to a way cooks in professional kitchens and restaurants
849+
set up their work stations—first by gathering all ingredients for a
850+
recipes, partially preparing them (like measuring out and chopping),
851+
and setting them all near each other. Setting up mise en place before
852+
cooking is another top tip for home cooks, as it seriously helps with
853+
organization. It’ll pretty much guarantee you never forget to add an
854+
ingredient and save you time from running back and forth from the
855+
pantry ten times.
856+
</Text>
857+
<Button
858+
title="Okay"
859+
color={colors.primary}
860+
style={{ alignSelf: 'flex-end' }}
861+
onPress={navigation.goBack}
862+
/>
863+
</Animated.View>
864+
</View>
865+
);
866+
}
867+
```
868+
869+
Here we animate the scale of the dialog, and also add an overlay to close the dialog.

0 commit comments

Comments
 (0)