@trellixio/roaster-coffee
Version:
Beans' product component library
62 lines (61 loc) • 2.48 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;
};
import * as React from 'react';
import { Modal } from '../Modal';
import { getModalProviderFooterProps, modalReducer, ModalContextProvider, useModalEvents } from '../utils';
import { ModalProviderFooter } from './ModalProviderFooter';
export function ModalProvider({ children, modalProps: sharedProps }) {
const [state, dispatch] = React.useReducer(modalReducer, { props: sharedProps });
const stateRef = React.useRef(state);
stateRef.current = state;
const openModal = React.useCallback((_a) => {
var props = __rest(_a, []);
dispatch({
type: 'OPEN',
modal: {
props,
},
});
}, [dispatch]);
const closeModal = React.useCallback(() => {
dispatch({ type: 'CLOSE' });
}, [stateRef, dispatch]);
const ctx = React.useMemo(() => ({
modal: state,
openModal,
closeModal,
}), []);
useModalEvents({
openModal,
closeModal,
});
const getModalProps = () => {
var _a;
const modalState = (_a = stateRef.current) === null || _a === void 0 ? void 0 : _a.props;
if (modalState) {
const { modalProps, footerProps } = getModalProviderFooterProps(modalState);
return {
modalProps: Object.assign({}, modalProps),
children: modalState.children,
footer: React.createElement(ModalProviderFooter, Object.assign({}, footerProps, { labels: modalState.labels || footerProps.labels })),
};
}
return {
modalProps: {},
children: null,
};
};
const { modalProps, footer, children: modalChildren } = getModalProps();
return (React.createElement(ModalContextProvider, { value: ctx },
React.createElement(Modal, Object.assign({ footer: footer }, sharedProps, modalProps, { opened: modalChildren != null, onClose: () => closeModal() }), modalChildren),
children));
}