跳转到内容

🎉 Material UI v5 is out now! Check out the announcement blog post

Snackbar API

The API documentation of the Snackbar React component. Learn more about the props and the CSS customization points.

Import

import Snackbar from '@material-ui/core/Snackbar';
// or
import { Snackbar } from '@material-ui/core';

You can learn more about the difference by reading this guide.

Component name

The MuiSnackbar name can be used for providing default props or style overrides at the theme level.

Props

Name Type Default Description
action node The action to display. It renders after the message, at the end of the snackbar.
anchorOrigin { horizontal: 'center'
| 'left'
| 'right', vertical: 'bottom'
| 'top' }
{ vertical: 'bottom', horizontal: 'center' } The anchor of the Snackbar.
autoHideDuration number null The number of milliseconds to wait before automatically calling the onClose function. onClose should then set the state of the open prop to hide the Snackbar. This behavior is disabled by default with the null value.
children element Replace the SnackbarContent component.
classes object Override or extend the styles applied to the component. See CSS API below for more details.
ClickAwayListenerProps object Props applied to the ClickAwayListener element.
ContentProps object Props applied to the SnackbarContent element.
disableWindowBlurListener bool false If true, the autoHideDuration timer will expire even if the window is not focused.
key any When displaying multiple consecutive Snackbars from a parent rendering a single <Snackbar/>, add the key prop to ensure independent treatment of each message. e.g. <Snackbar key={message} />, otherwise, the message may update-in-place and features such as autoHideDuration may be canceled.
message node The message to display.
onClose func Callback fired when the component requests to be closed. Typically onClose is used to set state in the parent component, which is used to control the Snackbar open prop. The reason parameter can optionally be used to control the response to onClose, for example ignoring clickaway.

Signature:
function(event: object, reason: string) => void
event: The event source of the callback.
reason: Can be: "timeout" (autoHideDuration expired), "clickaway".
onEnter func Deprecated. Use the TransitionProps prop instead.

Callback fired before the transition is entering.
onEntered func Deprecated. Use the TransitionProps prop instead.

Callback fired when the transition has entered.
onEntering func Deprecated. Use the TransitionProps prop instead.

Callback fired when the transition is entering.
onExit func Deprecated. Use the TransitionProps prop instead.

Callback fired before the transition is exiting.
onExited func Deprecated. Use the TransitionProps prop instead.

Callback fired when the transition has exited.
onExiting func Deprecated. Use the TransitionProps prop instead.

Callback fired when the transition is exiting.
open bool If true, Snackbar is open.
resumeHideDuration number The number of milliseconds to wait before dismissing after user interaction. If autoHideDuration prop isn't specified, it does nothing. If autoHideDuration prop is specified but resumeHideDuration isn't, we default to autoHideDuration / 2 ms.
TransitionComponent elementType Grow The component used for the transition. Follow this guide to learn more about the requirements for this component.
transitionDuration number
| { appear?: number, enter?: number, exit?: number }
{ enter: duration.enteringScreen, exit: duration.leavingScreen,} The duration for the transition, in milliseconds. You may specify a single timeout for all transitions, or individually with an object.
TransitionProps object Props applied to the Transition element.

The ref is forwarded to the root element.

Any other props supplied will be provided to the root element (native element).

CSS

Rule name Global class Description
root .MuiSnackbar-root Styles applied to the root element.
anchorOriginTopCenter .MuiSnackbar-anchorOriginTopCenter Styles applied to the root element if anchorOrigin={{ 'top', 'center' }}.
anchorOriginBottomCenter .MuiSnackbar-anchorOriginBottomCenter Styles applied to the root element if anchorOrigin={{ 'bottom', 'center' }}.
anchorOriginTopRight .MuiSnackbar-anchorOriginTopRight Styles applied to the root element if anchorOrigin={{ 'top', 'right' }}.
anchorOriginBottomRight .MuiSnackbar-anchorOriginBottomRight Styles applied to the root element if anchorOrigin={{ 'bottom', 'right' }}.
anchorOriginTopLeft .MuiSnackbar-anchorOriginTopLeft Styles applied to the root element if anchorOrigin={{ 'top', 'left' }}.
anchorOriginBottomLeft .MuiSnackbar-anchorOriginBottomLeft Styles applied to the root element if anchorOrigin={{ 'bottom', 'left' }}.

You can override the style of the component thanks to one of these customization points:

If that's not sufficient, you can check the implementation of the component for more detail.

Demos