@rocket.chat/fuselage-toastbar
Version:
Fuselage ToastBar component
30 lines • 2.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const ToastBarContext_1 = require("./ToastBarContext");
const ToastBarPersistent_1 = __importDefault(require("./ToastBarPersistent"));
const ToastBarPortal_1 = __importDefault(require("./ToastBarPortal"));
const ToastBarTimed_1 = __importDefault(require("./ToastBarTimed"));
const ToastBarZone_1 = __importDefault(require("./ToastBarZone"));
const ToastBarProvider = ({ children }) => {
const [toasts, setToasts] = (0, react_1.useState)([]);
const contextValue = {
dispatch: (0, react_1.useCallback)((option) => setToasts((toasts) => [
...toasts,
{ ...option, time: option.time || 5, id: Math.random().toString() },
]), []),
dismiss: (0, react_1.useCallback)((id) => setToasts((prevState) => prevState.filter((toast) => toast.id !== id)), []),
};
return ((0, jsx_runtime_1.jsxs)(ToastBarContext_1.ToastBarContext.Provider, { value: contextValue, children: [children, (0, jsx_runtime_1.jsx)(ToastBarPortal_1.default, { children: Object.entries(toasts?.reduce((zones, toast) => {
zones[toast.position || 'top-end'] =
zones[toast.position || 'top-end'] || [];
zones[toast.position || 'top-end'].push(toast);
return zones;
}, {})).map(([zone, toasts]) => ((0, jsx_runtime_1.jsx)(ToastBarZone_1.default, { position: zone, children: toasts.map((toast) => toast.isPersistent ? ((0, jsx_runtime_1.jsx)(ToastBarPersistent_1.default, { ...toast }, toast.id)) : ((0, jsx_runtime_1.jsx)(ToastBarTimed_1.default, { ...toast }, toast.id))) }, zone))) })] }));
};
exports.default = (0, react_1.memo)(ToastBarProvider);
//# sourceMappingURL=ToastBarProvider.js.map