Promise-based utility to control modal states in React

Overview

Demodal Banner


Promise-based utility to control modal states in React
Zero-dependency library that easily integrates with your existing UI components and allows you to naturally use modals in React

GitHub Actions Coverage NPM bundle size Twitter Badge


Quick Features

  • Promise based: open your modal with a simple function call and await for the result.
  • Uncontrolled: open/close your modal from anywhere in the code (even inside the modal itself).
  • Decoupled: no need to import a modal component to use it. Modals can be managed by ID.
  • Tiny: zero-dependency to keep your bundle size under control: ~1.5kB.
  • Easy integration: easily integrate with any UI library.
  • Lazy-loading: delay the rendering of your modal component until it's open.

Examples

Try it on CodeSandbox or browse the examples folder.

Demodal Examples

Basic Usage

import Demodal from 'demodal'
import MyModal from './MyModal'

// ...
const result = await Demodal.open(MyModal, { myModalProp: 'value' })
// Do something with result

Use-Case: Confirmation Modal

/**
 * confirm.js
 */
import { Demodal, useModal } from 'demodal'

// Register your Confirm modal wrapping it with `Demodal.create`
const Confirm = Demodal.create(
  ({ title = 'Confirmation', message = 'Do you confirm this action?' }) => {
    // useModal hook to control UI components
    const modal = useModal()

    // Once resolved, automatically close the modal
    const resolve = value => () => {
      modal.resolve(value)
      modal.close()
    }

    // "title" and "message" are props sent with "modal.open()"
    return (
      <Modal open={modal.isOpen} onClose={modal.close} onExited={modal.remove}>
        <div>{title}</div>
        <div>{body}</div>
        <Button onClick={resolve(true)}>Yes</Button>
        <Button onClick={resolve(false)}>No</Button>
      </Modal>
    )
  }
)

// Create a custom confirm function
export const confirm = props => Demodal.open(Confirm, props)

/**
 * page.js
 */
import { confirm } from './confirm'

export const Page = () => {
  const handleClick = async () => {
    const confirmed = await confirm({
      title: 'Are you sure?',
      message: 'This action is irreversible',
    })
    console.log(confirmed)
  }

  return <Button onClick={handleClick}>Action</Button>
}

/**
 * app.js
 */
import { Demodal } from 'demodal'

function App() {
  // Remember to add a Demodal.Container to your app's root
  return (
    <>
      <Page />
      <Demodal.Container />
    </>
  )
}

Contributors

Thanks goes to these wonderful people (emoji key):


Thiago Zanivan

💻

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

You might also like...

Design-focused web3 modal based on Material UI

Design-focused web3 modal based on Material UI

@buildship/web3-login (beta) This is a design-focused web3 wallet connecting modal for React based on Material UI. It supports Metamask, WalletConnect

Jan 1, 2023

Dialogs based on Bootstrap 5 modal

Dialogs based on Bootstrap 5 modal

Simple dialogs based on Bootstrap 5 modal Lightweight pure js dialogs library, min ad gz size less 2 kB Dependencies bootstrap 5 demo Dialogs ok_cance

Oct 29, 2022

Promise based HTTP client for the browser and node.js

axios Promise based HTTP client for the browser and node.js New axios docs website: click here Table of Contents Features Browser Support Installing E

Jan 5, 2023

A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Oct 3, 2020

Accessible, lightweight, stylish modal library in pure JavaScript

accessible-minimodal Accessible, lightweight ( 8 kB), stylish modal library in pure JavaScript Example https://codepen.io/imhvost/pen/LYNazqo (with "

Aug 4, 2022

A Simple yet extendable jQuery modal script built for use with inline HTML, images, videos, and galleries.

jQuery Chaos Modal A Simple yet extendable jQuery modal script built for use with inline HTML, forms, and images in mind. There are many other modal p

Oct 8, 2020

