UNPKG

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