/Click feedback
Click feedback • React
Complete Spinner Button
A Complete button that shrinks only in width into a circle while a ring spinner turns inside it, then fills green with a soft green glow as a check draws itself in.
Installation
Terminal
npx shadcn@latest add @microkit/complete-spinner-buttonCode
1"use client";23import { useEffect, useLayoutEffect, useRef, useState } from "react";45type CompleteState = "idle" | "loading" | "done";67const LOADING_MS = 1600;8const DONE_MS = 1800;910export function CompleteSpinnerButton() {11const [state, setState] = useState<CompleteState>("idle");12const buttonRef = useRef<HTMLButtonElement>(null);13const labelRef = useRef<HTMLSpanElement>(null);1415useLayoutEffect(() => {16const button = buttonRef.current;17const label = labelRef.current;18if (!button || !label) return;19// Width only animates between two lengths, so the label's own width is written down and followed when fonts load.20const measure = () => button.style.setProperty("--complete-width", `${label.offsetWidth}px`);21measure();22const observer = new ResizeObserver(measure);23observer.observe(label);24return () => observer.disconnect();25}, []);2627useEffect(() => {28if (state === "idle") return;29const timer = window.setTimeout(30() => setState(state === "loading" ? "done" : "idle"),31state === "loading" ? LOADING_MS : DONE_MS,32);33return () => window.clearTimeout(timer);34}, [state]);3536return (37<button38ref={buttonRef}39type="button"40className="group grid h-[52px] w-[var(--complete-width,auto)] cursor-pointer appearance-none items-center justify-center overflow-hidden rounded-full border-0 bg-[light-dark(#ffffff,#121418)] p-0 [font-family:inherit] text-[17px] font-medium leading-none tracking-[-.01em] text-[light-dark(#14181e,#f2f4f6)] [box-shadow:inset_0_0_0_1px_light-dark(#dfe3ea,#2b2f36),0_10px_24px_light-dark(rgba(246,247,249,.35),rgba(0,0,0,.35))] data-[state=idle]:hover:bg-[light-dark(#f0f2f5,#1b1e23)] data-[state=idle]:active:[transform:scale(.97)] data-[state=loading]:w-[52px] data-[state=loading]:cursor-default data-[state=done]:w-[52px] data-[state=done]:cursor-default data-[state=done]:bg-[#22c55e] data-[state=done]:text-white data-[state=done]:[box-shadow:inset_0_0_0_1px_#22c55e,0_8px_22px_-6px_rgba(34,197,94,.55)] focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-[#f97316] motion-safe:[transition:width_.55s_cubic-bezier(.65,0,.35,1),background-color_.35s_ease,box-shadow_.45s_ease,transform_.2s_cubic-bezier(.16,1,.3,1)]"41data-state={state}42aria-busy={state === "loading"}43aria-label={state === "loading" ? "Completing" : state === "done" ? "Completed" : undefined}44onClick={() => {45if (state === "idle") setState("loading");46}}47>48<span className="[grid-area:1/1] justify-self-center whitespace-nowrap px-[28px] group-data-[state=loading]:opacity-0 group-data-[state=loading]:[filter:blur(2px)] group-data-[state=loading]:[transform:scale(.9)] group-data-[state=done]:opacity-0 group-data-[state=done]:[filter:blur(2px)] group-data-[state=done]:[transform:scale(.9)] motion-safe:[transition:opacity_.25s_ease_.2s,filter_.25s_ease_.2s,transform_.45s_cubic-bezier(.16,1,.3,1)_.15s] motion-safe:group-data-[state=loading]:[transition-duration:.18s] motion-safe:group-data-[state=loading]:[transition-delay:0s] motion-safe:group-data-[state=done]:[transition-duration:.18s] motion-safe:group-data-[state=done]:[transition-delay:0s]" ref={labelRef}>49Complete50</span>51<span className="[grid-area:1/1] block size-[20px] justify-self-center opacity-0 [transform:scale(.5)] group-data-[state=loading]:opacity-100 group-data-[state=loading]:[transform:scale(1)] motion-safe:[transition:opacity_.2s_ease,transform_.3s_cubic-bezier(.16,1,.3,1)] motion-safe:group-data-[state=loading]:[transition-delay:.3s]" aria-hidden="true">52<svg className="block size-full animate-spin [animation-duration:.9s]" viewBox="0 0 20 20" fill="none" stroke="currentColor" strokeWidth="2.4">53<circle cx="10" cy="10" r="8.8" strokeOpacity="0.2" />54<circle cx="10" cy="10" r="8.8" strokeLinecap="round" strokeDasharray="15.48 55.29" />55</svg>56</span>57<svg58className="[grid-area:1/1] block size-[22px] justify-self-center opacity-0 [stroke-dasharray:1] [stroke-dashoffset:1] group-data-[state=done]:opacity-100 group-data-[state=done]:[stroke-dashoffset:0] motion-safe:[transition:opacity_.2s_ease,stroke-dashoffset_.4s_cubic-bezier(.65,0,.35,1)] motion-safe:group-data-[state=done]:[transition-delay:.12s]"59viewBox="0 0 24 24"60fill="none"61stroke="currentColor"62strokeWidth="2.6"63strokeLinecap="round"64strokeLinejoin="round"65aria-hidden="true"66>67<path d="M5 12.5l4.5 4.5L19 7.5" pathLength={1} />68</svg>69</button>70);71}