{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "stateful-button",
  "title": "Stateful Button",
  "description": "Communicate async loading, success, and error states without leaving the button.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "files": [
    {
      "path": "src/registry/components/stateful-button/stateful-button.tsx",
      "content": "\"use client\";\n\nimport { CheckIcon, LoaderCircleIcon, XIcon } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { useEffect, useRef, useState } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nconst FEEDBACK_DURATION = 2000;\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\n\ntype StatefulButtonStatus = \"idle\" | \"loading\" | \"success\" | \"error\";\n\ntype StatefulButtonLabels = Partial<\n\tRecord<Exclude<StatefulButtonStatus, \"idle\">, string>\n>;\n\ninterface StatefulButtonProps\n\textends Omit<React.ComponentProps<\"button\">, \"children\" | \"onClick\"> {\n\tchildren: string;\n\tlabels?: StatefulButtonLabels;\n\tonClick?: (\n\t\tevent: React.MouseEvent<HTMLButtonElement>,\n\t) => void | Promise<void>;\n}\n\nconst defaultLabels = {\n\tloading: \"Working\",\n\tsuccess: \"Done\",\n\terror: \"Try again\",\n} satisfies Record<Exclude<StatefulButtonStatus, \"idle\">, string>;\n\nfunction AnimatedChars({ text }: { text: string }) {\n\treturn (\n\t\t<AnimatePresence mode=\"popLayout\" initial={false}>\n\t\t\t{Array.from(text, (char, offset) => (\n\t\t\t\t<motion.span\n\t\t\t\t\tkey={`${char}-${offset}`}\n\t\t\t\t\tlayout\n\t\t\t\t\tinitial={{ opacity: 0, scale: 0, filter: \"blur(4px)\" }}\n\t\t\t\t\tanimate={{ opacity: 1, scale: 1, filter: \"blur(0px)\" }}\n\t\t\t\t\texit={{ opacity: 0, scale: 0, filter: \"blur(4px)\" }}\n\t\t\t\t\ttransition={{\n\t\t\t\t\t\ttype: \"spring\",\n\t\t\t\t\t\tstiffness: 500,\n\t\t\t\t\t\tdamping: 30,\n\t\t\t\t\t\tmass: 1,\n\t\t\t\t\t}}\n\t\t\t\t\tclassName=\"inline-block\"\n\t\t\t\t>\n\t\t\t\t\t{char === \" \" ? \"\\u00A0\" : char}\n\t\t\t\t</motion.span>\n\t\t\t))}\n\t\t</AnimatePresence>\n\t);\n}\n\nfunction StatefulButton({\n\tchildren,\n\tclassName,\n\tdisabled,\n\tlabels,\n\tonClick,\n\t...props\n}: StatefulButtonProps) {\n\tconst [status, setStatus] = useState<StatefulButtonStatus>(\"idle\");\n\tconst isRunning = useRef(false);\n\tconst resetTimer = useRef<ReturnType<typeof setTimeout>>(undefined);\n\tconst resolvedLabels = { idle: children, ...defaultLabels, ...labels };\n\tconst label = resolvedLabels[status];\n\tconst isPending = status !== \"idle\";\n\n\tuseEffect(\n\t\t() => () => {\n\t\t\tif (resetTimer.current) clearTimeout(resetTimer.current);\n\t\t},\n\t\t[],\n\t);\n\n\tasync function handleClick(event: React.MouseEvent<HTMLButtonElement>) {\n\t\tif (isRunning.current) return;\n\n\t\tisRunning.current = true;\n\t\tsetStatus(\"loading\");\n\n\t\ttry {\n\t\t\tawait onClick?.(event);\n\t\t\tsetStatus(\"success\");\n\t\t} catch {\n\t\t\tsetStatus(\"error\");\n\t\t}\n\n\t\tresetTimer.current = setTimeout(() => {\n\t\t\tisRunning.current = false;\n\t\t\tsetStatus(\"idle\");\n\t\t}, FEEDBACK_DURATION);\n\t}\n\n\treturn (\n\t\t<MotionConfig reducedMotion=\"user\">\n\t\t\t<div className=\"relative inline-flex font-sans\">\n\t\t\t\t<button\n\t\t\t\t\tdata-slot=\"stateful-button\"\n\t\t\t\t\tdata-status={status}\n\t\t\t\t\ttype=\"button\"\n\t\t\t\t\taria-busy={status === \"loading\"}\n\t\t\t\t\tdisabled={disabled || isPending}\n\t\t\t\t\tonClick={handleClick}\n\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\"relative inline-flex h-12 min-w-36 select-none items-center justify-center rounded-full px-8 font-medium text-base outline-none duration-[160ms] ease-[cubic-bezier(0.23,1,0.32,1)] focus-visible:ring-3 focus-visible:ring-ring/50 active:not-disabled:scale-[0.97] motion-reduce:transform-none motion-reduce:transition-none\",\n\t\t\t\t\t\tstatus === \"idle\" &&\n\t\t\t\t\t\t\t\"bg-linear-to-b from-blue-400 to-blue-500 text-primary-foreground hover:from-blue-500 hover:to-blue-600 hover:shadow-sm\",\n\t\t\t\t\t\tstatus !== \"idle\" &&\n\t\t\t\t\t\t\t\"cursor-not-allowed bg-muted/60 text-muted-foreground\",\n\t\t\t\t\t\tclassName,\n\t\t\t\t\t)}\n\t\t\t\t\t{...props}\n\t\t\t\t>\n\t\t\t\t\t<span className=\"flex items-center justify-center\">\n\t\t\t\t\t\t<AnimatedChars text={label} />\n\t\t\t\t\t</span>\n\t\t\t\t</button>\n\n\t\t\t\t<AnimatePresence initial={false}>\n\t\t\t\t\t{status !== \"idle\" && (\n\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\tinitial={{ opacity: 0, scale: 0.94, x: -4 }}\n\t\t\t\t\t\t\tanimate={{ opacity: 1, scale: 1, x: 0 }}\n\t\t\t\t\t\t\texit={{ opacity: 0, scale: 0.94, x: -4 }}\n\t\t\t\t\t\t\ttransition={{ duration: 0.18, ease: EASE_OUT }}\n\t\t\t\t\t\t\taria-hidden=\"true\"\n\t\t\t\t\t\t\tclassName={cn(\n\t\t\t\t\t\t\t\t\"pointer-events-none absolute -top-1 -right-1 flex size-6 items-center justify-center overflow-hidden rounded-full\",\n\t\t\t\t\t\t\t\tstatus === \"loading\" && \"bg-muted text-muted-foreground\",\n\t\t\t\t\t\t\t\tstatus === \"success\" && \"bg-success text-white\",\n\t\t\t\t\t\t\t\tstatus === \"error\" && \"bg-destructive text-white\",\n\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t<AnimatePresence initial={false} mode=\"wait\">\n\t\t\t\t\t\t\t\t<motion.span\n\t\t\t\t\t\t\t\t\tkey={status}\n\t\t\t\t\t\t\t\t\tinitial={{ opacity: 0, scale: 0.94 }}\n\t\t\t\t\t\t\t\t\tanimate={{ opacity: 1, scale: 1 }}\n\t\t\t\t\t\t\t\t\texit={{ opacity: 0, scale: 0.94 }}\n\t\t\t\t\t\t\t\t\ttransition={{ duration: 0.12, ease: EASE_OUT }}\n\t\t\t\t\t\t\t\t\tclassName=\"absolute inset-0 flex items-center justify-center [&_svg]:size-3.5\"\n\t\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t\t{status === \"loading\" && (\n\t\t\t\t\t\t\t\t\t\t<LoaderCircleIcon className=\"animate-spin motion-reduce:animate-none\" />\n\t\t\t\t\t\t\t\t\t)}\n\t\t\t\t\t\t\t\t\t{status === \"success\" && <CheckIcon />}\n\t\t\t\t\t\t\t\t\t{status === \"error\" && <XIcon />}\n\t\t\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t\t\t</AnimatePresence>\n\t\t\t\t\t\t</motion.span>\n\t\t\t\t\t)}\n\t\t\t\t</AnimatePresence>\n\n\t\t\t\t<span className=\"sr-only\" role=\"status\" aria-live=\"polite\">\n\t\t\t\t\t{status === \"idle\" ? \"\" : label}\n\t\t\t\t</span>\n\t\t\t</div>\n\t\t</MotionConfig>\n\t);\n}\n\nexport {\n\tStatefulButton,\n\ttype StatefulButtonLabels,\n\ttype StatefulButtonProps,\n\ttype StatefulButtonStatus,\n};\n\nexport default StatefulButton;\n",
      "type": "registry:component",
      "target": "@components/stateful-button.tsx"
    }
  ],
  "meta": {
    "createdAt": "2026-07-19",
    "image": "https://cdn.tentui.com/images/components/stateful-button",
    "video": "https://cdn.tentui.com/videos/components/stateful-button"
  },
  "docs": "https://tentui.com/components/stateful-button",
  "categories": [
    "buttons"
  ],
  "type": "registry:component"
}