modal.tsx

Overview

modal.tsx defines a versatile and customizable Modal React component built on top of the @radix-ui/react-dialog primitive. It provides a rich set of features such as configurable size, header title, footer with customizable buttons or content, mask closability, loading states, and support for full-width display. It also integrates internationalization support through react-i18next and uses a portal-based modal management system for global control.

This file's core purpose is to provide an accessible, reusable modal dialog UI element that can be easily integrated into a React application with flexible control over its behavior and appearance.


Exports


Interfaces

ModalProps

Defines the properties accepted by the Modal component.

Property

Type

Description

Optional

Default

open

boolean

Controls whether the modal is open or closed.

No

onOpenChange

(open: boolean) => void

Callback fired when the open state changes (e.g., user closes or opens the modal).

Yes

title

ReactNode

Optional title content displayed in the modal header.

Yes

titleClassName

string

Additional CSS classes for the title container.

Yes

children

ReactNode

The main content of the modal.

No

footer

ReactNode

Custom footer content. If not provided, default footer with cancel/ok buttons is rendered.

Yes

footerClassName

string

Additional CSS classes for the footer container.

Yes

showfooter

boolean

Whether to show the footer section. If false, footer is hidden.

Yes

true

className

string

Additional CSS classes for the modal content container.

Yes

'' (empty)

size

`'small'

'default'

'large'`

Modal size, controlling max width.

closable

boolean

Whether the modal can be closed via the close button.

Yes

true

closeIcon

ReactNode

Custom close icon element.

Yes

<X className="w-4 h-4" /> (from lucide-react)

maskClosable

boolean

Whether clicking on the overlay mask closes the modal.

Yes

true

destroyOnClose

boolean

Whether to unmount modal children when closed.

Yes

false

full

boolean

Whether the modal should use full width (max-w-full).

Yes

false

confirmLoading

boolean

Shows a loading spinner on the OK button to indicate a pending action.

Yes

cancelText

`ReactNode

string`

Text or node for the cancel button label.

Yes

okText

`ReactNode

string`

Text or node for the OK button label.

Yes

onOk

() => void

Callback fired when OK button is clicked.

Yes

onCancel

() => void

Callback fired when cancel action is triggered (close, cancel button).

Yes


Component: Modal

A functional React component implementing a modal dialog with the above props.

Usage Example

import { Modal } from '@/components/ui/modal';
import { useState } from 'react';

function Example() {
  const [open, setOpen] = useState(false);

  return (
    <>
      <button onClick={() => setOpen(true)}>Open Modal</button>
      <Modal
        open={open}
        onOpenChange={setOpen}
        title="Example Modal"
        onOk={() => alert('Confirmed')}
        onCancel={() => alert('Cancelled')}
        confirmLoading={false}
      >
        <p>This is some modal content</p>
      </Modal>
    </>
  );
}

Props Explanation and Behavior


Internal Implementation Details


Methods

Modal.show

Modal.hide

These methods delegate to an instance created by createPortalModal() imported from ./modal-manage. This allows external code to open or close the modal without embedding the component in JSX.


Interaction with Other System Parts


Visual Diagram: Component Structure and Workflow

componentDiagram
    component Modal {
        +open: boolean
        +onOpenChange(open: boolean): void
        +title: ReactNode
        +footer: ReactNode
        +showfooter: boolean
        +size: 'small' | 'default' | 'large'
        +closable: boolean
        +closeIcon: ReactNode
        +maskClosable: boolean
        +destroyOnClose: boolean
        +full: boolean
        +confirmLoading: boolean
        +cancelText: ReactNode | string
        +okText: ReactNode | string
        +onOk(): void
        +onCancel(): void
        +show(): void
        +hide(): void
    }
    
    component RadixDialogPrimitive {
        +Root
        +Portal
        +Overlay
        +Content
        +Title
        +Close
    }
    
    component ModalManage {
        +createPortalModal(): { show(), hide() }
    }
    
    Modal --> RadixDialogPrimitive : uses
    Modal --> ModalManage : uses for show/hide static methods
    Modal --> lucide-react : uses icons (X, Loader)
    Modal --> react-i18next : uses for translations

Summary

modal.tsx provides a fully featured modal dialog React component with fine-grained control over appearance and behavior, leveraging Radix UI primitives for accessibility and portals for rendering. It supports internationalization, customizable buttons, dynamic loading states, and programmatic control through static methods. The component is designed to be flexible and composable, suitable for use across a React application’s UI.


If you have questions about integration or extending the modal, or need examples for specific usage scenarios, feel free to ask!