2026-03-19 01:12:17 +08:00
|
|
|
|
import { buttonTextVariants, buttonVariants } from '@/components/ui/button';
|
|
|
|
|
|
import { NativeOnlyAnimatedView } from '@/components/ui/native-only-animated-view';
|
|
|
|
|
|
import { TextClassContext } from '@/components/ui/text';
|
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
|
import * as AlertDialogPrimitive from '@rn-primitives/alert-dialog';
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
|
import { Platform, View, type ViewProps } from 'react-native';
|
|
|
|
|
|
import { FadeIn, FadeOut } from 'react-native-reanimated';
|
|
|
|
|
|
import { FullWindowOverlay as RNFullWindowOverlay } from 'react-native-screens';
|
|
|
|
|
|
|
|
|
|
|
|
const AlertDialog = AlertDialogPrimitive.Root;
|
|
|
|
|
|
|
|
|
|
|
|
const AlertDialogTrigger = AlertDialogPrimitive.Trigger;
|
|
|
|
|
|
|
|
|
|
|
|
const AlertDialogPortal = AlertDialogPrimitive.Portal;
|
|
|
|
|
|
|
|
|
|
|
|
const FullWindowOverlay = Platform.OS === 'ios' ? RNFullWindowOverlay : React.Fragment;
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogOverlay({
|
|
|
|
|
|
className,
|
|
|
|
|
|
children,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: Omit<AlertDialogPrimitive.OverlayProps, 'asChild'> &
|
|
|
|
|
|
React.RefAttributes<AlertDialogPrimitive.OverlayRef> & {
|
|
|
|
|
|
children?: React.ReactNode;
|
|
|
|
|
|
}) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<FullWindowOverlay>
|
|
|
|
|
|
<AlertDialogPrimitive.Overlay
|
|
|
|
|
|
className={cn(
|
2026-03-22 16:45:57 +08:00
|
|
|
|
// 不要用 items-center:RN 下子级不会在横向上拉伸,对话框宽度会随单行英文收缩导致不换行
|
|
|
|
|
|
// 见 https://medium.com/livefront/how-to-stop-text-overflow-in-react-native-8521d503e265
|
|
|
|
|
|
'absolute bottom-0 left-0 right-0 top-0 z-50 flex flex-col justify-center bg-black/50 px-4 py-2',
|
2026-03-19 01:12:17 +08:00
|
|
|
|
Platform.select({
|
2026-03-22 16:45:57 +08:00
|
|
|
|
web: 'animate-in fade-in-0 fixed items-center',
|
2026-03-19 01:12:17 +08:00
|
|
|
|
}),
|
|
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
{...props}>
|
|
|
|
|
|
<NativeOnlyAnimatedView
|
2026-03-22 16:45:57 +08:00
|
|
|
|
className={Platform.OS === 'web' ? undefined : 'w-full'}
|
2026-03-19 01:12:17 +08:00
|
|
|
|
entering={FadeIn.duration(200).delay(50)}
|
|
|
|
|
|
exiting={FadeOut.duration(150)}>
|
|
|
|
|
|
<>{children}</>
|
|
|
|
|
|
</NativeOnlyAnimatedView>
|
|
|
|
|
|
</AlertDialogPrimitive.Overlay>
|
|
|
|
|
|
</FullWindowOverlay>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogContent({
|
|
|
|
|
|
className,
|
|
|
|
|
|
portalHost,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: AlertDialogPrimitive.ContentProps &
|
|
|
|
|
|
React.RefAttributes<AlertDialogPrimitive.ContentRef> & {
|
|
|
|
|
|
portalHost?: string;
|
|
|
|
|
|
}) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<AlertDialogPortal hostName={portalHost}>
|
|
|
|
|
|
<AlertDialogOverlay>
|
|
|
|
|
|
<AlertDialogPrimitive.Content
|
|
|
|
|
|
className={cn(
|
2026-03-22 16:45:57 +08:00
|
|
|
|
'bg-background border-border z-50 flex w-full min-w-0 max-w-lg flex-col gap-4 rounded-lg border p-6 shadow-lg shadow-black/5',
|
2026-03-19 01:12:17 +08:00
|
|
|
|
Platform.select({
|
|
|
|
|
|
web: 'animate-in fade-in-0 zoom-in-95 duration-200',
|
2026-03-22 16:45:57 +08:00
|
|
|
|
native: 'max-w-full self-center',
|
2026-03-19 01:12:17 +08:00
|
|
|
|
}),
|
|
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</AlertDialogOverlay>
|
|
|
|
|
|
</AlertDialogPortal>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogHeader({ className, ...props }: ViewProps) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<TextClassContext.Provider value="text-center sm:text-left">
|
2026-03-22 16:45:57 +08:00
|
|
|
|
<View
|
|
|
|
|
|
className={cn('w-full min-w-0 flex flex-col gap-2', className)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
2026-03-19 01:12:17 +08:00
|
|
|
|
</TextClassContext.Provider>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogFooter({ className, ...props }: ViewProps) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<View
|
|
|
|
|
|
className={cn('flex flex-col-reverse gap-2 sm:flex-row sm:justify-end', className)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogTitle({
|
|
|
|
|
|
className,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: AlertDialogPrimitive.TitleProps & React.RefAttributes<AlertDialogPrimitive.TitleRef>) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<AlertDialogPrimitive.Title
|
2026-03-22 16:45:57 +08:00
|
|
|
|
className={cn('min-w-0 text-foreground text-lg font-semibold', className)}
|
2026-03-19 01:12:17 +08:00
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogDescription({
|
|
|
|
|
|
className,
|
2026-03-22 16:45:57 +08:00
|
|
|
|
style,
|
2026-03-19 01:12:17 +08:00
|
|
|
|
...props
|
|
|
|
|
|
}: AlertDialogPrimitive.DescriptionProps &
|
|
|
|
|
|
React.RefAttributes<AlertDialogPrimitive.DescriptionRef>) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<AlertDialogPrimitive.Description
|
2026-03-22 16:45:57 +08:00
|
|
|
|
className={cn(
|
|
|
|
|
|
// shrink: RN 中 Text 在 flex 内需 flexShrink 才能按父宽换行
|
|
|
|
|
|
'w-full min-w-0 shrink text-muted-foreground text-sm',
|
|
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
style={[
|
|
|
|
|
|
Platform.OS === 'web' ? undefined : { flexShrink: 1 },
|
|
|
|
|
|
style,
|
|
|
|
|
|
]}
|
2026-03-19 01:12:17 +08:00
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogAction({
|
|
|
|
|
|
className,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: AlertDialogPrimitive.ActionProps & React.RefAttributes<AlertDialogPrimitive.ActionRef>) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<TextClassContext.Provider value={buttonTextVariants({ className })}>
|
|
|
|
|
|
<AlertDialogPrimitive.Action className={cn(buttonVariants(), className)} {...props} />
|
|
|
|
|
|
</TextClassContext.Provider>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function AlertDialogCancel({
|
|
|
|
|
|
className,
|
|
|
|
|
|
...props
|
|
|
|
|
|
}: AlertDialogPrimitive.CancelProps & React.RefAttributes<AlertDialogPrimitive.CancelRef>) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<TextClassContext.Provider value={buttonTextVariants({ className, variant: 'outline' })}>
|
|
|
|
|
|
<AlertDialogPrimitive.Cancel
|
|
|
|
|
|
className={cn(buttonVariants({ variant: 'outline' }), className)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</TextClassContext.Provider>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
AlertDialog,
|
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
|
AlertDialogOverlay,
|
|
|
|
|
|
AlertDialogPortal,
|
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
|
AlertDialogTrigger,
|
|
|
|
|
|
};
|