{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "button-stateful",
  "type": "registry:component",
  "title": "Button Stateful",
  "description": "Morphs from idle to loading to success and back. The width animates to fit each label and the check draws itself. Driven by a promise returned from onClick or by a status prop.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "buttons",
    "button",
    "loading",
    "success",
    "async",
    "morph",
    "state"
  ],
  "docs": "https://hyperblur.dev/effects/button-stateful",
  "files": [
    {
      "path": "registry/hyperblur/button-stateful.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/button-stateful.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type ButtonStatefulStatus = \"idle\" | \"loading\" | \"success\";\n\nexport interface ButtonStatefulProps\n  extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"color\" | \"onClick\"> {\n  onClick?: (event: React.MouseEvent<HTMLButtonElement>) => unknown;\n  status?: ButtonStatefulStatus;\n  loadingLabel?: React.ReactNode;\n  successLabel?: React.ReactNode;\n  duration?: number;\n  resetAfter?: number;\n  variant?: \"solid\" | \"outline\";\n  accent?: string;\n  successAccent?: string;\n  size?: \"sm\" | \"md\" | \"lg\";\n}\n\nconst css = `\n.hb-qa7 {\n  --hb-qa7-c: #2f7bff;\n  --hb-qa7-ok: #22c55e;\n  --hb-qa7-bg: var(--hb-qa7-c);\n  --hb-qa7-fg: #ffffff;\n  --hb-qa7-ring: transparent;\n  position: relative;\n  display: inline-flex;\n  align-items: center;\n  justify-content: center;\n  box-sizing: border-box;\n  margin: 0;\n  padding: 0.8em 1.6em;\n  font: inherit;\n  font-size: 15px;\n  font-weight: 600;\n  line-height: 1.2;\n  color: var(--hb-qa7-fg);\n  background: var(--hb-qa7-bg);\n  border: 0;\n  border-radius: 12px;\n  box-shadow: inset 0 0 0 1px var(--hb-qa7-ring);\n  cursor: pointer;\n  overflow: hidden;\n  user-select: none;\n  -webkit-tap-highlight-color: transparent;\n  touch-action: manipulation;\n  transition: width 0.45s cubic-bezier(0.22, 1, 0.36, 1), background-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease, transform 0.18s cubic-bezier(0.2, 0.8, 0.2, 1);\n}\n.hb-qa7[data-size=\"sm\"] { font-size: 13px; }\n.hb-qa7[data-size=\"lg\"] { font-size: 17px; }\n.hb-qa7[data-variant=\"outline\"] {\n  --hb-qa7-bg: transparent;\n  --hb-qa7-fg: #ededed;\n  --hb-qa7-ring: #333333;\n}\n.hb-qa7[data-status=\"loading\"] { cursor: progress; }\n.hb-qa7[data-status=\"success\"] {\n  --hb-qa7-bg: var(--hb-qa7-ok);\n  --hb-qa7-fg: #04140a;\n  --hb-qa7-ring: transparent;\n  cursor: default;\n}\n.hb-qa7[data-variant=\"outline\"][data-status=\"success\"] {\n  --hb-qa7-bg: color-mix(in srgb, var(--hb-qa7-ok) 14%, transparent);\n  --hb-qa7-fg: var(--hb-qa7-ok);\n  --hb-qa7-ring: color-mix(in srgb, var(--hb-qa7-ok) 55%, transparent);\n}\n.hb-qa7-c {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  opacity: 0;\n  transform: translateY(0.7em) scale(0.94);\n  filter: blur(3px);\n  pointer-events: none;\n  transition: opacity 0.28s ease, transform 0.4s cubic-bezier(0.22, 1, 0.36, 1), filter 0.28s ease;\n}\n.hb-qa7-c[data-first=\"true\"] { position: relative; inset: auto; }\n.hb-qa7-c[data-on=\"true\"] { opacity: 1; transform: none; filter: none; }\n.hb-qa7-c[data-on=\"false\"][data-after=\"true\"] { transform: translateY(-0.7em) scale(0.94); }\n.hb-qa7-l { display: inline-flex; align-items: center; gap: 0.55em; white-space: nowrap; }\n.hb-qa7-sp {\n  width: 1em;\n  height: 1em;\n  flex: none;\n  border: 2px solid currentColor;\n  border-right-color: transparent;\n  border-radius: 50%;\n  animation: hb-qa7-spin 0.7s linear infinite;\n}\n.hb-qa7-ck {\n  width: 1.1em;\n  height: 1.1em;\n  flex: none;\n  fill: none;\n  stroke: currentColor;\n  stroke-width: 2.6;\n  stroke-linecap: round;\n  stroke-linejoin: round;\n  stroke-dasharray: 24;\n  stroke-dashoffset: 24;\n}\n.hb-qa7-c[data-on=\"true\"] .hb-qa7-ck { animation: hb-qa7-draw 0.45s cubic-bezier(0.65, 0, 0.35, 1) 0.12s forwards; }\n.hb-qa7:hover:not(:disabled) { filter: brightness(1.08); }\n.hb-qa7:active:not(:disabled) { transform: scale(0.97); }\n.hb-qa7:focus-visible { outline: 2px solid var(--hb-qa7-c); outline-offset: 3px; }\n.hb-qa7:disabled:not([data-status=\"loading\"]):not([data-status=\"success\"]) { opacity: 0.45; cursor: not-allowed; }\n@keyframes hb-qa7-spin { to { transform: rotate(360deg); } }\n@keyframes hb-qa7-draw { to { stroke-dashoffset: 0; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-qa7, .hb-qa7-c { transition-duration: 0.01s; }\n  .hb-qa7-c { filter: none; transform: none; }\n  .hb-qa7-c[data-on=\"true\"] .hb-qa7-ck { animation: none; stroke-dashoffset: 0; }\n  .hb-qa7:active:not(:disabled) { transform: none; }\n  .hb-qa7-sp { animation-duration: 1.6s; }\n}\n`;\n\nfunction isThenable(value: unknown): value is PromiseLike<unknown> {\n  return typeof value === \"object\" && value !== null && typeof (value as { then?: unknown }).then === \"function\";\n}\n\nexport function ButtonStateful({\n  children = \"Save changes\",\n  loadingLabel = \"Saving\",\n  successLabel = \"Saved\",\n  status,\n  duration = 1500,\n  resetAfter = 1800,\n  onClick,\n  variant = \"solid\",\n  accent = \"#2f7bff\",\n  successAccent = \"#22c55e\",\n  size = \"md\",\n  disabled = false,\n  type = \"button\",\n  className = \"\",\n  style,\n  ...rest\n}: ButtonStatefulProps) {\n  const [inner, setInner] = React.useState<ButtonStatefulStatus>(\"idle\");\n  const [width, setWidth] = React.useState<Record<ButtonStatefulStatus, number> | null>(null);\n  const refs = React.useRef<Record<ButtonStatefulStatus, HTMLSpanElement | null>>({\n    idle: null,\n    loading: null,\n    success: null,\n  });\n  const alive = React.useRef(true);\n  const timers = React.useRef<number[]>([]);\n  const controlled = status !== undefined;\n  const current: ButtonStatefulStatus = controlled ? status : inner;\n\n  React.useEffect(() => {\n    alive.current = true;\n    const pending = timers.current;\n    return () => {\n      alive.current = false;\n      pending.forEach((t) => window.clearTimeout(t));\n    };\n  }, []);\n\n  React.useLayoutEffect(() => {\n    const nodes = refs.current;\n    function measure() {\n      const next = {\n        idle: nodes.idle?.offsetWidth ?? 0,\n        loading: nodes.loading?.offsetWidth ?? 0,\n        success: nodes.success?.offsetWidth ?? 0,\n      };\n      setWidth((prev) =>\n        prev && prev.idle === next.idle && prev.loading === next.loading && prev.success === next.success ? prev : next,\n      );\n    }\n    measure();\n    const ro = new ResizeObserver(measure);\n    (Object.values(nodes) as (HTMLSpanElement | null)[]).forEach((n) => n && ro.observe(n));\n    return () => ro.disconnect();\n  }, [children, loadingLabel, successLabel, size]);\n\n  function later(fn: () => void, ms: number) {\n    timers.current.push(window.setTimeout(fn, ms));\n  }\n\n  function succeed() {\n    if (!alive.current) return;\n    setInner(\"success\");\n    later(() => alive.current && setInner(\"idle\"), resetAfter);\n  }\n\n  function handleClick(e: React.MouseEvent<HTMLButtonElement>) {\n    if (current !== \"idle\" || disabled) return;\n    const result = onClick?.(e);\n    if (controlled) return;\n    setInner(\"loading\");\n    if (isThenable(result)) {\n      result.then(succeed, () => alive.current && setInner(\"idle\"));\n    } else {\n      later(succeed, Math.max(0, duration));\n    }\n  }\n\n  const vars = {\n    \"--hb-qa7-c\": accent,\n    \"--hb-qa7-ok\": successAccent,\n    ...(width ? { width: `calc(${width[current]}px + 3.2em)` } : null),\n    ...style,\n  } as React.CSSProperties;\n\n  const order: ButtonStatefulStatus[] = [\"idle\", \"loading\", \"success\"];\n  const labels: Record<ButtonStatefulStatus, React.ReactNode> = {\n    idle: children,\n    loading: (\n      <>\n        <span className=\"hb-qa7-sp\" aria-hidden=\"true\" />\n        {loadingLabel}\n      </>\n    ),\n    success: (\n      <>\n        <svg className=\"hb-qa7-ck\" viewBox=\"0 0 24 24\" aria-hidden=\"true\" focusable=\"false\">\n          <path d=\"M5 12.5l4.5 4.5L19 7.5\" />\n        </svg>\n        {successLabel}\n      </>\n    ),\n  };\n\n  return (\n    <>\n      <style href=\"hb-button-stateful\" precedence=\"default\">{css}</style>\n      <button\n        type={type}\n        className={`hb-qa7 ${className}`}\n        style={vars}\n        data-variant={variant}\n        data-size={size}\n        data-status={current}\n        disabled={disabled || current !== \"idle\"}\n        aria-busy={current === \"loading\" || undefined}\n        aria-live=\"polite\"\n        onClick={handleClick}\n        {...rest}\n      >\n        {order.map((key) => (\n          <span\n            key={key}\n            className=\"hb-qa7-c\"\n            data-first={key === \"idle\" ? \"true\" : undefined}\n            data-on={current === key ? \"true\" : \"false\"}\n            data-after={order.indexOf(key) < order.indexOf(current) ? \"true\" : undefined}\n            aria-hidden={current === key ? undefined : true}\n          >\n            <span\n              className=\"hb-qa7-l\"\n              ref={(node) => {\n                refs.current[key] = node;\n              }}\n            >\n              {labels[key]}\n            </span>\n          </span>\n        ))}\n      </button>\n    </>\n  );\n}\n"
    }
  ]
}