Mantine Notifications
Installation
Install with npm:
Install with yarn:
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
false
to 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-left
top-right
top-center
bottom-left
bottom-right
bottom-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