modal-manage.tsx


Overview

The modal-manage.tsx file provides a React-based mechanism for rendering modal dialogs dynamically via React portals. It includes two main parts:

  1. PortalModal React component – a controlled modal component that renders its content into a specified DOM container using React portals, handling visibility and lifecycle.

  2. createPortalModal factory function – programmatically creates and manages modals outside the standard React component tree. It supports showing, hiding, updating, and destroying modals imperatively.

This file is designed to facilitate flexible, dynamic modal dialogs that can be created and managed at runtime, enabling modals to be rendered outside the main React app hierarchy while maintaining React state and lifecycle management.


Detailed Explanation

1. PortalModal Component

A React functional component that renders a modal dialog into a DOM node via React portals.

Signature

const PortalModal: React.FC<PortalModalProps>

Props (PortalModalProps)

Extends ModalProps with some adjustments:

Behavior

Usage Example

<PortalModal
  visible={isModalVisible}
  onVisibleChange={(visible) => setIsModalVisible(visible)}
  container={document.getElementById('modal-root')}
>
  <div>Modal Content Here</div>
</PortalModal>

2. createPortalModal Factory Function

Imperative API to dynamically create and control a modal instance outside React's normal render tree.

Signature

function createPortalModal(): {
  show: (props: PortalModalProps) => void;
  hide: () => void;
  update: (props?: Partial<PortalModalProps>) => void;
  destroy: () => void;
}

Internal Implementation Details

Modal Visibility Handling

Usage Example

const modal = createPortalModal();

modal.show({
  children: <div>Dynamic modal content</div>,
  onVisibleChange: (visible) => {
    console.log('Modal visibility changed:', visible);
  }
});

// Later update modal content or props
modal.update({ children: <div>Updated content</div> });

// Hide modal
modal.hide();

// Destroy modal instance and cleanup DOM
modal.destroy();

Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

componentDiagram
    component PortalModal {
      +visible: boolean
      +onVisibleChange(visible: boolean)
      +container?: HTMLElement
      +children: ReactNode
    }
    component createPortalModal {
      +show(props: PortalModalProps)
      +hide()
      +update(props?: Partial<PortalModalProps>)
      +destroy()
    }
    PortalModal <.. createPortalModal : uses
    createPortalModal --> Modal : renders via PortalModal

Summary

The modal-manage.tsx file provides a robust, imperative API for creating and managing React modals dynamically via portals. It abstracts modal lifecycle and rendering into a reusable hook-like factory function, enabling modals to be controlled outside of React component trees while preserving React's declarative rendering model underneath. This design pattern is well-suited for global modal managers, notification systems, or any scenario requiring runtime modal injection with full React support.