This is an illustration of AlertDialog component.
function Example() {
const [showAlertDialog, setShowAlertDialog] = React.useState(false)
return (
<Center h={300}>
<Button onPress={() => setShowAlertDialog(true)}>
<ButtonText>Click me</ButtonText>
</Button>
<AlertDialog
isOpen={showAlertDialog}
onClose={() => {
setShowAlertDialog(false)
}}
>
<AlertDialogBackdrop />
<AlertDialogContent>
<AlertDialogHeader>
<Heading size="lg">Deactivate account</Heading>
<AlertDialogCloseButton>
<Icon as={CloseIcon} />
</AlertDialogCloseButton>
</AlertDialogHeader>
<AlertDialogBody>
<Text size="sm">
Are you sure you want to deactivate your account? Your data will
be permanently removed and cannot be undone.
</Text>
</AlertDialogBody>
<AlertDialogFooter>
<ButtonGroup space="lg">
<Button
variant="outline"
action="secondary"
onPress={() => {
setShowAlertDialog(false)
}}
>
<ButtonText>Cancel</ButtonText>
</Button>
<Button
bg="$error600"
action="negative"
onPress={() => {
setShowAlertDialog(false)
}}
>
<ButtonText>Deactivate</ButtonText>
</Button>
</ButtonGroup>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</Center>
)
}

Installation

Step 1: Install the following dependencies:

npm i @gluestack-ui/alert-dialog

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

import {
AnimatePresence,
AnimatedView,
AnimatedPressable,
} from "@gluestack-style/animation-resolver"
import { createAlertDialog } from "@gluestack-ui/alert-dialog"
import { View, Pressable, ScrollView } from "react-native"
import { styled } from "@gluestack-style/react"
const StyledRoot = styled(
View,
{
w: "$full",
h: "$full",
justifyContent: "center",
alignItems: "center",
variants: {
size: {
xs: { _content: { w: "60%", maxWidth: 360 } },
sm: { _content: { w: "70%", maxWidth: 420 } },
md: { _content: { w: "80%", maxWidth: 510 } },
lg: { _content: { w: "90%", maxWidth: 640 } },
full: { _content: { w: "$full" } },
},
},
defaultProps: { size: "md" },
_web: {
pointerEvents: "box-none",
},
},
{
descendantStyle: ["_content"],
}
)
const StyledContent = styled(
AnimatedView,
{
bg: "$background50",
rounded: "$lg",
overflow: "hidden",
//@ts-ignore
":initial": {
scale: 0.9,
opacity: 0,
},
":animate": {
scale: 1,
opacity: 1,
},
":exit": {
scale: 0.9,
opacity: 0,
},
":transition": {
type: "spring",
damping: 18,
stiffness: 250,
opacity: {
type: "timing",
duration: 250,
},
},
defaultProps: {
softShadow: "3",
},
},
{
ancestorStyle: ["_content"],
}
)
const StyledCloseButton = styled(
Pressable,
{
zIndex: 1,
rounded: "$sm",
p: "$2",
_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 StyledHeader = styled(
View,
{
p: "$4",
borderColor: "$border300",
justifyContent: "space-between",
alignItems: "center",
flexDirection: "row",
},
{}
)
const StyledFooter = styled(
View,
{
p: "$4",
flexDirection: "row",
justifyContent: "flex-end",
alignItems: "center",
flexWrap: "wrap",
borderColor: "$border300",
},
{}
)
const StyledBody = styled(ScrollView, { px: "$4", py: "$2" }, {})
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 AccessibleAlertDialog = createAlertDialog({
Root: StyledRoot,
Content: StyledContent,
CloseButton: StyledCloseButton,
Header: StyledHeader,
Footer: StyledFooter,
Body: StyledBody,
Backdrop: StyledBackdrop,
//@ts-ignore
AnimatePresence: AnimatePresence,
})
export const AlertDialog = AccessibleAlertDialog
export const AlertDialogContent = AccessibleAlertDialog.Content
export const AlertDialogCloseButton = AccessibleAlertDialog.CloseButton
export const AlertDialogHeader = AccessibleAlertDialog.Header
export const AlertDialogFooter = AccessibleAlertDialog.Footer
export const AlertDialogBody = AccessibleAlertDialog.Body
export const AlertDialogBackdrop = AccessibleAlertDialog.Backdrop

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 {
AlertDialog,
AlertDialogBackdrop,
AlertDialogContent,
AlertDialogHeader,
AlertDialogCloseButton,
AlertDialogFooter,
AlertDialogBody,
} from "@gluestack-ui/themed"
export default () => (
<AlertDialog>
<AlertDialogBackdrop />
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogCloseButton />
</AlertDialogHeader>
<AlertDialogBody />
<AlertDialogFooter />
</AlertDialogContent>
</AlertDialog>
)

Component Props

This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.

AlertDialog

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
false
If true, the alert-dialog will open. Useful for controllable state behavior.
onClose
() => any
-
Callback invoked when the alert-dialog is closed.
useRNModal
boolean
false
If true, renders react-native native modal. (Only works in react-native)
defaultIsOpen
boolean
false
Specifies the default open state of the AlertDialog
initialFocusRef
React.RefObject<any>
-
The ref of element to receive focus when the alert-dialog opens.
finalFocusRef
React.RefObject<any>
-
The ref of element to receive focus when the alert-dialog closes.
avoidKeyboard
boolean
-
If true, the AlertDialog will avoid the keyboard.
closeOnOverlayClick
boolean
true
If true, the AlertDialog will close when the overlay is clicked.
isKeyboardDismissable
boolean
true
If true, the keyboard can dismiss the AlertDialog
animationPreset
slide | fade
slide
Specifies the animation preset for the AlertDialog
Descendants Styling Props Props to style child components.
Sx Prop
Description
_content
Prop to style AlertDialogContent Component

AlertDialogBackdrop

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.

AlertDialogContent

It is @legendapp/motion's Motion.View component. You can use any declarative animation library you prefer.

AlertDialogCloseButton

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

AlertDialogHeader

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

AlertDialogBody

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

AlertDialogFooter

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

Accessibility

We have outlined the various features that ensure the AlertDialog 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 WAI-ARIA Alert and Message Dialogs Pattern.

Props

AlertDialog 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.

AlertDialog

Name
Value
Default
size
xs | sm | md | lg | full
md

Spec Doc

Explore the comprehensive details of the AlertDialog 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.