Snackbar API
导入
import Snackbar from '@mui/material/Snackbar';
// 或
import { Snackbar } from '@mui/material';
组件名称
在主题中,名称“MuiSnackbar
”可用于提供默认属性(props),或者样式覆盖属性
原生(Native) 组件的属性也是可用的。
名称 | 类型 | 默认值 | 描述 |
---|---|---|---|
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 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: React.SyntheticEvent<any> | Event, reason: string) => void event: The event source of the callback. reason: Can be: "timeout" (autoHideDuration expired), "clickaway" , or "escapeKeyDown" . | |
open | bool | false | Control the popup` open state. |
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. | |
sx | Array<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. | |
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: 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. |
TransitionProps | object | {} | Props applied to the transition element. By default, the element is based on this Transition component. |
ref
则会被传递到根元素中。CSS
规则名称 | 全局类名 | 描述 |
---|---|---|
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 根元素 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' }} . |
您可以使用组件自定义选项对组件进行个性化:
- 具备 全局类名.
- 在自定义主体中,设定一个规则名称作为组件的
styleOverrides
属性的一部分。