New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-imperative-modal-dialog

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-imperative-modal-dialog

Extremely lightweight imperative API for using react Modal/Dialog/Popover components.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

React-Imperative-Modal / Dialogs

Extremely lightweight imperative API for using react Modal/Dialog/Popover components.

Installation

Install via npm or yarn

  npm install react-imperative-dialog-modal 

Usage/Examples

Example provided with material-ui dialog component use. However can be used with any library or custom dialog similarly.
import { DialogBuilder } from "react-imperative-modal-dialog";
import { Dialog } from "@mui/material";

interface MyConfirmDialogComponentProps {
  open: boolean;
  onClose: () => void;
  onConfirm: () => void;
}

// Your component here
// Component should accept open and onClose props at the very least.
const MyConfirmDialogComponent = (props: MyConfirmDialogComponentProps) => {
  const { open, onClose, onConfirm } = props;
  return (
    <Dialog sx={{ p: 5 }} open={open} onClose={onClose}>
      Are you sure you want to do this action?
      <button onClick={onConfirm}>Confirm</button>
    </Dialog>
  );
};

// Pass in your dialog component to the builder
// Note that props open and onClose are mandatory props that must be present for the dialog component that is passed in
// 'open' and 'onClose' are required because dialogbuilder intercepts these props and handles opening and closing actions
const ConfirmDialog = new DialogBuilder<MyConfirmDialogComponentProps>()
  .setComponent(MyConfirmDialogComponent)
  .build();

function App() {
  const handleDialogOpen = () => {
    ConfirmDialog
      // pass in component props here
      // no need to pass 'open' since it is intercepted and removed
      .withProps((dialog) => ({
        onConfirm: async () => {
          // await deleteItem();
          dialog.close();
        },
        onClose: () => console.log("closed"),
      }))
      .open();
  };

  return (
    <div style={{ display: "flex" }}>
      <div>Item 1</div>
      <button onClick={handleDialogOpen}>Delete</button>
    </div>
  );
}

export default App;

API Reference

Instance creation
    const dialog = new DialogBuilder<YourDialogComponentProps>()
        .setComponent(YourDialogComponent)
        .build();
Opening dialog
    dialog.open();
Passing dialog component props
    dialog.withProps((dialog) => {
        yourDialogComponentProp1: "prop1",
        yourDialogComponentProp2: "prop2",
        yourDialogComponentProp3: async () => {
            await doSomeAction();
            dialog.close();
        },
    });
Closing dialog
    dialog.close();
Unmount dialog
    dialog.unmount();

License

MIT

Keywords

FAQs

Package last updated on 24 Aug 2022

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