ToastmeJS is a very simple, flexible and light weigth plugin that shows Notifications and modal Dialogs on your website.

⚡ ToastmeJS ToastmeJS is a very simple, flexible and light weigth plugin that shows Notifications and modal Dialogs on your website. Customize positio

Jun 20, 2022

Modal Video Library

Modal Video Modal Video Library Features Not affected by dom structure. Beautiful transition Accessible for keyboard navigation and screen readers. Ri

Nov 30, 2022

Simple to use and manage modal.

vue-modal-provider nice-modal-react vue implementation. Simple to use and manage modal. benefit: Create modal without losing context No need to mainta

Nov 10, 2022
Comments
  • error Received malformed response from registry for

    error Received malformed response from registry for "react-unmodal". The registry may be down.

    Describe the bug

    When running yarn start i have this issue: error Received malformed response from registry for "react-unmodal". The registry may be down.

    Your minimal, reproducible example

    typescript

    Steps to reproduce

    1. run npm install

    Expected behavior

    The library should be installed

    How often does this bug happen?

    Every time

    Screenshots or Videos

    No response

    Platform

    -Windows

    • Command line

    react-unmodal version

    latest

    TypeScript version

    4.1.2

    Additional context

    No response

    opened by mbenhenda 2
Releases(v1.1.1)
Owner
Thiago Zanivan
CTO at @way-live. On a mission to change people's lives through quality software 🚀
Thiago Zanivan
Lightweight vanilla js modal component (just 2kb) , pure javascript Modal

Lightweight vanilla js modal component (just 2kb) pure javascript Modal , This is just 2kb Lightweight vanilla js modal component with zero dependenci

Salah Eddine Lalami 12 Dec 12, 2022
Inside-out promise; lets you call resolve and reject from outside the Promise constructor function.

Inside-out promise; lets you call resolve and reject from outside the Promise constructor function.

Lily Scott 3 Feb 28, 2022
Adds promise support (rejects(), doesNotReject()) to tape by decorating it using tape-promise.

Tape With Promises Adds promise support (rejects(), doesNotReject()) to tape by decorating it using tape-promise. Install npm install --save-dev @smal

Small Technology Foundation 3 Mar 21, 2022
Web based application that uses playerctl in it backend to control remotely your audio using the frontend as remote control.

Linux Remote This is a web based application that uses playerctl in it backend to control remotely your audio using the frontend as remote control. Do

Gabriel Guerra 4 Jul 6, 2022
utility library for promise, support both commonjs and ESM

promising-utils A utility library for promise, supports both commonjs and ESM npm install promising-utils --save yarn add promising-utils wait Used wh

Qiang Li 4 Oct 18, 2022
Node.js module for verifying Plumo proofs and reading states based on it

plumo-verifier Node.js module for verifying Plumo proofs and reading states based on it. Plumo is a SNARK-based light client verifier for the Celo blo

Celo 3 Dec 15, 2022
A three.js and roslibjs powered web-control for zju fast-drone-250 for laptop-free flight control

Web Control for ZJU Fast-Drone-250 A three.js and roslibjs powered web-control for zju fast-drone-250 for laptop-free flight control (tested on Xiaomi

null 6 Nov 11, 2022
Awesome TV is the First and Original streaming entertainment network for Global Africa from United States of America (USA).

LEADBOARD APP Awesome TV is the First and Original streaming entertainment network for Global Africa from United States of America (USA). Built With H

Aime Malaika 9 Apr 4, 2022
Search, fetch, and get data regarding United States presidents.

us-presidents Search, fetch, and get data regarding United States presidents. GitHub Documention Discord Examples Installation NPM npm install us-pres

Spen 3 May 7, 2022
Mailbox is the predictable states & transitions container for actors.

Mailbox (turns XState Machine into a REAL Actor) Mailbox is an NPM module built on top of the XState machine, by adding a message queue to the XState

Huan (李卓桓) 40 Aug 24, 2022