This is an illustration of Modal component.
function App() {
const [showModal, setShowModal] = useState(false)
console.log(showModal)
const ref = React.useRef(null)
return (
<Center className="h-[300px]">
<Button onPress={() => setShowModal(true)} ref={ref}>
<ButtonText>Show Modal</ButtonText>
</Button>
<Modal
isOpen={showModal}
onClose={() => {
setShowModal(false)
}}
finalFocusRef={ref}
>
<ModalBackdrop />
<ModalContent>
<ModalHeader>
<Heading size="lg">Engage with Modals</Heading>
<ModalCloseButton>
<Icon as={CloseIcon} />
</ModalCloseButton>
</ModalHeader>
<ModalBody>
<Text>
Elevate user interactions with our versatile modals. Seamlessly
integrate notifications, forms, and media displays. Make an impact
effortlessly.
</Text>
</ModalBody>
<ModalFooter>
<Button
variant="outline"
size="sm"
action="secondary"
className="mr-[3px]"
onPress={() => {
setShowModal(false)
}}
>
<ButtonText>Cancel</ButtonText>
</Button>
<Button
size="sm"
action="positive"
onPress={() => {
setShowModal(false)
}}
>
<ButtonText>Explore</ButtonText>
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</Center>
)
}

Installation

Step 1: Install the following dependencies:

npm i @gluestack-ui/modal

Step 2: Copy and paste the following code into your project.

