Notifications system
Mantine notifications system
Source
LLM docs
Docs
Package
License
Installation
After installation import package styles at the root of your application:
Add the Notifications component anywhere in your application. Note that:
- It is required to render the
Notificationscomponent inside MantineProvider - You do not need to wrap your application with the
Notificationscomponent – it is not a provider, it is a regular component - You should not render multiple
Notificationscomponents – if you do that, your notifications will be duplicated
All set! You can now use all notification system features.
Do not forget to import styles
Have you followed the installation instructions above but something is not working
(position prop not working, notifications are stuck at the bottom)?
You've fallen into the trap of not importing notification styles!
To fix the issue, import notification styles at the root of your application:
Functions
The @mantine/notifications package exports a notifications object with the following functions:
notifications.show– adds a given notification to the notifications list or queue, depending on the current state andlimitnotifications.hide– removes a notification with the givenidfrom the notifications state and queuenotifications.update– updates a notification that was previously added to the state or queuenotifications.updateState– executes a given callback with current notifications state and queue as an argument and updates state with the returned valuenotifications.clean– removes all notifications from the notifications state and queuenotifications.cleanQueue– removes all notifications from the queue
All functions can be imported from the @mantine/notifications package and can be used in any part of your application:
You can also import these functions separately:
Notification props
notifications.show and notifications.update functions can be called with an object that has the following properties:
id– notification id, it is used to update and remove notifications; by default,idis randomly generatedposition– notification position; by default, the value from thepositionprop of theNotificationscomponent is usedwithBorder– determines whether the notification should have a borderwithCloseButton– determines whether the close button should be visibleallowClose– determines whether notification can be closed with close button, drag or horizontal scroll swipe,trueby defaultonClose– called when the notification is unmountedonOpen– called when the notification is mountedautoClose– defines timeout in ms after which the notification will be automatically closed; usefalseto disable auto closepriority– display priority used when the number of active notifications exceedslimit; higher numbers are shown first, defaults to0message– required notification bodycolor, icon, title, radius, className, style, loading– props passed down to the Notification component
All properties except message are optional.
Notifications preview (message prop used as children):
Dismiss interactions
Notifications can be dismissed by dragging them left or right, or with a horizontal scroll swipe while hovered.
Use allowDragDismiss and allowScrollDismiss on Notifications to disable either interaction:
To make a specific notification non-dismissible, set allowClose: false in
notifications.show or notifications.update. This hides the close button and disables
drag/scroll dismissal for that notification.
Customize notification styles
You can use style, className or Styles API classNames, styles props to customize notification styles.
Usually, it is better to override Notification styles with classNames prop in the theme object.
Notifications container position
You can define the notification position in the notifications.show function. Possible position values:
top-lefttop-righttop-centerbottom-leftbottom-rightbottom-center
The position can be defined on the Notifications component.
In the following example, notifications will be displayed in the top right corner of the screen
if position is not defined in the notifications.show function:
Limit and queue
You can limit the maximum number of notifications that are displayed at a time by setting
the limit prop on Notifications:
All notifications added after the limit was reached are added to the queue
and displayed when a notification from the current state is hidden.
Priority
By default, notifications are distributed between the visible state and the queue in
insertion order (FIFO): the first limit notifications are visible and the rest wait
in the queue. Set the priority property in notifications.show or notifications.update
to control which notifications take the visible slots when there are more active
notifications than the limit allows.
- Notifications with a higher
priorityare shown before notifications with a lower one. - Notifications with equal
prioritykeep insertion order (FIFO). prioritydefaults to0, so notifications without it behave exactly as before.- Within the visible set, higher priority notifications are rendered first.
When a high priority notification is added while the visible state is already at limit,
it takes a visible slot and the lowest priority visible notification is moved into the queue –
even if it was already displayed. Priority is applied independently for each position.
Note that a notification that is moved from the visible state back into the queue is unmounted
and mounted again when it returns to a visible slot, so its onOpen callback runs each time it
becomes visible.
In the following example the Notifications component uses limit={1}. Show a low priority
notification first, then a high priority one: the high priority notification replaces the
low priority one, which is moved to the queue and shown again once the high priority
notification is closed.
Remove notifications from state and queue
To remove a specific notification from state or queue, use the notifications.hide function:
Use the notifications.cleanQueue function to remove all notifications from the queue and
notifications.clean to remove all notifications both from the state and queue:
Update notification
Auto close
You can configure the auto close timeout with Notifications:
Or per notification in the notifications.show/notifications.update functions:
notifications.show and notifications.update functions' autoClose prop has higher priority.
Pause auto close on hover
By default, hovering over any notification pauses the auto close timer of all
visible notifications. You can change this behavior with the pauseResetOnHover prop:
pauseResetOnHover="all"(default) – pauses auto close for all notifications when any notification is hoveredpauseResetOnHover="notification"– pauses auto close only for the hovered notification
Subscribe to notifications state
You can subscribe to notification state changes with the useNotifications hook.
The hook returns an object with notifications and queue arrays. The notifications
array contains all notifications that are currently displayed; queue contains
notifications that are waiting to be displayed.
Notifications state
[]
Notifications queue
[]