Skip to content

Modal 模态框

查看 sn-modal 的 2.0 版本差异

基础用法

  • 自带完整 UI 模板的模态对话框:标题 + 内容 + 确定/取消按钮
  • 弹出层类组件,必须放在 sn-page 中使用(作为 sn-page 的子节点或页面根节点),组件依赖 sn-page 注入的 CSS 变量
  • 内容区内置 scroll-view,超过窗口高度 80% 时自动限高滚动
  • 通过 ref 绑定组件后调用 open / close 方法控制显隐;confirm / cancel 事件在点击对应按钮时触发,按钮点击后自动关闭模态框
  • 与 API snu.showModal 的区别:组件需先放置到页面并用 ref 调用,但支持三个插槽自由定制内容,适用于特殊场景;一般场景用 API 即可
vue
<template>
	<sn-modal ref="modalEle" title="提示" content="确定执行此操作吗?" @confirm="onConfirm" @cancel="onCancel"></sn-modal>
</template>

更多演示请下载 demo 查看

自定义动画

openAnimation / closeAnimation 传入函数后完全接管默认动画,函数签名为 (mask: UniElement | null, content: UniElement | null, duration: number) => voidmask 为遮罩节点,content 为模态框节点,duration 为本次动画时长(ms)。

vue
<template>
	<sn-modal ref="modalEle" title="弹跳进入" :open-animation="bounceOpenAnim"></sn-modal>
</template>

<script lang="uts" setup>
	const bounceOpenAnim = (mask: UniElement | null, content: UniElement | null, duration: number): void => {
		if (mask != null) {
			mask.animate(
				[{ opacity: '0' } as UniAnimationKeyframe, { opacity: '1' } as UniAnimationKeyframe],
				{ duration: duration, easing: 'ease-out', fill: 'forwards' }
			)
		}
		if (content != null) {
			content.animate(
				[
					{ transform: 'scale(0.6)' } as UniAnimationKeyframe,
					{ transform: 'scale(1.08)' } as UniAnimationKeyframe,
					{ transform: 'scale(1)' } as UniAnimationKeyframe
				],
				{ duration: duration, easing: 'ease-out', fill: 'forwards' }
			)
		}
	}
</script>

属性

参数说明类型默认值可选值
title模态框标题String''-
titleAlign标题对齐方式Stringcenterleft | center | right
titleSize标题字体大小,空值时为 $17String | Number''-
titleFont标题字体String''-
titleColor标题颜色,支持 $ 简写主题色,空值时使用主题标题色String''-
bgColor模态框背景颜色,支持 $ 简写主题色,空值时使用主题前景色 $frontString''-
borderRadius模态框圆角大小,空值时为 $12String | Number''-
content模态框内容String''-
contentAlign内容对齐方式Stringcenterleft | center | right
contentSize内容字体大小,空值时为 $15String | Number''-
contentColor内容颜色,支持 $ 简写主题色,空值时使用主题正文色String''-
contentFont内容字体String''-
buttonType按钮类型,embed 为底部嵌入式文字按钮,float 为浮动式 sn-button 并排展示Stringembedembed | float
buttonBorder嵌入式按钮分隔边框样式,颜色支持 $ 简写主题色,空值时为 0.5px solid $lineString''-
confirmText确定按钮文本String确定-
confirmTextColor确定按钮文本颜色,支持 $ 简写主题色,空值时使用主题 $primaryDarkString''-
confirmTextSize确定按钮文本大小,空值时为 $16String | Number''-
showCancel是否显示取消按钮Booleantruetrue | false
showConfirm是否显示确定按钮Booleantruetrue | false
cancelText取消按钮文本String取消-
cancelTextColor取消按钮文本颜色,支持 $ 简写主题色,空值时使用主题正文色String''-
cancelTextSize取消按钮文本大小,空值时为 $16String | Number''-
position弹出位置,弹出动画随位置变化Stringcentercenter | top | bottom | left | right
aniTime动画时长(ms),支持 $ 前缀按动画乘数缩放,空值时使用框架长动画时长String | Number''$long | $normal | $short | 数值
maskClose点击遮罩是否关闭模态框Booleanfalsetrue | false
disabled是否禁用操作按钮,禁用后点击无效且颜色变为禁用色Booleanfalsetrue | false
preventBack是否阻止返回键关闭模态框Booleanfalsetrue | false
maskOpacity遮罩透明度Number0.4-
openAnimation自定义打开动画函数,传入后接管默认打开动画Function | nullnull-
closeAnimation自定义关闭动画函数,传入后接管默认关闭动画Function | nullnull-
customStyle自定义模态框主体样式UTSJSONObject | String''-

事件

名称类型说明
open() => Void打开时触发
close() => Void关闭时触发
clickMask() => Void点击遮罩时触发
confirm() => Void点击确定按钮时触发,触发后自动关闭模态框
cancel() => Void点击取消按钮时触发,触发后自动关闭模态框

方法

名称参数返回值描述
open--打开模态框
close--关闭模态框(不触发 confirm / cancel 事件)
confirm--相当于点击确定按钮:触发 confirm 事件并关闭模态框(禁用状态下无效)
cancel--相当于点击取消按钮:触发 cancel 事件并关闭模态框(禁用状态下无效)

插槽

名称说明
header替换标题区域
content替换内容区域
actions替换操作按钮区域

使用 MIT 协议