Last active
May 21, 2024 07:57
-
-
Save montasim/31b540dd30fc3afd6d13296b53c7f016 to your computer and use it in GitHub Desktop.
Custom Loading Spinner with SweetAlert2 and Tailwind CSS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const show = ( | |
text = 'Please wait, the system is preparing necessary data.' | |
) => { | |
Swal.fire({ | |
width: '550px', | |
html: ` | |
<div class="bg-transparent flex flex-col items-center justify-center gap-4"> | |
<img class="w-20" src="../media/gifs/lightBlueLoadingSpinner.gif" alt="Loading gif"> | |
<strong>${text}</strong> | |
</div> | |
`, | |
color: '#014766', | |
allowOutsideClick: false, | |
allowEscapeKey: false, | |
allowEnterKey: false, | |
showConfirmButton: false, | |
didOpen: () => { | |
Swal.showLoading(); | |
Swal.getActions().style.display = 'none'; // Hide the action bar as it's not needed | |
}, | |
}); | |
}; | |
const hide = () => { | |
Swal.close(); | |
}; | |
const loadingSpinner = { | |
show, | |
hide, | |
}; | |
export default loadingSpinner; | |
// How to use | |
import loadingSpinner from './path/to/loadingSpinner'; | |
// Show the loading spinner | |
loadingSpinner.show('Your custom message here'); | |
// Hide the loading spinner | |
loadingSpinner.hide(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description:
This gist contains a simple utility for displaying a custom loading spinner using SweetAlert2. The loadingSpinner object provides two methods: show and hide. The show method displays a SweetAlert2 modal with a loading spinner and a customizable message, while the hide method closes the modal. This is useful for indicating to users that the system is processing data and to prevent any interactions during this time.
Usage: