Skip to content

Button with loading state in WordPress

A button that announces the pending request then success, without changing width.

Here is an excerpt of this component's code in WordPress. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in WordPress

bouton-chargement.php
<?php
/**
 * Plugin Name: Forge, Bouton avec état de chargement
 * Description: Un bouton qui annonce l'envoi en cours puis la réussite, sans changer de largeur. Ajoute la composition « Bouton avec état de chargement » à l'éditeur de blocs.
 * Version: 1.0.0
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Author: Forge
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: forge-bouton-chargement
 *
pattern.html
<!-- wp:html -->
<button type="button" class="loading-button" data-loading-button>
        <span class="loading-button__label">Envoyer ma demande</span>
        <span class="loading-button__pending" aria-hidden="true">
          <span class="loading-button__spinner"></span>Envoi…
        </span>
        <span class="loading-button__done" aria-hidden="true">Demande envoyée</span>
      </button>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>Cliquez pour simuler un envoi de deux secondes.</p>
script.js
(() => {
  if (!document.querySelector(".loading-button")) return;
  // Replace `send` with your own request (fetch, form submission…); it must return a promise.
  function send() {
    return new Promise((resolve) => setTimeout(resolve, 2000));
  }

  document.querySelectorAll("[data-loading-button]").forEach((button) => {
    const status = document.querySelector("[data-loading-status]");
    button.addEventListener("click", async () => {
      if (button.getAttribute("aria-busy") === "true") return;
      button.style.minWidth = `${button.offsetWidth}px`;
style.css
:root {
  --loading-bg: #0f6b4f;
  --loading-bg-hover: #0b5a42;
  --loading-text: #ffffff;
  --loading-success: #1e8a5f;
  --loading-ring: #f2b84b;
  --loading-radius: 999px;
  --demo-bouton-chargement-bg: #f4f6f3;
  --demo-bouton-chargement-text: #33403a;
}

@media (prefers-color-scheme: dark) {

Free component: copy it freely, including for commercial projects.

Buttons components in WordPress
  • A row of buttons to share the page on Facebook, LinkedIn, WhatsApp or by email, with a copy-link button and no third-party script.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • Toggle heart buttons that count favourites, remember them between visits and announce them to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that adds an event to Google Calendar, Outlook or any calendar through an .ics file, with no third-party service.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A button that copies an IBAN, a reference, an address or a promo code and confirms "Copied", on screen and to screen readers.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A WhatsApp button pinned to the bottom of the screen that opens a welcome bubble with common topics and a pre-filled message, with no third-party widget.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A bar pinned to the bottom of mobile screens with Call and Directions buttons and a live open-now status, built for trades and local shops.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component