Modal

Installation

To install the Modal component, use the following command:

sh
npx gbs-add-block@latest -a Modal

Overview

The Modal component provides a flexible and customizable modal dialog box for displaying content over a semi-transparent backdrop. It supports automatic closing, customizable styling, and seamless integration into any React application.

Props Table

PropTypeDefaultDescription
showModalbooleanfalseControls the visibility of the modal.
setShowModal(show: boolean) => voidRequiredFunction to toggle the modal state.
modalTitlestring"Modal Title"Title of the modal.
modalClassstringDefault stylesAdditional classes for modal styling.
modalContentClassstringDefault stylesClasses for modal content customization.
classModalContentstring""Additional class for modal content.
modalTitleClassstringDefault stylesClasses for modal title.
classModalTitlestring""Additional class for modal title.
childrenReact.ReactNodeRequiredContent to be displayed inside the modal.
showCloseButtonbooleanfalseDetermines if a close button should be displayed.
dismissiblebooleanfalseAllows modal to be dismissed by clicking outside or pressing Esc.
titleIdstring"modal-title"ID for the modal title, useful for accessibility.
closeButtonContentReact.ReactNodeundefinedCustom close button content.
animationDurationnumber200Duration for fade-in and fade-out animations.
showTitlebooleanfalseShows title in the Modal

Usage Guide

To use the Modal component, import it into your React application and manage its visibility with a state variable.

tsx
import React, { useState } from "react";
import { Modal } from "./path/to/Modal";

const ExampleComponent = () => {
  const [isModalVisible, setModalVisible] = useState(false);

  const toggleModal = () => {
    setModalVisible(!isModalVisible);
  };

  return (
    <div>
      <button onClick={toggleModal} className="btn">
        Open Modal
      </button>

      <Modal
        showModal={isModalVisible}
        setShowModal={setModalVisible}
        modalTitle="Example Modal"
        modalContentClass="bg-white rounded-lg shadow-lg"
        showCloseButton={true}
        dismissible={true}
      >
        <p>This is the content of the modal!</p>
        <button onClick={toggleModal} className="btn">
          Close
        </button>
      </Modal>
    </div>
  );
};

export default ExampleComponent;

Key Functionalities

1. Visibility Control

  • The modal's visibility is managed via the showModal prop and the setShowModal function.

2. Automatic Closing

  • The dismissible prop allows users to close the modal by clicking outside it or pressing Esc.

3. Customizable Styling

  • You can customize the modal's appearance using Tailwind CSS classes, particularly via modalClass, modalContentClass, and modalTitleClass.

Notes

  • Ensure to provide children for the content displayed inside the modal.
  • Customize modal styles by overriding the default classes.
  • When using the dismissible feature, ensure it does not close unexpectedly, maintaining a good user experience.