I've tried to use result.dismiss
, result.isDismissed
, and result.isDenied
but I can't seem to get specific enough to check if the user clicked outside of the swal message.
Here is my code:
Swal.fire({
position: "top-end",
title: `${name} successfully added to cart!`,
icon: "success",
showCancelButton: true,
cancelButtonText: "Continue shopping",
cancelButtonColor: "green",
confirmButtonText: "Go To Cart",
confirmButtonColor: "blue",
}).then((result) => {
if (result.isConfirmed) history.push("/cart");
else if (location.pathname == "/" && !result.dismiss)
history.push("/products");
});
Thank you!
You can check if the alert was dismissed using the cancel button by checking if results .dismiss
reason is equal to Swal.DismissReason.cancel
:
Swal.fire({
position: "top-end",
title: "Item successfully added to cart!",
icon: "success",
showCancelButton: true,
cancelButtonText: "Continue shopping",
cancelButtonColor: "green",
confirmButtonText: "Go To Cart",
confirmButtonColor: "blue",
}).then((result) => {
if (result.dismiss === Swal.DismissReason.cancel) {
console.log("Redirect for cancel");
} else {
console.log("Redirect for non-cancel dismissals");
}
});
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.16/dist/sweetalert2.all.min.js"></script>