{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "button-submit-morph",
  "type": "registry:component",
  "title": "Button Submit Morph",
  "description": "A submit button that collapses into a circle with an orbiting spinner, then expands into a green success pill or a red error pill that shakes. It follows the promise returned by onSubmit.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "buttons",
    "button",
    "submit",
    "loading",
    "success",
    "error",
    "morph"
  ],
  "docs": "https://hyperblur.dev/effects/button-submit-morph",
  "files": [
    {
      "path": "registry/hyperblur/button-submit-morph.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/button-submit-morph.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport type ButtonSubmitMorphStatus = \"idle\" | \"loading\" | \"success\" | \"error\";\n\nexport interface ButtonSubmitMorphProps\n  extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"onSubmit\" | \"children\"> {\n  children?: React.ReactNode;\n  successLabel?: React.ReactNode;\n  errorLabel?: React.ReactNode;\n  status?: ButtonSubmitMorphStatus;\n  onSubmit?: () => unknown;\n  outcome?: \"success\" | \"error\";\n  duration?: number;\n  resetAfter?: number;\n  accent?: string;\n  successColor?: string;\n  errorColor?: string;\n}\n\nconst css = `\n.hb-xb4 {\n  --hb-xb4-c: #2f7bff;\n  --hb-xb4-ok: #1fb866;\n  --hb-xb4-err: #f04452;\n  --hb-xb4-bg: var(--hb-xb4-c);\n  --hb-xb4-h: 46px;\n  position: relative;\n  display: inline-flex;\n  align-items: center;\n  justify-content: center;\n  box-sizing: border-box;\n  height: var(--hb-xb4-h);\n  min-width: var(--hb-xb4-h);\n  margin: 0;\n  padding: 0;\n  border: 0;\n  border-radius: 12px;\n  font: inherit;\n  font-size: 15px;\n  font-weight: 600;\n  color: #fff;\n  background: linear-gradient(180deg, color-mix(in srgb, var(--hb-xb4-bg) 88%, #fff) 0%, var(--hb-xb4-bg) 60%);\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), inset 0 0 0 1px color-mix(in srgb, var(--hb-xb4-bg) 70%, #000), 0 8px 20px -10px var(--hb-xb4-bg);\n  cursor: pointer;\n  overflow: visible;\n  -webkit-tap-highlight-color: transparent;\n  transition: width 0.55s cubic-bezier(0.7, 0, 0.2, 1), border-radius 0.45s cubic-bezier(0.7, 0, 0.2, 1), background 0.35s ease, box-shadow 0.35s ease, transform 0.18s ease;\n}\n.hb-xb4 *, .hb-xb4 *::before, .hb-xb4 *::after { box-sizing: border-box; }\n.hb-xb4[data-state=\"loading\"] { border-radius: 999px; cursor: progress; --hb-xb4-bg: #1b1b1b; box-shadow: inset 0 0 0 1px #2a2a2a, 0 8px 20px -12px rgba(0, 0, 0, 0.9); }\n.hb-xb4[data-state=\"success\"] { --hb-xb4-bg: var(--hb-xb4-ok); border-radius: 999px; cursor: default; }\n.hb-xb4[data-state=\"error\"] { --hb-xb4-bg: var(--hb-xb4-err); animation: hb-xb4-shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97); }\n.hb-xb4:hover[data-state=\"idle\"]:not(:disabled) { filter: brightness(1.07); }\n.hb-xb4:active[data-state=\"idle\"]:not(:disabled) { transform: scale(0.97); }\n.hb-xb4:focus-visible { outline: 2px solid var(--hb-xb4-c); outline-offset: 3px; }\n.hb-xb4:disabled[data-state=\"idle\"] { opacity: 0.45; cursor: not-allowed; }\n.hb-xb4-layer {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  gap: 8px;\n  white-space: nowrap;\n  opacity: 0;\n  transform: scale(0.85);\n  filter: blur(4px);\n  pointer-events: none;\n  transition: opacity 0.25s ease, transform 0.45s cubic-bezier(0.34, 1.56, 0.64, 1), filter 0.25s ease;\n}\n.hb-xb4[data-state=\"idle\"] .hb-xb4-layer[data-for=\"idle\"],\n.hb-xb4[data-state=\"loading\"] .hb-xb4-layer[data-for=\"loading\"],\n.hb-xb4[data-state=\"success\"] .hb-xb4-layer[data-for=\"success\"],\n.hb-xb4[data-state=\"error\"] .hb-xb4-layer[data-for=\"error\"] { opacity: 1; transform: none; filter: none; transition-delay: 0.18s; }\n.hb-xb4[data-state=\"idle\"] .hb-xb4-layer[data-for=\"idle\"] { transition-delay: 0.12s; }\n.hb-xb4-layer svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }\n.hb-xb4-spin { width: 24px !important; height: 24px !important; animation: hb-xb4-rot 0.9s cubic-bezier(0.5, 0.1, 0.5, 0.9) infinite; }\n.hb-xb4-spin circle:first-child { stroke: #333; stroke-width: 2.6; }\n.hb-xb4-spin circle:last-child { stroke: var(--hb-xb4-c); stroke-width: 2.6; stroke-dasharray: 18 60; animation: hb-xb4-arc 1.4s ease-in-out infinite; }\n.hb-xb4-draw { stroke-dasharray: 24; stroke-dashoffset: 24; }\n.hb-xb4[data-state=\"success\"] .hb-xb4-draw, .hb-xb4[data-state=\"error\"] .hb-xb4-draw { animation: hb-xb4-draw 0.45s cubic-bezier(0.65, 0, 0.35, 1) 0.3s forwards; }\n.hb-xb4-burst { position: absolute; inset: -1px; border-radius: inherit; border: 2px solid var(--hb-xb4-ok); opacity: 0; pointer-events: none; }\n.hb-xb4[data-state=\"success\"] .hb-xb4-burst { animation: hb-xb4-burst 0.9s cubic-bezier(0.22, 1, 0.36, 1) 0.35s; }\n.hb-xb4-ghost { position: absolute; left: 0; top: 0; visibility: hidden; pointer-events: none; display: flex; flex-direction: column; align-items: flex-start; }\n.hb-xb4-ghost > span { display: inline-flex; gap: 8px; align-items: center; white-space: nowrap; padding: 0 24px; font-size: 15px; font-weight: 600; }\n.hb-xb4-ghost svg { width: 18px; height: 18px; }\n@keyframes hb-xb4-rot { to { transform: rotate(360deg); } }\n@keyframes hb-xb4-arc { 0%, 100% { stroke-dasharray: 10 60; } 50% { stroke-dasharray: 36 60; } }\n@keyframes hb-xb4-draw { to { stroke-dashoffset: 0; } }\n@keyframes hb-xb4-burst { 0% { opacity: 0.8; transform: scale(1); } 100% { opacity: 0; transform: scale(1.25, 1.6); } }\n@keyframes hb-xb4-shake { 10%, 90% { translate: -1px 0; } 20%, 80% { translate: 3px 0; } 30%, 50%, 70% { translate: -5px 0; } 40%, 60% { translate: 5px 0; } }\n@media (prefers-reduced-motion: reduce) {\n  .hb-xb4, .hb-xb4-layer { transition-duration: 0.01s !important; transition-delay: 0s !important; }\n  .hb-xb4-layer { filter: none; transform: none; }\n  .hb-xb4[data-state=\"error\"], .hb-xb4-burst { animation: none !important; }\n  .hb-xb4[data-state=\"success\"] .hb-xb4-draw, .hb-xb4[data-state=\"error\"] .hb-xb4-draw { animation: none; stroke-dashoffset: 0; }\n  .hb-xb4-spin, .hb-xb4-spin circle:last-child { animation-duration: 2.4s; }\n}\n`;\n\nfunction isThenable(v: unknown): v is PromiseLike<unknown> {\n  return typeof v === \"object\" && v !== null && typeof (v as { then?: unknown }).then === \"function\";\n}\n\nconst checkIcon = (\n  <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\">\n    <path className=\"hb-xb4-draw\" d=\"M5 12.5l4.5 4.5L19 7.5\" />\n  </svg>\n);\nconst errIcon = (\n  <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\">\n    <path className=\"hb-xb4-draw\" d=\"M12 7v6M12 17h.01\" />\n  </svg>\n);\n\nexport function ButtonSubmitMorph({\n  children = \"Create account\",\n  successLabel = \"Account created\",\n  errorLabel = \"Try again\",\n  status,\n  onSubmit,\n  outcome = \"success\",\n  duration = 1600,\n  resetAfter = 2200,\n  accent = \"#2f7bff\",\n  successColor = \"#1fb866\",\n  errorColor = \"#f04452\",\n  disabled = false,\n  type = \"button\",\n  className = \"\",\n  style,\n  ...rest\n}: ButtonSubmitMorphProps) {\n  const [inner, setInner] = React.useState<ButtonSubmitMorphStatus>(\"idle\");\n  const [widths, setWidths] = React.useState<Record<\"idle\" | \"success\" | \"error\", number> | null>(null);\n  const ghost = React.useRef<HTMLSpanElement>(null);\n  const timers = React.useRef<number[]>([]);\n  const alive = React.useRef(true);\n  const current = status ?? inner;\n\n  React.useEffect(() => {\n    alive.current = true;\n    const list = timers.current;\n    const el = ghost.current;\n    let ro: ResizeObserver | null = null;\n    if (el) {\n      const read = () => {\n        const [a, b, c] = Array.from(el.children) as HTMLElement[];\n        setWidths({ idle: a.offsetWidth, success: b.offsetWidth, error: c.offsetWidth });\n      };\n      ro = new ResizeObserver(read);\n      ro.observe(el);\n    }\n    return () => {\n      alive.current = false;\n      ro?.disconnect();\n      list.forEach((t) => window.clearTimeout(t));\n    };\n  }, []);\n\n  const finish = (result: \"success\" | \"error\") => {\n    if (!alive.current) return;\n    setInner(result);\n    timers.current.push(window.setTimeout(() => alive.current && setInner(\"idle\"), resetAfter));\n  };\n\n  const handle = () => {\n    if (disabled || current !== \"idle\") return;\n    setInner(\"loading\");\n    const started = performance.now();\n    const settle = (result: \"success\" | \"error\") => {\n      const wait = Math.max(0, 700 - (performance.now() - started));\n      timers.current.push(window.setTimeout(() => finish(result), wait));\n    };\n    let result: unknown;\n    try {\n      result = onSubmit?.();\n    } catch {\n      settle(\"error\");\n      return;\n    }\n    if (isThenable(result)) {\n      result.then(\n        () => settle(\"success\"),\n        () => settle(\"error\"),\n      );\n    } else {\n      timers.current.push(window.setTimeout(() => finish(outcome), duration));\n    }\n  };\n\n  const h = 46;\n  const w = widths ? (current === \"loading\" ? h : widths[current]) : undefined;\n  const live = current === \"loading\" ? \"Submitting\" : current === \"success\" ? successLabel : current === \"error\" ? errorLabel : \"\";\n\n  return (\n    <button\n      type={type}\n      disabled={disabled}\n      aria-busy={current === \"loading\" || undefined}\n      className={`hb-xb4 ${className}`.trim()}\n      data-state={current}\n      style={\n        {\n          \"--hb-xb4-c\": accent,\n          \"--hb-xb4-ok\": successColor,\n          \"--hb-xb4-err\": errorColor,\n          width: w !== undefined ? `${w}px` : undefined,\n          ...style,\n        } as React.CSSProperties\n      }\n      onClick={handle}\n      {...rest}\n    >\n      <style href=\"hb-button-submit-morph\" precedence=\"default\">{css}</style>\n      <span className=\"hb-xb4-ghost\" ref={ghost} aria-hidden=\"true\">\n        <span>{children}</span>\n        <span>\n          <svg viewBox=\"0 0 24 24\" />\n          {successLabel}\n        </span>\n        <span>\n          <svg viewBox=\"0 0 24 24\" />\n          {errorLabel}\n        </span>\n      </span>\n      {widths === null && <span style={{ padding: \"0 24px\", visibility: \"hidden\", whiteSpace: \"nowrap\" }} aria-hidden=\"true\">{children}</span>}\n      <span className=\"hb-xb4-layer\" data-for=\"idle\" aria-hidden={current !== \"idle\"}>{children}</span>\n      <span className=\"hb-xb4-layer\" data-for=\"loading\" aria-hidden=\"true\">\n        <svg className=\"hb-xb4-spin\" viewBox=\"0 0 24 24\">\n          <circle cx=\"12\" cy=\"12\" r=\"9\" />\n          <circle cx=\"12\" cy=\"12\" r=\"9\" />\n        </svg>\n      </span>\n      <span className=\"hb-xb4-layer\" data-for=\"success\" aria-hidden={current !== \"success\"}>\n        {checkIcon}\n        {successLabel}\n      </span>\n      <span className=\"hb-xb4-layer\" data-for=\"error\" aria-hidden={current !== \"error\"}>\n        {errIcon}\n        {errorLabel}\n      </span>\n      <span className=\"hb-xb4-burst\" aria-hidden=\"true\" />\n      <span role=\"status\" aria-live=\"polite\" style={{ position: \"absolute\", width: 1, height: 1, overflow: \"hidden\", clip: \"rect(0 0 0 0)\" }}>\n        {live}\n      </span>\n    </button>\n  );\n}\n"
    }
  ]
}