--- productId: material-ui title: React Snackbar component components: Snackbar, SnackbarContent githubLabel: 'scope: snackbar' materialDesign: https://m2.material.io/components/snackbars waiAria: https://www.w3.org/TR/wai-aria-1.1/#alert githubSource: packages/mui-material/src/Snackbar --- # Snackbar Snackbars (also known as toasts) are used for brief notifications of processes that have been or will be performed. ## Introduction The Snackbar component appears temporarily and floats above the UI to provide users with (non-critical) updates on an app's processes. The demo below, inspired by Google Keep, shows a basic Snackbar with a text element and two actions: ```tsx import * as React from 'react'; import Button from '@mui/material/Button'; import Snackbar, { SnackbarCloseReason } from '@mui/material/Snackbar'; import IconButton from '@mui/material/IconButton'; import CloseIcon from '@mui/icons-material/Close'; export default function SimpleSnackbar() { const [open, setOpen] = React.useState(false); const handleClick = () => { setOpen(true); }; const handleClose = ( event: React.SyntheticEvent | Event, reason?: SnackbarCloseReason, ) => { if (reason === 'clickaway') { return; } setOpen(false); }; const action = ( ); return (
); } ``` ### Usage Snackbars differ from [Alerts](/material-ui/react-alert/) in that Snackbars have a fixed position and a high z-index, so they're intended to break out of the document flow; Alerts, on the other hand, are usually part of the flow—except when they're [used as children of a Snackbar](#use-with-alerts). Snackbars also differ from [Dialogs](/material-ui/react-dialog/) in that Snackbars are not intended to convey _critical_ information or block the user from interacting with the rest of the app; Dialogs, by contrast, require input from the user in order to be dismissed. ## Basics ### Import ```jsx import Snackbar from '@mui/material/Snackbar'; ``` ### Position Use the `anchorOrigin` prop to control the Snackbar's position on the screen. ```tsx import * as React from 'react'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; import Button from '@mui/material/Button'; import Snackbar, { SnackbarOrigin } from '@mui/material/Snackbar'; interface State extends SnackbarOrigin { open: boolean; } export default function PositionedSnackbar() { const [state, setState] = React.useState({ open: false, vertical: 'top', horizontal: 'center', }); const { vertical, horizontal, open } = state; const handleClick = (newState: SnackbarOrigin) => () => { setState({ ...newState, open: true }); }; const handleClose = () => { setState({ ...state, open: false }); }; const buttons = ( ); return ( {buttons} ); } ``` ### Content ```jsx import SnackbarContent from '@mui/material/SnackbarContent'; ``` Use the Snackbar Content component to add text and actions to the Snackbar. ```tsx import Button from '@mui/material/Button'; import Stack from '@mui/material/Stack'; import SnackbarContent from '@mui/material/SnackbarContent'; const action = ( ); export default function LongTextSnackbar() { return ( ); } ``` ### Automatic dismiss Use the `autoHideDuration` prop to automatically trigger the Snackbar's `onClose` function after a set period of time (in milliseconds). Make sure to [provide sufficient time](https://www.w3.org/WAI/WCAG22/Understanding/timing-adjustable.html) for the user to process the information displayed on it. ```tsx import * as React from 'react'; import Button from '@mui/material/Button'; import Snackbar, { SnackbarCloseReason } from '@mui/material/Snackbar'; export default function AutohideSnackbar() { const [open, setOpen] = React.useState(false); const handleClick = () => { setOpen(true); }; const handleClose = ( event: React.SyntheticEvent | Event, reason?: SnackbarCloseReason, ) => { if (reason === 'clickaway') { return; } setOpen(false); }; return (
); } ``` ### Transitions Use `slots.transition` and `slotProps.transition` to change the transition of the Snackbar from [Grow](/material-ui/transitions/#grow) (the default) to others such as [Slide](/material-ui/transitions/#slide). ```tsx import * as React from 'react'; import Button from '@mui/material/Button'; import Snackbar from '@mui/material/Snackbar'; import Fade from '@mui/material/Fade'; import Slide, { SlideProps } from '@mui/material/Slide'; import Grow, { GrowProps } from '@mui/material/Grow'; import { TransitionProps } from '@mui/material/transitions'; function SlideTransition(props: SlideProps) { return ; } function GrowTransition(props: GrowProps) { return ; } export default function TransitionsSnackbar() { const [state, setState] = React.useState<{ open: boolean; Transition: React.ComponentType< TransitionProps & { children: React.ReactElement; } >; }>({ open: false, Transition: Fade, }); const handleClick = ( Transition: React.ComponentType< TransitionProps & { children: React.ReactElement; } >, ) => () => { setState({ open: true, Transition, }); }; const handleClose = () => { setState({ ...state, open: false, }); }; return (
); } ``` ## Customization ### Preventing default click away event If you would like to prevent the default onClickAway behavior, you can set the event's `defaultMuiPrevented` property to `true`: ```jsx { // Prevent's default 'onClickAway' behavior. event.defaultMuiPrevented = true; }, }, }} /> ``` ### Use with Alerts Use an Alert inside a Snackbar for messages that communicate a certain severity. ```tsx import * as React from 'react'; import Button from '@mui/material/Button'; import Snackbar, { SnackbarCloseReason } from '@mui/material/Snackbar'; import Alert from '@mui/material/Alert'; export default function CustomizedSnackbars() { const [open, setOpen] = React.useState(false); const handleClick = () => { setOpen(true); }; const handleClose = ( event?: React.SyntheticEvent | Event, reason?: SnackbarCloseReason, ) => { if (reason === 'clickaway') { return; } setOpen(false); }; return (
This is a success Alert inside a Snackbar!
); } ``` ### Use with Floating Action Buttons If you're using a [Floating Action Button](/material-ui/react-floating-action-button/) on mobile, Material Design recommends positioning snackbars directly above it, as shown in the demo below: ```tsx import * as React from 'react'; import AppBar from '@mui/material/AppBar'; import CssBaseline from '@mui/material/CssBaseline'; import GlobalStyles from '@mui/material/GlobalStyles'; import Toolbar from '@mui/material/Toolbar'; import IconButton from '@mui/material/IconButton'; import MenuIcon from '@mui/icons-material/Menu'; import Typography from '@mui/material/Typography'; import Button from '@mui/material/Button'; import Fab from '@mui/material/Fab'; import AddIcon from '@mui/icons-material/Add'; import Snackbar from '@mui/material/Snackbar'; export default function FabIntegrationSnackbar() { return ( ({ body: { backgroundColor: theme.palette.background.paper }, })} />
App bar ({ position: 'absolute', bottom: theme.spacing(2), right: theme.spacing(2), })} > Undo } sx={{ bottom: { xs: 90, sm: 0 } }} />
); } ``` ## Common examples ### Consecutive Snackbars This demo shows how to display multiple Snackbars without stacking them by using a consecutive animation. ```tsx import * as React from 'react'; import Button from '@mui/material/Button'; import Snackbar, { SnackbarCloseReason } from '@mui/material/Snackbar'; import IconButton from '@mui/material/IconButton'; import CloseIcon from '@mui/icons-material/Close'; export interface SnackbarMessage { message: string; key: number; } export default function ConsecutiveSnackbars() { const [snackPack, setSnackPack] = React.useState([]); const [open, setOpen] = React.useState(false); const [messageInfo, setMessageInfo] = React.useState( undefined, ); React.useEffect(() => { if (snackPack.length && !messageInfo) { // Set a new snack when we don't have an active one setMessageInfo({ ...snackPack[0] }); setSnackPack((prev) => prev.slice(1)); setOpen(true); } else if (snackPack.length && messageInfo && open) { // Close an active snack when a new one is added setOpen(false); } }, [snackPack, messageInfo, open]); const handleClick = (message: string) => () => { setSnackPack((prev) => [...prev, { message, key: new Date().getTime() }]); }; const handleClose = ( event: React.SyntheticEvent | Event, reason?: SnackbarCloseReason, ) => { if (reason === 'clickaway') { return; } setOpen(false); }; const handleExited = () => { setMessageInfo(undefined); }; return (
} />
); } ``` ## Supplementary components ### notistack ![stars](https://img.shields.io/github/stars/iamhosseindhv/notistack.svg?style=social&label=Star) ![npm downloads](https://img.shields.io/npm/dm/notistack.svg) With an imperative API, [notistack](https://github.com/iamhosseindhv/notistack) lets you vertically stack multiple Snackbars without having to handle their open and close states. Even though this is discouraged in the Material Design guidelines, it is still a common pattern. ```tsx import * as React from 'react'; import Button from '@mui/material/Button'; import { SnackbarProvider, VariantType, useSnackbar } from 'notistack'; function MyApp() { const { enqueueSnackbar } = useSnackbar(); const handleClick = () => { enqueueSnackbar('I love snacks.'); }; const handleClickVariant = (variant: VariantType) => () => { // variant could be success, error, warning, info, or default enqueueSnackbar('This is a success message!', { variant }); }; return ( ); } export default function IntegrationNotistack() { return ( ); } ``` :::warning Note that notistack prevents Snackbars from being [closed by pressing Escape](#accessibility). ::: ## Accessibility The user should be able to dismiss Snackbars by pressing Escape. If there are multiple instances appearing at the same time and you want Escape to dismiss only the oldest one that's currently open, call `event.preventDefault` in the `onClose` prop. ```jsx export default function MyComponent() { const [open, setOpen] = React.useState(true); return ( { // `reason === 'escapeKeyDown'` if `Escape` was pressed setOpen(false); // call `event.preventDefault` to only close one Snackbar at a time. }} /> setOpen(false)} /> ); } ``` ## Anatomy The Snackbar component is composed of a root `
` that houses interior elements like the Snackbar Content and other optional components (such as buttons or decorators). ```html ``` # Snackbar API ## Demos For examples and details on the usage of this React component, visit the component demo pages: - [Snackbar](https://mui.com/material-ui/react-snackbar/) ## Import ```jsx import Snackbar from '@mui/material/Snackbar'; // or import { Snackbar } from '@mui/material'; ``` ## Props | Name | Type | Default | Required | Description | |------|------|---------|----------|-------------| | action | `node` | - | No | | | anchorOrigin | `{ horizontal: 'center' \| 'left' \| 'right', vertical: 'bottom' \| 'top' }` | `{ vertical: 'bottom', horizontal: 'left' }` | No | | | autoHideDuration | `number` | `null` | No | | | children | `element` | - | No | | | classes | `object` | - | No | Override or extend the styles applied to the component. | | disableWindowBlurListener | `bool` | `false` | No | | | key | `any` | - | No | | | message | `node` | - | No | | | onClose | `function(event: React.SyntheticEvent \| Event, reason: string) => void` | - | No | | | open | `bool` | - | No | | | resumeHideDuration | `number` | - | No | | | slotProps | `{ clickAwayListener?: func \| object, content?: func \| object, root?: func \| object, transition?: func \| object }` | `{}` | No | | | slots | `{ clickAwayListener?: elementType, content?: elementType, root?: elementType, transition?: elementType }` | `{}` | No | | | sx | `Array \| func \| object` | - | No | The system prop that allows defining system overrides as well as additional CSS styles. | | transitionDuration | `number \| { appear?: number, enter?: number, exit?: number }` | `{ enter: theme.transitions.duration.enteringScreen, exit: theme.transitions.duration.leavingScreen, }` | No | | > **Note**: The `ref` is forwarded to the root element (HTMLDivElement). > Any other props supplied will be provided to the root element (native element). ## Theme default props You can use `MuiSnackbar` to change the default props of this component with the theme. ## Slots | Name | Default | Class | Description | |------|---------|-------|-------------| | root | `'div'` | `.MuiSnackbar-root` | The component that renders the root slot. | | content | `SnackbarContent` | - | The component that renders the content slot. | | clickAwayListener | `ClickAwayListener` | - | The component that renders the clickAwayListener slot. | | transition | `Grow` | - | The component that renders the transition. [Follow this guide](/material-ui/transitions/#transition-slots) to learn more about the requirements for this component. | ## CSS ### Rule name | Global class | Rule name | Description | |--------------|-----------|-------------| | - | anchorOriginBottomCenter | Styles applied to the root element if `anchorOrigin={{ 'bottom', 'center' }}`. | | - | anchorOriginBottomLeft | Styles applied to the root element if `anchorOrigin={{ 'bottom', 'left' }}`. | | - | anchorOriginBottomRight | Styles applied to the root element if `anchorOrigin={{ 'bottom', 'right' }}`. | | - | anchorOriginTopCenter | Styles applied to the root element if `anchorOrigin={{ 'top', 'center' }}`. | | - | anchorOriginTopLeft | Styles applied to the root element if `anchorOrigin={{ 'top', 'left' }}`. | | - | anchorOriginTopRight | Styles applied to the root element if `anchorOrigin={{ 'top', 'right' }}`. | ## Source code If you did not find the information on this page, consider having a look at the implementation of the component for more detail. - [/packages/mui-material/src/Snackbar/Snackbar.js](https://github.com/mui/material-ui/tree/HEAD/packages/mui-material/src/Snackbar/Snackbar.js) # SnackbarContent API ## Demos For examples and details on the usage of this React component, visit the component demo pages: - [Snackbar](https://mui.com/material-ui/react-snackbar/) ## Import ```jsx import SnackbarContent from '@mui/material/SnackbarContent'; // or import { SnackbarContent } from '@mui/material'; ``` ## Props | Name | Type | Default | Required | Description | |------|------|---------|----------|-------------| | action | `node` | - | No | | | classes | `object` | - | No | Override or extend the styles applied to the component. | | message | `node` | - | No | | | role | `string` | `'alert'` | No | | | sx | `Array \| func \| object` | - | No | The system prop that allows defining system overrides as well as additional CSS styles. | > **Note**: The `ref` is forwarded to the root element (HTMLDivElement). > Any other props supplied will be provided to the root element ([Paper](https://mui.com/material-ui/api/paper/)). ## Inheritance While not explicitly documented above, the props of the [Paper](https://mui.com/material-ui/api/paper/) component are also available on SnackbarContent. ## Theme default props You can use `MuiSnackbarContent` to change the default props of this component with the theme. ## CSS ### Rule name | Global class | Rule name | Description | |--------------|-----------|-------------| | - | action | Styles applied to the action wrapper element if `action` is provided. | | - | message | Styles applied to the message wrapper element. | | - | root | Styles applied to the root element. | ## Source code If you did not find the information on this page, consider having a look at the implementation of the component for more detail. - [/packages/mui-material/src/SnackbarContent/SnackbarContent.js](https://github.com/mui/material-ui/tree/HEAD/packages/mui-material/src/SnackbarContent/SnackbarContent.js)