Skip to content

harendra-iitg/sweetalert

This branch is 33 commits behind t4t5/sweetalert:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

bfa29ec · Dec 14, 2017
Aug 31, 2017
Dec 14, 2017
Dec 14, 2017
Dec 14, 2017
Dec 14, 2017
Dec 14, 2017
May 5, 2017
Oct 12, 2014
Oct 1, 2017
Sep 6, 2017
Aug 31, 2017
Sep 6, 2017
Dec 14, 2017
Dec 14, 2017
Apr 15, 2017
Oct 4, 2017
Apr 15, 2017
Oct 16, 2017

Repository files navigation

SweetAlert

A beautiful replacement for JavaScript's "alert"

npm version Build status

A success modal

Installation

$ npm install --save sweetalert

Usage

import swal from 'sweetalert'

swal("Hello world!")

Upgrading from 1.X

Many improvements and breaking changes have been introduced in the 2.0 release. Make sure you read the upgrade guide to avoid nasty suprises!

Guides

Documentation

Examples

An error message:

swal("Oops!", "Something went wrong!", "error")

A warning message, with a function attached to the confirm message:

  • Using promises:
swal({
  title: "Are you sure?",
  text: "Are you sure that you want to leave this page?",
  icon: "warning",
  dangerMode: true,
})
.then(willDelete => {
  if (willDelete) {
    swal("Deleted!", "Your imaginary file has been deleted!", "success");
  }
});
  • Using async/await:
const willDelete = await swal({
  title: "Are you sure?",
  text: "Are you sure that you want to delete this file?",
  icon: "warning",
  dangerMode: true,
})

if (willDelete) {
  swal("Deleted!", "Your imaginary file has been deleted!", "success");
}

A prompt modal, where the user's input is logged:

  • Using promises:
swal("Type something:", {
  content: "input",
})
.then((value) => {
  swal(`You typed: ${value}`);
})
  • Using async/await:
const value = await swal("Type something:", {
  content: "input",
})

swal(`You typed: ${value}`);

In combination with Fetch:

  • Using promises:
swal({
  text: 'Wanna log some information about Bulbasaur?',
  button: {
    text: "Search!",
    closeModal: false,
  },
})
.then(willSearch => {
  if (willSearch) {
    return fetch(`http://pokeapi.co/api/v2/pokemon/1`)
  }
})
.then(result => result.json())
.then(json => console.log(json))
.catch(err => {
  swal("Oops!", "Seems like we couldn't fetch the info", "error")
})
  • Using async/await:
const willSearch = await swal({
  text: 'Wanna log some information about Bulbasaur?',
  button: {
    text: "Search!",
    closeModal: false,
  },
})

if (willSearch) {
  try {
    const result = await fetch(`http://pokeapi.co/api/v2/pokemon/1`)
    const json = await result.json()
    console.log(json)
  } catch (err) {
    swal("Oops!", "Seems like we couldn't fetch the info", "error")
  }
}

Contributing

If you're changing the core library:

  1. Make changes in the src folder.
  2. Preview changes by running npm run docs
  3. Submit pull request

If you're changing the documentation:

  1. Make changes in the docs-src folder.
  2. Preview changes by running npm run docs
  3. Run npm run builddocs to compile the changes to the docs folder
  4. Submit pull request

About

A beautiful replacement for JavaScript's "alert"

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 50.6%
  • CSS 29.3%
  • JavaScript 11.9%
  • HTML 8.2%