Done!

create modal with react native reanimated animation slide with spring mass

Technology
Engineering
Physics
Computer Science

Progress

6 steps completed!15.87 sec

Answer

To create a modal with a slide-in animation using React Native Reanimated and a spring mass effect, you'll need to follow these steps. This example assumes you're using React Native Reanimated 2.x.

  1. Install Dependencies: Make sure you have the necessary dependencies installed. Run the following command to install react-native-reanimated if you haven't done so yet:

    npm install react-native-reanimated
  2. Setup Reanimated: Ensure that you follow any platform-specific installation steps mentioned in the Reanimated documentation.

  3. Implement the Modal: Below is a complete example code to create a modal that slides in from the bottom using spring animation.

import React, { useState } from 'react'; import { View, Text, Button, Modal, StyleSheet, TouchableOpacity } from 'react-native'; import Animated, { useSharedValue, useAnimatedStyle, withSpring } from 'react-native-reanimated'; const App = () => { const [isVisible, setIsVisible] = useState(false); const translateY = useSharedValue(1000); // Offscreen position const showModal = () => { setIsVisible(true); translateY.value = withSpring(0, { mass: 0.5, tension: 100, friction: 10 }); // Spring animation }; const hideModal = () => { translateY.value = withSpring(1000, { mass: 0.5, tension: 100, friction: 10 }, () => { setIsVisible(false); }); }; const animatedStyle = useAnimatedStyle(() => { return { transform: [{ translateY: translateY.value }], }; }); return ( <View style={styles.container}> <Button title="Show Modal" onPress={showModal} /> <Modal transparent visible={isVisible} animationType="none" // Disable default animation onRequestClose={hideModal} // Handle back button on Android > <TouchableOpacity style={styles.overlay} onPress={hideModal}> <Animated.View style={[styles.modalContainer, animatedStyle]}> <Text style={styles.modalText}>This is a modal!</Text> <Button title="Close" onPress={hideModal} /> </Animated.View> </TouchableOpacity> </Modal> </View> ); }; const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', }, overlay: { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.5)', justifyContent: 'flex-end', }, modalContainer: { backgroundColor: 'white', padding: 20, borderTopLeftRadius: 20, borderTopRightRadius: 20, }, modalText: { fontSize: 18, marginBottom: 10, }, }); export default App;

Explanation:

  • State Management: We manage the modal's visibility with a state variable (isVisible).
  • Animated Values: We use useSharedValue to hold the vertical translation value (translateY).
  • Animation Trigger: The showModal function sets the translation to animate it into view, while hideModal animates it out and then hides the modal.
  • Animated Style: We create an animated style that updates based on the translateY value.
  • Modal Overlay: The modal can be dismissed by tapping outside of it (the overlay).

Remember:

  • Always run your project on a real device or an emulator that supports React Native Reanimated features.
  • Customize the styles and transition settings as per your design needs.