apps/mantine.dev/src/pages/x/notifications.mdx
import { NotificationDemos, NotificationsDemos } from '@docs/demos'; import { Layout } from '@/layout'; import { MDX_DATA } from '@/mdx';
export default Layout(MDX_DATA.Notifications);
After installation import package styles at the root of your application:
import '@mantine/core/styles.css';
// ‼️ import notifications styles after core package styles
import '@mantine/notifications/styles.css';
Add the Notifications component anywhere in your application. Note that:
Notifications component inside MantineProviderNotifications component – it is not a provider, it is a regular componentNotifications components – if you do that, your notifications will be duplicatedimport { MantineProvider } from '@mantine/core';
import { Notifications } from '@mantine/notifications';
function Demo() {
return (
<MantineProvider>
<Notifications />
</MantineProvider>
);
}
All set! You can now use all notification system features.
<Demo data={NotificationsDemos.base} />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:
import '@mantine/notifications/styles.css';
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 and limitnotifications.hide – removes a notification with the given id from 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 queueAll functions can be imported from the @mantine/notifications package and can be used in any part of your application:
import { notifications } from '@mantine/notifications';
You can also import these functions separately:
// alias functions
import {
cleanNotifications, // notifications.clean
cleanNotificationsQueue, // notifications.cleanQueue
hideNotification, // notifications.hide
showNotification, // notifications.show
updateNotification, // notifications.update
updateNotificationsState, // notifications.updateState
} from '@mantine/notifications';
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, id is randomly generatedposition – notification position; by default, the value from the position prop of the Notifications component 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, true by 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; use false to disable auto closemessage – required notification bodycolor, icon, title, radius, className, style, loading – props passed down to the Notification componentAll properties except message are optional.
import { XIcon } from '@phosphor-icons/react';
import { notifications } from '@mantine/notifications';
// Bare minimum – message is required for all notifications
notifications.show({ message: 'Hello' });
// Most used notification props
notifications.show({
id: 'hello-there',
position: 'bottom-center',
withCloseButton: true,
allowClose: true,
onClose: () => console.log('unmounted'),
onOpen: () => console.log('mounted'),
autoClose: 5000,
title: "You've been compromised",
message: 'Leave the building immediately',
color: 'red',
icon: <XIcon />,
className: 'my-notification-class',
style: { backgroundColor: 'red' },
loading: false,
});
Notifications preview (message prop used as children):
<Demo data={NotificationDemos.configurator} configuratorProps={{ includeCode: false }} />
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:
import { Notifications } from '@mantine/notifications';
function Demo() {
return <Notifications allowDragDismiss={false} allowScrollDismiss={false} />;
}
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.
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.
You can define the notification position in the notifications.show function. Possible position values:
top-lefttop-righttop-centerbottom-leftbottom-rightbottom-centerThe 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:
import { Notifications } from '@mantine/notifications';
function Demo() {
return <Notifications position="top-right" zIndex={1000} />;
}
You can limit the maximum number of notifications that are displayed at a time by setting
the limit prop on Notifications:
import { Notifications } from '@mantine/notifications';
function Demo() {
return <Notifications limit={5} />;
}
All notifications added after the limit was reached are added to the queue
and displayed when a notification from the current state is hidden.
To remove a specific notification from state or queue, use the notifications.hide function:
import { notifications } from '@mantine/notifications';
const id = notifications.show({ message: 'Hello!' });
notifications.hide(id);
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:
You can configure the auto close timeout with Notifications:
import { Notifications } from '@mantine/notifications';
// All notifications will be closed automatically in 4000ms
function Demo() {
return <Notifications autoClose={4000} />;
}
Or per notification in the notifications.show/notifications.update functions:
import { notifications } from '@mantine/notifications';
notifications.show({
message: 'I will close in 500ms seconds',
autoClose: 500,
});
notifications.update({
id: 'hello',
message: 'I will never close',
autoClose: false,
});
notifications.show and notifications.update functions' autoClose prop has higher priority.
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 notificationimport { Notifications } from '@mantine/notifications';
function Demo() {
return <Notifications pauseResetOnHover="notification" />;
}
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.