Cross-platform React Native modal components and utilities for building flexible dialogs, bottom sheets, and animated modals on iOS and Android.
Maintained and enhanced by Harold - @phattran1201 👨💻
- Declarative
Modalcomponent with customizable title, content, footer and animations - Bottom sheet-style
BottomModal - Imperative
ModalPortalAPI for showing/updating/dismissing modals from anywhere in your app - Built-in animations:
FadeAnimation,ScaleAnimation,SlideAnimationand the baseAnimationclass for custom animations - Backdrop control and swipe-to-dismiss support
- TypeScript types included
Install the published package (scoped):
npm install --save @haroldtran/react-native-modals
# or
yarn add @haroldtran/react-native-modalsPeer dependencies: react, react-native
The library exposes an imperative portal that must be mounted near the root of your app. Add ModalPortal to your app root so the portal can render modals:
import React from 'react';
import { ModalPortal } from '@haroldtran/react-native-modals';
export default function Root({ children }) {
return (
<>
{children}
<ModalPortal />
</>
);
}If you use Redux or other providers, mount ModalPortal alongside them.
import React, { useState } from 'react';
import { View, Button, Text } from 'react-native';
import { Modal, ModalContent } from '@haroldtran/react-native-modals';
export default function Example() {
const [visible, setVisible] = useState(false);
return (
<View>
<Button title="Show Modal" onPress={() => setVisible(true)} />
<Modal
visible={visible}
onTouchOutside={() => setVisible(false)}
>
<ModalContent>
<Text>Hello from the modal</Text>
</ModalContent>
</Modal>
</View>
);
}Use the ModalPortal to show modals programmatically from anywhere in your app. The portal returns an id which you can use to update or dismiss that modal.
import { ModalPortal } from '@haroldtran/react-native-modals';
// Show a modal and keep the returned id
const id = ModalPortal.show(
<View>
<Text>Imperative modal</Text>
</View>
);
// Update the modal content later
ModalPortal.update(id, {
children: (
<View>
<Text>Updated</Text>
</View>
),
});
// Dismiss a specific modal (plays out-animation and hides)
ModalPortal.dismiss(id);
// Dismiss all open modals
ModalPortal.dismissAll();
// Forcefully unmount a modal from memory (useful if using destroyOnDismiss: false)
ModalPortal.destroy(id);
// Forcefully unmount all modals from memory
ModalPortal.destroyAll();The library includes a base Animation class and several concrete implementations:
FadeAnimation— fade in/outScaleAnimation— scale from/to a valueSlideAnimation— slide fromtop,bottom,leftorright
Example: passing a SlideAnimation to a Modal
import { Modal, SlideAnimation } from '@haroldtran/react-native-modals';
<Modal
visible={visible}
modalAnimation={new SlideAnimation({ slideFrom: 'bottom' })}
>
<ModalContent />
</Modal>Create a custom animation by extending Animation and overriding in, out and getAnimations().
The package exports the following components and TypeScript types:
- Modal (default export)
- BottomModal
- ModalPortal
- Backdrop
- ModalButton
- ModalContent
- ModalTitle
- ModalFooter
- Animation, FadeAnimation, ScaleAnimation, SlideAnimation
Types:
- DragEvent, SwipeDirection, ModalProps, ModalFooterProps, ModalButtonProps, ModalTitleProps, ModalContentProps, BackdropProps
For more details see the src folder and the types in src/type.ts.
- The
ModalPortalmust be mounted for the imperative APIs to work. Modalsupports swipe-to-dismiss and provides callbacks for swipe move, release and completed swipe events.- The modal backdrop, overlay color and opacity are configurable via props.
|
Harold Tran |