mui-notifications
v1.0.0
Published
Application notifications component using Material UI.
Maintainers
Readme
MUI Notifications
Application notifications component using Material UI. Imperative APIs to show and interact with application notifications.
Install
Make sure you have @mui/material in your project, then:
npm install mui-notificationsUsage
Install the NotificationsProvider.
import { NotificationsProvider } from 'mui-notifications';
function App({ children }) {
return <NotificationsProvider>{children}</NotificationsProvider>;
}Now you can get access to the notifications APIs through the useNotifications hook.
import { useNotifications } from 'mui-notifications';
function MyApp() {
const { show, close } = useNotifications();
// ...
}useNotifications API
useNotifications() returns an object with two methods:
const { show, close } = useNotifications();show(message, options?)
message: React.ReactNodeThe notification content to display.options?: ShowNotificationOptionsAdditional configuration for the notification.- Returns:
stringThe notification key. You can store it and pass it toclose(key)later.
Available show options:
key?: stringCustom unique key for the notification. If omitted, one is generated automatically.severity?: 'info' | 'warning' | 'error' | 'success'Renders the notification as a MUIAlertwith the selected severity. If omitted, a default snackbar layout is used.autoHideDuration?: numberTime in milliseconds before the notification closes automatically.actionText?: React.ReactNodeLabel for the action button shown whenonActionis provided.onAction?: () => voidCallback fired when the action button is clicked.
close(key)
key: stringCloses a notification by its key.
Basic notification
You can notify your users with a neutral message by calling show. To have the notification automatically hide, add the autoHideDuration option. This expresses the time in milliseconds after which to close the notification.
show('Consider yourself notified!', {
autoHideDuration: 3000,
});Action example
const key = show('Storage is reaching its project quota.', {
severity: 'warning',
actionText: 'Open settings',
onAction: () => {
console.log('Open settings');
},
});
close(key);