Skip to content

A beautiful, responsive, highly customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies.

License

Notifications You must be signed in to change notification settings

Insayt/sweetalert2

 
 

Repository files navigation

SweetAlert2


Build Status: Linux Build Status: Windows Coverage Status Version jsdelivr gzip size Support me on Patreon PayPal Donate

A beautiful, responsive, customizable, accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies.


See SweetAlert2 in action ↗


👉 Upgrading from v6.x to v7.x? Read the release notes!

👉 Migrating from SweetAlert? SweetAlert 1.x to SweetAlert2 migration guide


Installation

npm install --save sweetalert2

Or:

bower install --save sweetalert2

Or download from CDN: jsdelivr.net/npm/sweetalert2

Usage

<script src="sweetalert2/dist/sweetalert2.all.min.js"></script>

<!-- Include a polyfill for ES6 Promises (optional) for IE11, UC Browser and Android browser support -->
<script src="https://cdn.jsdelivr.net/npm/promise-polyfill"></script>

You can also include the stylesheet separately if desired:

<script src="sweetalert2/dist/sweetalert2.min.js"></script>
<link rel="stylesheet" href="sweetalert2/dist/sweetalert2.min.css">

Or:

// ES6 Modules or TypeScript
import Swal from 'sweetalert2'

// CommonJS
const Swal = require('sweetalert2')

It's possible to import JS and CSS separately, e.g. if you need to customize styles:

import swal from 'sweetalert2/dist/sweetalert2.js'

import 'sweetalert2/src/sweetalert2.scss'

Please note that TypeScript is well-supported, so you don't have to install a third-party declaration file.

Examples

The most basic message:

Swal('Hello world!')

A message signaling an error:

Swal('Oops...', 'Something went wrong!', 'error')

Handling the result of SweetAlert2 modal:

Swal({
  title: 'Are you sure?',
  text: 'You will not be able to recover this imaginary file!',
  type: 'warning',
  showCancelButton: true,
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No, keep it'
}).then((result) => {
  if (result.value) {
    Swal(
      'Deleted!',
      'Your imaginary file has been deleted.',
      'success'
    )
  // For more information about handling dismissals please visit
  // https://sweetalert2.github.io/#handling-dismissals
  } else if (result.dismiss === Swal.DismissReason.cancel) {
    Swal(
      'Cancelled',
      'Your imaginary file is safe :)',
      'error'
    )
  }
})

Browser compatibility

IE11* Edge Chrome Firefox Safari Opera Android Browser* UC Browser*
✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️

* ES6 Promise polyfill should be included, see usage example.

Note that SweetAlert2 does not and will not provide support or functionality of any kind on IE10 and lower.

Related projects

Related community projects

Collaborators

@zenflow @toverux @acupajoe @samturrell @birjolaxew

Contributors

This project exists thanks to all the people who contribute.

Contributing

If you would like to contribute enhancements or fixes, please do the following:

  1. Fork the sweetalert2 repository and clone it locally.

  2. Make sure you have npm or yarn installed.

  3. When in the SweetAlert2 directory, run npm install or yarn install to install dependencies.

  4. To begin active development, run npm start or yarn start. This does several things for you:

Donations

Has SweetAlert2 helped you create an amazing application? You can show your support by making a donation in one of several ways:

About

A beautiful, responsive, highly customizable and accessible (WAI-ARIA) replacement for JavaScript's popup boxes. Zero dependencies.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 82.0%
  • CSS 17.5%
  • HTML 0.5%