Pular para o conteúdo

Snackbar API

Documentação da API para o componente React Snackbar . Aprenda sobre as propriedades disponíveis e a API CSS.

Importação

import Snackbar from '@mui/material/Snackbar';
// ou
import { Snackbar } from '@mui/material';
Você pode aprender sobre a diferença lendo este guia sobre como minimizar o tamanho do pacote.

Nome do componente

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

Propriedades

Propriedades do componente nativo também estão disponíveis.

NomeTipoPadrãoDescrição
actionnode
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: 'left' }
The anchor of the Snackbar. On smaller screens, the component grows to occupy all the available width, the horizontal alignment is ignored.
autoHideDurationnumbernull
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.
childrenelement
Replace the SnackbarContent component.
classesobject
Sobrescreve ou extende os estilos aplicados para o componente. Veja a API CSS abaixo para maiores detalhes.
ClickAwayListenerPropsobject
Props applied to the ClickAwayListener element.
ContentPropsobject
Props applied to the SnackbarContent element.
disableWindowBlurListenerboolfalse
If true, the autoHideDuration timer will expire even if the window is not focused.
keyany
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.
messagenode
The message to display.
onClosefunc
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: React.SyntheticEvent<any> | Event, reason: string) => void
event: The event source of the callback.
reason: Can be: "timeout" (autoHideDuration expired), "clickaway", or "escapeKeyDown".
openboolfalse
Control the popup` open state.
resumeHideDurationnumber
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.
sxArray<func
| object
| bool>
| func
| object
The system prop that allows defining system overrides as well as additional CSS styles. See the `sx` page for more details.
TransitionComponentelementTypeGrow
The 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: theme.transitions.duration.enteringScreen, exit: theme.transitions.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.

O ref é encaminhado para o elemento raiz.

CSS

Nome da regraClasse globalDescrição
root.MuiSnackbar-rootEstilos aplicados ao elemento raiz.
anchorOriginTopCenter.MuiSnackbar-anchorOriginTopCenterEstilos aplicados para o elemento raiz se anchorOrigin={{ 'top', 'center' }}.
anchorOriginBottomCenter.MuiSnackbar-anchorOriginBottomCenterEstilos aplicados para o elemento raiz se anchorOrigin={{ 'bottom', 'center' }}.
anchorOriginTopRight.MuiSnackbar-anchorOriginTopRightEstilos aplicados para o elemento raiz se anchorOrigin={{ 'top', 'right' }}.
anchorOriginBottomRight.MuiSnackbar-anchorOriginBottomRightEstilos aplicados para o elemento raiz se anchorOrigin={{ 'bottom', 'right' }}.
anchorOriginTopLeft.MuiSnackbar-anchorOriginTopLeftEstilos aplicados para o elemento raiz se anchorOrigin={{ 'top', 'left' }}.
anchorOriginBottomLeft.MuiSnackbar-anchorOriginBottomLeftEstilos aplicados para o elemento raiz se anchorOrigin={{ 'bottom', 'left' }}.

Você pode sobrescrever o estilo do componente usando uma dessas opções de customização:

Demonstrações