UNPKG

5.57 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/esm/extends";
2import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3const _excluded = ["autoHideDuration", "children", "disableWindowBlurListener", "exited", "onBlur", "onClose", "onFocus", "onMouseEnter", "onMouseLeave", "open", "resumeHideDuration", "slotProps", "slots"];
4import * as React from 'react';
5import PropTypes from 'prop-types';
6import ClickAwayListener from '../ClickAwayListener';
7import composeClasses from '../composeClasses';
8import { getSnackbarUtilityClass } from './snackbarClasses';
9import useSnackbar from '../useSnackbar';
10import { useSlotProps } from '../utils';
11import { useClassNamesOverride } from '../utils/ClassNameConfigurator';
12import { jsx as _jsx } from "react/jsx-runtime";
13const useUtilityClasses = () => {
14 const slots = {
15 root: ['root']
16 };
17 return composeClasses(slots, useClassNamesOverride(getSnackbarUtilityClass));
18};
19/**
20 *
21 * Demos:
22 *
23 * - [Snackbar](https://mui.com/base/react-snackbar/)
24 * - [Snackbar](https://mui.com/material-ui/react-snackbar/)
25 *
26 * API:
27 *
28 * - [Snackbar API](https://mui.com/base/react-snackbar/components-api/#snackbar)
29 */
30const Snackbar = /*#__PURE__*/React.forwardRef(function Snackbar(props, forwardedRef) {
31 const {
32 autoHideDuration = null,
33 children,
34 disableWindowBlurListener = false,
35 exited = true,
36 onClose,
37 open,
38 resumeHideDuration,
39 slotProps = {},
40 slots = {}
41 } = props,
42 other = _objectWithoutPropertiesLoose(props, _excluded);
43 const classes = useUtilityClasses();
44 const {
45 getRootProps,
46 onClickAway
47 } = useSnackbar(_extends({}, props, {
48 autoHideDuration,
49 disableWindowBlurListener,
50 onClose,
51 open,
52 resumeHideDuration
53 }));
54 const ownerState = props;
55 const Root = slots.root || 'div';
56 const rootProps = useSlotProps({
57 elementType: Root,
58 getSlotProps: getRootProps,
59 externalForwardedProps: other,
60 externalSlotProps: slotProps.root,
61 additionalProps: {
62 ref: forwardedRef
63 },
64 ownerState,
65 className: classes.root
66 });
67 const clickAwayListenerProps = useSlotProps({
68 elementType: ClickAwayListener,
69 externalSlotProps: slotProps.clickAwayListener,
70 additionalProps: {
71 onClickAway
72 },
73 ownerState
74 });
75
76 // ClickAwayListener doesn't support ownerState
77 delete clickAwayListenerProps.ownerState;
78
79 // So that we only render active snackbars.
80 if (!open && exited) {
81 return null;
82 }
83 return /*#__PURE__*/_jsx(ClickAwayListener, _extends({}, clickAwayListenerProps, {
84 children: /*#__PURE__*/_jsx(Root, _extends({}, rootProps, {
85 children: children
86 }))
87 }));
88});
89process.env.NODE_ENV !== "production" ? Snackbar.propTypes /* remove-proptypes */ = {
90 // ----------------------------- Warning --------------------------------
91 // | These PropTypes are generated from the TypeScript type definitions |
92 // | To update them edit TypeScript types and run "yarn proptypes" |
93 // ----------------------------------------------------------------------
94 /**
95 * The number of milliseconds to wait before automatically calling the
96 * `onClose` function. `onClose` should then set the state of the `open`
97 * prop to hide the Snackbar. This behavior is disabled by default with
98 * the `null` value.
99 * @default null
100 */
101 autoHideDuration: PropTypes.number,
102 /**
103 * @ignore
104 */
105 children: PropTypes.node,
106 /**
107 * If `true`, the `autoHideDuration` timer will expire even if the window is not focused.
108 * @default false
109 */
110 disableWindowBlurListener: PropTypes.bool,
111 /**
112 * The prop used to handle exited transition and unmount the component.
113 * @default true
114 */
115 exited: PropTypes.bool,
116 /**
117 * Callback fired when the component requests to be closed.
118 * Typically `onClose` is used to set state in the parent component,
119 * which is used to control the `Snackbar` `open` prop.
120 * The `reason` parameter can optionally be used to control the response to `onClose`,
121 * for example ignoring `clickaway`.
122 *
123 * @param {React.SyntheticEvent<any> | Event} event The event source of the callback.
124 * @param {string} reason Can be: `"timeout"` (`autoHideDuration` expired), `"clickaway"`, or `"escapeKeyDown"`.
125 */
126 onClose: PropTypes.func,
127 /**
128 * If `true`, the component is shown.
129 */
130 open: PropTypes.bool,
131 /**
132 * The number of milliseconds to wait before dismissing after user interaction.
133 * If `autoHideDuration` prop isn't specified, it does nothing.
134 * If `autoHideDuration` prop is specified but `resumeHideDuration` isn't,
135 * we default to `autoHideDuration / 2` ms.
136 */
137 resumeHideDuration: PropTypes.number,
138 /**
139 * The props used for each slot inside the Snackbar.
140 * @default {}
141 */
142 slotProps: PropTypes.shape({
143 clickAwayListener: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
144 children: PropTypes.element.isRequired,
145 disableReactTree: PropTypes.bool,
146 mouseEvent: PropTypes.oneOf(['onClick', 'onMouseDown', 'onMouseUp', 'onPointerDown', 'onPointerUp', false]),
147 onClickAway: PropTypes.func,
148 touchEvent: PropTypes.oneOf(['onTouchEnd', 'onTouchStart', false])
149 })]),
150 root: PropTypes.oneOfType([PropTypes.func, PropTypes.object])
151 }),
152 /**
153 * The components used for each slot inside the Snackbar.
154 * Either a string to use a HTML element or a component.
155 * @default {}
156 */
157 slots: PropTypes.shape({
158 root: PropTypes.elementType
159 })
160} : void 0;
161export default Snackbar;
\No newline at end of file