AlertDialog
Prompt users to take action with interruptions for confirmations or important prompts.
Installation
API Reference
This is an illustration of AlertDialog component.
size
function Example() {const [showAlertDialog, setShowAlertDialog] = React.useState(false)const handleClose = () => setShowAlertDialog(false)return (<><Button onPress={() => setShowAlertDialog(true)}><ButtonText>Open Dialog</ButtonText></Button><AlertDialog isOpen={showAlertDialog} onClose={handleClose} size="md"><AlertDialogBackdrop /><AlertDialogContent><AlertDialogHeader><Heading className="text-typography-950 font-semibold" size="md">Are you sure you want to delete this post?</Heading></AlertDialogHeader><AlertDialogBody className="mt-3 mb-4"><Text size="sm">Deleting the post will remove it permanently and cannot be undone.Please confirm if you want to proceed.</Text></AlertDialogBody><AlertDialogFooter className=""><Buttonvariant="outline"action="secondary"onPress={handleClose}size="sm"><ButtonText>Cancel</ButtonText></Button><Button size="sm" onPress={handleClose}><ButtonText>Delete</ButtonText></Button></AlertDialogFooter></AlertDialogContent></AlertDialog></>)}
Installation
CLI
Manual
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.
Features
- Keyboard support for actions.
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.
Keyboard
- Tab:
- Moves focus to the next tabbable element inside the dialog.
- If focus is on the last tabbable element inside the dialog, moves focus to the first tabbable element inside the dialog.
- Shift + Tab:
- Moves focus to the previous tabbable element inside the dialog.
- If focus is on the first tabbable element inside the dialog, moves focus to the last tabbable element inside the dialog.
- Escape: Closes the dialog.
Screen Reader
- VoiceOver: When the button is focused, the screen reader will announce the button's label and provide instructions on how to click it. When the alert dialog is triggered, the screen reader will read its content.
States
- When the alert dialog is triggered, aria-modal="true" is added to the alert dialog content. This attribute informs assistive technologies that the windows underneath the current alert dialog are not available for interaction(inert).
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 image
function Example() {const [showAlertDialog, setShowAlertDialog] = React.useState(false)const handleClose = () => setShowAlertDialog(false)return (<><Button onPress={() => setShowAlertDialog(true)}><ButtonText>Pay</ButtonText></Button><AlertDialog isOpen={showAlertDialog} onClose={handleClose}><AlertDialogBackdrop /><AlertDialogContent className="p-0 max-w-[590px] sm:flex-row border-primary-800 rounded-xl"><Box className="bg-primary-600 min-w-[123px] items-center justify-center native:max-h-[95px]"><Imagesource={{uri: "https://gluestack.github.io/public-blog-video-assets/Image%20Container.png",}}alt="image"className="min-h-[95px] min-w-[95px] h-full w-full"size="none"/></Box><AlertDialogBodyclassName=""contentContainerClassName="p-6 flex-row justify-between gap-6 md:gap-9 items-center"><VStack><Headingsize="md"className="text-typography-950 font-semibold leading-6">Get Additional Discount</Heading><Text className="pt-2 text-typography-950" size="sm">Upgrade your plan before your trial ends yo get 5% discount. Usecode{" "}<Text className="font-bold" size="sm">#PRO005</Text></Text></VStack><Button size="sm" className="hidden sm:flex" onPress={handleClose}><ButtonText>Upgrade</ButtonText></Button></AlertDialogBody></AlertDialogContent></AlertDialog></>)}
AlertDialog with icon + cta
function Example() {const [showAlertDialog, setShowAlertDialog] = React.useState(false)const handleClose = () => setShowAlertDialog(false)return (<><Button onPress={() => setShowAlertDialog(true)}><ButtonText>Upload</ButtonText></Button><AlertDialog isOpen={showAlertDialog} onClose={handleClose}><AlertDialogBackdrop /><AlertDialogContent className="p-4 gap-4 max-w-[649px] w-full md:flex-row mx-2"><AlertDialogBodyclassName=""contentContainerClassName="flex-row gap-4"><Box className="h-10 min-[350px]:h-14 w-12 min-[350px]:w-14 rounded-full bg-background-50 items-center justify-center"><Iconas={UploadCloud}className="stroke-background-900"size="xl"/></Box><VStack className="gap-1"><Heading size="md" className="text-typography-950 font-semibold">Cloud storage full!</Heading><Text size="sm">You have used up all the storage you have.</Text></VStack></AlertDialogBody><AlertDialogFooter><Buttonvariant="outline"action="secondary"onPress={handleClose}size="sm"><ButtonText>Cancel</ButtonText></Button><Button size="sm" onPress={handleClose}><ButtonText>Upgrade Storage</ButtonText></Button></AlertDialogFooter></AlertDialogContent></AlertDialog></>)}
AlertDialog with Delete Option
function Example() {const [showAlertDialog, setShowAlertDialog] = React.useState(false)const handleClose = () => setShowAlertDialog(false)return (<><Button onPress={() => setShowAlertDialog(true)}><ButtonText>Delete Invoice</ButtonText></Button><AlertDialog isOpen={showAlertDialog} onClose={handleClose}><AlertDialogBackdrop /><AlertDialogContent className="w-full max-w-[415px] gap-4 items-center"><Box className="rounded-full h-[52px] w-[52px] bg-background-error items-center justify-center"><Icon as={TrashIcon} size="lg" className="stroke-error-500" /></Box><AlertDialogHeader className="mb-2"><Heading size="md">Delete account?</Heading></AlertDialogHeader><AlertDialogBody><Text size="sm" className="text-center">The invoice will be deleted from the invoices section and in thedocuments folder. This cannot be undone.</Text></AlertDialogBody><AlertDialogFooter className="mt-5"><Buttonsize="sm"action="negative"onPress={handleClose}className="px-[30px]"><ButtonText>Delete</ButtonText></Button><Buttonvariant="outline"action="secondary"onPress={handleClose}size="sm"className="px-[30px]"><ButtonText>Cancel</ButtonText></Button></AlertDialogFooter></AlertDialogContent></AlertDialog></>)}