A React context provider that manages a global notification system. It renders notifications using a portal to document.body and provides a hook-based API for showing, dismissing, and managing notifications throughout your application.
Key Features
- Global Notification Management: Centralized notification state across your entire application
- Portal-based Rendering: Notifications are rendered outside the component tree using React portals
- Position Control: Configurable notification container positioning
- Maximum Notifications: Built-in queue management with configurable limits
- Rich Notification Options: Support for headings, icons, actions, emphasis levels, and feedback types
- TypeScript Support: Full TypeScript definitions for all interfaces and options
Basic Setup
- Wrap your application with the NotificationProvider at the root level:
import { NotificationProvider } from '@nova-design-system/nova-react';
function App() {
return (
<NotificationProvider position="top-right" maxNotifications={5}>
{/* Your app components */}
</NotificationProvider>
);
}
- Use the hook in any component within the provider:
import { useNotifications } from '@nova-design-system/nova-react';
function MyComponent() {
const { show, dismiss, notifications, removeAll } = useNotifications();
const handleSuccess = () => {
show({
heading: 'Success!',
message: 'Operation completed successfully',
feedback: 'success',
icon: 'check-circle'
});
};
return (
<button onClick={handleSuccess}>
Show Success Notification
</button>
);
}
Notifications inside dialogs
The provider renders its nv-notificationcontainer with followDialogs, so while a modal dialog is open the notifications move into it and stay above the dialog, then return to the page when the dialog closes. The container sits alone inside a wrapper <div data-nv-notifications> that React inserts, hides and removes, so React never moves the container itself. If you render NvNotificationcontainer yourself instead, set followDialogs 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.
Using Actions
You can add interactive buttons to notifications using either the actions prop for simple callbacks or actionSlot for custom React elements:
Action Callbacks
Use the actions prop to add simple button actions:
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'
});
}
}
]
});
};
Custom Action Slot
Use the actionSlot prop for more complex custom layouts:
const handleUpdate = () => {
const id = show({
heading: 'Update Available',
message: 'A new version is available. Update now?',
feedback: 'information',
dismissible: false,
actionSlot: (
<div className="flex gap-2">
<NvButton emphasis="low" onClick={() => dismiss(id)}>
Later
</NvButton>
<NvButton onClick={() => {
startUpdate();
dismiss(id);
}}>
Update Now
</NvButton>
</div>
)
});
};
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 React element 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:
const { show } = useNotifications();
// Duration defaults to 0 (sticky)
show({
message: 'This notification stays until dismissed',
feedback: 'information'
});
Auto-dismiss Notifications
Set a duration in milliseconds to automatically dismiss notifications:
const { show } = useNotifications();
// Auto-dismiss after 5 seconds
show({
heading: 'Upload Complete',
message: 'Your file has been uploaded',
feedback: 'success',
duration: 5000
});
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 Hook API
The hook 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: Array of currently active notifications
Best Practices
- Place the NotificationProvider 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