Skip to content

@neosjs/modal ​

弹窗组件。通过 Teleport 挂载到指定容器,支持标题栏拖拽移动、遮罩层、ESC 关闭与关闭时销毁内容,自带淡入与缩放动画。

安装 ​

sh
pnpm add @neosjs/modal
sh
npm i @neosjs/modal
sh
yarn add @neosjs/modal

快速上手 ​

用 open 控制弹窗显示,通过 header、默认、footer 三个插槽组织内容。

vue
<script setup lang="ts">
import { Modal } from '@neosjs/modal'
import { ref } from 'vue'

const open = ref(false)
</script>

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

  <Modal :open="open" @update:visible="open = $event">
    <template #header>
      <span>标题</span>
    </template>
    <p>弹窗内容</p>
    <template #footer>
      <button @click="open = false">
        关闭
      </button>
    </template>
  </Modal>
</template>

TIP

组件关闭时通过 update:visible 与 onClosed 事件通知父组件,请监听 @update:visible 同步 open 状态。

拖拽与定位 ​

  • moveable 为 true 时,可按住头部区域拖动弹窗,拖动范围被限制在视口内,每次拖动会触发 onDragMove 事件。
  • left / top 大于 0 时按坐标定位;小于等于 0 时在视口中按该偏移居中(默认居中)。
  • centered 为 true 时使用 CSS 居中(transform),不受拖拽影响。

Props ​

参数类型默认值说明
openboolean必填是否打开弹窗
widthnumber480弹窗宽度(px)
heightnumber270弹窗高度(px)
leftnumber0弹窗初始横坐标;<= 0 时按偏移水平居中
topnumber0弹窗初始纵坐标;<= 0 时按偏移垂直居中
centeredbooleanfalse是否使用 CSS 方式居中显示
moveablebooleantrue是否可拖拽移动
maskbooleanfalse是否显示遮罩层
closablebooleantrue是否显示右上角关闭按钮
mask-closablebooleantrue点击遮罩是否关闭
close-on-escbooleantrue按 ESC 是否关闭
content-styleCSSProperties—弹窗内容的自定义样式
append-tostring'body'挂载的目标容器选择器
destroy-on-closebooleantrue关闭时是否销毁弹窗内容

Emits ​

事件名参数说明
update:visible(payload: boolean)弹窗关闭流程触发,值为 false
onClosed—弹窗关闭时触发
onDragMove(x: number, y: number)拖拽移动时触发,返回当前坐标

Slots ​

插槽名说明
header头部区域,按住该区域可拖动弹窗
default弹窗内容
footer底部区域