# promise-dialog

> Promise Based Dialog

Latest version **2.5.0** (published 2021-10-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-dialog
pnpm add promise-dialog
yarn add promise-dialog
bun add promise-dialog
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.5.0 |
| Published | 2021-10-25 |
| First published | 2019-07-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 346.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jinxin Hu |
| Maintainers | hjxenjoy |
| Keywords | dialog, toast, promise |

## Links

- npm: https://www.npmjs.com/package/promise-dialog
- Repository: https://github.com/hjxenjoy/promise-dialog
- Homepage: https://github.com/hjxenjoy/promise-dialog#readme
- Issues: https://github.com/hjxenjoy/promise-dialog/issues
- npm.io page: https://npm.io/package/promise-dialog

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 2.5.0 (latest) — 2021-10-25
- 2.4.0 — 2021-06-28
- 2.3.0 — 2021-06-28
- 2.2.9 — 2021-06-28
- 2.2.8 — 2021-05-10
- 2.2.7 — 2021-05-10
- 2.2.6 — 2021-05-10
- 2.2.5 — 2021-05-10
- 2.2.4 — 2021-05-10
- 2.2.3 — 2021-05-10
- 2.2.2 — 2021-05-10
- 2.2.1 — 2021-05-10
- 2.2.0 — 2021-03-25
- 2.1.0 — 2021-03-24
- 2.0.2 — 2021-03-23
- … 17 more at https://npm.io/package/promise-dialog/versions

## README

# Promise Dialog

Promise Based Dialog(alert, confirm, toast, prompt, Loading) Utils

## Demo

```sh
git clone git@github.com:hjxenjoy/promise-dialog.git
cd promise-dialog
pnpm i
npm start
```

## Installation

Using [npm](https://www.npmjs.com/package/promise-dialog):

```sh
npm install --save promise-dialog
pnpm add promise-dialog
yarn add promise-dialog
```

## Usage

Basic Example

```js
import * as pd from 'promise-dialog'

// only config in browser if using ssr like nextjs
if (process.browser) {
  // customize button text
  pd.setConfig({ okText: 'Sure', cancelText: 'Dismiss' })

  // customize className like tailwindcss (example with tailwindcss@^2.2)
  pd.setConfig({
    classes: {
      root: 'text-base text-gray-800',
      button:
        'relative flex-1 flex items-center justify-center h-12 bg-transparent focus:outline-none focus:ring focus:ring-inset focus:ring-green-400',
      okButton: 'text-green-600',
      cancelButton:
        'text-gray-500 before:absolute before:top-3 before:right-0 before:h-6 before:w-px before:border-l before:border-l-gray-200',
      title: 'text-center font-medium break-words leading-7',
      content: 'mt-2 text-gray-500 text-sm break-words text-center leading-6',
      html: 'mt-2 text-sm break-words',
      layer:
        'fixed inset-0 z-[1000] flex flex-col items-center justify-center p-4 bg-black bg-opacity-50',
      dialog: 'w-full max-w-xs bg-white overflow-hidden rounded-md shadow',
      body: 'p-5 text-center',
      actions: 'flex border-t border-1 border-gray-300',
      input:
        'w-full border border-gray-300 px-2 py-1 rounded placeholder-gray-400 focus:outline-none focus:ring-1 focus:ring-green-400 focus:border-green-400',
      textarea: 'resize-none',
      control: 'mt-2 min-w-[16rem]',
      loading:
        'fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 z-[1000] px-5 py-6 rounded-md bg-black/50 text-white flex flex-col items-center justify-center',
      loadingIcon: 'w-9 h-9 animate-spin',
      toast:
        'fixed top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 z-[1000] p-5 flex flex-col items-center justify-center bg-black/50 rounded-md text-white',
      toastIcon: 'w-9 h-9',
    }
  })
}

const isDark = false

const html = `<div style="background-color: #eee;padding: 8px 0;">
  <h3>HTML Content is OK!</h3>
  <p>paragraph</p>
  <button class="mt-1 font-medium px-2 py-0.5 text-green-600 border border-green-500 rounded text-sm uppercase">click button</button>
  <script type="text/javascript">alert('script')</script>
</div>`

function testAlert(isAsync) {
  const props = {
    theme: isDark ? 'dark' : undefined,
    title: `Hello${isDark ? ' Dark Mode' : ''} Alert`,
    content: `This is a${isDark ? ' Dark Mode' : ''} alert content.`,
    html,
    buttonText: 'Sure!',
    onClose() {
      console.log('alert closed')
    },
  }
  if (isAsync) {
    pd.alert(props).then()
  } else {
    pd.syncAlert(props)
  }
}

function testConfirm(isAsync) {
  const props = {
    theme: isDark ? 'dark' : undefined,
    title: 'Hello Confirm',
    content: 'This is a confirm content.',
    html,
    onOk() {
      console.log('confirm ok')
    },
    onCancel() {
      console.log('confirm cancel')
    },
  }
  if (isAsync) {
    pd.confirm(props).then()
  } else {
    pd.syncConfirm(props)
  }
}

function testPrompt(isAsync, useInput = false) {
  const props = {
    theme: isDark ? 'dark' : undefined,
    title: 'Hello Prompt',
    placeholder: `Please Enter (in ${useInput ? 20 : 50} characters)`,
    defaultValue: useInput ? '' : 'Default Values...',
    maxLength: useInput ? 20 : 50,
    useInput,
    onBlur() {
      pd.scrollBack(0, 0)
    },
    onOk(value) {
      console.log(value)
    },
    onCancel() {
      console.log('prompt cancel')
    },
  }

  if (isAsync) {
    pd.prompt(props).then()
  } else {
    pd.syncPrompt(props)
  }
}

function testToast(isAsync, iconType = 'success') {
  const props = {
    theme: isDark ? 'dark' : undefined,
    title: 'Hello Toast',
    iconType,
    onClose() {
      console.log('toast disappear')
    },
  }
  if (isAsync) {
    pd.toast(props).then()
  } else {
    pd.syncToast(props)
  }
}

function testLoading() {
  pd.loading({ title: 'Loading...' })
}

function testLoaded() {
  pd.loaded()
} 
```

---
_Source: https://npm.io/package/promise-dialog · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
