diff --git a/packages/components/src/components/dialog/Dialog.story.vue b/packages/components/src/components/dialog/Dialog.story.vue
index 876f2267..985016d3 100644
--- a/packages/components/src/components/dialog/Dialog.story.vue
+++ b/packages/components/src/components/dialog/Dialog.story.vue
@@ -1,6 +1,6 @@
-
- 删除
-
+
+ 确定
diff --git a/packages/components/src/components/dialog/DialogProvider.vue b/packages/components/src/components/dialog/DialogProvider.vue
deleted file mode 100644
index fcfac149..00000000
--- a/packages/components/src/components/dialog/DialogProvider.vue
+++ /dev/null
@@ -1,27 +0,0 @@
-
-
-
-
-
diff --git a/packages/components/src/components/dialog/dialog-manager.ts b/packages/components/src/components/dialog/dialog-manager.ts
new file mode 100644
index 00000000..f11407d8
--- /dev/null
+++ b/packages/components/src/components/dialog/dialog-manager.ts
@@ -0,0 +1,75 @@
+import DialogComponent from "./Dialog.vue";
+import { createVNode, render, type Component } from "vue";
+import type { DialogProps } from "./interface";
+
+export type DialogApiProps = Omit;
+
+export type DialogApi = (props?: DialogApiProps) => void;
+
+export interface DialogEntry {
+ (props: DialogProps): void;
+ info: DialogApi;
+ success: DialogApi;
+ error: DialogApi;
+ warning: DialogApi;
+}
+
+const defaultProps: DialogProps = {
+ title: "",
+ visible: false,
+};
+
+const dialog: DialogEntry = (userProps: DialogProps) => {
+ const props = {
+ ...defaultProps,
+ ...userProps,
+ };
+
+ let container = document.body.querySelector(".dialog-container");
+ if (!container) {
+ container = document.createElement("div");
+ container.className = "dialog-container";
+ document.body.appendChild(container);
+ }
+
+ const { vnode, container: hostContainer } = createVNodeComponent(
+ DialogComponent,
+ props
+ );
+
+ if (hostContainer.firstElementChild) {
+ container.appendChild(hostContainer.firstElementChild);
+ }
+
+ if (vnode.component?.props) {
+ vnode.component.props.visible = true;
+ }
+
+ if (vnode?.props) {
+ // close emit
+
+ vnode.props.onClose = () => {
+ container?.remove();
+ render(null, hostContainer);
+ };
+ }
+};
+
+function createVNodeComponent(
+ component: Component,
+ props: Record
+) {
+ const vnode = createVNode(component, props);
+ const container = document.createElement("div");
+ render(vnode, container);
+ return { vnode, container };
+}
+
+dialog.success = (props?: DialogApiProps) =>
+ dialog({ ...props, type: "success" });
+dialog.info = (props?: DialogApiProps) => dialog({ ...props, type: "info" });
+dialog.warning = (props?: DialogApiProps) =>
+ dialog({ ...props, type: "warning" });
+dialog.error = (props?: DialogApiProps) => dialog({ ...props, type: "error" });
+
+export { dialog as Dialog };
diff --git a/packages/components/src/components/dialog/index.ts b/packages/components/src/components/dialog/index.ts
index 3ddc98c3..04c96459 100644
--- a/packages/components/src/components/dialog/index.ts
+++ b/packages/components/src/components/dialog/index.ts
@@ -1,3 +1,2 @@
export { default as VDialog } from "./Dialog.vue";
-export { default as VDialogProvider } from "./DialogProvider.vue";
-export * from "./use-dialog";
+export { Dialog } from "./dialog-manager";
diff --git a/packages/components/src/components/dialog/interface.ts b/packages/components/src/components/dialog/interface.ts
index ca513c8f..fe81823e 100644
--- a/packages/components/src/components/dialog/interface.ts
+++ b/packages/components/src/components/dialog/interface.ts
@@ -14,4 +14,16 @@ export interface useDialogOptions {
onCancel?: () => void;
}
+export interface DialogProps {
+ type?: Type;
+ visible?: boolean;
+ title?: string;
+ description?: string;
+ confirmType?: ButtonType;
+ confirmText?: string;
+ cancelText?: string;
+ onConfirm?: () => void;
+ onCancel?: () => void;
+}
+
export type useDialogUserOptions = Omit;
diff --git a/packages/components/src/components/dialog/use-dialog.ts b/packages/components/src/components/dialog/use-dialog.ts
deleted file mode 100644
index 3bb7978d..00000000
--- a/packages/components/src/components/dialog/use-dialog.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import type { Ref } from "vue";
-import { inject } from "vue";
-import type {
- Type,
- useDialogOptions,
- useDialogUserOptions,
-} from "@/components/dialog/interface";
-import { DialogProviderProvideKey } from "@/components/dialog/interface";
-
-interface useDialogReturn {
- success: (options: useDialogUserOptions) => void;
- info: (options: useDialogUserOptions) => void;
- warning: (options: useDialogUserOptions) => void;
- error: (options: useDialogUserOptions) => void;
-}
-
-export function useDialog(): useDialogReturn {
- const dialogOptions = inject[>(DialogProviderProvideKey);
-
- if (!dialogOptions) {
- throw new Error("DialogProvider is not mounted");
- }
-
- const createDialog = (type: Type) => (options: useDialogUserOptions) => {
- // clear previous dialog
- dialogOptions.value = { title: "", visible: false };
-
- dialogOptions.value = { ...dialogOptions.value, ...options };
- dialogOptions.value.type = type;
- dialogOptions.value.visible = true;
- };
-
- return {
- success: createDialog("success"),
- info: createDialog("info"),
- warning: createDialog("warning"),
- error: createDialog("error"),
- };
-}
diff --git a/packages/shared/src/layouts/BasicLayout.vue b/packages/shared/src/layouts/BasicLayout.vue
index e399218b..6dfe6f31 100644
--- a/packages/shared/src/layouts/BasicLayout.vue
+++ b/packages/shared/src/layouts/BasicLayout.vue
@@ -8,7 +8,7 @@ import {
VAvatar,
VSpace,
VButton,
- useDialog,
+ Dialog,
} from "@halo-dev/components";
import type { MenuGroupType, MenuItemType } from "../types/menus";
import type { User } from "@halo-dev/api-client";
@@ -21,7 +21,6 @@ const menus = inject("menus");
const minimenus = inject("minimenus");
const route = useRoute();
const router = useRouter();
-const dialog = useDialog();
const moreMenuVisible = ref(false);
const moreMenuRootVisible = ref(false);
@@ -34,7 +33,7 @@ const handleRouteToProfile = () => {
};
const handleLogout = () => {
- dialog.warning({
+ Dialog.warning({
title: "是否确认退出登录?",
onConfirm: async () => {
try {
diff --git a/src/App.vue b/src/App.vue
index a685b76b..2152c201 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -1,6 +1,5 @@
-
-
-
-
+
+
]