@magma73/modal-react-typescript
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

React version Typescript version CSS version Vite version NPM version Node version Visual Studio Code

@magma73/modal-react-typescript

Table of Contents

Description

The Modal React component provides a flexible and customizable modal window for use in React applications. With support for various configurations and styling options, this component allows developers to easily implement modal dialogs for displaying additional content, notifications, or interactive elements within their applications.

Technologies

  • React V18.2.0
  • React-Dom V18.2.0
  • TypeScript V5.2.2
  • ESLint V8.57.0
  • Vite V5.2.0
  • NodeJS V20.10.0
  • VS Code V1.87.2

Install

To install, you can use npm or yarn:

$ npm install --save @magma73/modal-react-typescript

$ yarn add @magma73/modal-react-typescript

Usage

Example

import React, { useState } from 'react';
import { createPortal } from "react-dom";
import { Modal } from '@magma73/modal-react-typescript';
import '@magma73/modal-react-typescript/dist/style.css';

const App = () => {

  const [showModal, setShowModal] = useState(false);

  const openModal = () => {
    setShowModal(true);
  };

  const closeModal = () => {
    setShowModal(false);
  };

    return (

    <>

      <button onClick={openModal}>Ouvrir la modal</button>
      {showModal &&
        createPortal(
          <Modal
            onClose={closeModal}
            title="Le titre de la modale"
            titleClose="Fermer la modale"
          />,
          document.body
        )}

    </>

    )

}


export default App

Step by step

  1. Import component and style file
import { Modal } from '@magma73/modal-react-typescript';
import '@magma73/modal-react-typescript/dist/style.css';
  1. Create open/close state and save the currently active element with useState hook
import React, { useState } from 'react';
import { createPortal } from "react-dom";

const App = () => {

    const [showModal, setShowModal] = useState(false);

  ...

}
  1. Create functions open/close
  const openModal = () => {
    setShowModal(true);
  };

  const closeModal = () => {
    setShowModal(false);
  };
  1. Add component in render with props
    {showModal &&
        createPortal(
            <Modal
            onClose={closeModal}
            title="Le titre de la modale"
            titleClose="Fermer la modale"
            />,
            document.body
    )}
  1. Add children to the component
    {showModal &&
        createPortal(
            <Modal
                onClose={closeModal}
                title="Le titre de la modale"
                titleClose="Fermer la modale"
            >
                <p>Hello world !</p>
            </Modal>,
            document.body
    )}
  1. Control modal by changing state
    <button onClick={openModal}>Open modal</button>
  1. Choose to customize styles and modal

You can choose to :

  • Display the icon with : showCloseIcon={false}
  • Delete the style importation import '@magma73/modal-react-typescript/dist/style.css'; and add your own style using : {styles.yourStyle}
    {showModal &&
        createPortal(
            <Modal
                onClose={closeModal}
                customModal={styles.modalContent}
                customContainerInformations={styles.containerInformations}
                customTitle={styles.title}
                customBtnClose={styles.btnClose}
                customIconClose={styles.picture}
                title="Your Message Has Been Sent"
                titleClose="Close the message"
                showCloseIcon={false}
            >
                <p>Hello world !</p>
            </Modal>,
            document.body
    )}

Authors

Marine Magnin

License

ISC © Marine Magnin

Package Sidebar

Install

npm i @magma73/modal-react-typescript

Weekly Downloads

3

Version

1.0.6

License

none

Unpacked Size

50 kB

Total Files

8

Last publish

Collaborators

  • magma73