UNPKG

@trellixio/roaster-coffee

Version:
69 lines (66 loc) 1.97 kB
import * as React from 'react'; import { Modal } from '../Modal.js'; import { useModalEvents } from '../utils/events.js'; import { modalReducer, getModalProviderFooterProps } from '../utils/reducer.js'; import { ModalContextProvider } from '../utils/useModal.js'; import { ModalProviderFooter } from './ModalProviderFooter.js'; 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( ({ ...props }) => { 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 = () => { const modalState = stateRef.current?.props; if (modalState) { const { modalProps: modalProps2, footerProps } = getModalProviderFooterProps(modalState); return { modalProps: { ...modalProps2 }, children: modalState.children, footer: /* @__PURE__ */ React.createElement(ModalProviderFooter, { ...footerProps, labels: modalState.labels || footerProps.labels }) }; } return { modalProps: {}, children: null }; }; const { modalProps, footer, children: modalChildren } = getModalProps(); return /* @__PURE__ */ React.createElement(ModalContextProvider, { value: ctx }, /* @__PURE__ */ React.createElement( Modal, { footer, ...sharedProps, ...modalProps, opened: modalChildren != null, onClose: () => closeModal() }, modalChildren ), children); } export { ModalProvider }; //# sourceMappingURL=index.js.map