Coder Social home page Coder Social logo

paul-taiwo / react-prompt-modal Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 783 KB

A lightweight helper library for creating and managing modals in React applications.

License: MIT License

TypeScript 89.73% JavaScript 10.27%
helper modal react react-dialog react-modal react-components prompt-modal modal-prompt modal-utility

react-prompt-modal's Introduction

React.js Prompt Modal

GitHub Workflow Status (with branch) Codecov branch NPM

Features

  • Display prompt modals with custom content and options.
  • Support for loading state and callback functions.
  • Customizable styling and animations.
  • Easy integration with React applications.
  • Lightweight and dependency-free.

Installation

Install the package using npm:

npm install react-prompt-modal

or using yarn:

yarn add react-prompt-modal

Usage

Import the createModal function from the package and use it to create modal instances:

import React from "react";
import { createModal } from "react-prompt-modal";
import PropTypes from "prop-types";

// Define your custom component to be used as the modal content
const CustomModalContent = ({ show, isLoading, abort, close, proceed, ...others }) => {
  // Your component code here
};

CustomModalContent.propTypes = {
  show: PropTypes.bool, // Indicates if the dialog is shown or not (from react-prompt-modal)
  isLoading: PropTypes.bool, // Indicates whether the modal is in a loading state (from react-prompt-modal)
  abort: PropTypes.func, // Function to abort and close the modal, triggering any before-close effects if specified (from react-prompt-modal)
  close: PropTypes.func, // Function to directly close the modal without any additional effects (from react-prompt-modal)
  proceed: PropTypes.func, // Function to handle the proceed action in the modal (from react-prompt-modal)
};

// Create a modal instance
const { open, close } = createModal({
  component: CustomModalContent,
});

// Trigger the modal
open();

You can customize the behavior of the modal by providing additional options:

const { open, close, abort, setIsLoading } = createModal({
  component: CustomModalContent,
  proceed: () => {
    // Perform an action when the user clicks "Proceed"
    console.log("Proceed button clicked");
  },
  beforeClose: () => {
    // Perform any necessary cleanup or validation before closing the modal
    console.log("Before closing the modal");
  },
  // Additional options can be provided here
});

API

The createModal function accepts the following options:

  • component: The component to be rendered inside the modal. It should be a valid React component.
  • proceed: A callback function to be executed when the user clicks the "Proceed" button.
  • beforeClose (optional): A callback function to be executed before closing the modal.
  • Additional options: Any other custom options can be passed and accessed within the modal component.

The createModal function returns an object with the following methods:

  • open(): Opens the modal and displays it to the user.
  • abort(): Cancels the modal action and closes the modal. If a beforeClose callback is provided, it will be executed before closing the modal.
  • close(): Closes the modal without any additional effects.
  • setIsLoading(loading: boolean): Sets the loading state of the modal. Pass true to show a loading indicator, and false to hide it.

Examples

Basic Usage

import React from "react";
import { createModal } from "react-prompt-modal";

const CustomModalContent = ({ close }) => {
  return (
    <div>
      <h2>Welcome to the Modal</h2>
      <button onClick={close}>Close</button>
    </div>
  );
};

const { open } = createModal({
  component: CustomModalContent,
});

const App = () => {
  return (
    <div>
      <h1>React App</h1>
      <button onClick={open}>Open Modal</button>
    </div>
  );
};

export default App;

Customization and Callbacks

import React from "react";
import { createModal } from "react-prompt-modal";

const CustomModalContent = ({ abort, proceed }) => {
  return (
    <div>
      <h2>Are you sure you want to proceed?</h2>
      <button onClick={proceed}>Proceed</button>
      <button onClick={abort}>Cancel</button>
    </div>
  );
};

const { open } = createModal({
  component: CustomModalContent,
  proceed: () => {
    console.log("Proceed button clicked");
  },
  beforeClose: () => {
    console.log("Before closing the modal");
  },
});

const App = () => {
  return (
    <div>
      <h1>React App</h1>
      <button onClick={open}>Open Modal</button>
    </div>
  );
};

export default App;

You can find examples of how to use the react-prompt-modal package in the CodeSandbox demo:

React Prompt Modal CodeSandbox Bootstrap Examples

React Prompt Modal CodeSandbox Chakra UI Examples

React Prompt Modal CodeSandbox Tailwind CSS Examples

Benefits

  • Simplicity: React Prompt Modal is designed to be easy to use and integrate into your React applications. It provides a straightforward API for creating and customizing modal dialogs.
  • Flexibility: The package allows you to create modals with custom content and options tailored to your application's specific needs. You have full control over the modal's behavior and appearance.
  • Lightweight: React Prompt Modal is a lightweight package with no external dependencies. It won't bloat your project and ensures fast and efficient performance.
  • Customizable Styling: You can easily customize the styling of the modal component to match your application's design. Apply your own CSS classes or use a CSS-in-JS solution like styled-components.
  • Loading State: React Prompt Modal supports a loading state, allowing you to indicate progress or data fetching to the user while the modal is open.

License

This package is licensed under the MIT License.

⭐️ Please Star this Project

If you find this project helpful or valuable, please consider giving it a star on GitHub. It's a great way to show your support and it helps the project gain visibility.

Thank you! 🌟

GitHub Paul Taiwo

react-prompt-modal's People

Contributors

jayprecode avatar paul-taiwo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jayprecode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.