Notification
Displays a notification card with a title in a corner of the window. Use it for information that does not require immediate action, such as a completed background task or a new message. Notifications disappear after a few seconds. Use the function API; no template component is needed.
Import
import { HuiNotification } from '@nscode/handy-ui'
Basic Usage
Pass an options object with title for the title and message for the content. By default, the notification appears at the top right with the info type, closes after 4.5 seconds and has a close button at the top right.
Passing only a string uses it as the content, without a title.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const show = () => {
HuiNotification({
title: 'Export complete',
message: 'The report has been generated. View it in the download center.'
})
}
const showText = () => {
HuiNotification('This is a notification')
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="show">Show notification</HuiButton>
<HuiButton @click="showText">Text only (no title)</HuiButton>
</HuiSpace>
</template>
Types
The four types, success, warning, info and error, use different leading icons. Use the corresponding shortcut method or set type in the options. Shortcut methods also accept a string.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">Shortcut methods</span>
<HuiButton
type="success"
@click="HuiNotification.success({ title: 'Success', message: 'Data synchronized' })"
>
Success (success)
</HuiButton>
<HuiButton
type="warning"
@click="
HuiNotification.warning({
title: 'Warning',
message: 'Less than 10% storage space remaining'
})
"
>
Warning (warning)
</HuiButton>
<HuiButton
type="info"
@click="
HuiNotification.info({ title: 'Information', message: 'A new version is available' })
"
>
Information (info)
</HuiButton>
<HuiButton
type="danger"
@click="
HuiNotification.error({ title: 'Error', message: 'Synchronization failed. Try again' })
"
>
Error (error)
</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Set type</span>
<HuiButton
@click="
HuiNotification({ title: 'success', message: 'Data synchronized', type: 'success' })
"
>
success
</HuiButton>
<HuiButton
@click="
HuiNotification({
title: 'warning',
message: 'Less than 10% storage space remaining',
type: 'warning'
})
"
>
warning
</HuiButton>
<HuiButton
@click="
HuiNotification({ title: 'info', message: 'A new version is available', type: 'info' })
"
>
info
</HuiButton>
<HuiButton
@click="
HuiNotification({
title: 'error',
message: 'Synchronization failed. Try again',
type: 'error'
})
"
>
error
</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Text only</span>
<HuiButton @click="HuiNotification.success('Data synchronized')">success</HuiButton>
<HuiButton @click="HuiNotification.warning('Less than 10% storage space remaining')"
>warning</HuiButton
>
<HuiButton @click="HuiNotification.info('A new version is available')">info</HuiButton>
<HuiButton @click="HuiNotification.error('Synchronization failed. Try again')"
>error</HuiButton
>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 72px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
When a shortcut method receives an options object, the method name determines the type; the type option is ignored.
Position
position accepts top-right (default), top-left, bottom-right or bottom-left. Notifications at each position stack independently.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
import type { NotificationPosition } from '@nscode/handy-ui'
const show = (position: NotificationPosition) => {
HuiNotification({ title: position, message: `Appears at the window's ${position}`, position })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="show('top-right')">Top right (top-right, default)</HuiButton>
<HuiButton @click="show('top-left')">Top left (top-left)</HuiButton>
<HuiButton @click="show('bottom-right')">Bottom right (bottom-right)</HuiButton>
<HuiButton @click="show('bottom-left')">Bottom left (bottom-left)</HuiButton>
</HuiSpace>
</template>
Multiple Notifications and offset
At a top position, newer notifications stack below earlier ones. At a bottom position, newer notifications stack above earlier ones. offset specifies the gap from the previous notification at the same position, or from the top (top-*) or bottom (bottom-*) of the window for the first notification. The default is 16 pixels. When one closes, the remaining notifications move to fill the gap. The horizontal distance from the window edge is fixed at 16 pixels.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const showTop = () => {
HuiNotification({ title: 'Notification 1', message: '16px from the top of the window' })
HuiNotification({
title: 'Notification 2',
message: '60px from the previous notification',
offset: 60
})
}
const showBottom = () => {
HuiNotification({
title: 'Notification 1',
message: 'Shown first, at the bottom',
position: 'bottom-right'
})
HuiNotification({
title: 'Notification 2',
message: 'Shown later, stacked above',
position: 'bottom-right'
})
HuiNotification({
title: 'Notification 3',
message: '40px from the previous notification',
position: 'bottom-right',
offset: 40
})
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="showTop">Two at the top right (second offset 60)</HuiButton>
<HuiButton @click="showBottom">Three at the bottom right (stacked upward)</HuiButton>
</HuiSpace>
</template>
Duration and Manual Closing
duration specifies the number of milliseconds before automatic closing and defaults to 4500. Set it to 0 to disable automatic closing. Hovering pauses the timer; leaving restarts it. showClose defaults to true. Set it to false to hide the close button.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiButton
@click="
HuiNotification({ title: '2 seconds', message: 'Closes after 2 seconds', duration: 2000 })
"
>
2 seconds
</HuiButton>
<HuiButton
@click="HuiNotification({ title: '4.5 seconds', message: 'Closes after 4.5 seconds' })"
>
4.5 seconds (default)
</HuiButton>
<HuiButton
@click="
HuiNotification({
title: 'No automatic closing',
message: 'Click × at the top right to close',
duration: 0
})
"
>
No automatic closing (0)
</HuiButton>
<HuiButton
@click="
HuiNotification({
title: 'No close button',
message: 'Closes automatically after 3 seconds',
showClose: false,
duration: 3000
})
"
>
Hide the close button
</HuiButton>
</HuiSpace>
</template>
Each call returns a NotificationInstance. Call its close() method to close that notification at any time.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
import type { NotificationInstance } from '@nscode/handy-ui'
let instance: NotificationInstance | null = null
const open = () => {
instance = HuiNotification({
title: 'Uploading',
message: 'Stays open until the upload completes',
duration: 0,
showClose: false
})
}
const close = () => {
instance?.close()
instance = null
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="open">Open</HuiButton>
<HuiButton @click="close">Close</HuiButton>
</HuiSpace>
</template>
Click and Close Callbacks
onClickruns when the notification is clicked. Clicking its close button does not invoke it.onCloseruns once after the notification closes and its fade-out animation ends. It runs for automatic closing, the close button,close()andcloseAll().
Clicking the notification itself does not close it. Close it inside onClick if needed.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMessage, HuiNotification } from '@nscode/handy-ui'
import type { NotificationInstance } from '@nscode/handy-ui'
const status = ref('No notification opened yet')
const open = () => {
status.value = 'Notification is visible; click it to try the callback'
const instance: NotificationInstance = HuiNotification({
title: 'New message',
message: 'Click here to view',
duration: 0,
onClick: () => {
HuiMessage.success('onClick: open the message center')
instance.close()
},
onClose: () => {
status.value = `onClose called (${new Date().toLocaleTimeString()})`
}
})
}
</script>
<template>
<HuiButton @click="open">Open notification</HuiButton>
<p style="margin-top: 12px">{{ status }}</p>
</template>
VNode Content
Along with strings, message accepts a VNode created with h(). Strings are displayed as plain text; HTML is not parsed, so tags within a string are shown literally. title accepts only a string.
<script setup lang="ts">
import { h } from 'vue'
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const showVNode = () => {
HuiNotification({
title: 'Approved',
message: h('span', ['Request ', h('b', 'SQ-0012'), ' has been approved'])
})
}
const showString = () => {
HuiNotification({ title: 'Approved', message: 'Request <b>SQ-0012</b> has been approved' })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="showVNode">VNode: bold request number</HuiButton>
<HuiButton @click="showString">String: display tags literally</HuiButton>
</HuiSpace>
</template>
Closing All Notifications
HuiNotification.closeAll() closes notifications at all positions. Each notification plays its normal fade-out animation and calls its own onClose callback.
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const openMany = () => {
HuiNotification.success({ title: 'Top right', message: 'Stays open', duration: 0 })
HuiNotification.warning({
title: 'Top left',
message: 'Stays open',
duration: 0,
position: 'top-left'
})
HuiNotification.info({
title: 'Bottom right',
message: 'Stays open',
duration: 0,
position: 'bottom-right'
})
HuiNotification.error({
title: 'Bottom left',
message: 'Stays open',
duration: 0,
position: 'bottom-left'
})
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="openMany">Show one in each corner</HuiButton>
<HuiButton type="primary" @click="HuiNotification.closeAll()">Close all</HuiButton>
</HuiSpace>
</template>
Reactive Content
Pass () => VNode to let the displayed content update with reactive data.
<script setup lang="ts">
import { h, onBeforeUnmount, ref } from 'vue'
import { HuiNotification, HuiButton, HuiSpace } from '@nscode/handy-ui'
const count = ref(1)
let instance: { close: () => void } | undefined
const open = () => {
instance?.close()
instance = HuiNotification({
message: () => h('strong', 'Processed ' + count.value + ' items'),
duration: 0,
showClose: true
})
}
onBeforeUnmount(() => instance?.close())
</script>
<template>
<HuiSpace
><HuiButton @click="open">Show progress</HuiButton
><HuiButton @click="count++">Increase progress</HuiButton><span>{{ count }}</span></HuiSpace
>
</template>
Calling the API
| Syntax | Description |
|---|---|
HuiNotification(options: NotificationOptions) | Shows a notification with the supplied options |
HuiNotification(message: string) | Shows an info notification with content and no title |
HuiNotification.success(message | options) | Shows a success notification |
HuiNotification.warning(message | options) | Shows a warning notification |
HuiNotification.info(message | options) | Shows an info notification |
HuiNotification.error(message | options) | Shows an error notification |
HuiNotification.closeAll() | Closes notifications at all positions and returns nothing |
All calls except closeAll return a NotificationInstance for that notification.
Routing and Global Components in Content
VNodes or components passed through message use the application's context. They can access globally registered components, routing (<RouterLink>, useRouter()), plugins such as Pinia and i18n, and values from app.provide.
- Full installation with
app.use(HandyUI)sets this up automatically. - For on-demand imports, call
setAppContext(app)once in your entry file:
// main.ts
import { createApp } from 'vue'
import { setAppContext } from '@nscode/handy-ui'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
setAppContext(app)
app.mount('#app')
import { h } from 'vue'
import { RouterLink } from 'vue-router'
import { HuiNotification } from '@nscode/handy-ui'
HuiNotification({
title: 'New order',
message: h(RouterLink, { to: '/orders' }, () => 'Process order')
})
When a page contains multiple Vue applications, such as micro frontends, set appContext in the options for a particular call to choose its application. It takes precedence over the default: HuiNotification({ message: '...', appContext: otherApp._context }).
HuiConfigProvider belongs to the component tree. Function calls cannot read its size or zIndex, regardless of whether an application context is set.
Options
| Option | Type | Default | Description |
|---|---|---|---|
| title | string | '' | Title. The title row is hidden when empty |
| message | string | VNode | (() => VNode) | '' | Content. Strings are displayed as plain text |
| type | 'success' | 'warning' | 'info' | 'error' | 'info' | Type, which determines the icon |
| duration | number | 4500 | Milliseconds before automatic closing. 0 disables automatic closing |
| showClose | boolean | true | Whether to show a close button |
| position | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' | 'top-right' | Window corner in which to display the notification |
| offset | number | 16 | Gap from the previous notification at the same position in pixels, or from the window top or bottom for the first notification |
| onClick | () => void | — | Called when the notification is clicked, excluding its close button |
| onClose | () => void | — | Called after the closing fade-out animation ends |
| appContext | AppContext | null | — | Application context for rendering; see Routing and Global Components in Content. Defaults to the application set by app.use(HandyUI) or setAppContext(app) |
Return Value
NotificationInstance:
| Property | Type | Description |
|---|---|---|
| id | string | Notification identifier |
| vm | HTMLElement | null | Notification DOM element; null for the empty server-side handle |
| props | NotificationProps | Resolved options, including defaults for omitted options |
| close | () => void | Closes this notification with a fade-out animation. Does nothing when already closing |
Types
import type {
NotificationType,
NotificationPosition,
NotificationProps,
NotificationOptions,
NotificationInstance,
NotificationFn
} from '@nscode/handy-ui'
import type { AppContext, VNode } from 'vue'
type NotificationType = 'success' | 'warning' | 'info' | 'error'
type NotificationPosition = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
interface NotificationProps {
title?: string
message: string | VNode | (() => VNode)
type?: NotificationType
duration?: number
showClose?: boolean
position?: NotificationPosition
offset?: number
onClose?: () => void
onClick?: () => void
}
interface NotificationOptions extends Omit<NotificationProps, 'message'> {
title?: string
message?: string | VNode | (() => VNode)
/** Application context for rendering; defaults to the application set by app.use(HandyUI) or setAppContext(app) */
appContext?: AppContext | null
}
interface NotificationInstance {
id: string
vm: HTMLElement | null
props: NotificationProps
close: () => void
}
interface NotificationFn {
(options: NotificationOptions): NotificationInstance
(message: string): NotificationInstance
success: (message: string | NotificationOptions) => NotificationInstance
warning: (message: string | NotificationOptions) => NotificationInstance
info: (message: string | NotificationOptions) => NotificationInstance
error: (message: string | NotificationOptions) => NotificationInstance
closeAll: () => void
}
HuiNotificationComponent, the component used internally to render notifications, is also exported. Direct use is usually unnecessary.