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
titleTitle.stringundefined
messageNotification content. Supports strings, VNode, and functions returning a VNode.string | VNode | (() => VNode)undefined
typeNotification type.'success' | 'warning' | 'info' | 'error'undefined
iconCustom icon prop. Declared in the type, but the current template does not render the prop value itself. Use the icon slot for custom icons.string | VNodeundefined
showCloseWhether to show close button.booleantrue
durationDisplay duration in milliseconds. Set to 0 to keep the notification open.number4500
offsetOffset from the viewport edge.number16
positionPopup position.'top-right' | 'top-left' | 'top-center' | 'bottom-right' | 'bottom-left' | 'bottom-center''top-right'
idInternal id used by the service runtime.stringundefined
dangerouslyUseHTMLStringWhether to treat message as an HTML snippet.booleanfalse
onCloseCallback when closing.() => voidundefined
onClickCallback when clicking the notification root.() => voidundefined
zIndexz-index level.numberundefined
customClassCustom class name.stringundefined
maxMaximum count allowed at the same position. When exceeded, the oldest notifications at that position are closed first.numberundefined
themeOverridesComponent-level theme overrides.ComponentThemeVarsundefined

Events

EventDescriptionParameters
destroyTriggered after the leave transition finishes.() => void
clickTriggered when the notification root is clicked.() => void

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 currently consumes the following dedicated CSS variables:

VariableDescriptionDefault
--yh-notification-bg-colorBackground colorvar(--yh-bg-color-overlay)
--yh-notification-border-colorBorder colorvar(--yh-border-color-lighter)
--yh-notification-shadowShadowvar(--yh-shadow-lg)
--yh-notification-title-colorTitle colorvar(--yh-text-color-primary)
--yh-notification-content-colorContent text colorvar(--yh-text-color-regular)

Type Exports

NameDescription
YhNotificationPropsProps type for the internal notification component
YhNotificationEmitsEmits type for the internal notification component
YhNotificationSlotsSlots type for the internal notification component
YhNotificationExposeExpose type for the internal notification component
YhNotificationInstancePublic instance type for a single notification
YhNotificationOptionsService options type for YhNotification(...)
YhNotificationHandlerReturned handler type
YhNotificationContextInternal runtime context type
YhNotificationFnNotification function signature type
YhNotificationTypeNotification type union
YhNotificationPositionNotification position union

Released under the MIT License.