Professional Documents
Culture Documents
A basic message
Try me!
Swal.fire(
'The Internet?',
'That thing is still around?',
'question'
)
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Something went wrong!',
footer: '<a href>Why do I have this issue?</a>'
})
Swal.fire({
imageUrl: 'https://placeholder.pics/svg/300x1500',
imageHeight: 1500,
imageAlt: 'A tall image'
})
Custom HTML description and buttons with ARIA labels
Try me!
Swal.fire({
title: '<strong>HTML <u>example</u></strong>',
icon: 'info',
html:
'You can use <b>bold text</b>, ' +
'<a href="//sweetalert2.github.io">links</a> ' +
'and other HTML tags',
showCloseButton: true,
showCancelButton: true,
focusConfirm: false,
confirmButtonText:
'<i class="fa fa-thumbs-up"></i> Great!',
confirmButtonAriaLabel: 'Thumbs up, great!',
cancelButtonText:
'<i class="fa fa-thumbs-down"></i>',
cancelButtonAriaLabel: 'Thumbs down'
})
Swal.fire({
position: 'top-end',
icon: 'success',
title: 'Your work has been saved',
showConfirmButton: false,
timer: 1500
})
Swal.fire({
title: 'Custom animation with Animate.css',
showClass: {
popup: 'animated fadeInDown faster'
},
hideClass: {
popup: 'animated fadeOutUp faster'
}
})
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.value) {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
}
})
... and by passing a parameter, you can execute something else for
"Cancel".
Try me!
swalWithBootstrapButtons.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, cancel!',
reverseButtons: true
}).then((result) => {
if (result.value) {
swalWithBootstrapButtons.fire(
'Deleted!',
'Your file has been deleted.',
'success'
)
} else if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.cancel
) {
swalWithBootstrapButtons.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
})
Swal.fire({
title: 'Sweet!',
text: 'Modal with a custom image.',
imageUrl: 'https://unsplash.it/400/200',
imageWidth: 400,
imageHeight: 200,
imageAlt: 'Custom image',
})
Swal.fire({
title: 'Custom width, padding, background.',
width: 600,
padding: '3em',
background: '#fff url(/images/trees.png)',
backdrop: `
rgba(0,0,123,0.4)
url("/images/nyan-cat.gif")
left top
no-repeat
`
})
let timerInterval
Swal.fire({
title: 'Auto close alert!',
html: 'I will close in <b></b> milliseconds.',
timer: 2000,
timerProgressBar: true,
onBeforeOpen: () => {
Swal.showLoading()
timerInterval = setInterval(() => {
const content = Swal.getContent()
if (content) {
const b = content.querySelector('b')
if (b) {
b.textContent = Swal.getTimerLeft()
}
}
}, 100)
},
onClose: () => {
clearInterval(timerInterval)
}
}).then((result) => {
/* Read more about handling dismissals below */
if (result.dismiss === Swal.DismissReason.timer) {
console.log('I was closed by the timer')
}
})
Swal.fire({
title: ''هل تريد االستمرار؟,
icon: 'question',
iconHtml: ''؟,
confirmButtonText: ''نعم,
cancelButtonText: ''ال,
showCancelButton: true,
showCloseButton: true
})
Swal.fire({
title: 'Submit your Github username',
input: 'text',
inputAttributes: {
autocapitalize: 'off'
},
showCancelButton: true,
confirmButtonText: 'Look up',
showLoaderOnConfirm: true,
preConfirm: (login) => {
return fetch(`//api.github.com/users/${login}`)
.then(response => {
if (!response.ok) {
throw new Error(response.statusText)
}
return response.json()
})
.catch(error => {
Swal.showValidationMessage(
`Request failed: ${error}`
)
})
},
allowOutsideClick: () => !Swal.isLoading()
}).then((result) => {
if (result.value) {
Swal.fire({
title: `${result.value.login}'s avatar`,
imageUrl: result.value.avatar_url
})
}
})
Swal.queue([{
title: 'Your public IP',
confirmButtonText: 'Show my public IP',
text:
'Your public IP will be received ' +
'via AJAX request',
showLoaderOnConfirm: true,
preConfirm: () => {
return fetch(ipAPI)
.then(response => response.json())
.then(data => Swal.insertQueueStep(data.ip))
.catch(() => {
Swal.insertQueueStep({
icon: 'error',
title: 'Unable to get your public IP'
})
})
}
}])
<script
src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
USAGE
<script src="sweetalert2.all.min.js"></script>
<!-- Optional: include a polyfill for ES6 Promises for IE11
-->
<script src="https://cdn.jsdelivr.net/npm/promise-
polyfill"></script>
<script src="sweetalert2.min.js"></script>
<link rel="stylesheet" href="sweetalert2.min.css">
Or
// CommonJS
const Swal = require('sweetalert2')
It's possible to import JS and CSS separately, e.g. if you need to customize
styles:
Swal.fire({
title: 'Error!',
text: 'Do you want to continue',
icon: 'error',
confirmButtonText: 'Cool'
})
React
CONFIGURATION
Here are the keys that you can use if you pass an object into sweetAlert2:
Defa
ult
Argument Description
valu
e
title '' The title of the modal, as HTML. It can either be
added to the object under the key "title" or passed as
the first parameter of the function.
titleText '' The title of the modal, as text. Useful to avoid HTML
injection.
bac
kdr
op:
'sw
al2
-
bac
kdr
op-
sho
w',
ico
n:
'sw
al2
-
ico
n-
sho
w'
}
bac
kdr
op:
'sw
al2
-
bac
kdr
op-
hid
e',
ico
n:
'sw
al2
-
ico
n-
hid
e'
}
footer '' The footer of the modal. Can be either plain text or
HTML.
backdrop true Whether or not SweetAlert2 should show a full screen
click-to-dismiss backdrop. Can be either a boolean or
a string which will be assigned to the
CSS background property.
grow false Paired with window position, sets the direction the
modal should grow in, can be set
to 'row', 'column', 'fullscreen', or false.
timerPro false If set to true, the timer will have a progress bar at the
gressBar bottom of a popup. Mostly, this feature is useful with
toasts.
allowOut true If set to false, the user can't dismiss the modal by
sideClick clicking outside it.
You can also pass a custom function returning a
boolean value, e.g. if you want to disable outside
clicks for the loading state of a modal.
allowEsc true If set to false, the user can't dismiss the modal by
apeKey pressing the Esc key. You can also pass a custom
function returning a boolean value, e.g. if you want to
disable the Esc key for the loading state of a modal.
allowEnt true If set to false, the user can't confirm the modal by
erKey pressing the Enter or Space keys, unless they
manually focus the confirm button. You can also pass
a custom function returning a boolean value.
keydown false Useful for those who are using SweetAlert2 along
Listener with Bootstrap modals. By
Capture default keydownListenerCapture is false which
means when a user hits Esc , both SweetAlert2 and
Bootstrap modals will be closed.
Set keydownListenerCapture to true to fix that
behavior.
focusCon true Set to false if you want to focus the first element in
firm tab order instead of "Confirm"-button by default.
closeButt '&ti Use this to change the content of the close button.
onHtml mes;
'
imageUrl und Add a customized icon for the modal. Should contain
efin a string with the path or URL to the image.
ed
onRende und Function to run after modal DOM has been updated.
r efin Typically, this will happen
ed after Swal.fire() or Swal.update(). If you want to
perform changes in the modal's DOM, that
survive Swal.update(), onRender is a good place for
that.
onAfterC und Function to run after popup has been disposed by user
lose efin interaction (and not by another popup).
ed
onDestro und Function to run after popup has been destroyed either
y efin by user interaction or by another popup.
ed
Mixin example
Try me!
HANDLING DISMISSALS
ICONS
success Try me!
warning
! Try me!
info
i Try me!
question
? Try me!
INPUT TYPES
const ipAPI = '//api.ipify.org? Try
text format=json'
me!
const inputValue = fetch(ipAPI)
.then(response =>
response.json())
.then(data => data.ip)
if (ipAddress) {
Swal.fire(`Your IP address is $
{ipAddress}`)
}
if (password) {
Swal.fire(`Entered password: $
{password}`)
}
if (text) {
Swal.fire(text)
}
if (fruit) {
Swal.fire(`You selected: $
{fruit}`)
}
if (color) {
Swal.fire({ html: `You selected:
${color}` })
}
if (accept) {
Swal.fire('You agreed with
T&C :)')
}
if (file) {
const reader = new FileReader()
reader.onload = (e) => {
Swal.fire({
title: 'Your uploaded
picture',
imageUrl: e.target.result,
imageAlt: 'The uploaded
picture'
})
}
reader.readAsDataURL(file)
}
Swal.fire({
title: 'How old are you?',
icon: 'question',
input: 'range',
inputAttributes: {
Try
range min: 8,
max: 120, me!
step: 1
},
inputValue: 25
})
if (formValues) {
Swal.fire(JSON.stringify(formValues))
}
METHODS
Method Description
THEMES
Dark
Minimal
Borderless
Bootstrap 4
Material UI
WordPress Admin
Themes Installation
Themes Usage
With CSS:
<!-- Include a required theme -->
<link rel="stylesheet"
href="@sweetalert2/themes/dark/dark.css">
<script src="sweetalert2/dist/sweetalert2.min.js"></script>
With SASS:
// your-app.js
import Swal from 'sweetalert2/src/sweetalert2.js'
// your-app.scss
@import '~@sweetalert2/themes/dark/dark.scss';
COLLABORATORS
@gverni
@zenflow
@toverux
@acupajoe
DONATIONS
PayPal
GitHub Sponsors
Cryptocurrencies
Hall of Donators 🏆
SPONSORS
Become a sponsor
FlowCrypt
SexualAlpha (NSWF)
STED (NSWF)
YourDoll (NSWF)
PriceListo
LoveLoxy (NSWF)
SebaEBC
SheetJS LLC
Unique-P GmbH
STC (NSFW)
Or, click the banner at the top of this page. We show high-quality tech ads
via Carbon.
Ads injected into examples are only shown on this page, they are not the
part of SweetAlert2's codebase and you will not have them when using
SweetAlert2 for your projects.
CONTRIBUTE
Feel free to fork SweetAlert2 on GitHub if you have any features that you
want to add!