@trellixio/roaster-coffee
Version:
Beans' product component library
56 lines (51 loc) • 1.32 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
require('react');
var index = require('../../../../utils/useQueue/index.js');
require('@floating-ui/react');
function useToastsState({ limit }) {
const { state, queue, update, cleanQueue } = index.useQueue({
initialValues: [],
limit
});
const showToast = (toast) => {
const { id } = toast;
update((toasts) => {
if (toast.id && toasts.some((n) => n.id === toast.id)) {
return toasts;
}
return [...toasts, { ...toast, id }];
});
return id;
};
const updateToast = (toast) => update((toasts) => {
const index = toasts.findIndex((n) => n.id === toast.id);
if (index === -1) {
return toasts;
}
const newToasts = [...toasts];
newToasts[index] = toast;
return newToasts;
});
const hideToast = (id) => update(
(toasts) => toasts.filter((toast) => {
if (toast.id === id) {
typeof toast.onClose === "function" && toast.onClose(toast);
return false;
}
return true;
})
);
const clean = () => update(() => []);
return {
toasts: state,
queue,
showToast,
updateToast,
hideToast,
cleanQueue,
clean
};
}
exports.default = useToastsState;
//# sourceMappingURL=useToastsState.js.map