Modals to the rescue for boring products! In this article I will show you how to create awesome, cool looking and functional modals in react-native with one of the most popular library, which is react-native-modal!
The quality of an application or a website depends on the time that users spend in it. Creators do their best to make the best products that, will engage users and not bore them to death. But nowadays simple navigating between pages/screens or scrolling is not enough. The users need to feel alive every single second. What is one of the best tools to engage them more? The answer is modals - popup dialog windows for informing, warning, or stimulating user action. The majority of the best products use them, and the rest should start doing so today.
In this article, I will present examples of modals usage in the world of React-Native. RN has a built-in modal component, but there are many noteworthy libraries, which enhance their capabilities. One of the most popular is the react-native-modal, and that’s not without a reason! I will show you how to use this library to create interesting and quite simple solutions, which will definitely find a place in your own project.
Below is a little sneak peek of the modals we will create in this project.
Project Setup
First of all, we need to clone the project repository. The finished project is on the master branch, but I encourage you to use the starter branch, which I’ve created for you to code by yourself.
Run the project on a simulator or a physical device:
yarn ios
// or
yarn android
Playground Intro
As you probably saw in the sneak peek, I created a transparent playground for this project. It contains a bottom bar navigation, which directs you to four different screens. Every single screen has custom buttons, which will be used for firing up the modals. Three screens - Slide, Fade, Rotate will give you possibilities of making cool animated modals which you will find in the most popular products. The last screen - Utils is for modals with extra functionalities, which you can implement wherever you need, and will definitely take your project to the next level! That’s all waiting for you in the starter branch. No more time to waste, let’s jump into the world of modals!
Library Intro
In this chapter, I will present tools from the react-native-modal library. It’s fully functional and basically all you need to create great modals.
Props
To get full control of the created modal, you need to pass certain props. Below is a list of props used in this project. Please check it out to feel more comfortable in the next chapters:
animationIn - show animation type,
animationInTiming - show animation duration,
animationOut - hide animation type,
animationOutTiming - hide animation duration,
backdropOpacity - backdrop opacity for visible modal,
backdropTransitionInTiming - show backdrop transition duration,
onBackButtonPress - function called on Android back button press,
onBackdropPress - function called on backdrop press,
onSwipeComplete - function called when swipe is complete,
swipeDirection - direction where modal can be swiped.
Animations
Apart from styling, you can get most of the user impressions with implementing cool animations. As you probably saw, we can define certain animation types for showing and hiding the modal. Below is a short list of some animations which you can implement directly from this library:
slideInUp - slide in from bottom to top,
slideOutDown- slide out from top to bottom,
fadeInUp- fade in with slide from bottom to top,
fadeOut - fade out,
shake - show in with shake vibrations,
rotate - rotation before show or hide.
These are only a few from a huge list of built-in library animations. Feel free to check them out in the library documentation.
Modal
The most important component we need to create is a modal. It must be reusable because we want to use this component repeatedly in different forms.
Customizable component
Let’s build the modal structure. We can import its container from the react-native-modal library, as well as its Props, which will help us create a generic and fully customizable component.
As for the props, we pass:
hideModal - function closing the modal,
hideCloseButton - boolean to hide close modal button,
style - styles of the modal.
import React from 'react';
import {View, StyleSheet, StyleProp, ViewStyle, Text} from 'react-native';
import {theme} from '../../theme/theme';
import Modal, {ModalProps} from 'react-native-modal';
import ButtonComponent from '../Button/ButtonComponent';
interface Props {
hideModal: () => void;
hideCloseButton?: boolean;
style?: StyleProp<ViewStyle>;
}
// ModalComponent.tsx
The component body needs to be wrapped with the Modal, which takes the rest of ...props - this is where we pass library-specific props. Inside we nest a View container, which takes style props from a parent. With the hideCloseButton prop you can define if the close button is needed or not. It’s not necessary, because Modal with proper props will hide after a click on its backdrop. If you don’t find any method satisfactory, the modal can hide even on swipe, which we will implement later. Inside you can put whatever you want to, and if you want to pass dynamic content, just use children. We also add some specified text, to show it in every modal component.
Modal implementation into the screen is a piece of cake. We need to import the recently created modal component and add it with proper props. The most significant prop is the isVisible, it’s a boolean which tells the modal to show or hide. The best way to control it is by using a boolean variable from the useState hook - in the below code, it’s a showFirstModal. If it’s true, the modal will show, otherwise it will hide. We have a button, which will set it to true on click. Whenever we want to hide the modal, we just need to change showFirstModal to false. That’s the reason why we pass in props onBackdropPress and onBackButtonPress functions which sets it to false. As their names suggest, these props will do some action when clicking outside of the modal or on the back button, which is closing the modal in our case. This is a regular approach to close any modals.
To create awesome modals we need to combine props and animations mentioned in the previous chapter. I will show you some examples of my implementations. They can be easily modified, so feel free to experiment with your own ideas!
Modals are not only used to show some information to users, they can be more functional. In this chapter, I will present two practical uses.
Confirmation wrapper
First comes the confirmation wrapper modal. It pops up to ask the user for confirmation. You can wrap it on any button and pass in any action which will be triggered only after confirmation. In fact, you can use it anywhere.
Component
The component itself is mainly a View container with its own modal nested inside, which is controlled by a boolean state. The confirmation button in this modal should trigger a function passed to this wrapper. Cancel just hides the modal. The most important thing is to use React.cloneElement on children (button passed in), to add the onClick action, which will show the modal on button press.
To implement a confirmation wrapper you just need to wrap it on any button and pass onConfirm with the action it should trigger. As simple as that. There is no need to add a new state to control the modal, everything is hidden inside.
The second example is a swipe picker modal. It is made to let the user swipe it to left or right. Different swipe directions will trigger different actions, which you can define as you want.
Component
In this component, we need to know which side the user picks. We save it in the state as 'left' or 'right'. To check swipe directions we can use onLayout handler, which is a standard react-native View component prop. It gives us an event which is triggered when a component layout has been calculated. With it, we can define horizontal position x, and set a proper direction in the state. The last thing is to trigger a proper function with onSwipeComplete props.
As for the implementation, we use it as a normal modal controlled with the boolean state. One thing we need to do is pass our functions as swipeLeftAction and swipeRightAction and start swiping!
As you can see there are plenty of possibilities to create interesting modals. Not only cool looking and animated, but also fully functional ones. This project is a playground, and you play with however you want! I encourage you to change the styling, modify the animations or add your own functionalities. The created components are ready to put into your own projects. Just fill them with your own ideas.
Thank you for reading. I hope that you enjoyed it and found something interesting to use in your own projects.
Oops! Something went wrong while submitting the form.
By clicking “Accept all”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.