sweetalert README

The project's README from github.com/t4t5/sweetalert. As one markdown file: README.md.

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 surprises!

Guides

Documentation

Examples

An error message:

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

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

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");
  }
});
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:

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

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

In combination with Fetch:

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");
});
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:

import React from 'react'
import swal from '@sweetalert/with-react'

swal(
  <div>
    <h1>Hello world!</h1>
    <p>
      This is now rendered with JSX!
    </p>
  </div>
)

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]

sweetalert (t4t5/sweetalert)

Searching Database…

What is this?SweetAlert is a drop-in replacement for JavaScript's native alert() that shows beautiful popups - error messages, warning dialogs and prompt modals - on web pages.
Is it a whole framework, or just a library I drop into my page?Just a lightweight frontend library, not a framework: you call swal() wherever you would have used the native alert, and it also ships tools for integrating with rendering libraries like React.
How do I add it to my project?Install the sweetalert package from npm, then call swal() in place of alert(); the README's Installation and Getting started guides cover your first popup.
Will it drag in a pile of dependencies?No - the runtime dependencies are just promise-polyfill and es6-object-assign, so it stays light.
I am still on version 1 - can I just upgrade to 2.0?Not blindly: the 2.0 release introduced many breaking changes, and the README tells you to read the upgrade guide first to avoid nasty surprises.
Does this run only in the browser, or on the server too?It is a client-side web library that replaces the browser's alert dialog, so it lives in web pages - there is no server-side or Node version.
What kinds of dialogs can it show?The README shows error messages, warning messages with a function attached to the confirm button, and prompt modals that capture the user's input - including examples wired up with Fetch calls.
How do I run code when the user confirms?swal() is promise-based: you can react to the user's choice with .then() or with async/await - both styles are shown in the README examples.
Can I put my own React components inside the alerts?Yes - alongside the main library you install SweetAlert with React, which lets you easily add React components to your alerts.
Can I restyle the popups to match my site?Yes - the documentation has dedicated sections for Configuration, Methods and Theming, so looks and behavior are adjustable.
Creator
t4t5
Size
5.3 MB
Stars
22.2K
Age
12y
Commits · 90 Days