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 contentstring | VNode
typeMessage type'success' | 'warning' | 'info' | 'error''info'
iconCustom iconstring | VNode
show-closeWhether to show close buttonbooleanfalse
durationDisplay duration (ms), set to 0 for no auto-closenumber3000
offsetOffset from the top (px)number64
dangerously-use-html-stringWhether to treat message as an HTML snippetbooleanfalse
centerWhether to center contentbooleanfalse
on-closeCallback function when closing() => void
z-indexz-index levelnumber
custom-classCustom class namestring
groupingWhether to support grouping mergebooleanfalse
placementMessage placement'top' | 'top-left' | 'top-right' | 'bottom' | 'bottom-left' | 'bottom-right''top'

Slots

Slot NameDescription
defaultMessage content (used when the message property is insufficient)
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
visibleWhether the current message is visibleRef<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-box-shadow-light)
--yh-message-text-colorText colorvar(--yh-text-color-primary)
--yh-message-close-colorClose button colorvar(--yh-text-color-placeholder)
--yh-message-close-hover-colorClose button hover colorvar(--yh-text-color-regular)

Released under the MIT License.