modal-manager.tsx

Overview

modal-manager.tsx is a lightweight React functional component designed to manage the visibility state of a modal dialog or any UI element that requires toggling between visible and hidden states. It follows the render prop pattern, providing child components with control methods (showModal and hideModal) and the current visibility state (visible). This approach allows for flexible integration of modal logic without enforcing any UI or styling decisions.


Detailed Documentation

Interfaces

IModalManagerChildrenProps

Defines the shape of the props passed to the child function of ModalManager.

Property

Type

Description

showModal

() => void

Function to set the modal visibility to true.

hideModal

() => void

Function to set the modal visibility to false.

visible

boolean

Current visibility state of the modal.


IProps

Defines the props accepted by the ModalManager component.

Property

Type

Description

children

(props: IModalManagerChildrenProps) => React.ReactNode

A render prop function that receives visibility state and control functions and returns React nodes.


ModalManager Component

const ModalManager = ({ children }: IProps) => {
  const [visible, setVisible] = useState(false);

  const showModal = () => {
    setVisible(true);
  };
  const hideModal = () => {
    setVisible(false);
  };

  return children({ visible, showModal, hideModal });
};

Description

ModalManager internally manages a boolean state visible which indicates whether a modal is shown or hidden. It exposes two handler functions to toggle this state:

The component does not render any UI by itself. Instead, it uses the render prop pattern by invoking its children prop with an object containing the current visibility state and the two control functions. This design allows parent or child components to decide how the modal and its controls should be rendered and behave.

Parameters

Returns

Usage Example

import ModalManager from './modal-manager';

const App = () => (
  <ModalManager>
    {({ visible, showModal, hideModal }) => (
      <>
        <button onClick={showModal}>Open Modal</button>
        {visible && (
          <div className="modal">
            <p>This is a modal window.</p>
            <button onClick={hideModal}>Close Modal</button>
          </div>
        )}
      </>
    )}
  </ModalManager>
);

Implementation Details


Interaction with Other Parts of the Application


Visual Diagram

classDiagram
    class ModalManager {
        - visible: boolean
        - setVisible: React.Dispatch<React.SetStateAction<boolean>>
        + showModal(): void
        + hideModal(): void
        + children(props: IModalManagerChildrenProps): React.ReactNode
    }
    class IModalManagerChildrenProps {
        + showModal(): void
        + hideModal(): void
        + visible: boolean
    }
    ModalManager --> IModalManagerChildrenProps : passes props

Summary

modal-manager.tsx is a simple, reusable React component that encapsulates modal visibility state management and exposes control handlers through a render prop interface. It provides a clean separation of state logic from UI rendering, enabling flexible modal implementations across the application.