Back to React Native

Modal

website/versioned_docs/version-0.83/modal.md

latest8.2 KB
Original Source

The Modal component is a basic way to present content above an enclosing view.

Example

SnackPlayer
import React, {useState} from 'react';
import {Alert, Modal, StyleSheet, Text, Pressable, View} from 'react-native';
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';

const App = () => {
  const [modalVisible, setModalVisible] = useState(false);
  return (
    <SafeAreaProvider>
      <SafeAreaView style={styles.centeredView}>
        <Modal
          animationType="slide"
          transparent={true}
          visible={modalVisible}
          onRequestClose={() => {
            Alert.alert('Modal has been closed.');
            setModalVisible(!modalVisible);
          }}>
          <View style={styles.centeredView}>
            <View style={styles.modalView}>
              <Text style={styles.modalText}>Hello World!</Text>
              <Pressable
                style={[styles.button, styles.buttonClose]}
                onPress={() => setModalVisible(!modalVisible)}>
                <Text style={styles.textStyle}>Hide Modal</Text>
              </Pressable>
            </View>
          </View>
        </Modal>
        <Pressable
          style={[styles.button, styles.buttonOpen]}
          onPress={() => setModalVisible(true)}>
          <Text style={styles.textStyle}>Show Modal</Text>
        </Pressable>
      </SafeAreaView>
    </SafeAreaProvider>
  );
};

const styles = StyleSheet.create({
  centeredView: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  modalView: {
    margin: 20,
    backgroundColor: 'white',
    borderRadius: 20,
    padding: 35,
    alignItems: 'center',
    shadowColor: '#000',
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 4,
    elevation: 5,
  },
  button: {
    borderRadius: 20,
    padding: 10,
    elevation: 2,
  },
  buttonOpen: {
    backgroundColor: '#F194FF',
  },
  buttonClose: {
    backgroundColor: '#2196F3',
  },
  textStyle: {
    color: 'white',
    fontWeight: 'bold',
    textAlign: 'center',
  },
  modalText: {
    marginBottom: 15,
    textAlign: 'center',
  },
});

export default App;

Reference

Props

View Props

Inherits View Props.


🗑️ animated

:::warning Deprecated Use the animationType prop instead. :::


animationType

The animationType prop controls how the modal animates.

Possible values:

  • slide slides in from the bottom
  • fade fades into view
  • none appears without an animation
TypeDefault
enum('none', 'slide', 'fade')none

backdropColor

The backdropColor of the modal (or background color of the modal's container.) Defaults to white if not provided and transparent is false. Ignored if transparent is true.

TypeDefault
colorwhite

hardwareAccelerated <div className="label android">Android</div>

The hardwareAccelerated prop controls whether to force hardware acceleration for the underlying window.

TypeDefault
boolfalse

The navigationBarTranslucent prop determines whether your modal should go under the system navigation bar. However, statusBarTranslucent also needs to be set to true to make navigation bar translucent.

TypeDefault
boolfalse

onDismiss <div className="label ios">iOS</div>

The onDismiss prop allows passing a function that will be called once the modal has been dismissed.

Type
function

onOrientationChange <div className="label ios">iOS</div>

The onOrientationChange callback is called when the orientation changes while the modal is being displayed. The orientation provided is only 'portrait' or 'landscape'. This callback is also called on initial render, regardless of the current orientation.

Type
function

allowSwipeDismissal <div className="label ios">iOS</div>

Controls whether the modal can be dismissed by swiping down on iOS. This requires you to implement the onRequestClose prop to handle the dismissal.

TypeDefault
boolfalse

ref

A ref setter that will be assigned an element node when mounted.


onRequestClose

The onRequestClose callback is called when the user taps the hardware back button on Android or the menu button on Apple TV. Because of this required prop, be aware that BackHandler events will not be emitted as long as the modal is open. On iOS, this callback is called when a Modal is being dismissed using a drag gesture when presentationStyle is pageSheet or formSheet. When allowSwipeDismissal is enabled this callback will be called after dismissing the modal.

Type
function <div className="label basic required">Required</div><div className="label android">Android</div><div className="label tv">TV</div><hr />function <div className="label ios">iOS</div>

onShow

The onShow prop allows passing a function that will be called once the modal has been shown.

Type
function

presentationStyle <div className="label ios">iOS</div>

The presentationStyle prop controls how the modal appears (generally on larger devices such as iPad or plus-sized iPhones). See https://developer.apple.com/reference/uikit/uimodalpresentationstyle for details.

Possible values:

  • fullScreen covers the screen completely
  • pageSheet covers portrait-width view centered (only on larger devices)
  • formSheet covers narrow-width view centered (only on larger devices)
  • overFullScreen covers the screen completely, but allows transparency
TypeDefault
enum('fullScreen', 'pageSheet', 'formSheet', 'overFullScreen')fullScreen if transparent={false}<hr />overFullScreen if transparent={true}

statusBarTranslucent <div className="label android">Android</div>

The statusBarTranslucent prop determines whether your modal should go under the system statusbar.

TypeDefault
boolfalse

supportedOrientations <div className="label ios">iOS</div>

The supportedOrientations prop allows the modal to be rotated to any of the specified orientations. On iOS, the modal is still restricted by what's specified in your app's Info.plist's UISupportedInterfaceOrientations field.

:::note When using presentationStyle of pageSheet or formSheet, this property will be ignored on iOS. :::

TypeDefault
array of enums('portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right')['portrait']

transparent

The transparent prop determines whether your modal will fill the entire view. Setting this to true will render the modal over a transparent background.

TypeDefault
boolfalse

visible

The visible prop determines whether your modal is visible.

TypeDefault
booltrue