Saltar al contenido

Snackbar API

API documentation for the React Snackbar component. Learn about the available props, and the CSS API.

Import

import Snackbar from '@material-ui/core/Snackbar';
// o
import { Snackbar } from '@material-ui/core';
You can learn about the difference by reading this guide on minimizing bundle size.

Component name

The name MuiSnackbar can be used when providing default props or style overrides in the theme.

Props

NombreTipoPor defectoDescripción
actionnodeThe 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: 'left' }The anchor of the Snackbar. On smaller screens, the component grows to occupy all the available width, the horizontal alignment is ignored.
autoHideDurationnumbernullThe 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.
childrenelementReplace the SnackbarContent component.
classesobjectOverride or extend the styles applied to the component. See CSS API below for more details.
ClickAwayListenerPropsobjectProps applied to the ClickAwayListener element.
ContentPropsobjectProps applied to the SnackbarContent element.
disableWindowBlurListenerboolfalseIf true, the autoHideDuration timer will expire even if the window is not focused.
keyanyWhen 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.
messagenodeThe message to display.
onClosefuncCallback 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".
openboolfalseControl the popup` open state.
resumeHideDurationnumberThe 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.
TransitionComponentelementTypeGrowThe component used for the transition. Follow this guide to learn more about the requirements for this component.
transitionDurationnumber
| { 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.
TransitionPropsobject{}Props applied to the transition element. By default, the element is based on this Transition component.

The ref is forwarded to the root element.
Any other props supplied will be provided to the root element (native element).

CSS

Rule nameGlobal classDescripción
root.MuiSnackbar-rootStyles applied to the root element.
anchorOriginTopCenter.MuiSnackbar-anchorOriginTopCenterStyles applied to the root element if anchorOrigin={{ 'top', 'center' }}.
anchorOriginBottomCenter.MuiSnackbar-anchorOriginBottomCenterStyles applied to the root element if anchorOrigin={{ 'bottom', 'center' }}.
anchorOriginTopRight.MuiSnackbar-anchorOriginTopRightStyles applied to the root element if anchorOrigin={{ 'top', 'right' }}.
anchorOriginBottomRight.MuiSnackbar-anchorOriginBottomRightStyles applied to the root element if anchorOrigin={{ 'bottom', 'right' }}.
anchorOriginTopLeft.MuiSnackbar-anchorOriginTopLeftStyles applied to the root element if anchorOrigin={{ 'top', 'left' }}.
anchorOriginBottomLeft.MuiSnackbar-anchorOriginBottomLeftStyles applied to the root element if anchorOrigin={{ 'bottom', 'left' }}.

You can override the style of the component using one of these customization options: If that isn't sufficient, you can check the implementation of the component for more detail.

Demos