Modal 模态弹窗

模态框。

何时使用

1.需要用户处理事务,又不希望跳转页面以致打断工作流程时,可以使用 Modal 在当前页面正中打开一个浮层,承载相应的操作。

2.Modal 起到与用户进行交互的作用,用户可以在 Modal 中输入信息、阅读提示、设置选项等操作。

基础用法

v-model双向绑定,控制 Modal 是否显示;title参数设置 Modal 标题。
<template>
  <d-button @click="handleClick">打开 modal</d-button>
  <d-modal v-model="visible" title="Start Snapshot Version">
    <div>name: {{ data.name }}</div>
    <div>age: {{ data.age }}</div>
    <div>address: {{ data.address }}</div>
  </d-modal>
</template>

<script>
import { defineComponent, ref, reactive } from 'vue';

export default defineComponent({
  setup() {
    const visible = ref(false);
    const data = reactive({
      name: 'Tom',
      age: 20,
      address: 'Chengdu',
    });
    const handleClick = () => {
      visible.value = true;
    };

    return { visible, data, handleClick };
  },
});
</script>

保留最后一次关闭位置

keep-last可使当前 modal 再次打开时保留上次关闭位置。
<template>
  <d-button @click="handleClick">打开 modal</d-button>
  <d-modal v-model="visible" title="Start Keep Last" :keep-last="true">
    <div>name: {{ data.name }}</div>
    <div>age: {{ data.age }}</div>
    <div>address: {{ data.address }}</div>
  </d-modal>
</template>

<script>
import { defineComponent, ref, reactive } from 'vue';

export default defineComponent({
  setup() {
    const visible = ref(false);
    const data = reactive({
      name: 'Tom',
      age: 20,
      address: 'Chengdu',
    });
    const handleClick = () => {
      visible.value = true;
    };

    return { visible, data, handleClick };
  },
});
</script>

自定义标题和操作按钮

header插槽可以自定义 Modal 顶部区域,子组件d-modal-header为顶部区域提供了默认样式,自定义样式可通过在子组件设置style/class实现。footer插槽同理。
<template>
  <d-button @click="handleClick">打开 modal</d-button>
  <d-modal v-model="visible">
    <template #header>
      <d-modal-header>
        <d-icon name="like"></d-icon>
        <span>Good Title</span>
      </d-modal-header>
    </template>
    <div>name: {{ data.name }}</div>
    <div>age: {{ data.age }}</div>
    <div>address: {{ data.address }}</div>
    <template #footer>
      <d-modal-footer style="text-align: right; padding-right: 20px;">
        <d-button @click="hidden">取消</d-button>
        <d-button @click="hidden">确认</d-button>
      </d-modal-footer>
    </template>
  </d-modal>
</template>
<script>
import { ref, defineComponent, reactive } from 'vue';
export default defineComponent({
  setup() {
    const visible = ref(false);
    const data = reactive({
      name: 'Tom',
      age: 20,
      address: 'Chengdu',
    });
    const handleClick = () => {
      visible.value = true;
    };
    const hidden = () => {
      visible.value = false;
    };

    return { visible, data, handleClick, hidden };
  },
});
</script>

信息提示

各种类型的信息提示框。
<template>
  <d-button class="mr-1" @click="handleClick('success')">success</d-button>
  <d-button class="mr-1" @click="handleClick('failed')">failed</d-button>
  <d-button class="mr-1" @click="handleClick('warning')">warning</d-button>
  <d-button class="mr-1" @click="handleClick('info')">info</d-button>
  <d-modal v-model="visible" title="Start Snapshot Version" :type="type">
    <div>name: {{ data.name }}</div>
    <div>age: {{ data.age }}</div>
    <div>address: {{ data.address }}</div>
  </d-modal>
</template>

<script>
import { defineComponent, ref, reactive } from 'vue';

