Modal

A Modal is a window on top of the primary content to draw the user's attention to important information or actions. It provides a focused and interruptive way to interact with the application.
Installation
API Reference
This is an illustration of Modal component.
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>
)
}

Installation

Run the following command:

npx gluestack-ui add modal

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

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