Socket
Socket
Sign inDemoInstall

modal-top

Package Overview
Dependencies
8
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    modal-top

React component to easily create customizable modals


Version published
Weekly downloads
1
Maintainers
1
Created
Weekly downloads
 

Readme

Source

forthebadge forthebadge forthebadge

Get started

Examples

Prerequisites for install

  • You need Git to clone the repository
  • You need Node (v16.10.0) to run the npm commands

Installation

  • using NPM
    $ npm install react-top-modal
  • using yarn
    $ yarn install react-top-modal

Link to access the package here

Basic Usage

Import the Modal component and useModal Hook in your React components:

import { Modal, useModal } from "react-top-modal";

After you've imported the Modal component and useModal Hook, you're ready to start using Modal inside your components! 🎉 (isOpen & close are required)

import React from "react";
import Modal, { useModal } from "react-top-modal";

const App = () => {
  const { showModal, activeModal, handleOpenModal, handleCloseModal } = useModal();

  return (
    <>
      <button type="button" className="buttonDefault" onClick={() => handleOpenModal("classic")}>
        Modal (classic Modal)
      </button>
      <Modal isOpen={showModal && activeModal === "classic"} close={handleCloseModal}>
        <h2>classic</h2>
      </Modal>
    </>
  );
};

export default App;

Features

  • Add content in modal using createPortal 🚪
  • Close the modal with Esc key ⌨️
  • Close the modal on click overlay 🖱
  • Display a closing icon in the header ✖️
  • Customize the className of each element ⏺
  • Display a close button in the footer 🅱️
  • Add a spinner before the modal displays ♾

Props

PropsTypeRequiredDefaultDescription
isOpenObjectRequired-An object containing two props, 'showModal' & 'activeModal', this object passed to modal allows to display it
showModalBoolRequiredFalseState allows you to display the modal
activeModalStringRequired-Add a string to identify which modal should be opened
CloseFunctionRequired-Function allows you to remove the modal
addCloseEscapeBoolOptionalFalseAllows to add the functionality of modal closure using the 'Esc' key
addCloseOverlayBoolOptionalFalseAllows to add the functionality of modal closing by clicking on the overlay
addCloseIconBoolOptionalTrueIf true, a closing icon will be displayed in the header of the modal
customClassNameStringOptional-Allows you to customize the class name of each element
addFooterButtonBoolOptionalFalseIf true, a close button will be displayed in the footer of the modal
spinnerBoolOptionalFalseAllows to add a loader which will be displayed during the loading of the modal

Modal with close Esc

You have the possibility to add the functionality of modal closure using the 'Esc' key by adding the props 'addCloseEsc' with the value 'true'.

import React from "react";
import Modal, { useModal } from "react-top-modal";

const App = () => {
  const { showModal, activeModal, handleOpenModal, handleCloseModal } = useModal();

  return (
    <>
      <button type="button" className="buttonDefault" onClick={() => handleOpenModal("closeEscape")}>
        Modal (with closeEscape)
      </button>
      <Modal isOpen={showModal && activeModal === "closeEscape"} close={handleCloseModal} addCloseEscape={true}>
        <h2>closeEscape</h2>
      </Modal>
    </>
  );
};

export default App;

Modal with Close Overlay

You have the option to add the functionality of modal closing using the overlay click by adding the props 'addCloseOverlay' with the value 'true'.

import React from "react";
import Modal, { useModal } from "react-top-modal";

const App = () => {
  const { showModal, activeModal, handleOpenModal, handleCloseModal } = useModal();

  return (
    <>
      <button type="button" className="buttonDefault" onClick={() => handleOpenModal("closeOverlay")}>
        Modal (with closeOverlay)
      </button>
      <Modal isOpen={showModal && activeModal === "closeOverlay"} close={handleCloseModal} addCloseOverlay={true}>
        <h2>closeOverlay</h2>
      </Modal>
    </>
  );
};

export default App;

Modal with footerButton

You have the possibility to add a close button in the footer by adding the props 'addFooterButton' with the value 'true'.

import React from "react";
import Modal, { useModal } from "react-top-modal";

const App = () => {
  const { showModal, activeModal, handleOpenModal, handleCloseModal } = useModal();

  return (
    <>
      <button type="button" className="buttonDefault" onClick={() => handleOpenModal("footerButton")}>
        Modal (with footerButton)
      </button>
      <Modal isOpen={showModal && activeModal === "footerButton"} close={handleCloseModal} addFooterButton={true} addCloseIcon={false}>
        <h2>footerButton</h2>
      </Modal>
    </>
  );
};

export default App;

Modal with Spinner

You have the option to add a spinner when you expect an asynchronous function. Launch the spinner, the toggle will automatically close it and make the modal appear once your data has been recovered.

import React from "react";
import Modal, { useModal } from "react-top-modal";

const App = () => {
  const { showModal, activeModal, handleOpenModal, handleCloseModal, isLoading, toggleSpinner } = useModal();

  const spinnerTimer = () => {
    toggleSpinner();

    setTimeout(() => {
      handleOpenModal("spinner");
    }, 1000);
  };
  return (
    <>
      <button type="button" className="buttonDefault" onClick={spinnerTimer}>
        Modal (with spinner 1s)
      </button>
      <Modal isOpen={showModal && activeModal === "spinner"} close={handleCloseModal} spinner={isLoading}>
        <h2>spinner</h2>
      </Modal>
    </>
  );
};

export default App;

Dependencies

Auteur

Keywords

FAQs

Last updated on 30 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc