From f53229d3d392b1c7c242859f379a14c518634710 Mon Sep 17 00:00:00 2001 From: gushen610140 Date: Mon, 15 Sep 2025 08:35:52 +0800 Subject: [PATCH] feat: add confirm util --- src/utils/confirm.tsx | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/utils/confirm.tsx diff --git a/src/utils/confirm.tsx b/src/utils/confirm.tsx new file mode 100644 index 0000000..8a8c977 --- /dev/null +++ b/src/utils/confirm.tsx @@ -0,0 +1,92 @@ +import { createApp, ref } from "vue"; +import { AnimatePresence, motion } from "motion-v"; + +export interface ConfirmOptions { + title?: string; + message: string; + confirmText?: string; + cancelText?: string; + onConfirm?: () => void; + onCancel?: () => void; +} + +export function confirm(options: ConfirmOptions): void { + const confirmHideFlag = ref(false); + const defaultOptions = { + title: "确认", + confirmText: "确定", + cancelText: "取消", + onConfirm: () => {}, + onCancel: () => {}, + ...options, + }; + + const handleConfirm = () => { + confirmHideFlag.value = true; + defaultOptions.onConfirm?.(); + setTimeout(() => { + confirmApp.unmount(); + document.body.removeChild(confirmContainer); + }, 300); // 动画结束后移除 + }; + + const handleCancel = () => { + confirmHideFlag.value = true; + defaultOptions.onCancel?.(); + setTimeout(() => { + confirmApp.unmount(); + document.body.removeChild(confirmContainer); + }, 300); // 动画结束后移除 + }; + + const confirmInstance = () => ( + + {confirmHideFlag.value ? null : ( +
+ + {defaultOptions.title && ( +
+

+ {defaultOptions.title} +

+
+ )} +
+

{defaultOptions.message}

+
+
+ {defaultOptions.cancelText && ( + + )} + {defaultOptions.confirmText && ( + + )} +
+
+
+ )} +
+ ); + + const confirmContainer = document.createElement("div"); + document.body.appendChild(confirmContainer); + + const confirmApp = createApp(confirmInstance); + confirmApp.mount(confirmContainer); +} \ No newline at end of file