Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sweetalert

> 编程语言
Open source

A beautiful replacement for JavaScript's "alert"

22.3K stars0 likes0 views
WebsiteGitHub

About

A beautiful replacement for JavaScript's "alert"

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(
  

)

Read more about integrating with React

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

Contributors

This project exists thanks to all the people who contribute. [Contribute].

Backers

Thank you to all our backers! 🙏 [Become a backer]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Issues· 200 open

View all issuesOpen on GitHub
  • #1021

    แนะนำ 4 เว็บ สล็อตเว็บตรง เว็บแท้ สล็อต pg เว็บตรง แตกหนัก RTP สูงสุด 98% รับวอเลท ปี2026

    Updated Sep 4, 2026
  • #1020

    Community-maintained fork created (sweetalert-maintained)

    Updated Mar 5, 2026
  • #1019

    Maintainer request: willing to actively maintain SweetAlert

    Updated Mar 5, 2026
  • #668

    Preserving onfocus

    bugUpdated Jan 14, 2026
  • #1000

    Failure to install Sweetalert React package

    Updated Jan 14, 2026
  • #997

    Getting the instance of the current swal to change it's text dynamically?

    Updated Aug 24, 2025
  • #1014

    Set default text button for all SweetAlert in project

    Updated Jun 28, 2025
  • #869

    Content Security Policy Compatibility

    Updated Jun 6, 2025
  • #1013

    Unpkg Link for SweetAlert returns 503 error

    Updated Mar 31, 2025
  • #1012

    Added support for rtl pages

    Updated May 24, 2024

> Tags

TypeScriptalertdialogjavascriptmodal

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言