Skip to content

Message

Used for feedback hints after active operations. The difference from Notification is that the latter is used more for passive reminders of system-level notifications.

Basic Usage

Appears from the top and disappears automatically after 3 seconds.

Basic Usage

Different States

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

Different States

Closable

A close button can be added. By default, Message cannot be closed manually. If you need a manually closable Message, use the showClose property. Also, setting duration to 0 can prevent the message from closing automatically.

Closable

Centered Text

Use the center property to horizontally center the text.

Centered Text

Use HTML Snippet

Set dangerouslyUseHTMLString to true to treat the message property as an HTML snippet.

Warning

While the message property supports passing HTML snippets, dynamic rendering of arbitrary HTML is very dangerous. Ensure the content of message is trusted. Never assign user-submitted content to the message property.

Use HTML Snippet

Grouping

Set grouping to true, and message units with the same content will be merged.

Grouping

Placement

Set the placement property to control where the message appears.

Placement

Use in Nuxt

The Message component fully supports Nuxt 3/4 environments. As a functional directive component, it automatically performs environment detection during SSR (Server-Side Rendering) to ensure that the message popup logic is only executed on the web browser side.

Use in Nuxt

SSR Notes:

  • Safe Calling: Calling functions directly in templates or at the top level of setup is safe, as window detection is handled internally.
  • Auto-import: In Nuxt projects, the YhMessage function and its alias callers are automatically imported without manual import.
  • ⚠️ Server Constraints: Calling message hints during Nuxt's asyncData or fetch server execution phase is ineffective (since there is no DOM container).
  • 💡 Style Consistency: Message styles will be loaded asynchronously along with the CSS, ensuring that the visual style after first-screen hydration is highly unified with the SSR page.

Error Catching Suggestion

In Nuxt's useFetch interceptors or onError hooks, you can directly use YhMessage.error to provide users with friendly server-side error message hints.

Call Signature

YhMessage supports multiple calling methods. Choose the appropriate one according to your project needs.

Import and use directly in <script setup>:

vue
<template>
  <yh-button @click="showMessage">Show Message</yh-button>
</template>

<script setup lang="ts">
import { YhMessage } from '@yh-ui/yh-ui'

const showMessage = () => {
  YhMessage.success('This is a success message')
}
</script>

Options API

YH-UI adds the global method $message to app.config.globalProperties. You can call it using this.$message in the Options API:

vue
<template>
  <yh-button @click="showMessage">Show Message</yh-button>
</template>

<script>
export default {
  methods: {
    showMessage() {
      this.$message.success('This is a success message')
    }
  }
}
</script>

Individual Import

typescript
import { YhMessage } from '@yh-ui/yh-ui'

API

Methods

NameDescriptionParameters
YhMessageShow messageoptions | string
YhMessage.successShow success messageoptions | string
YhMessage.warningShow warning messageoptions | string
YhMessage.infoShow info messageoptions | string
YhMessage.errorShow error messageoptions | string
YhMessage.closeAllClose all messages-

Props

PropDescriptionTypeDefault
messageMessage content. The current template renders prop content as plain text or HTML only, so prop-based VNode content is declared in the type but is not rendered as a vnode by the current implementation.string | VNodeundefined
typeMessage type.'success' | 'warning' | 'info' | 'error''info'
iconCustom icon prop. Declared in the type, but the current implementation does not consume it. Use the icon slot for custom icons.string | VNodeundefined
show-closeWhether to show the close button.booleanfalse
durationDisplay duration in milliseconds. Set to 0 to keep the message open until manually closed.number3000
offsetOffset used by the component instance. In the service API, omitted top placements start from 64 and bottom placements start from 20.number20
idInternal id used by the service runtime.stringundefined
dangerously-use-html-stringWhether to render message as HTML.booleanfalse
centerWhether to center the content text.booleanfalse
on-closeCallback invoked when the message closes.() => voidundefined
z-indexCustom z-index.numberundefined
custom-classExtra class name.stringundefined
groupingMerge messages with the same message and type.booleanfalse
repeat-numRuntime repeat count used by grouped messages. This is maintained by the service implementation.numberundefined
placementMessage placement.'top' | 'top-left' | 'top-right' | 'bottom' | 'bottom-left' | 'bottom-right''top'
theme-overridesComponent-level theme override variables.ComponentThemeVarsundefined

Events

EventDescriptionParameters
destroyTriggered after the leave transition finishes.() => void

Slots

Slot NameDescription
defaultCustom message content for component usage.
iconCustom icon content.

Expose

Calling YhMessage returns an instance of the current Message. If you need to manually close the instance, call its close method.

NameDescriptionType
closeClose current Message.() => void
visibleCurrent visibility state.Ref<boolean>

Theme Variables

VariableDescriptionDefault
--yh-message-bg-colorBackground colorvar(--yh-bg-color-overlay)
--yh-message-border-colorBorder colorvar(--yh-border-color-lighter)
--yh-message-shadowMessage box shadowvar(--yh-shadow-lg)
--yh-message-text-colorText colorvar(--yh-text-color-primary)
--yh-message-close-colorClose button colorvar(--yh-text-color-secondary)
--yh-message-close-hover-colorClose button hover colorvar(--yh-text-color-primary)

Type Exports

NameDescription
YhMessagePropsProps type for the internal message component
YhMessageEmitsEmits type for the internal message component
YhMessageSlotsSlots type for the internal message component
YhMessageExposeExpose type for the internal message component
YhMessageInstancePublic instance type for a single message
YhMessageOptionsService options type for YhMessage(...)
YhMessageHandlerReturned handler type
YhMessageContextInternal runtime context type
YhMessageFnMessage function signature type
YhMessageTypeMessage type union
YhMessagePlacementMessage placement union

Released under the MIT License.