useActionStatus
One async action as a FancyButton status: pending while it runs, done briefly once it lands, then idle.
Call run with a promise, or with a function that returns one. status is pending while it runs, done for doneMs (1500 by default) once it resolves, then idle again. A rejection goes straight back to idle and is thrown again, so showing the error stays with the caller. A newer run supersedes an older one: the older result never changes the status. Hand status to @khaeli/fancy-button, which draws the loader and the done label from it.
Save button
Status: idle
"use client"
import { FancyButton } from "@/components/ui/fancy-button"
import { useActionStatus } from "@/hooks/use-action-status"
// Stands in for a request.
const request = () => new Promise((resolve) => setTimeout(resolve, 1200))
export default function UseActionStatusSaveButton() {
const save = useActionStatus()
return (
<div className="flex flex-col items-center gap-3">
<FancyButton variant="primary" status={save.status} onClick={() => save.run(request)}>
{save.status === "pending" ? "Saving" : "Save changes"}
</FancyButton>
<p className="text-sm text-muted-foreground">
Status: <code className="font-mono text-foreground">{save.status}</code>
</p>
</div>
)
}
A longer done state
"use client"
import { FancyButton } from "@/components/ui/fancy-button"
import { useActionStatus } from "@/hooks/use-action-status"
// Stands in for a request.
const request = () => new Promise((resolve) => setTimeout(resolve, 1200))
export default function UseActionStatusALongerDoneState() {
// Done shows for four seconds before the button goes back to idle.
const publish = useActionStatus({ doneMs: 4000 })
return (
<FancyButton status={publish.status} doneLabel="Published" onClick={() => publish.run(request)}>
{publish.status === "pending" ? "Publishing" : "Publish release"}
</FancyButton>
)
}
When the action fails
"use client"
import { useRef, useState } from "react"
import { FancyButton } from "@/components/ui/fancy-button"
import { useActionStatus } from "@/hooks/use-action-status"
export default function UseActionStatusWhenTheActionFails() {
const send = useActionStatus()
const [failed, setFailed] = useState(false)
const attempts = useRef(0)
// Stands in for a request that fails the first time and works on the retry.
const request = () =>
new Promise<void>((resolve, reject) => {
const attempt = ++attempts.current
setTimeout(() => (attempt % 2 === 1 ? reject(new Error("offline")) : resolve()), 1200)
})
async function onSend() {
setFailed(false)
try {
await send.run(request)
} catch {
// The hook is back at idle and has thrown the error again: showing it is the caller's job.
setFailed(true)
}
}
return (
<div className="flex flex-col items-center gap-3">
<FancyButton variant="primary" status={send.status} doneLabel="Sent" onClick={onSend}>
{send.status === "pending" ? "Sending" : failed ? "Try again" : "Send invoice"}
</FancyButton>
<p role="alert" className="min-h-5 text-sm text-destructive-ink">
{failed ? "The invoice was not sent. Check the connection and try again." : null}
</p>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/use-action-statusbunx --bun shadcn@latest add @khaeli/use-action-statusInstalls with
Nothing else. This item needs no other Registry item and no npm package.
Keyboard
EnterSpace- Start the action. While the button is pending or shows its done label, a press does nothing, so an action is never started twice.
Accessible name
- The hook renders nothing. The button that takes its
statuskeeps the label you give it, so change the label while the action runs (Saving) and @khaeli/fancy-button announces its done label once. - A failure is not a status. Catch what
runthrows and say what went wrong in an element withrole="alert", next to the button.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/use-action-status.json