<h1 align="center">
  <a href="https://npmjs.com/package/sweetalert2-preset" target="_blank" rel="noopener noreferrer">
    SweetAlert2 Preset <sup><img alt="version" src="https://img.shields.io/npm/v/sweetalert2-preset.svg?style=flat-square&color=white&label="></sup>
  </a>
</h1>

<p align="center">
  5 out-of-the-box presets for <a href="https://sweetalert2.github.io">sweetalert2</a>, refer to <a href="https://element-plus.org/en-US/component/message-box.html">MessageBox</a> of element-plus.
</p>

<p align="center">
  <a href="https://bundlephobia.com/package/sweetalert2-preset"><img alt="minzipped size" src="https://img.shields.io/bundlephobia/minzip/sweetalert2-preset"></a>
  <a href="https://conventionalcommits.org"><img alt="conventional commits" src="https://img.shields.io/badge/commits-Conventional-FE5196.svg?logo=conventionalcommits"></a>
  <a href="https://github.com/antfu/eslint-config"><img alt="code style" src="https://antfu.me/badge-code-style.svg"></a>
</p>

<br>

## ⚠ DEPRECATED

> **Warning**
>
> SweetAlert2 Preset has been upgraded to [faim](https://github.com/cloydlau/faim) by the the same author [cloydlau](https://github.com/cloydlau)
>
> Not only are the capabilities enhanced, but for subsequent bug fixes and iterative optimization, please migrate as soon as possible

<br>

## Install

### Peer Dependencies

- `sweetalert2`

<br>

### NPM

```shell
npm i sweetalert2-preset sweetalert2
```

```ts
import Swal from 'sweetalert2'
import SwalPreset from 'sweetalert2-preset'

Object.assign(Swal, SwalPreset)

Swal.fire()
Swal.success()
Swal.info()
Swal.warning()
Swal.error()
Swal.confirm()
```

<br>

### CDN + ESM

> ⚠ Not yet supported because `sweetalert2` does not export ESM.

<br>

### CDN + IIFE

```html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
</head>

<body>
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2"></script>
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2-preset"></script>
  <script>
    Object.assign(Swal, SwalPreset)

    Swal.fire()
    Swal.success()
    Swal.info()
    Swal.warning()
    Swal.error()
    Swal.confirm()
  </script>
</body>

</html>
```

<br>

## Life Cycle

```ts
SwalPreset.success('Operation Success').then(() => {
  // onClose
})

SwalPreset.info('Information').then(() => {
  // onClose
})

SwalPreset.warning('Warning').then(() => {
  // onClose
})

SwalPreset.error('Error Occurred').then(() => {
  // onClose
})

SwalPreset.confirm('Are You Sure?').then(() => {
  // onConfirmed
}).catch((e) => {
  if (e.isDenied) {
    // onDenied
  } else if (e.isDismissed) {
    // onDismissed
  }
})
```

<br>

## Example: Coercive Confirm

No cancel, must confirm.

```ts
SwalPreset.confirm({
  titleText: 'Confirm to continue',
  showCancelButton: false,
  allowOutsideClick: false,
  allowEscapeKey: false,
})
```

<br>

## Example: Complex Confirm

```ts
// form with async submitting
SwalPreset.confirm({
  input: 'text',
  inputAttributes: {
    placeholder: 'Remark'
  },
  confirmButtonText: 'Agree',
  showLoaderOnConfirm: true,
  preConfirm: (input) => {
    return new Promise((resolve) => {
      setTimeout(resolve, 500)
    }).then(() => {
      alert('Agree Success')
    }).catch((e) => {
      alert('Agree Failed')
    })
  },
  showDenyButton: true,
  denyButtonText: 'Deny',
  returnInputValueOnDeny: true,
  preDeny: (input) => {
    if (input) {
      return new Promise((resolve, reject) => {
        setTimeout(reject, 500)
      }).then(() => {
        alert('Deny Success')
      }).catch((e) => {
        alert('Deny Failed')
      })
    } else {
      Swal.showValidationMessage('Please fill in the remark')
      return false
    }
  },
}).then((e) => {
  alert('Agreed')
}).catch((e) => {
  if (e.isDenied) {
    alert('Denied')
  } else if (e.isDismissed) {
    alert('Dismissed')
  }
})
```

<br>

## Changelog

Detailed changes for each release are documented in the [release notes](https://github.com/cloydlau/sweetalert2-preset/releases)

<br>
