Mantine Notifications
Installation
Package depends on @mantine/core and @mantine/hooks.
Install with yarn:
Install with npm:
Demo
Usage
Wrap your application with NotificationsProvider:
Then use showNotification function anywhere in your application:
Functions
Notifications system is based on custom events,
@mantine/notifications package exports the following functions:
- showNotification – adds given notification to notifications list or queue depending on current state and limit
- updateNotification – updates notification that was previously added to the state or queue
- hideNotification – removes notification with given id from notifications state and queue
- cleanNotifications – removes all notifications from notifications state and queue
- cleanNotificationsQueue – removes all notifications from queue
All functions can be imported from @mantine/notifications package and can be used in any part of your application:
Notification props
Notification state item can have these properties:
- id – notification id, it is used to update and remove notification, by default id is randomly generated
- disallowClose – removes close button, notification can be closed only programmatically
- onClose – calls when notification is unmounted
- onOpen – calls when notification is mounted
- autoClose – defines timeout in ms on which notification will be automatically closed, use
falseto disable auto close - message – required notification body
- color, icon, title, radius, className, style, sx, loading – props spread to Notification component
All properties except message are optional.
Notifications preview (message prop used as children):
Customize notification styles
You can use sx, style, className or Styles API classNames, styles props to customize notification styles:
Notifications container position
NotificationsProvider renders notifications container with fixed position inside Portal. Position cannot be changed per notification. NotificationsProvider supports the following positions:
top-lefttop-righttop-centerbottom-leftbottom-rightbottom-center
Limit and queue
NotificationsProvider uses use-queue hook to manage state.
You can limit maximum amount of notifications that can be displayed by setting
limit prop on NotificationsProvider:
All notifications added after limit was reached will be added into queue and displayed when notification from current state is closed.
Remove notifications from state and queue
To remove specific notification from state or queue use hideNotification function:
Use cleanNotificationsQueue function to remove all notifications that are not currently displayed and
cleanNotifications function to remove all notifications from state and queue:
Update notification
Auto close
You can configure auto close timeout with NotificationsProvider:
Or in showNotification/updateNotification functions:
showNotification and updateNotification functions autoClose prop always has higher priority.
use-notifications hook
To subscribe to notifications state use useNotifications hook, it returns object with the following properties:
- notifications – array of notifications that are currently displayed
- queue – array of notifications in queue
NotificationsProvider component props
| Name | Type | Description |
|---|---|---|
| autoClose | number | false | Auto close timeout for all notifications, false to disable auto close, can be overwritten for individual notifications by showNotification function |
| children | ReactNode | Your application |
| containerWidth | number | Notification width in px, cannot exceed 100% |
| limit | number | Maximum amount of notifications displayed at a time, other new notifications will be added to queue |
| notificationMaxHeight | number | Notification max-height in px, used for transitions |
| position | "bottom-center" | "top-center" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | Notifications position |
| target | string | HTMLElement | Target element of Portal component |
| transitionDuration | number | Notification transitions duration, 0 to turn transitions off |
| zIndex | ZIndex | Notifications container z-index |