Modal 模态对话框

模态对话框,用于确认、提示或展示内容

何时使用

  • 需要模态窗口承载内容时。

导入

import { Modal, useModal, ModalMethods } from "@reglow/reui";

基础用法

<script setup>
import { ref } from "vue";
import { Modal } from "@reglow/reui";

const visible = ref(false);

const handleOk = () => {
  visible.value = false;
};

const handleCancel = () => {
  visible.value = false;
};

const afterClose = () => {
  console.log("对话框已关闭");
};
</script>

<template>
  <button @click="visible = true">打开弹窗</button>

  <Modal
    v-model:visible="visible"
    title="基本对话框"
    @ok="handleOk"
    @cancel="handleCancel"
    @after-close="afterClose"
  >
    <p>This is the content of a basic modal.</p>
    <p>More content...</p>
  </Modal>
</template>

更多示例

自定义头部和页脚

通过 headerfooter 自定义。

<!-- 无头部 -->
<Modal v-model:visible="visible" :header="null">
  <p>无头部对话框</p>
</Modal>

<!-- 无页脚 -->
<Modal v-model:visible="visible" :footer="null">
  <p>无页脚对话框</p>
</Modal>

<!-- 自定义页脚 -->
<Modal v-model:visible="visible" title="自定义页脚" :footer="null">
  <p>内容</p>
  <template #footer>
    <button class="bg-primary text-white px-4 py-2 rounded-md" @click="visible = false">
      Yes, I Understand
    </button>
  </template>
</Modal>

尺寸

通过 size 设置宽度尺寸。

<Modal v-model:visible="visible" size="small" title="Small (448px)">...</Modal>
<Modal v-model:visible="visible" size="medium" title="Medium (684px)">...</Modal>
<Modal v-model:visible="visible" size="large" title="Large (920px)">...</Modal>
<Modal v-model:visible="visible" size="full-width" title="Full Width">...</Modal>

全屏 Modal

设置 fullScreen 开启全屏对话框。

<Modal
  v-model:visible="visible"
  title="全屏对话框"
  full-screen
  @ok="handleOk"
  @cancel="handleCancel"
>
  <p>This is a full screen modal</p>
</Modal>

确认按钮 loading

通过 confirmLoading 显示确认按钮 loading 状态。

<script setup>
import { ref } from "vue";

const visible = ref(false);
const loading = ref(false);

const handleOk = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
    visible.value = false;
  }, 2000);
};
</script>

<template>
  <Modal
    v-model:visible="visible"
    title="Loading"
    :confirm-loading="loading"
    @ok="handleOk"
  >
    <p>点击确定后按钮显示 loading</p>
  </Modal>
</template>

命令式调用

使用 ModalMethods 进行命令式调用。

<script setup>
import { ModalMethods } from "@reglow/reui";

const showInfo = () => {
  ModalMethods.info({ title: "提示", content: "这是一条信息提示" });
};

const showSuccess = () => {
  ModalMethods.success({ title: "成功", content: "操作成功" });
};

const showError = () => {
  ModalMethods.error({ title: "错误", content: "操作失败" });
};

const showWarning = () => {
  ModalMethods.warning({ title: "警告", content: "请谨慎操作" });
};

const showConfirm = () => {
  ModalMethods.confirm({
    title: "确认",
    content: "确定要执行此操作吗?",
    onOk: () => {
      console.log("确认");
    },
    onCancel: () => {
      console.log("取消");
    },
  });
};
</script>

<template>
  <button @click="showInfo">Info</button>
  <button @click="showSuccess">Success</button>
  <button @click="showError">Error</button>
  <button @click="showWarning">Warning</button>
  <button @click="showConfirm">Confirm</button>
</template>

命令式调用(异步确认)

onOk 返回 Promise 时,确认按钮自动显示 loading。

<script setup>
import { ModalMethods } from "@reglow/reui";

const showAsyncConfirm = () => {
  ModalMethods.confirm({
    title: "异步确认",
    content: "点击确定后模拟异步请求",
    onOk: () => {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve();
        }, 2000);
      });
    },
  });
};
</script>

<template>
  <button @click="showAsyncConfirm">异步确认</button>
</template>

Hooks 用法

<script setup>
import { useModal } from "@reglow/reui";

const [modal] = useModal();

const showConfirm = () => {
  modal.confirm({
    title: "确认",
    content: "确定要执行此操作吗?",
  });
};
</script>

<template>
  <button @click="showConfirm">Confirm Modal</button>
</template>

API

参数说明类型默认值
visible (v-model)是否可见booleanfalse
title标题VNode | string-
content内容(命令式调用用)VNode | string-
type类型info | success | error | warning | confirmconfirm
icon自定义图标VNode-
size尺寸small | medium | large | full-widthsmall
width宽度number | string-
height高度number | string-
centered是否居中显示booleanfalse
fullScreen全屏booleanfalse
closable显示关闭按钮booleantrue
closeIcon自定义关闭图标VNode-
closeOnEscESC 关闭booleantrue
mask显示遮罩booleantrue
maskClosable点击遮罩关闭booleantrue
maskStyle遮罩样式CSSProperties-
bodyStyle内容样式CSSProperties-
style自定义样式CSSProperties-
okText确认按钮文字string确定
cancelText取消按钮文字string取消
okType确认按钮类型primary | secondary | tertiary | warning | dangerprimary
okButtonProps确认按钮属性Record<string, any>-
cancelButtonProps取消按钮属性Record<string, any>-
hasCancel是否显示取消按钮booleantrue
footer自定义页脚VNode | null-
header自定义头部VNode | null-
footerFill底部按钮撑满booleanfalse
confirmLoading确认按钮 loadingbooleanfalse
motion动画效果booleantrue
zIndexz-indexnumber1000
class附加类名string-
事件说明回调参数
ok点击确认(e: MouseEvent)
cancel点击取消(e: MouseEvent)
afterClose完全关闭后()
名称说明
default内容
title标题
header头部
footer页脚
closeIcon关闭图标

ModalMethods

方法说明
ModalMethods.info(config)信息提示
ModalMethods.success(config)成功提示
ModalMethods.error(config)错误提示
ModalMethods.warning(config)警告提示
ModalMethods.confirm(config)确认对话框

返回 { destroy(), update(config) }

ModalConfig

参数说明类型默认值
title标题string | VNode-
content内容string | VNode-
type类型info | success | error | warning | confirmconfirm
icon自定义图标VNode-
okText确认文字string-
cancelText取消文字string-
okType确认按钮类型stringprimary
okButtonProps确认按钮属性Record<string, any>-
cancelButtonProps取消按钮属性Record<string, any>-
hasCancel显示取消按钮boolean-
centered居中显示booleanfalse
closable显示关闭按钮booleantrue
maskClosable点击遮罩关闭booleantrue
onOk确认回调(返回 Promise 自动 loading)(e) => void | Promise<void>-
onCancel取消回调(e) => void | Promise<void>-
afterClose关闭后回调() => void-

尺寸对照

size宽度
small448px
medium684px
large920px
full-widthcalc(100vw - 64px)

类型对照

type图标说明
info蓝色信息信息提示
success绿色对勾成功提示
error红色错误错误提示
warning黄色警告警告提示
confirm蓝色问号确认对话框