notistack-v2-maintained
Version:
Highly customizable notification snackbars (toasts) that can be stacked on top of each other
81 lines (80 loc) • 3.97 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Snackbar;
const jsx_runtime_1 = require("@emotion/react/jsx-runtime");
const ClickAwayListener_1 = __importDefault(require("@mui/material/ClickAwayListener"));
const react_1 = require("react");
const constants_1 = require("./constants");
const useEventCallback_1 = __importDefault(require("./useEventCallback"));
function Snackbar(props) {
const { children, autoHideDuration, ClickAwayListenerProps, disableWindowBlurListener = false, onClose, onMouseEnter, onMouseLeave, open, ref, resumeHideDuration } = props, other = __rest(props, ["children", "autoHideDuration", "ClickAwayListenerProps", "disableWindowBlurListener", "onClose", "onMouseEnter", "onMouseLeave", "open", "ref", "resumeHideDuration"]);
const timerAutoHide = (0, react_1.useRef)(null);
const handleClose = (0, useEventCallback_1.default)(onClose);
const setAutoHideTimer = (0, useEventCallback_1.default)((autoHideDurationParam) => {
if (!onClose || autoHideDurationParam == null) {
return;
}
clearTimeout(timerAutoHide.current);
timerAutoHide.current = setTimeout(() => {
handleClose(null, constants_1.REASONS.TIMEOUT);
}, autoHideDurationParam);
});
(0, react_1.useEffect)(() => {
if (open) {
setAutoHideTimer(autoHideDuration);
}
return () => {
clearTimeout(timerAutoHide.current);
};
}, [open, autoHideDuration, setAutoHideTimer]);
/**
* Pause the timer when the user is interacting with the Snackbar
* or when the user hide the window.
*/
const handlePause = () => {
clearTimeout(timerAutoHide.current);
};
/**
* Restart the timer when the user is no longer interacting with the Snackbar
* or when the window is shown back.
*/
const handleResume = (0, react_1.useCallback)(() => {
if (autoHideDuration != null) {
setAutoHideTimer(resumeHideDuration != null ? resumeHideDuration : autoHideDuration * 0.5);
}
}, [autoHideDuration, resumeHideDuration, setAutoHideTimer]);
(0, react_1.useEffect)(() => {
if (!disableWindowBlurListener && open) {
window.addEventListener("focus", handleResume);
window.addEventListener("blur", handlePause);
return () => {
window.removeEventListener("focus", handleResume);
window.removeEventListener("blur", handlePause);
};
}
return undefined;
}, [disableWindowBlurListener, handleResume, open]);
return ((0, jsx_runtime_1.jsx)(ClickAwayListener_1.default, Object.assign({}, ClickAwayListenerProps, { onClickAway: (event) => {
onClose === null || onClose === void 0 ? void 0 : onClose(event, constants_1.REASONS.CLICKAWAY);
}, children: (0, jsx_runtime_1.jsx)("div", Object.assign({}, other, { onMouseEnter: (event) => {
onMouseEnter === null || onMouseEnter === void 0 ? void 0 : onMouseEnter(event);
handlePause();
}, onMouseLeave: (event) => {
onMouseLeave === null || onMouseLeave === void 0 ? void 0 : onMouseLeave(event);
handleResume();
}, children: children })) })));
}