Loading Spinner Button Template
Provide clear feedback for background processes with a button that displays a loading spinner.
This component is essential for any action that isn't instantaneous, assuring users that their request is being processed and preventing duplicate submissions.

About the Loading Spinner Button
When a user submits a form, saves data, or performs any other asynchronous action, the interface must communicate that the system is working. A loading spinner button does exactly that. It visually changes the button to an "in-progress" state, usually disabling it to stop the user from clicking again. This simple piece of feedback vastly improves the user experience by managing expectations and reducing uncertainty.
Features
- Clear Visual Feedback: The button text is replaced by a CSS-only spinner animation to indicate a loading process.
- Prevents Duplicate Actions: The included JavaScript disables the button during the loading state, a best practice for form submissions.
- Accessible States: The script can be expanded to use
aria-live
regions to announce the completion of the action to screen reader users. - Self-Contained: The HTML, CSS, and demonstrative JavaScript are all included in one block for easy implementation.
Code Breakdown
This component combines HTML, CSS, and JavaScript. The button
contains the button text and a loader element, which is hidden by default. The CSS defines an animation using @keyframes
to spin the loader element.
When the button is clicked, the JavaScript adds an is-loading
class. This class triggers CSS rules that hide the text and display the loader. The script also sets the button's disabled
property to true
. A setTimeout
is used here to simulate a network delay, after which the class is removed and the button is re-enabled.
Code
Here is the complete code for the loading spinner button.