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';
// 或
import { Snackbar } from '@material-ui/core';Component name
The nameMuiSnackbar can be used when providing default props or style overrides in the theme.属性
| 名称 | 类型 | 默认值 | 描述 | 
|---|---|---|---|
| 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: 'left' } | The anchor of the Snackbar. On smaller screens, the component grows to occupy all the available width, the horizontal alignment is ignored. | 
| autoHideDuration | number | null | The number of milliseconds to wait before automatically calling the onClosefunction.onCloseshould then set the state of theopenprop to hide the Snackbar. This behavior is disabled by default with thenullvalue. | 
| children | element | Replace the SnackbarContentcomponent. | |
| classes | object | Override or extend the styles applied to the component. See CSS API below for more details. | |
| ClickAwayListenerProps | object | Props applied to the ClickAwayListenerelement. | |
| ContentProps | object | Props applied to the SnackbarContentelement. | |
| disableWindowBlurListener | bool | false | If true, theautoHideDurationtimer 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 onCloseis used to set state in the parent component, which is used to control theSnackbaropenprop. Thereasonparameter can optionally be used to control the response toonClose, for example ignoringclickaway.Signature: function(event: object, reason: string) => voidevent: The event source of the callback. reason: Can be: "timeout"(autoHideDurationexpired),"clickaway". | |
| open | bool | false | Control the popup` open state. | 
| resumeHideDuration | number | The number of milliseconds to wait before dismissing after user interaction. If autoHideDurationprop isn't specified, it does nothing. IfautoHideDurationprop is specified butresumeHideDurationisn't, we default toautoHideDuration / 2ms. | |
| 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. By default, the element is based on this Transitioncomponent. | 
ref 则会被传递到根元素中。Any other props supplied will be provided to the root element (native element).
CSS
| Rule name | Global class | 描述 | 
|---|---|---|
| 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 using one of these customization options:
- With a rule name of the classesobject prop.
- With a global class name.
- With a theme and an styleOverridesproperty.