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:

typescript
snu.showModal({
	title: '提示',
	content: '确定执行此操作吗?',
	success: (confirm, cancel) => {}
})

API 无需在页面放置组件、无需绑定 ref,import { snu } from '@/uni_modules/sinle-ui' 后即可在任意位置调用,常用配置字段与组件 props 一致,回调结果通过 success 返回。

组件与 API 的唯一差异是组件支持 header / content / actions 三个插槽,可自由替换标题、内容与操作区;不需要插槽定制时,绝大多数场景使用 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) => void:mask 为遮罩节点,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标题字体大小String | Number$17-
titleFont标题字体String''-
titleColor标题颜色,支持 $ 简写主题色String$title-
bgColor模态框背景颜色,支持 $ 简写主题色String$front-
borderRadius模态框圆角大小String | Number$12-
content模态框内容String''-
contentAlign内容对齐方式Stringcenterleft | center | right
contentSize内容字体大小String | Number$15-
contentColor内容颜色,支持 $ 简写主题色String$text-
contentFont内容字体String''-
buttonType按钮类型,embed 为底部嵌入式文字按钮,float 为浮动式 sn-button 并排展示Stringembedembed | float
buttonBorder嵌入式按钮分隔边框样式,颜色支持 $ 简写主题色String0.5px solid $line-
confirmText确定按钮文本String确定-
confirmTextColor确定按钮文本颜色,支持 $ 简写主题色String$primaryDark-
confirmTextSize确定按钮文本大小String | Number$16-
showCancel是否显示取消按钮Booleantruetrue | false
showConfirm是否显示确定按钮Booleantruetrue | false
cancelText取消按钮文本String取消-
cancelTextColor取消按钮文本颜色,支持 $ 简写主题色String$text-
cancelTextSize取消按钮文本大小String | Number$16-
position弹出位置,弹出动画随位置变化Stringcentercenter | top | bottom | left | right
aniTime动画时长(ms),支持 $ 前缀按动画乘数缩放String | Number$long$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 协议