StatefulButton

Submit button that morphs through saving, saved and failed. It resizes on a transform and swaps its label with a blur. Buttons.

Open playground

About

StatefulButton is a React component in the Buttons category of smoui. It is typed, ships no stylesheet, follows your theme through CSS variables, and honors prefers-reduced-motion. Import it with import { StatefulButton } from "@ashutoshswamy/smoui" and set children, onClick, loadingLabel among its 6 props.

Install

Terminal
npm i @ashutoshswamy/smoui

Usage

TSX
import { StatefulButton } from "@ashutoshswamy/smoui";

<StatefulButton
  className="rounded-full bg-black px-6 py-3 text-white"
  onClick={() => api.save()} // resolve: success, reject: error
>
  Save changes
</StatefulButton>

Props 6

children

Idle label.

ReactNode
onClick

Return a promise. Resolves to success, rejects to error.

() => void | Promise<unknown>
loadingLabel

Label while the promise is pending.

ReactNodedefault "Saving"
successLabel

Label after it resolves.

ReactNodedefault "Saved"
errorLabel

Label after it rejects.

ReactNodedefault "Failed"
resetMs

Time before it returns to idle.

numberdefault 1800