Skip to content

Dialog API

v0.3.11 · Latest stable

Methods

MethodSignatureDescription
openModal(options: SoDialogModalOptions)Open a modal dialog.
openOffcanvas(options: Omit<SoDialogOffcanvasOptions, 'kind'>)Open an offcanvas panel dialog.
confirmModal(options?: SoDialogConfirmOptions)Open a confirm dialog and return Promise<boolean>.
promptModal(options?: SoDialogPromptOptions)Open a prompt dialog and return `Promise<string
formModal(options: SoDialogFormOptions)Open a dynamic form dialog and return submitted values.
SoDialog.open(options: SoDialogOptions)Open modal or offcanvas by kind.

openModal

签名

ts
openModal(options: SoDialogModalOptions): SoDialogHandle

参数

参数名类型默认值必填描述SinceDeprecated
titlestring | HTMLElement-Dialog 标题;HTMLElement 会原样放入标题区域。--
contentstring | Node-Dialog 主体内容。--
widthnumber | stringauto面板宽度;数字按 px 处理,字符串接受 CSS 尺寸。--
presetdeploy-启用可选预设视觉风格。--
closeOnEscbooleantrue是否允许 Escape 关闭。--
onLayoutStableSoLayoutStableHook-布局稳定后触发,用于测量、埋点或提示。--

返回值

返回 SoDialogHandle,可调用 close()refit()setFooterButtons()onAction()

最小示例

ts
import { openModal } from 'sodialog'

openModal({
  title: 'Hello',
  content: '<p>Your dialog is ready.</p>',
})

行为说明

openModal 默认使用原生 showModal(),保留 <dialog> 焦点语义。传入显式 widthheight 后会优先采用显式尺寸。

边界情况

如果同一个 id 已存在,实例复用行为以当前实现为准;需要销毁时使用返回句柄或 footer action 的 destroy 策略。

相关 API

Core Options (SoDialogBaseOptions)

NameTypeRequiredDefaultDescription
titlestring | HTMLElementYes-Dialog title text, or an HTMLElement rendered inside the title region.
contentstring | NodeYes-Dialog content.
hideHeaderbooleanNofalseHide the generated header. Dialog keeps aria-label from title.
hideCloseButtonbooleanNofalseHide the header close button.
closeButtonLabelstringNoCloseAccessible label for the close button.
closeButtonTextstringNo×Visible close button text.
confirmTextstringNo确认Confirm button text.
cancelTextstringNo取消Cancel button text.
confirmAction'hide' | 'destroy'NohideConfirm click close strategy.
closeOnEscbooleanNotrueAllow Esc to close the dialog.
closeOnBackdropbooleanNotrueAllow backdrop click close.
hideFooterbooleanNofalseHide default footer buttons.
footerAlign'start' | 'center' | 'end' | 'between'NoendFooter layout alignment.
footerButtonsSoDialogFooterButton[]Nobuilt-in confirm/cancelCustom footer buttons.
traceIdstringNo-Diagnostic trace identifier.
onLayoutStable(ctx) => voidNo-Triggered when layout is stable.
onAction(ctx) => voidNo-Triggered on footer actions.
onLifecycle/onBeforeOpen/...SoLifecycleHookNo-Dialog lifecycle hooks.
NameTypeDefaultDescription
idstringauto idReuse an existing modal instance by id.
position'center' | 'top' | 'bottom'centerModal position.
animation'slide' | 'fade' | 'zoom'fadeModal animation.
widthnumber | stringautoPanel width. Numbers are interpreted as pixels; strings accept CSS sizes.
heightnumber | stringautoPanel height. Numbers are interpreted as pixels; strings accept CSS sizes.
useModalbooleantrueUse native showModal() behavior.
draggablebooleantrueEnable drag interactions. Set false to disable dragging.
dragHandleSoModalDragHandle | falseheaderDrag handle selector target(s). Supports header, title, body, footer, panel, CSS selectors, arrays, or false.
autoFitSizebooleantrueAuto fit panel to content changes.
scrollMode'body' | 'viewport' | 'none' | 'hybrid'autoAuto-fit scroll strategy.

Providing width or height disables automatic modal sizing so the explicit dimensions take precedence.

Presets

preset: 'deploy' applies the compact confirmation style used by deploy/release dialogs. It is implemented as the sod-preset-deploy class inside the standard stylesheet, so it keeps zero runtime dependencies and can still be overridden with --sod-* CSS variables.

Offcanvas-only Options (SoDialogOffcanvasOptions)

NameTypeDefaultDescription
placement'start' | 'end' | 'top' | 'bottom'endPanel placement.
animation'slide' | 'fade' | 'zoom'slideOffcanvas animation.
widthnumber | stringplacement defaultPanel width. Numbers are interpreted as pixels; strings accept CSS sizes.
heightnumber | stringplacement defaultPanel height. Numbers are interpreted as pixels; strings accept CSS sizes.

Return Handle (SoDialogHandle)

openModal and openOffcanvas return a SoDialogHandle which can be used to close, update, or inspect state.

FieldTypeDescription
dialogHTMLDialogElementNative dialog element.
close() => voidClose dialog.
refit() => voidTrigger layout refit manually.
setFooterButtons(buttons) => voidReplace footer button set.
updateFooterButton(id, updates) => booleanPatch one button by id.
onAction(listener) => () => voidSubscribe footer actions and return unsubscribe.

Usage Example

ts
import { openModal } from 'sodialog'

const handle = openModal({
  title: 'Delete item',
  content: '<p>Do you want to continue?</p>',
  confirmText: 'Delete',
  cancelText: 'Cancel',
})

handle.close()

Released under the MIT License.