Message
Shows brief feedback, such as a successful save, centered near the top of the window. Messages disappear automatically after a few seconds. Use the function API; no template component is needed.
Import
import { HuiMessage } from '@nscode/handy-ui'
Basic Usage
Pass a string to show a message. The default type is info, and it closes automatically after 3 seconds.
<script setup lang="ts">
import { HuiButton, HuiMessage } from '@nscode/handy-ui'
const show = () => {
HuiMessage('This is a message')
}
</script>
<template>
<HuiButton @click="show">Show message</HuiButton>
</template>
Types
The four types, success, warning, info and error, use different colors and icons. Use the corresponding shortcut method or set type in the options.
<script setup lang="ts">
import { HuiButton, HuiMessage, 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="HuiMessage.success('Saved successfully')"
>Success (success)</HuiButton
>
<HuiButton type="warning" @click="HuiMessage.warning('Insufficient stock')"
>Warning (warning)</HuiButton
>
<HuiButton type="info" @click="HuiMessage.info('Copied to the clipboard')"
>Information (info)</HuiButton
>
<HuiButton type="danger" @click="HuiMessage.error('Network error. Try again later')">
Error (error)
</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">Set type</span>
<HuiButton @click="HuiMessage({ message: 'Saved successfully', type: 'success' })"
>success</HuiButton
>
<HuiButton @click="HuiMessage({ message: 'Insufficient stock', type: 'warning' })"
>warning</HuiButton
>
<HuiButton @click="HuiMessage({ message: 'Copied to the clipboard', type: 'info' })"
>info</HuiButton
>
<HuiButton @click="HuiMessage({ message: 'Network error. Try again later', type: 'error' })">
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>
Shortcut methods also accept an options object. The method name determines the type; an option named type is ignored:
import { HuiMessage } from '@nscode/handy-ui'
// This shows a success message
HuiMessage.success({ message: 'Saved successfully', type: 'error', showClose: true })
Closable
Set showClose to true to show a close button on the right. Clicking it closes the message immediately. All message types support this option.
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
import type { MessageType } from '@nscode/handy-ui'
const show = (type: MessageType) => {
HuiMessage({ message: `A closable ${type} message`, type, showClose: true })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton type="success" @click="show('success')">Success (success)</HuiButton>
<HuiButton type="warning" @click="show('warning')">Warning (warning)</HuiButton>
<HuiButton type="info" @click="show('info')">Information (info)</HuiButton>
<HuiButton type="danger" @click="show('error')">Error (error)</HuiButton>
</HuiSpace>
</template>
Centered Content
Set center to true to center the icon and text inside the message box. The box is between 280 and 480 pixels wide, so this is most noticeable with short text.
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="HuiMessage({ message: 'Left aligned by default' })">Default</HuiButton>
<HuiButton @click="HuiMessage({ message: 'Centered text', center: true })"
>Centered (center)</HuiButton
>
<HuiButton
@click="
HuiMessage({
message: 'Centered and closable',
type: 'success',
center: true,
showClose: true
})
"
>
Centered and closable
</HuiButton>
</HuiSpace>
</template>
Duration and Manual Closing
duration specifies the number of milliseconds before automatic closing and defaults to 3000. Set it to 0 to disable automatic closing. Hovering over the message pauses the timer; leaving restarts it.
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="HuiMessage({ message: 'Closes after 1 second', duration: 1000 })"
>1 second</HuiButton
>
<HuiButton @click="HuiMessage({ message: 'Closes after 3 seconds' })"
>3 seconds (Default)</HuiButton
>
<HuiButton @click="HuiMessage({ message: 'Closes after 10 seconds', duration: 10000 })"
>10 seconds</HuiButton
>
<HuiButton
@click="HuiMessage({ message: 'Stays open; click × to close', duration: 0, showClose: true })"
>
No automatic closing (0)
</HuiButton>
</HuiSpace>
</template>
Each call returns a MessageInstance. Call its close() method to close that message at any time.
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
import type { MessageInstance } from '@nscode/handy-ui'
let instance: MessageInstance | null = null
const open = () => {
instance = HuiMessage({ message: 'Stays open; use the button to close', duration: 0 })
}
const close = () => {
instance?.close()
instance = null
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="open">Open</HuiButton>
<HuiButton @click="close">Close</HuiButton>
</HuiSpace>
</template>
Multiple Messages and offset
New messages appear below earlier ones. offset sets the gap from the previous message, or from the top of the window for the first message. The default is 20 pixels. When a message closes, messages below it move up to fill the gap.
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const showThree = () => {
HuiMessage({ message: 'Message 1', duration: 2000 })
HuiMessage({
message: 'Message 2, closes first after 2 seconds',
type: 'success',
duration: 2000
})
HuiMessage({
message: 'Message 3, closes after 4 seconds and moves up to fill the gap',
type: 'warning',
duration: 4000
})
}
const showOffset = () => {
HuiMessage({ message: 'Message 1, 20px from the top of the window' })
HuiMessage({ message: 'Message 2, 80px from the previous message', offset: 80 })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="showThree">Show 3 messages</HuiButton>
<HuiButton @click="showOffset">Message 2 with offset 80</HuiButton>
</HuiSpace>
</template>
VNode Content
Along with strings, message accepts a VNode created with h() to display bold text, links and other content. Strings are displayed as plain text; HTML is not parsed, so tags within a string are shown literally.
<script setup lang="ts">
import { h } from 'vue'
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const showVNode = () => {
HuiMessage({
message: h('span', ['Order ', h('b', '20260101001'), ' submitted'])
})
}
const showString = () => {
HuiMessage({ message: 'Order <b>20260101001</b> submitted' })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="showVNode">VNode: bold order number</HuiButton>
<HuiButton @click="showString">String: display tags literally</HuiButton>
</HuiSpace>
</template>
Close Callback
onClose is called once after the message closes and its fade-out animation ends. It runs for automatic closing, the close button, close() and closeAll().
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMessage } from '@nscode/handy-ui'
const status = ref('No message opened yet')
const open = () => {
status.value = 'Message is visible...'
HuiMessage({
message: 'Closes after 1 second',
duration: 1000,
showClose: true,
onClose: () => {
status.value = `onClose called (${new Date().toLocaleTimeString()})`
}
})
}
</script>
<template>
<HuiButton @click="open">Open message</HuiButton>
<p style="margin-top: 12px">{{ status }}</p>
</template>
Closing All Messages
HuiMessage.closeAll() closes all current messages. Each message plays its normal fade-out animation and calls its own onClose callback.
<script setup lang="ts">
import { HuiButton, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const openMany = () => {
HuiMessage.success({ message: 'Message 1', duration: 0 })
HuiMessage.warning({ message: 'Message 2', duration: 0 })
HuiMessage.error({ message: 'Message 3', duration: 0 })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="openMany">Show 3 persistent messages</HuiButton>
<HuiButton type="primary" @click="HuiMessage.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 { HuiMessage, HuiButton, HuiSpace } from '@nscode/handy-ui'
const count = ref(1)
let instance: { close: () => void } | undefined
const open = () => {
instance?.close()
instance = HuiMessage({
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 |
|---|---|
HuiMessage(options: MessageOptions) | Shows a message with the supplied options |
HuiMessage(message: string) | Shows an info message with all other options using their defaults |
HuiMessage.success(message | options) | Shows a success message |
HuiMessage.warning(message | options) | Shows a warning message |
HuiMessage.info(message | options) | Shows an info message |
HuiMessage.error(message | options) | Shows an error message |
HuiMessage.closeAll() | Closes all current messages and returns nothing |
All calls except closeAll return a MessageInstance for that message.
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 { HuiMessage } from '@nscode/handy-ui'
HuiMessage({
message: h('span', ['Order submitted. ', h(RouterLink, { to: '/orders' }, () => 'View 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: HuiMessage({ 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 |
|---|---|---|---|
| message | string | VNode | (() => VNode) | '' | Message content. Strings are displayed as plain text |
| type | 'success' | 'warning' | 'info' | 'error' | 'info' | Type, which determines the color and icon |
| duration | number | 3000 | Milliseconds before automatic closing. 0 disables automatic closing |
| showClose | boolean | false | Whether to show a close button |
| center | boolean | false | Whether to center the icon and text |
| offset | number | 20 | Gap from the previous message in pixels, or from the window top for the first message |
| 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
MessageInstance:
| Property | Type | Description |
|---|---|---|
| id | string | Message identifier |
| vm | HTMLElement | null | Message DOM element; null for the empty server-side handle |
| props | MessageProps | Resolved options, including defaults for omitted options |
| close | () => void | Closes this message with a fade-out animation. Does nothing when already closing |
Types
import type {
MessageType,
MessageProps,
MessageOptions,
MessageInstance,
MessageFn
} from '@nscode/handy-ui'
import type { AppContext, VNode } from 'vue'
type MessageType = 'success' | 'warning' | 'info' | 'error'
interface MessageProps {
message: string | VNode | (() => VNode)
type?: MessageType
duration?: number
showClose?: boolean
center?: boolean
offset?: number
onClose?: () => void
}
interface MessageOptions extends Omit<MessageProps, 'message'> {
message?: string | VNode | (() => VNode)
/** Application context for rendering; defaults to the application set by app.use(HandyUI) or setAppContext(app) */
appContext?: AppContext | null
}
interface MessageInstance {
id: string
vm: HTMLElement | null
props: MessageProps
close: () => void
}
interface MessageFn {
(options: MessageOptions): MessageInstance
(message: string): MessageInstance
success: (message: string | MessageOptions) => MessageInstance
warning: (message: string | MessageOptions) => MessageInstance
info: (message: string | MessageOptions) => MessageInstance
error: (message: string | MessageOptions) => MessageInstance
closeAll: () => void
}
HuiMessageComponent, the component used internally to render messages, is also exported. Direct use is usually unnecessary.