Skip to content

MessageBox

A message dialog mimicking the native system experience, integrated with YH-UI's signature acrylic visual language, GPU-accelerated drag interactions, and rigorous business lifecycle control.

Basic Usage

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

Alert
Confirm Dialog
Prompt

Different States

Used to display operational feedback such as "Success, Warning, Info, Error".

Success State
Warning State
Error State
Info State

Custom Content

The message property supports passing HTML strings or VNodes.

VNode Rendering
Using HTML Snippet

Warning

While dangerouslyUseHTMLString is convenient, it can easily lead to XSS attacks. Ensure the content of message is trusted. Never assign user-submitted content to the message property.

Vision and Layout

YH-UI flagship features, providing frosted glass texture, drag positioning, and full axis alignment.

Center Layout
Draggable

Asynchronous Close Interception

The beforeClose hook can precisely intercept exit actions. It is commonly used for handling async API logic, with the component automatically maintaining the confirm button's loading state.

Async Interception

Loading State

By using the confirmButtonLoading property or manually setting instance.confirmLoading = true in the beforeClose hook, you can display a loading animation directly on the confirm button. The component automatically handles button disabling.

Async Loading Demo

Global Default Configuration

If most popups in your application share consistent visual preferences (e.g., globally enabling the acrylic effect), you can use setDefaults for one-click global configuration to avoid repetitive parameter passing.

Global Configuration Demo

Use in Nuxt

YhMessageBox fully supports Nuxt 3/4. As a directive-called component, it automatically detects the environment during SSR (Server-Side Rendering) to ensure popup logic only executes on the client.

Nuxt Usage Demo

Notes:

  • Full Auto Import: In Nuxt projects, YhMessageBox and its alias functions are automatically mapped without explicit import.
  • SSR Safe: process.client detection is encapsulated inside the functions, so you can safely call them in lifecycles or methods.
  • 💡 Style Sync: Component styles are automatically injected via Nuxt plugins, ensuring visual continuity after the first screen is hydrated.

Production Practice

In the Nuxt ecosystem, it is recommended to use YhMessageBox in defineNuxtComponent or setup logic for business interception, achieving an excellent global error alert experience when combined with useFetch interceptors.

Global Methods

If you have fully imported YH-UI, it adds the following global methods to app.config.globalProperties: $msgbox, $alert, $confirm, and $prompt. Thus, you can call MessageBox using the methods shown on this page in a Vue instance.

  • $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

Now MessageBox accepts the constructor's context as the second parameter (if you are using MessageBox variables). This parameter allows you to inject the current application context into the message, inheriting all application properties.

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

// In your setup method
const { appContext } = getCurrentInstance()!
// Pass parameters like this:
YhMessageBox({}, appContext)
// Or use different calling methods:
YhMessageBox.alert('Hello world!', 'Title', {}, appContext)

API

Methods

MethodDescriptionParameter TypeReturn Value
alertPops up an alert message box(message, title?, options?, appContext?)Promise<void>
confirmPops up an operation confirmation box(message, title?, options?, appContext?)Promise<MessageBoxAction>
promptPops up a content input box(message, title?, options?, appContext?)Promise<{ value, action }>
setDefaultsModifies global default configuration items(defaults: MessageBoxOptions)

MessageBoxOptions

PropDescriptionTypeDefault
titleTitlestringTip
messageContentstring | VNode | (() => VNode)
typePopup type'alert' | 'confirm' | 'prompt'
iconTypeState icon type'success' | 'warning' | 'info' | 'error'
iconCustom iconstring | Component | VNode
widthPopup widthstring | number420
dangerouslyUseHTMLStringWhether to render message as HTMLbooleanfalse
showCloseWhether to show close button at top-rightbooleantrue
showConfirmButtonWhether to show confirm buttonbooleantrue
showCancelButtonWhether to show cancel buttonbooleantrue
confirmButtonTextConfirm button textstringOK
cancelButtonTextCancel button textstringCancel
closeOnClickModalWhether to close when clicking the overlaybooleantrue
closeOnPressEscapeWhether to close when pressing ESCbooleantrue
lockScrollWhether to lock scrollbarbooleantrue
glassWhether to enable acrylic glass modebooleanfalse
centerWhether to center content layoutbooleanfalse
roundButtonWhether to use rounded buttonsbooleanfalse
draggableWhether popup is draggablebooleanfalse
draggableBoundaryWhether to prevent dragging out of viewportbooleantrue
customClassCustom class namestring
inputPlaceholderInput placeholder (prompt only)string
inputValueInitial input value (prompt only)string
inputPatternInput validation regex (prompt only)RegExp
inputValidatorCustom input validation function (prompt only)(value: string) => boolean | string
inputErrorMessageValidation error message (prompt only)string
beforeCloseHook before closing(action, instance, done) => void
callbackCallback after closing(action, instance) => void
appContextApplication context (Vue Context)AppContext
autofocusWhether to autofocus on openingbooleantrue
appendToSets the root element for the componentstring | HTMLElementdocument.body
confirmButtonLoadingWhether confirm button shows loading statusbooleanfalse
cancelButtonLoadingWhether cancel button shows loading statusbooleanfalse
loadingIconCustom loading iconstring | Component | VNode

MessageBoxInstance (instance in beforeClose)

Prop/MethodDescriptionType
confirmLoadingLoading state of the confirm buttonboolean
cancelLoadingLoading state of the cancel buttonboolean
openOpens the popup(options: MessageBoxOptions) => void
closeCloses the popup() => void

Theme Variables

VariableDescriptionDefault
--yh-scrollbar-widthScrollbar width of the current system (auto-injected)
--yh-bg-color-overlayPopup background colorvar(--yh-bg-color-overlay)
--yh-text-color-primaryTitle colorvar(--yh-text-color-primary)

Released under the MIT License.