import React from 'react' import hoistStatics from 'hoist-non-react-statics' import type { ModalfyParams, ModalProp } from '../types' import ModalContext from './ModalContext' import { invariant } from '../utils' import { modalfy } from './ModalState' /** * HOC that provides the `modal` prop to a wrapped Class component. * * Note: Prefer `useModal()` Hook if you're using a Hook component. * * @param { React.ComponentClass } Component - Component class. * * @returns Provided component class enhanced with the `modal` prop. * * @see https://colorfy-software.gitbook.io/react-native-modalfy/api/withmodal */ const withModal =

(Component: React.ComponentClass) => { const displayName = Component.displayName || Component.name const { closeModal, closeModals, closeAllModals } = modalfy

() const withModalComponent = class WithModalComponent extends React.Component>> { static readonly WrappedComponent = Component static displayName = `withModal(${displayName})` render() { return ( {context => { invariant(context, `You should not use ${displayName} outside a `) return ( ) }} ) } } return hoistStatics(withModalComponent, Component) } export default withModal