Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-use-modals

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-use-modals

Adds HTML Dialog capabilities as custom hooks

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source
traffic

react-use-modals

Adds HTML Dialog capabilities as custom hooks

NPM


StatementsBranchesFunctionsLines
StatementsBranchesFunctionsLines

Table of Contents


Install

npm install --save react-use-modals

Usage

  • You can use it as follows by destructuring its return object:
import React from 'react';
import useModals from 'react-use-modals';

const MyComponent = () => {
  const { modalRef, isOpen, openModal, closeModal } = useModals();

  return (
    <>
      <button onClick={openModal}>Open Modal</button>
      <dialog ref={modalRef}>
        <p>Modal Content</p>
        <button onClick={closeModal}>Close Modal</button>
      </dialog>
      <p>Modal is {isOpen ? 'open' : 'closed'}</p>
    </>
  );
};
  • You can also use it with a return array just like React does:
  • This is particularly useful when you must use multiple useModals.
import React from 'react';
import useModals from 'react-use-modals';

const MyComponent = () => {
  const [modalRef, isOpen, openModal, closeModal] = useModals({
    preventCloseOnEscape: true,
  });
  const [modalRef2, isOpen2, openModal2, closeModal2] = useModals({
    closeOnBackdropClick: true,
    onCloseCallback: (modalId) =>
      console.log(`modal with id: ${modalId} closed!`),
  });

  return (
    <>
      <button onClick={openModal}>Open Modal</button>
      <dialog ref={modalRef}>
        <p>Modal Content</p>
        <button onClick={closeModal}>Close Modal</button>
      </dialog>
      <p>Modal is {isOpen ? 'open' : 'closed'}</p>
      <br />
      <br />
      <br />
      <button onClick={openModal2}>Open Modal2</button>
      <dialog ref={modalRef2} id="modal-id-2">
        <p>Modal2 Content</p>
        <button onClick={closeModal2}>Close Modal2</button>
      </dialog>
      <p>Modal2 is {isOpen2 ? 'open' : 'closed'}</p>
    </>
  );
};

Documentation

useModals() accepts the following options:

keydescriptionargumentsexample
closeOnBackdropClickBoolean controlling closing on backdrop clickN/AN/A
preventCloseOnEscapeBoolean controlling closing on escape key clickN/AN/A
onCloseCallbackFunction callback run when closing. Receives modal id if availablemodalId?: string(modalId) => console.log(`modal with id: ${modalId} closed!`)

And useModals() returns:

  • An object/tupple with the following keys:
keydescriptionargumentsexample
isOpenBoolean stating state of open or closedN/AN/A
modalRefHTMLDialogElement refN/AN/A
openModalFunction to open modalN/AN/A
closeModalFunction to open modalN/AN/A
  • PS.: If you need to change backdrop's CSS, please do use its pseudo-element as per documentation, like so:
dialog::backdrop {
  background: rgba(255, 0, 0, 0.25);
}

Contributors

Thanks goes to these wonderful people (emoji key):

Olavo Parno
Olavo Parno

🤔 💻 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!


License

react-use-modals is MIT licensed.


This hook is created using create-react-hook.

FAQs

Package last updated on 09 May 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc