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"
borderWidth="$0"
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 { createModal } from "@gluestack-ui/modal"
import {
AnimatePresence,
AnimatedPressable,
AnimatedView,
} from "@gluestack-style/animation-resolver"
import { Pressable, View, ScrollView } from "react-native"
import { styled } from "@gluestack-style/react"
const StyledRoot = styled(
View,
{
width: "$full",
height: "$full",
justifyContent: "center",
alignItems: "center",
variants: {
size: {
xs: { _content: { width: "60%", maxWidth: 360 } },
sm: { _content: { width: "70%", maxWidth: 420 } },
md: { _content: { width: "80%", maxWidth: 510 } },
lg: { _content: { width: "90%", maxWidth: 640 } },
full: { _content: { width: "100%" } },
},
},
defaultProps: { size: "md" },
_web: {
pointerEvents: "box-none",
},
},
{
descendantStyle: ["_content"],
}
)
const StyledBackdrop = styled(
AnimatedPressable,
{
":initial": {
opacity: 0,
},
":animate": {
opacity: 0.5,
},
":exit": {
opacity: 0,
},
":transition": {
type: "spring",
damping: 18,
stiffness: 250,
opacity: {
type: "timing",
duration: 250,
},
},
position: "absolute",
left: 0,
top: 0,
right: 0,
bottom: 0,
bg: "$background950",
// @ts-ignore
_web: {
cursor: "default",
},
},
{}
)
const StyledContent = styled(
AnimatedView,
{
bg: "$background50",
rounded: "$lg",
overflow: "hidden",
":initial": {
opacity: 0,
scale: 0.9,
},
":animate": {
opacity: 1,
scale: 1,
},
":exit": {
opacity: 0,
},
":transition": {
type: "spring",
damping: 18,
stiffness: 250,
opacity: {
type: "timing",
duration: 250,
},
},
defaultProps: {
softShadow: "3",
},
},
{ ancestorStyle: ["_content"] }
)
const StyledBody = styled(
ScrollView,
{ px: "$4", paddingTop: 0, paddingBottom: "$2" },
{}
)
const StyledCloseButton = styled(
Pressable,
{
zIndex: 1,
p: "$2",
rounded: "$sm",
_icon: {
color: "$background400",
},
_text: {
color: "$background400",
},
":hover": {
_icon: {
color: "$background700",
},
_text: {
color: "$background700",
},
},
":active": {
_icon: {
color: "$background900",
},
_text: {
color: "$background900",
},
},
":focusVisible": {
bg: "$background100",
_icon: {
color: "$background900",
},
_text: {
color: "$background900",
},
},
_web: {
outlineWidth: 0,
cursor: "pointer",
},
},
{ descendantStyle: ["_icon", "_text"] }
)
const StyledFooter = styled(
View,
{
p: "$4",
flexDirection: "row",
justifyContent: "flex-end",
alignItems: "center",
flexWrap: "wrap",
},
{}
)
const StyledHeader = styled(
View,
{
px: "$4",
paddingTop: "$4",
paddingBottom: "$2",
justifyContent: "space-between",
alignItems: "center",
flexDirection: "row",
},
{}
)
const UIModal = createModal({
Root: StyledRoot,
Backdrop: StyledBackdrop,
Content: StyledContent,
Body: StyledBody,
CloseButton: StyledCloseButton,
Footer: StyledFooter,
Header: StyledHeader,
AnimatePresence: AnimatePresence, // TODO: Add support for this
})
export const Modal = UIModal
export const ModalBackdrop = UIModal.Backdrop
export const ModalContent = UIModal.Content
export const ModalCloseButton = UIModal.CloseButton
export const ModalHeader = UIModal.Header
export const ModalBody = UIModal.Body
export const ModalFooter = UIModal.Footer

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.