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

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 "@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
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

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 h={300}>
<Button onPress={() => setShowAlertDialog(true)}>
<ButtonText>Click me</ButtonText>
</Button>
</Center>
<AlertDialog
isOpen={showAlertDialog}
onClose={() => {
setShowAlertDialog(false)
}}
>
<AlertDialogBackdrop />
<AlertDialogContent>
<AlertDialogHeader borderBottomWidth="$0">
<HStack space="sm" alignItems="center">
<Icon
as={CheckCircleIcon}
color="$success700"
$dark-color="$success300"
/>
<Heading size="lg">Order placed</Heading>
</HStack>
</AlertDialogHeader>
<AlertDialogBody>
<Text size="sm">
Congratulations, your order has been placed! You will receive a
confirmation email shortly. Thank you for shopping with us.
</Text>
</AlertDialogBody>
<AlertDialogFooter borderTopWidth="$0">
<Button
variant="outline"
size="sm"
action="secondary"
mr="$3"
onPress={() => {
setShowAlertDialog(false)
}}
>
<ButtonText>Okay</ButtonText>
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</>
)
}

AlertDialog Sizes

An AlertDialog component with adjustable sizes, providing flexible and visually appealing pop-up notifications and interactive prompts to users.
size
function App() {
const [showAlertDialog, setShowAlertDialog] = React.useState(false)
return (
<>
<Center h={300}>
<Button onPress={() => setShowAlertDialog(true)}>
<ButtonText>Click me</ButtonText>
</Button>
</Center>
<AlertDialog
isOpen={showAlertDialog}
onClose={() => {
setShowAlertDialog(false)
}}
size="md"
>