Modal
npm: sodialog
CDN 复制说明:生产示例固定当前文档版本,不建议使用
@latest。
css:https://cdn.jsdelivr.net/npm/[email protected]/dist/sodialog.css
js:https://cdn.jsdelivr.net/npm/[email protected]/dist/sodialog.es.js
Modal Basic Demo
Open in New TabSource Code
Modal Promise Flow Demo
Open in New TabSource Code
Level 1. Basic Usage
基础打开
ts
import { openModal } from 'sodialog'
openModal({
title: '基础示例',
content: '<p>这是 Modal 的基础示例。</p>',
width: 640,
height: '70vh',
confirmText: '确认',
cancelText: '取消',
})width 和 height 支持数字(按 px)或 CSS 尺寸字符串;设置任一尺寸后,将关闭自动尺寸适配并优先采用显式尺寸。
Modal can be dragged from the header by default. Use draggable: false to disable dragging, or dragHandle: ['header', 'body', 'footer'] when those regions should all start a drag.
Level 2. Add lifecycle and tracing
布局稳定钩子与 trace
ts
import { openDialog, pushMessage } from 'sodialog'
openDialog({
title: '稳定时机示例',
content: '<p>观察 onLayoutStable 与 action 回调输出。</p>',
traceId: 'trace-modal-lab-001',
onLayoutStable: ({ traceId }) => {
pushMessage('success', '布局已稳定', { traceId, duration: 1300 })
},
onAction: ({ action, traceId }) => {
console.log(action, traceId)
},
})Preset style
ts
openModal({
title: 'Ready to deploy',
content: '<p>All checks passed. Production is ready.</p>',
preset: 'deploy',
confirmText: 'Deploy now',
cancelText: 'Cancel',
})preset: 'deploy' applies the built-in sod-preset-deploy class from the standard stylesheet. It adds no dependency and can still be customized with --sod-* variables.
Level 3. Promise flow composition
Promise 组合流程
ts
import { confirmModal, promptModal, formModal, pushMessage } from 'sodialog'
const ok = await confirmModal({
title: '确认',
content: '<p>继续执行串行流程?</p>',
})
if (!ok) return
const note = await promptModal({ title: '输入备注', placeholder: '请输入内容' })
if (note === null) return
await formModal({
title: '补充信息',
fields: [{ name: 'owner', label: '负责人', required: true }],
})
pushMessage('info', `流程完成,备注:${note}`, {
duration: 1400,
traceId: 'trace-modal-lab-001',
})Level 4. Configure defaults
全局默认配置
ts
import { configureDialog, openModal } from 'sodialog'
configureDialog({
modalDefaults: {
footerAlign: 'center',
closeOnEsc: false,
},
})
openModal({
title: '默认配置生效验证',
content: '<p>本次调用未传 footerAlign/closeOnEsc。</p>',
})Related API
更多可视化示例见 Examples Hub。