react-inline-modal
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

React Inline Modal

Use React modals directly in your handlers to align with the user's experience.

const handleClick = async () => {
  // Collect user input asynchronously
  // (ConfirmationModal is a React component)
  const confirmation = await modal.show(ConfirmationModal, {
    // Pass props to the modal
    message: "Are you sure?",
  });

  // Handle logical branches
  if (!confirmation) return;

  // Continue with the happy path
  alert("Proceeding...");
};

Quick Start

Add the InlineModalProvider to your app:

import { InlineModalProvider } from "react-inline-modal";

const App = () => {
  return (
    <InlineModalProvider>
      <MyContent />
    </InlineModalProvider>
  );
};

Prepare your modal:

// Bring your own stylized modal
import { Modal } from "any-component-library";

const ConfirmationModal = ({ resolve, message }) => {
  return (
    <Modal open onClose={() = resolve(false)}>
      <h1>{message}</h1>
      <button onClick={() => resolve(true)}>Yes</button>
      <button onClick={() => resolve(false)}>No</button>
    </Modal>
  );
};

Use the modal in a handler:

const DeleteButton = () => {
  const modal = useInlineModal();

  const handleClick = async () => {
    const confirmation = await modal.show(ConfirmationModal, {
      message: "Are you sure?",
    });

    if (!confirmation) return;

    alert("Proceeding...");
  };

  return <button onClick={handleClick}>Delete</button>;
};

Package Sidebar

Install

npm i react-inline-modal

Weekly Downloads

11

Version

1.1.5

License

MIT

Unpacked Size

4.67 kB

Total Files

4

Last publish

Collaborators

  • myarask