UNPKG

4.07 kBJavaScriptView Raw
1"use strict";
2"use client";
3
4var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
5exports.__esModule = true;
6exports.default = void 0;
7var React = _interopRequireWildcard(require("react"));
8var _classnames = _interopRequireDefault(require("classnames"));
9var _useTimeout = _interopRequireDefault(require("@restart/hooks/useTimeout"));
10var _ToastFade = _interopRequireDefault(require("./ToastFade"));
11var _ToastHeader = _interopRequireDefault(require("./ToastHeader"));
12var _ToastBody = _interopRequireDefault(require("./ToastBody"));
13var _ThemeProvider = require("./ThemeProvider");
14var _ToastContext = _interopRequireDefault(require("./ToastContext"));
15var _jsxRuntime = require("react/jsx-runtime");
16function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
17function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
18const Toast = /*#__PURE__*/React.forwardRef(({
19 bsPrefix,
20 className,
21 transition: Transition = _ToastFade.default,
22 show = true,
23 animation = true,
24 delay = 5000,
25 autohide = false,
26 onClose,
27 onEntered,
28 onExit,
29 onExiting,
30 onEnter,
31 onEntering,
32 onExited,
33 bg,
34 ...props
35}, ref) => {
36 bsPrefix = (0, _ThemeProvider.useBootstrapPrefix)(bsPrefix, 'toast');
37
38 // We use refs for these, because we don't want to restart the autohide
39 // timer in case these values change.
40 const delayRef = (0, React.useRef)(delay);
41 const onCloseRef = (0, React.useRef)(onClose);
42 (0, React.useEffect)(() => {
43 delayRef.current = delay;
44 onCloseRef.current = onClose;
45 }, [delay, onClose]);
46 const autohideTimeout = (0, _useTimeout.default)();
47 const autohideToast = !!(autohide && show);
48 const autohideFunc = (0, React.useCallback)(() => {
49 if (autohideToast) {
50 onCloseRef.current == null ? void 0 : onCloseRef.current();
51 }
52 }, [autohideToast]);
53 (0, React.useEffect)(() => {
54 // Only reset timer if show or autohide changes.
55 autohideTimeout.set(autohideFunc, delayRef.current);
56 }, [autohideTimeout, autohideFunc]);
57 const toastContext = (0, React.useMemo)(() => ({
58 onClose
59 }), [onClose]);
60 const hasAnimation = !!(Transition && animation);
61 const toast = /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
62 ...props,
63 ref: ref,
64 className: (0, _classnames.default)(bsPrefix, className, bg && `bg-${bg}`, !hasAnimation && (show ? 'show' : 'hide')),
65 role: "alert",
66 "aria-live": "assertive",
67 "aria-atomic": "true"
68 });
69 return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ToastContext.default.Provider, {
70 value: toastContext,
71 children: hasAnimation && Transition ? /*#__PURE__*/(0, _jsxRuntime.jsx)(Transition, {
72 in: show,
73 onEnter: onEnter,
74 onEntering: onEntering,
75 onEntered: onEntered,
76 onExit: onExit,
77 onExiting: onExiting,
78 onExited: onExited,
79 unmountOnExit: true,
80 children: toast
81 }) : toast
82 });
83});
84Toast.displayName = 'Toast';
85var _default = Object.assign(Toast, {
86 Body: _ToastBody.default,
87 Header: _ToastHeader.default
88});
89exports.default = _default;
90module.exports = exports.default;
\No newline at end of file