Try 'magnetic button'
View code

Number 59: Loading button

Specification

Catalogue
No. 59 of 110
Interaction
Loading
Element
Button, Loader
Preview
Live component
Imports
react 19.3.1
Added

Notes

The button keeps its place through the whole save: it narrows into a circle with a spinner while the request runs, then widens into a confirmation, then returns to its label.

The three states share one grid cell and cross-fade with a slight blur and scale, while the button's width animates between fixed values on a springy curve. Fixed widths avoid measuring text and keep the layout around it still.

The button is marked busy with aria-disabled during the save, and a hidden status region announces Saving and Saved.

Code

React and plain CSS
"use client";

import { useEffect, useRef, useState } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./loading-button.module.css";

type Phase = "idle" | "loading" | "success";

const DEMO: Array<[Phase, number]> = [
  ["idle", 500],
  ["loading", 1500],
  ["success", 1400],
];

export default function LoadingButton({ active, context }: LivePreviewProps) {
  const [phase, setPhase] = useState<Phase>("idle");
  const [step, setStep] = useState(0);
  const timers = useRef<number[]>([]);
  const demo = active && context === "card";

  // Each time the demo starts, it starts from the first step.
  const [demoRunning, setDemoRunning] = useState(demo);
  if (demoRunning !== demo) {
    setDemoRunning(demo);
    setStep(0);
  }

  useEffect(() => {
    if (!demo) return;
    let n = 0;
    let id = window.setTimeout(function next() {
      n += 1;
      setStep(n);
      id = window.setTimeout(next, DEMO[n % DEMO.length]?.[1] ?? 500);
    }, DEMO[0]?.[1] ?? 500);
    return () => window.clearTimeout(id);
  }, [demo]);

  useEffect(() => {
    const pending = timers.current;
    return () => pending.forEach((t) => window.clearTimeout(t));
  }, []);

  function save() {
    if (phase !== "idle") return;
    setPhase("loading");
    timers.current.push(
      window.setTimeout(() => setPhase("success"), 1600),
      window.setTimeout(() => setPhase("idle"), 3000),
    );
  }

  const shown: Phase = context === "card" ? (demo ? (DEMO[step % DEMO.length]?.[0] ?? "idle") : "idle") : phase;

  return (
    <div className={styles.root}>
      <button
        type="button"
        className={styles.button}
        data-phase={shown}
        aria-disabled={shown !== "idle" || undefined}
        tabIndex={context === "card" ? -1 : 0}
        onClick={save}
      >
        <span className={styles.layer} data-visible={shown === "idle" || undefined}>
          Save changes
        </span>
        <span className={styles.layer} data-visible={shown === "loading" || undefined} aria-hidden="true">
          <svg className={styles.spinner} width="22" height="22" viewBox="0 0 24 24" fill="none">
            <circle cx="12" cy="12" r="9" stroke="currentColor" strokeOpacity="0.25" strokeWidth="2.6" />
            <path d="M21 12a9 9 0 0 0-9-9" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" />
          </svg>
        </span>
        <span className={styles.layer} data-visible={shown === "success" || undefined} aria-hidden="true">
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none">
            <path d="m5 12.5 4.5 4.5L19 7.5" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round" />
          </svg>
          Saved
        </span>
      </button>
      <span className="visually-hidden" role="status">
        {shown === "loading" ? "Saving" : shown === "success" ? "Saved" : ""}
      </span>
    </div>
  );
}

More like this

Dotted orb that thinks

Loading, AutoplayLoading and progress

Picture that resolves from a mosaic

LoadingLoading and progress

Liquid metal ring

Hover, PressButtons and toggles

Button that shows its own progress

Press, LoadingButtons and toggles

Slingshot button

Press, DragButtons and toggles

Sphere of dots that parts for the pointer

Hover, PressPage sections