export default defineComponent({
  setup() {
    const visible = ref(false);
    const type = ref('');
    const data = reactive({
      name: 'Tom',
      age: 20,
      address: 'Chengdu',
    });
    const handleClick = (t) => {
      visible.value = true;
      type.value = t;
    };

    return { visible, data, handleClick, type };
  },
});
</script>

关闭前回调

before-close在用户点击关闭按钮或者遮罩层时会被调用,可在完成某些异步操作后,通过done参数关闭。close在Modal关闭时触发。
<template>
  <d-button @click="handleClick">打开 modal</d-button>
  <d-modal v-model="visible" :before-close="beforeClose" @close="handleClose" style="width: 500px;">
    <div>name: {{ data.name }}</div>
    <div>age: {{ data.age }}</div>
    <div>address: {{ data.address }}</div>
    <template #footer>
      <d-modal-footer style="text-align: right; padding-right: 20px;">
        <d-button @click="hidden">取消</d-button>
        <d-button @click="hidden">确认</d-button>
      </d-modal-footer>
    </template>
  </d-modal>
</template>

<script>
import { ref, defineComponent, reactive } from 'vue';

export default defineComponent({
  setup() {
    const visible = ref(false);
    const data = reactive({
      name: 'Tom',
      age: 20,
      address: 'Chengdu',
    });
    const handleClick = () => {
      visible.value = true;
    };
    const hidden = () => {
      visible.value = false;
    };
    const beforeClose = (done) => {
      new Promise((resolve) => {
        setTimeout(resolve, 1000);
      }).then(done);
    };
    const handleClose = () => {
      console.log("Modal关闭")
    }

    return { visible, data, handleClick, hidden, beforeClose, handleClose };
  },
});
</script>

弹窗中的按钮配置 autofocus 自动获取焦点

button 配置 autofocus 可以自动获取焦点,可以通过回车 enter 触发按钮按钮点击
<template>
  <d-button @click="handleClick">打开 modal</d-button>
  <d-modal v-model="visible" title="Start Snapshot Version">
    <div>name: {{ data.name }}</div>
    <div>age: {{ data.age }}</div>
    <div>address: {{ data.address }}</div>
    <template #footer>
      <d-modal-footer style="text-align: right; padding-right: 20px;">
        <d-button @click="hidden" autofocus>取消</d-button>
        <d-button @click="hidden">确认</d-button>
      </d-modal-footer>
    </template>
  </d-modal>
</template>

<script>
import { defineComponent, ref, reactive } from 'vue';
export default defineComponent({
  setup() {
    const visible = ref(false);
    const data = reactive({
      name: 'Tom',
      age: 20,
      address: 'Chengdu',
    });
    const handleClick = () => {
      visible.value = true;
    };
    const hidden = () => {
      visible.value = false;
    };
    return { visible, data, handleClick, hidden };
  },
});
</script>
参数名类型默认值说明跳转 Demo
v-modelbooleanfalse是否显示 Modal基础用法
titlestring-可选,Modal 的标题基础用法
keep-lastbooleanfalse可选,是否保留上次移动位置保留最后一次关闭位置
lock-scrollbooleantrue可选,是否将 body 滚动锁定
close-on-click-overlaybooleantrue可选,点击空白处是否能关闭 Modal
before-close(done) => void-可选,关闭前的回调,调用 done 可关闭 Modal关闭前回调
escapablebooleantrue可选,是否支持 esc 键关闭弹窗
show-closebooleantrue可选,是否展示关闭按钮
draggablebooleantrue可选,弹框是否可拖拽
show-animationbooleantrue可选,是否显示动画
show-overlaybooleantrue可选,是否展示遮罩层
append-to-bodybooleantrue可选,是否将 Modal 提升到 body 层
typesuccess | failed | warning | info-可选,弹框信息提示
事件名类型说明
close-Modal 关闭时触发
插槽名说明
defaultModal 内容
header自定义 Modal 顶部,使用 title 和 header 会优先渲染 header
footer自定义 Modal 底部
Contributors