t4t5 /sweetalert
A beautiful replacement for JavaScript's "alert"
★ 22,237⑂ 2,736👁 437⑃ 293● 719▤ 13
TA的项目
+0
日增长
-2
周增长
-12
月增长
22,237
stars
2,736
forks
437
watchers
293
prs
719
issues
13
releases
README
A beautiful replacement for JavaScript's "alert"
Installation
bash
$ npm install --save sweetalert
Usage
javascript
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 surprises!
Guides
- Installation
- Getting started
- Advanced examples
- Using with libraries
- Upgrading from 1.X
Documentation
- Configuration
- Methods
- Theming
Examples
An error message:
javascript
swal("Oops!", "Something went wrong!", "error");
A warning message, with a function attached to the confirm message:
- Using promises:
javascript
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:
javascript
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:
javascript
swal("Type something:", {
content: "input",
})
.then((value) = {
swal(You typed: ${value});
});
- Using async/await:
javascript
const value = await swal("Type something:", {
content: "input",
});
swal(You typed: ${value});
In combination with Fetch:
- Using promises:
javascript
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:
javascript
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");
}
}
Using with React
SweetAlert has tools for integrating with your favourite rendering library..
If you're using React, you can install SweetAlert with React in addition to the main library, and easily add React components to your alerts like this:
javascript
import React from 'react'
import swal from '@sweetalert/with-react'
swal(
Hello world!
Top issues
#890Typings error TS2403: Subsequent variable declarations must have the same type.25 comments · 2019-01-25#391tab is not working in the form when sweet alert is pop up22 comments · 2015-06-27#127Tab key not working after opening two Sweet Alert Modals in a row21 comments · 2014-10-15#457Handling pending sweet alerts16 comments · 2015-10-05#889Uncaught TypeError: Cannot read property 'constructor' of undefined at Se (sweetalert2:1)15 comments · 2019-01-23#799Angular 5.2.3 - ERROR TypeError: sweetalert_1.default is not a function13 comments · 2018-02-06#869Content Security Policy Compatibility12 comments · 2018-10-11#738the latest version cannot work in ng2+, npm install result is not complete10 comments · 2017-10-04
Latest release
Bug fixes v2.1.0 2017-12-14- Multiple class names on buttons - Correct default input for input elements - Support for \n in text - Correct button interface typings for TypeScript - Remove ugly Windows scrollbars - Default export for TypeScript - Some basic a11y attributes Thanks to everyone who helped contribute!