Stateful Button

A button that moves from loading to success or error as its width morphs.

Preview

"use client";

import { ArrowRight } from "lucide-react";
import { useState } from "react";
import { type ButtonState, StatefulButton } from "@/components/motion/button";

export function ButtonStatefulPreview() {
  const [okState, setOkState] = useState<ButtonState>("idle");
  const [errState, setErrState] = useState<ButtonState>("idle");

  const run = (target: "ok" | "err") => {
    const setter = target === "ok" ? setOkState : setErrState;
    setter("loading");
    setTimeout(() => {
      setter(target === "ok" ? "success" : "error");
      setTimeout(() => setter("idle"), 1800);
    }, 1400);
  };

  return (
    <div className="flex flex-col items-center justify-center gap-3">
      <StatefulButton
        state={okState}
        variant="primary"
        size="md"
        onClick={() => run("ok")}
        loadingText="Saving"
        successText="Saved"
        icon={<ArrowRight className="h-4 w-4" />}
      >
        Save changes
      </StatefulButton>
      <StatefulButton
        state={errState}
        variant="secondary"
        size="md"
        onClick={() => run("err")}
        loadingText="Submitting"
        errorText="Failed"
      >
        Submit
      </StatefulButton>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/button-stateful

API Reference

className?
string

Classes merged onto the root element.

Default—
labels?
Partial<{ loading: string; success: string; error: string; }>

Override any built-in string.

Default—
size?
"sm" | "md" | "lg" | "icon"

Height and padding; "icon" is a square icon-only button. Default: "md"

Default—
variant?
"outline" | "ghost" | "primary" | "secondary"

Visual style: filled, card, ghost or outline. Default: "primary"

Default—
icon?
ReactNode

Trailing icon shown only while idle.

Default—
pressScale?
number

Scale while pressed. Default 0.97: subtle, so the press reads as feedback, not a squash.

Default—
ripple?
boolean

Spawn a Material-style ripple from the press point. Off by default.

Default—
sound?
boolean

Play a soft press sound. Off by default; a `SoundProvider` can mute it app-wide.

Default—
state?
"loading" | "error" | "success" | "idle"

Which state to show. Loading also disables the button. Default: "idle"

Defaultidle
loadingText?
ReactNode

Text shown while loading. Default: "Loading"

Default—
successText?
ReactNode

Text shown on success. Default: "Done"

Default—
errorText?
ReactNode

Text shown on error. Default: "Try again"

Default—
Also on React Native. Spring press, ripple, icons and a stateful variant.

More in Button

Updated