Skip to content

Notification

Floats in the corners of the page, displaying global notification and reminder messages.

Basic Usage

Notification is suitable for passive reminders of system-level notifications.

Basic Usage

Different States

Used to display system notifications such as "Success, Warning, Info, Error".

Different States

Custom Position

Use the position property to set the position of the notification popup.

Custom Position

Custom Duration

Set the duration property to 0 to prevent the notification from closing automatically.

Custom Duration

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

Hide Close Button

Set showClose to false to hide the close button.

Hide Close Button

Max Count

Use the max property to limit the maximum number of notifications displayed at the same position. When the limit is exceeded, the oldest notification will be automatically closed.

Max Limit

Functional Message 2.8.0

The message property can be a VNode, or a function that returns a VNode.

Warning

While the message property supports passing HTML snippets, dynamic rendering of arbitrary HTML is very dangerous as it can easily lead to XSS attacks. Therefore, when dangerouslyUseHTMLString is enabled, ensure the content of message is trusted. Never assign user-submitted content to the message property.

Functional Message

Use in Nuxt

The Notification component is deeply integrated with Nuxt 3/4. As an imperative component, it automatically identifies server/client environments, ensuring that notification popups are only executed in the client browser to avoid errors during the SSR phase.

Use in Nuxt

SSR Notes:

  • Safe environment identification: Environment detection is encapsulated inside the functions, so calling them in Nuxt's setup or lifecycles is safe.
  • Auto-import: In Nuxt projects, the YhNotification function is automatically scanned and imported without additional manual import.
  • Style preloading: Component styles are automatically injected with the Nuxt page rendering, ensuring visual effects meet expectations when popping up.
  • ⚠️ Server constraints: Calling this function during useAsyncData or in a server-side Middleware will have no visual output (since there is no DOM).
  • 💡 Global methods: The Nuxt plugin automatically mounts $notify globally. You can also call it via this.$notify in options-style components.

Error Log Integration

Combined with Nuxt's app:error hook, you can use YhNotification.error to capture and display global unhandled runtime errors.

Call Signature

YhNotification 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="showNotification">Show Notification</yh-button>
</template>

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

const showNotification = () => {
  YhNotification.success('Success', 'This is a success message')
}
</script>

Options API

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

vue
<template>
  <yh-button @click="showNotification">Show Notification</yh-button>
</template>

<script>
export default {
  methods: {
    showNotification() {
      this.$notify.success('Success', 'Operation successful!')
    }
  }
}
</script>

Individual Import

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

API

Methods

NameDescriptionParameters
YhNotificationShow notificationoptions
YhNotification.successShow success notification(title, message | options)
YhNotification.warningShow warning notification(title, message | options)
YhNotification.infoShow info notification(title, message | options)
YhNotification.errorShow error notification(title, message | options)
YhNotification.closeAllClose all notifications

Props

PropDescriptionTypeDefault
titleTitlestring
messageNotification contentstring | VNode | (() => VNode)
typeNotification type'success' | 'warning' | 'info' | 'error'
iconCustom iconstring | VNode
showCloseWhether to show close buttonbooleantrue
durationDisplay duration (ms), set to 0 to prevent auto-closenumber4500
offsetOffset from the window edge (px)number16
positionPopup position'top-right' | 'top-left' | 'top-center' | 'bottom-right' | 'bottom-left' | 'bottom-center''top-right'
dangerouslyUseHTMLStringWhether to treat message as an HTML snippetbooleanfalse
onCloseCallback function when closing() => void
onClickCallback function when clicking the notification() => void
zIndexz-index levelnumber
customClassCustom class namestring
maxLimit the number of notifications shown at the same positionnumber

Return Value

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

MethodDescription
closeClose current Notification
typescript
const handler = YhNotification.success('Success', 'Message content')

// Close manually
handler.close()

Slots

Slot NameDescription
defaultCustom content area
iconCustom icon area

Expose

NameDescriptionType
visibleNotification visibility statusRef<boolean>
closeClose current notification() => void

Theme Variables

The Notification component use following CSS variables, which can be overridden to customize the style:

VariableDescriptionDefault
--yh-notification-bg-colorBackground colorvar(--yh-color-bg-elevated)
--yh-notification-border-colorBorder colorvar(--yh-border-color-light)
--yh-notification-shadowShadowvar(--yh-box-shadow-small)
--yh-notification-title-colorTitle colorvar(--yh-color-text-primary)
--yh-notification-text-colorContent text colorvar(--yh-color-text-secondary)
--yh-notification-close-colorClose button colorvar(--yh-color-text-secondary)
--yh-notification-close-hover-colorClose button hover colorvar(--yh-color-text-primary)
--yh-notification-icon-sizeIcon size24px
--yh-notification-widthNotification width330px
--yh-notification-paddingPadding20px
--yh-notification-radiusBorder radiusvar(--yh-border-radius-base)

Released under the MIT License.