A Vue plugin that provides a global notification system. It automatically creates a notification container and offers a composable API for showing, dismissing, and managing notifications throughout your Vue application.
Key Features
- Global Service: Plugin-based architecture that works across your entire Vue app
- Portal-based Rendering: Notifications are rendered outside the component tree
- Position Control: Configurable notification container positioning
- Maximum Notifications: Built-in queue management with configurable limits
- Rich Notification Options: Support for titles, icons, actions, emphasis levels, and feedback types
- TypeScript Support: Full TypeScript definitions for all interfaces and options
Basic Setup
- Install the plugin when creating your Vue app:
// main.ts
import { createApp } from 'vue';
import App from './App.vue';
import { NovaComponents } from '@nova-design-system/nova-vue';
createApp(App)
.use(NovaComponents) // Includes notification service automatically
.mount('#app');
- Configure with custom options if needed:
// main.ts
import { createApp } from 'vue';
import App from './App.vue';
import { NovaComponents } from '@nova-design-system/nova-vue';
createApp(App)
.use(NovaComponents, {
notifications: {
position: 'top-right', // Notification position
maxNotifications: 5, // Maximum notifications
className: 'custom-class' // Additional CSS class
}
})
.mount('#app');
- Use the composable in any component:
<template>
<NvButton @click="handleSuccess">
Show Success Notification
</NvButton>
</template>
<script setup lang="ts">
import { useNotifications, NvButton } from '@nova-design-system/nova-vue';
const { show } = useNotifications();
const handleSuccess = () => {
show({
heading: 'Success!',
message: 'Operation completed successfully',
feedback: 'success',
icon: 'check-circle'
});
};
</script>
Using Actions
You can add interactive buttons to notifications using either the actions prop for simple callbacks or actionSlot for custom Vue components:
Action Callbacks
Use the actions prop to add simple button actions:
<script setup lang="ts">
import { useNotifications } from '@nova-design-system/nova-vue';
const { show, dismiss } = useNotifications();
const handleDelete = () => {
const id = show({
heading: 'Delete File',
message: 'Are you sure you want to delete this file?',
feedback: 'warning',
dismissible: false,
actions: [
{
label: 'Cancel',
onClick: () => dismiss(id)
},
{
label: 'Delete',
onClick: () => {
// Perform delete operation
deleteFile();
dismiss(id);
// Show confirmation
show({
message: 'File deleted successfully',
feedback: 'success'
});
}
}
]
});
};
</script>
Custom Action Slot
Use the actionSlot prop for more complex custom layouts:
<script setup lang="ts">
import { defineComponent, h } from 'vue';
import { useNotifications, NvButton } from '@nova-design-system/nova-vue';
const { show, dismiss } = useNotifications();
const CustomActions = defineComponent({
props: ['notificationId'],
setup(props) {
return () => h('div', { class: 'flex gap-2' }, [
h(NvButton, {
emphasis: 'low',
onClick: () => dismiss(props.notificationId)
}, { default: () => 'Later' }),
h(NvButton, {
onClick: () => {
startUpdate();
dismiss(props.notificationId);
}
}, { default: () => 'Update Now' })
]);
}
});
const handleUpdate = () => {
const id = show({
heading: 'Update Available',
message: 'A new version is available. Update now?',
feedback: 'information',
dismissible: false,
actionSlot: h(CustomActions, { notificationId: id })
});
};
</script>
NotificationOptions Interface
When calling show(options), you can pass the following options:
- id (optional): Unique identifier. Auto-generated if not provided
- heading (optional): Short, concise heading text
- message: Main notification content
- dismissible (default: true): Whether users can manually close the notification
- emphasis ('low' | 'medium' | 'high'): Visual prominence level
- feedback ('information' | 'success' | 'warning' | 'error'): Determines color scheme and default icon
- icon (optional): Custom icon name to override the default
- actions (optional): Array of action objects with label and onClick
- actionSlot (optional): Custom Vue component for complex action layouts
- duration (default: 0): Duration in milliseconds before auto-dismissing. Set to 0 for sticky notifications that don't auto-dismiss
Auto-dismiss Notifications
Control notification lifetime using the duration option. Notifications can either be sticky (remain until manually dismissed) or auto-dismiss after a specified time.
Sticky Notifications (Default)
By default, notifications are sticky and remain visible until manually dismissed:
<script setup lang="ts">
import { useNotifications } from '@nova-design-system/nova-vue';
const { show } = useNotifications();
// Duration defaults to 0 (sticky)
const showSticky = () => {
show({
message: 'This notification stays until dismissed',
feedback: 'information'
});
};
</script>
Auto-dismiss Notifications
Set a duration in milliseconds to automatically dismiss notifications:
<script setup lang="ts">
import { useNotifications } from '@nova-design-system/nova-vue';
const { show } = useNotifications();
// Auto-dismiss after 5 seconds
const showAutoUpload = () => {
show({
heading: 'Upload Complete',
message: 'Your file has been uploaded',
feedback: 'success',
duration: 5000
});
};
</script>
When to Use Each Pattern
- Sticky (duration: 0): Use for important notifications requiring user acknowledgment, errors, or confirmations
- Auto-dismiss (duration > 0): Use for success messages, status updates, or informational notices that don't require action
Note: If a user manually dismisses an auto-dismiss notification, the timer is automatically cleared to prevent issues.
useNotifications Composable API
The composable returns an object with the following methods and properties:
- show(options): Display a new notification, returns the notification ID
- dismiss(id): Animate out and remove a specific notification (also clears auto-dismiss timer if present)
- remove(id): Immediately remove a notification without animation (also clears auto-dismiss timer)
- removeAll(): Clear all notifications immediately (also clears all auto-dismiss timers)
- notifications: Reactive array of currently active notifications
Notifications inside dialogs
Notifications shown through the service stay above open dialogs and drawers. If you render nv-notificationcontainer yourself instead, set follow-dialogs on it; without it the container stays where you render it. Render the container alone in a stable wrapper element, and add, remove or re-key the wrapper, never the container. A wrapper removed while a dialog is open leaves the container in that dialog until the dialog closes.
Best Practices
- Install the NovaComponents plugin at your app's root level
- Use appropriate feedback types (success, error, warning, information)
- Keep notification messages concise and actionable
- Use dismissible: false sparingly, only for critical notifications
- Consider using emphasis: 'high' for important system messages