Looking for documentation on gluestack-ui v2 with NativeWind? Refer to this link.
This is an illustration of a Themed Modal component with default configuration.
function App() {
const [showModal, setShowModal] = useState(false)
console.log(showModal)
const ref = React.useRef(null)
return (
<Center h={300}>
<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"
mr="$3"
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>
)
}

API Reference

Import

To use this component in your project, include the following import statement in your file.
import { Modal } from "@gluestack-ui/themed"

Anatomy

The structure provided below can help you identify and understand a Modal component's various parts.
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.

Themed

The themed version of the component is a pre-styled version of the component, which allows you to quickly integrate the component into your project. The component's design and functionality are fully defined, allowing you to focus on the more important aspects of your project. To know more about Themed Library please visit this link.

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
Note: These props are exclusively applicable when utilizing the default configuration of gluestack-ui/config. If you are using a custom theme, these props may not be available.

Examples

The Examples section provides visual representations of the different variants of the component, allowing you to quickly and easily determine which one best fits your needs. Simply copy the code and integrate it into your project.

Multiple Modals

A user interface featuring multiple modal components that allow for layered interaction and context-specific information display.
function App() {
const [showModal, setShowModal] = useState(false)
const [showModal2, setShowModal2] = useState(false)
const [showModal3, setShowModal3] = useState(false)
return (
<Center>
<Center h={400}>
<Button onPress={() => setShowModal(true)}>
<ButtonText>Forgot password?</ButtonText>
</Button>
</Center>
<Modal
isOpen={showModal}
onClose={() => {
setShowModal(false)
}}
>
<ModalBackdrop />
<ModalContent>
<ModalHeader borderBottomWidth="$0">
<VStack space="sm">
<Heading size="lg">Forgot password?</Heading>
<Text size="sm">
No worries, we’ll send you reset instructions
</Text>
</VStack>
</ModalHeader>
<ModalBody>
<Input>
<InputField placeholder="Enter your email" />
</Input>
</ModalBody>
<ModalFooter borderTopWidth="$0">
<VStack space="lg" w="$full">
<Button
onPress={() => {
setShowModal2(true)
}}
>
<ButtonText>Submit</ButtonText>
</Button>
<HStack>
<Button
variant="link"
size="sm"
onPress={() => {
setShowModal(false)
}}
>
<ButtonIcon as={ArrowLeftIcon} />
<ButtonText>Back to login</ButtonText>
</Button>
</HStack>
</VStack>
</ModalFooter>
</ModalContent>
</Modal>
<Modal
isOpen={showModal2}
onClose={() => {
setShowModal2(false)
}}
>
<ModalBackdrop />
<ModalContent>
<ModalHeader borderBottomWidth="$0">
<VStack space="sm">
<Heading size="lg">Reset password</Heading>
<Text size="sm">
A verification code has been sent to you. Enter code below.
</Text>
</VStack>
</ModalHeader>
<ModalBody>
<Input>
<InputField placeholder="Enter verification code" />
</Input>
</ModalBody>
<ModalFooter borderTopWidth="$0">
<VStack space="lg" w="$full">
<Button
onPress={() => {
setShowModal3(true)
}}
>
<ButtonText>Continue</ButtonText>
</Button>
<Text size="sm">
Didn’t receive the email?
<Link ml="$1">
<Link.Text
size="sm"
$dark-color="$textDark200"
color="$textLight700"
fontWeight="$semibold"
>
Click to resend
</Link.Text>
</Link>
</Text>
<HStack space="xs" alignItems="center">
<Button
variant="link"
size="sm"
onPress={() => {
setShowModal2(false)
}}
>
<ButtonIcon as={ArrowLeftIcon} />
<ButtonText>Back to login</ButtonText>
</Button>
</HStack>
</VStack>
</ModalFooter>
</ModalContent>
</Modal>
<Modal
isOpen={showModal3}
onClose={() => {
setShowModal3(false)
}}
>
<ModalBackdrop />
<ModalContent>
<ModalHeader borderBottomWidth="$0">
<VStack space="sm">
<Heading size="lg">Set new password</Heading>
<Text size="sm">
Almost done. Enter your new password and you are all set.
</Text>
</VStack>
</ModalHeader>
<ModalBody>
<VStack space="xl">
<Input>
<InputField placeholder="New password" />
</Input>
<Input>
<InputField placeholder="Confirm new password" />
</Input>
</VStack>
</ModalBody>
<ModalFooter borderTopWidth="$0">
<VStack space="lg" w="$full">
<Button
onPress={() => {
setShowModal(false)
setShowModal2(false)
setShowModal3(false)
}}
>
<ButtonText>Submit</ButtonText>
</Button>
<HStack space="xs" alignItems="center">
<Button
variant="link"
size="sm"
onPress={() => {
setShowModal3(false)
}}
>
<ButtonIcon as={ArrowLeftIcon} />
<ButtonText>Back to login</ButtonText>
</Button>
</HStack>
</VStack>
</ModalFooter>
</ModalContent>
</Modal>
</Center>
)
}
A versatile modal component offering different sizes to accommodate various content types and optimize user experience.
function App() {
const [showModal, setShowModal] = React.useState(false)
const [size, setSize] = React.useState(undefined)
const sizes = ["xs", "sm", "md", "lg", "full"]
const ref = React.useRef(null)
const handleClick = (currentSize) => {
setShowModal(true)
setSize(currentSize)
}
return (
<Center h={400}>
<VStack space="md">
{sizes.map((currentSize) => {
return (
<Button key={currentSize} onPress={() => handleClick(currentSize)}>
<ButtonText>{currentSize}</ButtonText>
</Button>
)
})}
</VStack>
<Modal
isOpen={showModal}
onClose={() => {
setShowModal(false)
}}
size={size}
finalFocusRef={ref}
>
<ModalBackdrop />
<ModalContent>
<ModalHeader>
<Heading size="lg">Delete Folder</Heading>
<ModalCloseButton>
<Icon as={CloseIcon} />
</ModalCloseButton>
</ModalHeader>
<ModalBody>
<Text size="sm">
Whoa, slow down there! This modal is like a red light at an
intersection, reminding you to stop and think before you proceed.
Is deleting this folder the right choice?
</Text>
</ModalBody>
<ModalFooter>
<Button
variant="outline"
size="sm"
action="secondary"
mr="$3"
onPress={() => {
setShowModal(false)
}}
>
<ButtonText>Cancel</ButtonText>
</Button>
<Button
size="sm"
action="primary"
borderWidth="$0"
onPress={() => {
setShowModal(false)
}}
bg="$error700"
$hover-bg="$error800"
$active-bg="$error900"
>
<ButtonText>Explore</ButtonText>
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</Center>
)
}

Unstyled

All the components in gluestack-ui are unstyled by default. To customize your UI using the extendedTheme, please refer to this link. The import names of components serve as keys to customize each component.

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.