import React, { useEffect } from "react"
import { createModal } from "@gluestack-ui/modal"
import { Pressable, View, StyleSheet } from "react-native"
import { cn } from "@gluestack-ui/nativewind-utils"
import { cssInterop } from "nativewind"
import Animated, {
Easing,
useSharedValue,
withSpring,
withTiming,
} from "react-native-reanimated"
const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
const UIModal = createModal({
Root: View,
Backdrop: AnimatedPressable,
Content: Animated.View,
Body: View,
CloseButton: Pressable,
Footer: View,
Header: View,
AnimatePresence: React.Fragment, // TODO: Add support for this
})
cssInterop(UIModal, { className: "style" })
cssInterop(UIModal.Backdrop, { className: "style" })
cssInterop(UIModal.Content, { className: "style" })
cssInterop(UIModal.Body, { className: "style" })
cssInterop(UIModal.CloseButton, { className: "style" })
cssInterop(UIModal.Footer, { className: "style" })
cssInterop(UIModal.Header, { className: "style" })
const Modal = React.forwardRef(({ className, ...props }: any, ref) => (
<UIModal
className={cn(
"flex-1 w-full h-full justify-center items-center",
className
)}
ref={ref}
pointerEvents="box-none"
/>
))
const ModalBackdrop = React.forwardRef(({ className, ...props }: any, ref) => {
const opacity = useSharedValue(0)
useEffect(() => {
opacity.value = withTiming(0.5, {
easing: Easing.linear,
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return (
<UIModal.Backdrop
className={cn(
"h-full w-full bg-background-950 cursor-default opacity-50 pointer-events-auto",
className
)}
style={[
StyleSheet.absoluteFill,
{
opacity: opacity,
},
]}
{...props}
ref={ref}
/>
)
})
const ModalContent = React.forwardRef(({ className, ...props }: any, ref) => {
const opacity = useSharedValue(0)
const scale = useSharedValue(0.9)
useEffect(() => {
opacity.value = withTiming(1, {
easing: Easing.linear,
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
useEffect(() => {
scale.value = withSpring(1, {
damping: 18,
stiffness: 250,
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return (
<UIModal.Content
pointerEvents="auto"
className={cn(
"w-70% max-w-[420px] bg-background-50 rounded-md",
className
)}
{...props}
style={{
opacity: opacity,
transform: [{ scale: scale }],
}}
ref={ref}
/>
)
})
const ModalHeader = React.forwardRef(({ className, ...props }: any, ref) => (
<UIModal.Header
className={cn(
"px-4 pt-4 pb-2 justify-between item-center flex-row",
className
)}
{...props}
ref={ref}
/>
))
const ModalBody = React.forwardRef(({ className, ...props }: any, ref) => (
<UIModal.Body className={cn("px-4 pb-2", className)} {...props} ref={ref} />
))
const ModalFooter = React.forwardRef(({ className, ...props }: any, ref) => (
<UIModal.Footer
className={cn("p-4 flex-row flex-wrap justify-end items-center", className)}
{...props}
ref={ref}
/>
))
const ModalCloseButton = React.forwardRef(
({ className, ...props }: any, ref) => (
<UIModal.CloseButton
className={cn(
"group/modal-close-button p-2 rounded-lg focus-visible:bg-background-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary-500",
className
)}
{...props}
ref={ref}
/>
)
)
// Assign display names
Modal.displayName = "Modal"
ModalBackdrop.displayName = "ModalBackdrop"
ModalContent.displayName = "ModalContent"
ModalHeader.displayName = "ModalHeader"
ModalBody.displayName = "ModalBody"
ModalFooter.displayName = "ModalFooter"
ModalCloseButton.displayName = "ModalCloseButton"
export {
Modal,
ModalBackdrop,
ModalContent,
ModalCloseButton,
ModalHeader,
ModalBody,
ModalFooter,
}

Step 3: Update the import paths to match your project setup.

API Reference

To use this component in your project, include the following import statement in your file.
import { Modal } from "@/components/ui/Modal"
export default () => (
<Modal>
<ModalBackdrop />
<ModalContent>
<ModalHeader>
<ModalCloseButton></ModalCloseButton>
</ModalHeader>
<ModalBody />
<ModalFooter />
</ModalContent>
</Modal>
)

Component Props

This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.
Contains all View related layout style props and actions. It inherits all the properties of React Native's View component.
Prop
Type
Default
Description
isOpen
boolean
-
If true, the modal will open. Useful for controllable state behavior.
onClose
() => any
-
Callback invoked when the modal is closed.
useRNModal
boolean
false
If true, renders react-native native modal.
defaultIsOpen
boolean
-
Specifies the default open state of the Modal
initialFocusRef
React.RefObject<any>
-
The ref of element to receive focus when the modal opens.
finalFocusRef
React.RefObject<any>
-
The ref of element to receive focus when the modal closes
avoidKeyboard
boolean
-
If true, the Modal will avoid the keyboard.
closeOnOverlayClick
boolean
-
If true, the Modal will close when the overlay is clicked.
isKeyboardDismissable
boolean
-
If true, the keyboard can dismiss the Modal
children
any
-
The content to display inside the Modal
Descendants Styling Props Props to style child components.
Sx Prop
Description
_content
Prop to style ModalContent Component

ModalBackdrop

It is React Native's Pressable component, created using @legendapp/motion's createMotionAnimatedComponent function to add animation to the component. You can use any declarative animation library you prefer.

ModalContent

It is @legendapp/motion's Motion.View component. You can use any declarative animation library you prefer.
Prop
Type
Default
Description
focusable
boolean
false
If true, Modal Content will be focusable.

ModalHeader

It inherits all the properties of React Native's View component.

ModalCloseButton

It inherits all the properties of React Native's View component.
Descendants Styling Props Props to style child components.
Sx Prop
Description
_text
Prop to style ModalCloseButton Text Component
_icon
Prop to style ModalCloseButton Icon Component

ModalBody

It inherits all the properties of React Native's View component.

ModalFooter

It inherits all the properties of React Native's View component.

Accessibility

We have outlined the various features that ensure the Modal component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards. It uses React Native ARIA @react-native-aria/focus which follows the Dialog Modal WAI-ARIA design pattern.

Props

Modal component is created using View component from react-native. It extends all the props supported by React Native View, utility props and the props mentioned below.
Name
Value
Default
size
xs | sm | md | lg | full
md

Spec Doc

Explore the comprehensive details of the Modal in this document, including its implementation details, checklist, and potential future additions. Dive into the thought process behind the component and gain insights into its development journey.