AlertDialog

AlertDialog effectively interrupts a user's flow and prompts them to take a specific action. It's commonly used for mandatory confirmations or call-to-actions.
Installation
API Reference
This is an illustration of AlertDialog component.
function Example() {
const [showAlertDialog, setShowAlertDialog] = React.useState(false)
return (
<Center className="h-[300px]">
<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>
<Button
variant="outline"
action="secondary"
className="mr-3"
onPress={() => {
setShowAlertDialog(false)
}}
>
<ButtonText>Cancel</ButtonText>
</Button>
<Button
action="negative"
className="bg-error-600"
onPress={() => {
setShowAlertDialog(false)
}}
>
<ButtonText>Deactivate</ButtonText>
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</Center>
)
}

Installation

Run the following command:

npx gluestack-ui add alert-dialog

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 "@/components/ui/alert-dialog"
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

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.

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, and the props mentioned below.

AlertDialog

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.

AlertDialog with semantic icon

An example of an AlertDialog component incorporating an icon component for visually representing important information or actions.
function App() {
const [showAlertDialog, setShowAlertDialog] = React.useState(false)
return (
<>
<Center className="h-[300px]">
<Button onPress={() => setShowAlertDialog(true)}>
<ButtonText>Click me</ButtonText>
</Button>
</Center>
<AlertDialog
isOpen={showAlertDialog}
onClose={() => {
setShowAlertDialog(false)
}}
>
<AlertDialogBackdrop />
<AlertDialogContent>
<AlertDialogHeader className="border-b-0">
<HStack space="sm" className="items-center">
<Icon as={CheckCircleIcon} className="text-success-700" />
<Heading size="lg">Order placed</Heading>