Loading to Success

The label slides away for a spinner while the request runs, then a check mark draws itself when it succeeds.

  • CSS
  • Buttons
  • Click
  • Added

AI prompt

Build a React submit button with three states: idle, loading and done, stored in data-state. Keep the button width fixed so it never jumps. On click, switch to loading: the label slides up and fades out (translateY(-100%), 200ms) while a 18px spinner (a circle with a 3/4 border, rotating 360deg every 700ms linear) fades in. Simulate a 1.4s request, then switch to done: the spinner fades out and an SVG check mark draws itself by animating stroke-dashoffset from its path length to 0 over 400ms with cubic-bezier(0.16, 1, 0.3, 1), with the label "Order placed". Return to idle after 2s. Disable the button and set aria-busy while loading, and announce the state text with aria-live="polite". Under prefers-reduced-motion, replace the slide and draw with 150ms fades and keep the spinner static. The idle label reads "Place order".

Source

Demo.tsx
'use client'

import { useEffect, useRef, useState } from 'react'
import s from './demo.module.css'

type State = 'idle' | 'loading' | 'done'

export default function LoadingButton({ labels }: { labels: Record<string, string> }) {
  const [state, setState] = useState<State>('idle')
  const timers = useRef<ReturnType<typeof setTimeout>[]>([])
  useEffect(() => () => timers.current.forEach(clearTimeout), [])

  const submit = () => {
    setState('loading')
    timers.current.push(
      setTimeout(() => setState('done'), 1400),
      setTimeout(() => setState('idle'), 3400),
    )
  }

  return (
    <button
      type="button"
      className={s.button}
      data-state={state}
      disabled={state === 'loading'}
      aria-busy={state === 'loading'}
      onClick={() => state === 'idle' && submit()}
    >
      <span className={s.label}>{state === 'done' ? labels.done : labels.idle}</span>
      <span className={s.spinner} aria-hidden="true" />
      <svg className={s.check} viewBox="0 0 24 24" aria-hidden="true">
        <path d="M5 12.5l4.5 4.5L19 7.5" />
      </svg>
      <span className={s.sr} aria-live="polite">
        {state === 'loading' ? labels.loading : state === 'done' ? labels.done : ''}
      </span>
    </button>
  )
}
demo.module.css
.button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 200px;
  height: 52px;
  overflow: hidden;
  border: 0;
  border-radius: 12px;
  background: var(--stage-ink);
  color: var(--stage-bg);
  font: 600 var(--text-base) / 1 var(--font-body);
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 200ms var(--ease-out);
}

.button:focus-visible { outline: 2px solid var(--color-focus); outline-offset: 3px; }
.button:disabled { cursor: progress; }
.button[data-state='done'] { background: var(--stage-accent); color: var(--stage-accent-ink); }

.label { transition: transform 200ms var(--ease-out), opacity 200ms var(--ease-out); }
.button[data-state='loading'] .label { transform: translateY(-100%); opacity: 0; }

.spinner {
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  opacity: 0;
  transition: opacity 150ms linear;
  animation: spin 700ms linear infinite;
}

.button[data-state='loading'] .spinner { opacity: 1; }

.check { width: 0; height: 20px; overflow: visible; }
.button[data-state='done'] .check { width: 20px; }

.check path {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 22;
  stroke-dashoffset: 22;
}

.button[data-state='done'] .check path { animation: draw 400ms 100ms var(--ease-out) forwards; }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes draw { to { stroke-dashoffset: 0; } }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; }
  .label { transition: opacity 150ms linear; }
  .button[data-state='loading'] .label { transform: none; }
  .button[data-state='done'] .check path { animation: none; stroke-dashoffset: 0; }
}

Suggest an idea

Seen a motion or a theme you would like to find here? Describe it, and it opens as a prefilled GitHub issue for you to post.

What are you suggesting?

Needs a GitHub account. Suggestions are public.