Dialog
A modal dialog component for displaying content in an overlay with customizable header, content, and footer sections.
Installation
Install the component:
npx natively-ui add dialog
Usage
DialogDemo.jsx
1import { Dialog, AlertDialog } from '@/components/ui/dialog';
2
3export default function MyComponent() {
4 const [isOpen, setIsOpen] = useState(false);
5
6 return (
7 <Dialog
8 isOpen={isOpen}
9 onClose={() => setIsOpen(false)}
10 title="Dialog Title"
11 description="This is a dialog description."
12 >
13 <Text>Dialog content goes here</Text>
14 </Dialog>
15 );
16}
Dialog Props
Prop | Type | Default | Description |
---|---|---|---|
isOpen | boolean | - | Controls whether the dialog is visible. |
onClose | () => void | - | Function called when the dialog should be closed. |
title | string | - | Optional title displayed in the dialog header. |
description | string | - | Optional description text displayed below the title. |
children | React.ReactNode | - | Content to display in the dialog body. |
footer | React.ReactNode | - | Custom footer content, typically action buttons. |
className | string | "" | Additional CSS classes for the dialog container. |
contentClassName | string | "" | Additional CSS classes for the dialog content area. |
overlayClassName | string | "" | Additional CSS classes for the dialog overlay. |
AlertDialog Props
AlertDialog extends Dialog with additional props for common confirmation dialogs.
Prop | Type | Default | Description |
---|---|---|---|
cancelText | string | 'Cancel' | Text for the cancel button. |
confirmText | string | 'OK' | Text for the confirm button. |
onConfirm | () => void | - | Function called when the confirm button is pressed. |
danger | boolean | false | Whether to style the confirm button as destructive. |
Features
- Modal overlay with backdrop tap to close
- Responsive sizing with maximum width constraints
- Optional header with title and description
- Flexible content area for custom components
- Customizable footer with action buttons
- AlertDialog variant for common confirmation dialogs
- Close button in header for better UX
- Fade animation for smooth transitions
- Customizable styling with className props
- Built with React Native Modal and Expo Vector Icons
Examples
Basic Dialog
DialogDemo.jsx
1const [isOpen, setIsOpen] = useState(false);
2
3<View>
4 <Button onPress={() => setIsOpen(true)}>
5 Open Dialog
6 </Button>
7
8 <Dialog
9 isOpen={isOpen}
10 onClose={() => setIsOpen(false)}
11 title="Basic Dialog"
12 description="This is a simple dialog with a title and description."
13 >
14 <Text>This is the dialog content area.</Text>
15 </Dialog>
16</View>
Custom Footer
DialogDemo.jsx
1<Dialog
2 isOpen={isOpen}
3 onClose={() => setIsOpen(false)}
4 title="Custom Actions"
5 description="Dialog with custom footer buttons."
6 footer={
7 <>
8 <Button
9 variant="ghost"
10 size="sm"
11 onPress={() => setIsOpen(false)}
12 className="mr-2"
13 >
14 Cancel
15 </Button>
16 <Button
17 variant="default"
18 size="sm"
19 onPress={handleSave}
20 className="mr-2"
21 >
22 Save
23 </Button>
24 <Button
25 variant="destructive"
26 size="sm"
27 onPress={handleDelete}
28 >
29 Delete
30 </Button>
31 </>
32 }
33>
34 <Text>Are you sure you want to perform this action?</Text>
35</Dialog>
AlertDialog
DialogDemo.jsx
1const [showAlert, setShowAlert] = useState(false);
2
3<View>
4 <Button onPress={() => setShowAlert(true)}>
5 Show Alert
6 </Button>
7
8 <AlertDialog
9 isOpen={showAlert}
10 onClose={() => setShowAlert(false)}
11 title="Confirm Action"
12 description="Are you sure you want to proceed? This action cannot be undone."
13 onConfirm={() => {
14 console.log('Action confirmed');
15 // Perform action here
16 }}
17 confirmText="Yes, proceed"
18 cancelText="Cancel"
19 />
20</View>
Destructive Alert
DialogDemo.jsx
1<AlertDialog
2 isOpen={showDeleteAlert}
3 onClose={() => setShowDeleteAlert(false)}
4 title="Delete Item"
5 description="This will permanently delete the item. This action cannot be undone."
6 onConfirm={handleDelete}
7 confirmText="Delete"
8 cancelText="Keep"
9 danger={true}
10/>
Form Dialog
DialogDemo.jsx
1const [formData, setFormData] = useState({ name: '', email: '' });
2
3<Dialog
4 isOpen={isFormOpen}
5 onClose={() => setIsFormOpen(false)}
6 title="Add New User"
7 description="Please fill in the user details below."
8 contentClassName="space-y-4"
9 footer={
10 <>
11 <Button
12 variant="ghost"
13 size="sm"
14 onPress={() => setIsFormOpen(false)}
15 className="mr-2"
16 >
17 Cancel
18 </Button>
19 <Button
20 variant="default"
21 size="sm"
22 onPress={handleSubmit}
23 >
24 Add User
25 </Button>
26 </>
27 }
28>
29 <View className="space-y-4">
30 <TextInput
31 placeholder="Full Name"
32 value={formData.name}
33 onChangeText={(text) => setFormData({...formData, name: text})}
34 className="border border-gray-300 rounded p-3"
35 />
36 <TextInput
37 placeholder="Email Address"
38 value={formData.email}
39 onChangeText={(text) => setFormData({...formData, email: text})}
40 className="border border-gray-300 rounded p-3"
41 />
42 </View>
43</Dialog>
Custom Styling
DialogDemo.jsx
1<Dialog
2 isOpen={isOpen}
3 onClose={() => setIsOpen(false)}
4 title="Custom Styled Dialog"
5 className="border-2 border-blue-500"
6 contentClassName="bg-blue-50"
7 overlayClassName="bg-blue-900/30"
8>
9 <Text className="text-blue-800">
10 This dialog has custom styling applied.
11 </Text>
12</Dialog>