@trellixio/roaster-coffee
Version:
Beans' product component library
52 lines (49 loc) • 2.11 kB
JavaScript
import * as React from 'react';
import { Transition, TransitionGroup } from 'react-transition-group';
import { classNames } from '../../../utils/classNames/index.js';
import { guid } from '../../../utils/guid/index.js';
import { useDidUpdate } from '../../../utils/useDidUpdate/index.js';
import { useForceUpdate } from '../../../utils/useForceUpdate/index.js';
import '@floating-ui/react';
import { useToastsEvents } from '../events.js';
import { ToastsContext } from '../Toasts.context.js';
import { ToastsContainer } from '../ToastsContainer/index.js';
import useToastsState from './useToastsState/useToastsState.js';
function ToastsProvider({ className, autoClose = 4e3, limit = 5, children }) {
const forceUpdate = useForceUpdate();
const refs = React.useRef({});
const previousLength = React.useRef(0);
const { toasts, queue, showToast, updateToast, hideToast, clean, cleanQueue } = useToastsState({ limit });
useDidUpdate(() => {
if (toasts.length > previousLength.current) {
setTimeout(() => forceUpdate(), 0);
}
previousLength.current = toasts.length;
}, [toasts]);
useToastsEvents({
show: showToast,
hide: hideToast,
update: updateToast,
clean,
cleanQueue
});
const items = toasts.map((toast) => {
toast.id = toast.id || guid();
return /* @__PURE__ */ React.createElement(Transition, { key: toast.id, nodeRef: { current: refs.current[toast.id] } }, () => /* @__PURE__ */ React.createElement(
ToastsContainer,
{
innerRef: (node) => {
refs.current[toast.id] = node;
},
toast,
onHide: hideToast,
autoClose
}
));
});
const contextValue = React.useMemo(() => ({ toasts, queue }), []);
return /* @__PURE__ */ React.createElement(ToastsContext.Provider, { value: contextValue }, /* @__PURE__ */ React.createElement("section", { className: classNames("toasts", className) }, /* @__PURE__ */ React.createElement(TransitionGroup, null, items)), children);
}
ToastsProvider.displayName = "ToastsProvider";
export { ToastsProvider };
//# sourceMappingURL=index.js.map