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.promise– shows a loading notification and updates it with success or error state when the given promise settlesnotifications.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 closewithAutoCloseProgress– determines whether a progress line that fills up until the notification auto closes is displayed at the bottom of the notification; overrideswithAutoCloseProgressprop of theNotificationscomponentpriority– 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
Promise
Use notifications.promise function to display a loading notification while a promise is pending
and update it with success or error state once the promise settles. The function accepts a promise
and an object with loading, success and error notifications. success and error can be
functions – they receive the resolved value or the rejection reason and return notification props.
State objects accept all notification props except id.
notifications.promise applies the following defaults, all of them can be overridden
in the corresponding state object:
loading–loading: true,autoClose: falsesuccess–color: 'teal',loading: false,autoCloseis reset to the default valueerror–color: 'red',loading: false,autoCloseis reset to the default value
All three states are displayed in the same notification, props that are not set in the next state
are kept from the previous one. For example, if title is set only in loading, it is also displayed
in success and error states.
notifications.promise returns the promise that was passed to it, so you can await it or chain it.
If the loading notification was closed before the promise settled, the success or error state
is shown as a new notification. Use id option to reuse the same notification for consecutive calls –
when notifications.promise is called with the same id while the previous promise is still pending,
the result of the previous promise is ignored:
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.
Auto close progress
Set withAutoCloseProgress prop on the Notifications component to display a progress line
at the bottom of every notification that auto closes. The line fills up over the auto close
timeout and reaches 100% right before the notification is closed:
The Notifications prop sets the default for all notifications, it can be overridden for
individual notifications with the withAutoCloseProgress property in notifications.show
and notifications.update functions.
Note that:
- The progress line is not displayed for notifications with
autoClose: false - The progress line fades out when the auto close timer is paused (for example, when the notification is hovered) and fades back in, starting over, when the timer is restarted
- The progress line uses notification
color - The progress line is a part of the default notification rendering, it is not displayed for notifications rendered with
renderNotification - Use
progressStyles API selector of theNotificationscomponent to customize the line, for example, to change its height or color
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
Custom notification rendering
Use renderNotification prop on Notifications component or per notification in notifications.show/notifications.update
to completely replace the default notification with custom content. All animations (enter, exit, drag dismiss, scroll dismiss)
are preserved for custom notifications:
Stacked layout
Set layout="stacked" on the Notifications component to display notifications in a stacked layout
where only the latest notification is fully visible, and older notifications peek out behind it.
The stack expands when it is hovered with a mouse, when keyboard focus moves into it, and when it is
tapped on a touch device – a tap outside collapses it again. While collapsed, the notifications behind
the first one are inert: they are not focusable and are hidden from screen readers.
Note: the stacked demo uses a separate store to avoid affecting other notifications on the page.
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
[]