Skip to content

MessageBox

Displays message dialogs through the YhMessageBox function API, including alert, confirm, and prompt modes.

Basic Usage

Provides three common interaction modes: alert, confirm, and prompt.

Alert
Confirm Dialog
Prompt

Different States

Use iconType to display success, warning, info, or error feedback.

Success State
Warning State
Error State
Info State

Custom Content

The message option supports plain text, VNodes, and HTML strings.

VNode Rendering
Using HTML Snippet

Warning

While dangerouslyUseHTMLString is convenient, it can lead to XSS attacks. Only pass trusted content.

Vision and Layout

Use layout-related options such as glass, center, and draggable to adjust appearance and interaction behavior.

Center Layout
Draggable

Asynchronous Close Interception

Use beforeClose to intercept confirm, cancel, or close actions before the dialog is dismissed.

Async Interception

Loading State

Use confirmButtonLoading, cancelButtonLoading, or update the exposed instance state inside beforeClose to control button loading behavior.

Async Loading Demo

Global Default Configuration

Use setDefaults to update the global default options used by subsequent YhMessageBox calls.

Global Configuration Demo

Use in Nuxt

YhMessageBox can be called in Nuxt client-side interaction flows after the YH-UI module is registered. Because the function API rejects on the server, it should be triggered from browser-side logic such as click handlers or client-only lifecycle code.

Nuxt Usage Demo

Notes:

  • Register @yh-ui/nuxt to use the library in Nuxt with auto-import support.
  • The runtime rejects calls on the server, so open dialogs from browser-side code.
  • Component styles are injected through the normal YH-UI style pipeline after hydration.

Global Methods

When the full library is installed, the following methods are also attached to app.config.globalProperties:

  • $msgbox(options)
  • $alert(message, title, options) or $alert(message, options)
  • $confirm(message, title, options) or $confirm(message, options)
  • $prompt(message, title, options) or $prompt(message, options)

Application Context Inheritance

YhMessageBox accepts an app context so dialogs can inherit the current Vue application configuration.

ts
import { getCurrentInstance } from 'vue'
import { YhMessageBox } from '@yh-ui/yh-ui'

const { appContext } = getCurrentInstance()!

YhMessageBox({}, appContext)
YhMessageBox.alert('Hello world!', 'Title', {}, appContext)

API

Methods

MethodDescriptionParameter TypeReturn Value
YhMessageBoxOpens a message box with the provided options or message content.(options: YhMessageBoxOptions | string | VNode, appContext?: AppContext | null)Promise<{ value?: string; action: YhMessageBoxAction }>
alertOpens an alert dialog.(message, title?, options?, appContext?)Promise<void>
confirmOpens a confirmation dialog.(message, title?, options?, appContext?)Promise<YhMessageBoxAction>
promptOpens an input dialog.(message, title?, options?, appContext?)Promise<{ value: string; action: 'confirm' }>
setDefaultsUpdates the global default options used by later calls.(defaults: YhMessageBoxOptions)void

Message Box Options

PropDescriptionTypeDefault
titleTitle text.string'Tip'
messageDialog content.string | VNode | (() => VNode)undefined
typeDialog type.YhMessageBoxTypeundefined
dangerouslyUseHTMLStringWhether to render message as HTML.booleanfalse
iconTypeState icon type.YhMessageBoxStateundefined
iconCustom icon.string | Component | VNodeundefined
confirmButtonTextConfirm button text.string'确定'
cancelButtonTextCancel button text.string'取消'
showCancelButtonWhether to show the cancel button.booleantrue
showConfirmButtonWhether to show the confirm button.booleantrue
showCloseWhether to show the top-right close button.booleantrue
closeOnClickModalWhether clicking the overlay closes the dialog.booleantrue
closeOnPressEscapeWhether pressing Esc closes the dialog.booleantrue
lockScrollWhether to lock body scroll.booleantrue
glassWhether to enable glass mode.booleanfalse
centerWhether to center content layout.booleanfalse
roundButtonWhether to use rounded buttons.booleanfalse
draggableWhether the dialog is draggable.booleanfalse
draggableBoundaryWhether dragging is kept inside the viewport.booleantrue
widthDialog width.string | number420
customClassCustom class name.stringundefined
inputPlaceholderInput placeholder in prompt mode.stringundefined
inputValueInitial input value in prompt mode.stringundefined
inputPatternInput validation regex in prompt mode.RegExpundefined
inputValidatorCustom validation function in prompt mode.(value: string) => boolean | stringundefined
inputErrorMessageValidation error message in prompt mode.stringundefined
beforeCloseHook called before the dialog closes.(action: YhMessageBoxAction, instance: YhMessageBoxInstance, done: () => void) => voidundefined
callbackCallback invoked by the function API after the dialog closes.(action: YhMessageBoxAction, instance: YhMessageBoxInstance) => voidundefined
appContextVue app context used by the function API for inheritance.AppContext | nullundefined
autofocusWhether to autofocus when opening.booleantrue
appendToMount container used by the function API. If the selector cannot be found, it falls back to document.body.string | HTMLElementdocument.body
confirmButtonLoadingWhether the confirm button shows loading.booleanfalse
cancelButtonLoadingWhether the cancel button shows loading.booleanfalse
loadingIconCustom loading icon. Declared in the type, but the current implementation does not consume it.string | Component | VNodeundefined
themeOverridesComponent-level theme overrides.ComponentThemeVarsundefined

Events

This function entry does not expose component events.

Slots

This function entry does not expose component slots.

Expose

This function entry does not expose a template component instance. Runtime control is available through the YhMessageBoxInstance passed to beforeClose.

Message Box Instance

Prop/MethodDescriptionType
confirmLoadingLoading state of the confirm button.boolean
cancelLoadingLoading state of the cancel button.boolean
openOpens the popup.(options: YhMessageBoxOptions) => void
closeCloses the popup.() => void
setCallbackRegisters the close callback used by the function API.(cb: (res: { action: YhMessageBoxAction; value?: string }) => void) => void

Theme Variables

YhMessageBox supports themeOverrides. The stylesheet mainly consumes global tokens, while runtime overrides generate variables such as the following:

VariableDescriptionDefault
--yh-scrollbar-widthScrollbar width compensation injected at runtime.Runtime injected
--yh-message-box-bg-colorVariable generated from themeOverrides.bgColor.No built-in stylesheet fallback
--yh-message-box-title-colorVariable generated from themeOverrides.titleColor.No built-in stylesheet fallback

Type Exports

NameDescription
YhMessageBoxTypeMessage-box type union
YhMessageBoxDataPrompt result payload type
YhMessageBoxActionAction union returned by confirm / prompt flows
YhMessageBoxStateBuilt-in state icon union
YhMessageBoxInstanceRuntime instance type passed to hooks
YhMessageBoxOptionsOptions type for YhMessageBox(...)
YhMessageBoxHandlerPromise / handler return abstraction

Released under the MIT License.