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.

Screenshot of a button showing a loading spinner after being clicked.

Get Source Code Preview

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

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.

View Output