Skip to content

Instantly share code, notes, and snippets.

@collardeau
Last active September 8, 2023 16:59
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collardeau/6a0c9777246db4f7b1764b3ccafdf822 to your computer and use it in GitHub Desktop.
Save collardeau/6a0c9777246db4f7b1764b3ccafdf822 to your computer and use it in GitHub Desktop.
Svelte: SweetAlert2 setup
<script>
import Swal from "sweetalert2";
// npm i sweetalert2@9.7.2
// https://sweetalert2.github.io/
</script>
<!-- a simple alert -->
<button
on:click={() => {
Swal.fire('hello sweet alert');
}}>
alert
</button>
<!-- an alert with async flow -->
<button
on:click={() => {
Swal.fire({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, keep it'
}).then(result => {
if (result.value) {
Swal.fire('Deleted!', 'Your imaginary file has been deleted.', 'success');
} else if (result.dismiss === Swal.DismissReason.cancel) {
Swal.fire('Cancelled', 'Your imaginary file is safe :)', 'error');
}
});
}}>
confirm
</button>
@discoverlance-com
Copy link

found it very useful, thanks a bunch :)

@SergioJuniorCE
Copy link

just decided today to try swal on svelte and this was posted 10h ago, thanks!

@suryamsj
Copy link

suryamsj commented Jun 13, 2022

Don't forget to load the css too

import Swal from "sweetalert2";

//For the css, you can put it in main.js
import "sweetalert2/dist/sweetalert2.min.css";

@heyhadi
Copy link

heyhadi commented Sep 22, 2022

Hi Do you know how to make sweetalert text reactive so I can pas a variable to the text and update it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment