Dialog
Modal dialog overlay with fade animation.
Web preview — components render natively on iOS & Android
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog";
import { useState } from "react";
export function MyScreen() {
const [open, setOpen] = useState(false);
return (
<>
<Button onPress={() => setOpen(true)}>Open Dialog</Button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Are you sure?</DialogTitle>
<DialogDescription>
This action cannot be undone.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onPress={() => setOpen(false)}>Cancel</Button>
<Button onPress={() => setOpen(false)}>Confirm</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}Installation#
npx @aniui/cli add dialogUsage#
app/index.tsx
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from "@/components/ui/dialog";
import { useState } from "react";
export function MyScreen() {
const [open, setOpen] = useState(false);
return (
<>
<Button onPress={() => setOpen(true)}>Open Dialog</Button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Are you sure?</DialogTitle>
<DialogDescription>
This action cannot be undone.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button variant="outline" onPress={() => setOpen(false)}>Cancel</Button>
<Button onPress={() => setOpen(false)}>Confirm</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}Compound Components#
ComponentDescription
DialogRoot modal with backdrop and fade animation
DialogContentCard-style content container
DialogHeaderHeader section
DialogTitleTitle text
DialogDescriptionDescription text in muted color
DialogFooterFooter with row layout for action buttons
DialogTriggerPassthrough wrapper for trigger element
DialogClosePassthrough wrapper for close element
Props#
Dialog#
PropTypeDefault
openbooleanrequiredonOpenChange(open: boolean) => voidrequiredchildrenReact.ReactNoderequiredSub-components (DialogContent, DialogHeader, etc.) accept className and their respective React Native base props.
Accessibility#
- Uses Modal with backdrop dismiss and BackHandler support
accessibilityRole="dialog"on content
Source#
components/ui/dialog.tsx
import React from "react";
import { View, Pressable, Text, Modal } from "react-native";
import { cn } from "@/lib/utils";
export interface DialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
children: React.ReactNode;
}
export function Dialog({ open, onOpenChange, children }: DialogProps) {
return (
<Modal visible={open} transparent animationType="fade" onRequestClose={() => onOpenChange(false)}>
<View className="flex-1 items-center justify-center bg-black/50">
<Pressable className="absolute inset-0" onPress={() => onOpenChange(false)} />
{children}
</View>
</Modal>
);
}
export function DialogContent({ className, children, ...props }: React.ComponentPropsWithoutRef<typeof View> & { className?: string; children?: React.ReactNode }) {
return (
<View className={cn("mx-6 w-80 rounded-lg bg-card p-6 shadow-xl", className)} accessibilityRole="alert" accessible={true} {...props}>
{children}
</View>
);
}
export function DialogHeader({ className, ...props }: React.ComponentPropsWithoutRef<typeof View> & { className?: string }) {
return <View className={cn("pb-4", className)} {...props} />;
}
export function DialogTitle({ className, ...props }: React.ComponentPropsWithoutRef<typeof Text> & { className?: string }) {
return <Text className={cn("text-lg font-semibold text-card-foreground", className)} {...props} />;
}
export function DialogDescription({ className, ...props }: React.ComponentPropsWithoutRef<typeof Text> & { className?: string }) {
return <Text className={cn("text-sm text-muted-foreground mt-1", className)} {...props} />;
}
export function DialogFooter({ className, ...props }: React.ComponentPropsWithoutRef<typeof View> & { className?: string }) {
return <View className={cn("flex-row justify-end gap-3 pt-4", className)} {...props} />;
}
export function DialogTrigger({ children }: { children: React.ReactNode }) {
return <>{children}</>;
}
export function DialogClose({ children }: { children: React.ReactNode }) {
return <>{children}</>;
}