{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fancy-button",
  "title": "Fancy button",
  "description": "The app button on Underlay's control language: basic, primary, neutral and destructive, with pending and done states.",
  "dependencies": [
    "@keyline-icons/react@^2.2.0",
    "@radix-ui/react-slot@^1.1.0"
  ],
  "registryDependencies": [
    "@khaeli/icon-swap",
    "@khaeli/loader",
    "@khaeli/motion",
    "@khaeli/use-merged-ref",
    "@khaeli/utils"
  ],
  "files": [
    {
      "path": "registry/khaeli/ui/fancy-button.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Slot } from \"@radix-ui/react-slot\";\nimport { cn } from \"@/lib/utils\";\nimport { CheckIcon } from \"@keyline-icons/react\";\nimport { useMergedRef } from \"@/hooks/use-merged-ref\";\nimport { useSizeMorph } from \"@/lib/motion\";\nimport { IconSwap } from \"@/components/ui/icon-swap\";\nimport { Loader } from \"@/components/ui/loader\";\n\n/**\n * The library's one button, on the foundation's control language and radius\n * ladder: a 10px rounded rectangle with\n * 14px/500 type on desktop and a 6px icon gap.\n *\n * - `basic` is the white secondary control (`--shadow-secondary`: a hairline\n *   ring).\n * - `primary` is Underlay's flat accent: the primary with an inner hairline,\n *   darkening on hover. `neutral` is the near-black in-context submit\n *   (Reply, Send); `destructive` the solid red. Both carry `--shadow-cta`.\n * - Presses scale to 0.96 over 150ms, and only when the user allows motion;\n *   colour changes take 150ms; disabled buttons never scale and fade to half opacity.\n * - Size: `xsmall` (30px) in page chrome, toolbars and inline actions;\n *   `small` (36px) in dialog, sheet and onboarding footers; `medium` only on\n *   phone full-width actions.\n * - A transparent border keeps an edge in forced-colors mode, which drops\n *   every box-shadow.\n * - `type=\"button\"` by default, so a button inside a form never submits\n *   unless asked to. `iconOnly` squares the capsule for a lone glyph with an\n *   `aria-label`. `asChild` renders the styling onto its child (a Link).\n * - `pending` marks the button's own action as running: a Loader leads the\n *   label (and stands in for every other glyph), or replaces an `iconOnly`\n *   glyph. The button stays focusable as `aria-disabled` and swallows\n *   activation, so keyboard focus never drops to <body> mid-request the way\n *   native `disabled` makes it; the label stays the caller's (\"Saving…\").\n * - `status` drives a whole save from one hook (`useActionStatus`):\n *   \"pending\" is `pending`; \"done\" swaps the Loader for a check and the label\n *   for `doneLabel` (\"Saved\") in place, keeps the button focusable (presses\n *   are swallowed while it shows), and says the result in a polite live\n *   region. Buttons given `status` morph their width between faces\n *   (`useSizeMorph`, which skips under reduced motion).\n */\n\nconst VARIANTS = {\n\tbasic: \"bg-surface-raised text-foreground shadow-secondary hover:bg-surface-raised-hover\",\n\t// `bg-primary` stays as the hook foundation.css's phone chrome rules key on.\n\tprimary: \"bg-primary text-primary-foreground accent-badge hover:bg-primary-hover\",\n\tdestructive: \"bg-destructive-solid text-white shadow-cta hover:bg-destructive-solid-hover\",\n\tneutral: \"bg-foreground text-background shadow-cta hover:bg-foreground/90\",\n} as const;\n\n// Heights follow the reference's 30px control. The icon side gives back 2px\n// so a glyph sits optically centred; text nodes are invisible to CSS, so an\n// icon counts as leading or trailing only beside another element (the same\n// rule foundation.css applies to registry buttons).\nconst SIZES = {\n\txsmall: \"h-[30px] px-[11px] text-sm md:text-base has-[>[data-slot=fancy-button-icon]:first-child:not(:last-child)]:pl-2.5 has-[>[data-slot=fancy-button-icon]:last-child:not(:first-child)]:pr-2.5\",\n\tsmall: \"h-9 px-3.5 text-base has-[>[data-slot=fancy-button-icon]:first-child:not(:last-child)]:pl-3 has-[>[data-slot=fancy-button-icon]:last-child:not(:first-child)]:pr-3\",\n\tmedium: \"h-10 px-4 text-base has-[>[data-slot=fancy-button-icon]:first-child:not(:last-child)]:pl-3.5 has-[>[data-slot=fancy-button-icon]:last-child:not(:first-child)]:pr-3.5\",\n} as const;\n\nconst ICON_ONLY_SIZES = {\n\txsmall: \"size-[30px]\",\n\tsmall: \"size-9\",\n\tmedium: \"size-10\",\n} as const;\n\ntype FancyButtonStatus = \"idle\" | \"pending\" | \"done\";\n\ntype FancyButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> & {\n\tvariant?: keyof typeof VARIANTS;\n\tsize?: keyof typeof SIZES;\n\ticonOnly?: boolean;\n\tasChild?: boolean;\n\tpending?: boolean;\n\tstatus?: FancyButtonStatus;\n\tdoneLabel?: React.ReactNode;\n};\n\nconst FancyButton = React.forwardRef<HTMLButtonElement, FancyButtonProps>(function FancyButton(\n\t{\n\t\tvariant = \"basic\",\n\t\tsize = \"xsmall\",\n\t\ticonOnly = false,\n\t\tasChild = false,\n\t\tpending: pendingProp = false,\n\t\tstatus,\n\t\tdoneLabel = \"Saved\",\n\t\ttype = \"button\",\n\t\tclassName,\n\t\tonClick,\n\t\tchildren,\n\t\t...props\n\t},\n\tref,\n) {\n\tconst Component = asChild ? Slot : \"button\";\n\tconst pending = pendingProp || status === \"pending\";\n\tconst done = !pending && status === \"done\" && !asChild;\n\t// Only buttons that opt into `status` watch their own width: a morph observer on every button would be waste.\n\tconst morphRef = useSizeMorph<HTMLButtonElement>({ axis: \"width\", enabled: status !== undefined && !asChild && !iconOnly });\n\tconst setRef = useMergedRef<HTMLButtonElement>(ref, morphRef);\n\t// Slot needs its single child, so an asChild link keeps its content and only takes the busy state.\n\tconst glyph = (pending || done) && !asChild ? (\n\t\t<span data-slot=\"fancy-button-icon\" data-fancy-loader=\"\" className=\"flex shrink-0 items-center justify-center\">\n\t\t\t<IconSwap state={done ? \"done\" : \"pending\"}>\n\t\t\t\t{done ? <CheckIcon aria-hidden=\"true\" className=\"size-4\" /> : <Loader size={iconOnly ? 16 : 14} />}\n\t\t\t</IconSwap>\n\t\t</span>\n\t) : null;\n\t// While done the live region names the button, so the visible label is hidden from assistive tech to avoid \"Saved Saved\".\n\tconst label = done && !iconOnly ? <span aria-hidden=\"true\">{doneLabel}</span> : children;\n\tconst live =\n\t\tstatus !== undefined && !asChild ? (\n\t\t\t<span role=\"status\" aria-live=\"polite\" className=\"sr-only\">\n\t\t\t\t{done ? doneLabel : null}\n\t\t\t</span>\n\t\t) : null;\n\treturn (\n\t\t<Component\n\t\t\tref={setRef}\n\t\t\tdata-slot=\"fancy-button\"\n\t\t\tdata-variant={variant}\n\t\t\ttype={asChild ? undefined : type}\n\t\t\tclassName={cn(\n\t\t\t\t// `fancy-button` marks the element for foundation.css even when a trigger's render prop replaces data-slot.\n\t\t\t\t\"fancy-button relative inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-control border border-transparent font-medium whitespace-nowrap outline-none select-none data-morphing:overflow-hidden\",\n\t\t\t\t\"transition-[background-color,color,box-shadow,scale] duration-[100ms,100ms,100ms,150ms] ease-out-strong\",\n\t\t\t\t\"motion-safe:not-disabled:not-aria-disabled:active:scale-[0.96]\",\n\t\t\t\t// An offset outline, not a ring: a ring replaces the variant's own shadow ring and highlight while focused.\n\t\t\t\t\"focus-visible:outline-solid focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring [-webkit-tap-highlight-color:transparent]\",\n\t\t\t\t\"disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50\",\n\t\t\t\t// While pending the status glyph is the button's one glyph (done swaps the whole label).\n\t\t\t\t\"data-pending:[&>[data-slot=fancy-button-icon]:not([data-fancy-loader])]:hidden\",\n\t\t\t\t// Glyphs that reach the button bare (trigger children, asChild links) get FancyButtonIcon's 16px.\n\t\t\t\t\"[&_svg:not([class*='size-'])]:size-4 [&_svg]:shrink-0\",\n\t\t\t\tVARIANTS[variant],\n\t\t\t\ticonOnly ? cn(ICON_ONLY_SIZES[size], \"p-0\") : SIZES[size],\n\t\t\t\tclassName,\n\t\t\t)}\n\t\t\t{...props}\n\t\t\tdata-pending={pending || undefined}\n\t\t\tdata-done={done || undefined}\n\t\t\taria-disabled={pending || props[\"aria-disabled\"] || undefined}\n\t\t\tonClick={pending || done ? (event: React.MouseEvent<HTMLButtonElement>) => event.preventDefault() : onClick}\n\t\t>\n\t\t\t{asChild ? children : glyph && iconOnly ? <>{glyph}{live}</> : <>{glyph}{label}{live}</>}\n\t\t</Component>\n\t);\n});\n\ntype FancyButtonIconProps = {\n\tas: React.ElementType;\n\tclassName?: string;\n};\n\n/** A 16px glyph inside a FancyButton; decorative, so hidden from assistive tech. */\nfunction FancyButtonIcon({ as: Icon, className }: FancyButtonIconProps) {\n\treturn <Icon aria-hidden=\"true\" data-slot=\"fancy-button-icon\" className={cn(\"size-4 shrink-0\", className)} />;\n}\n\nexport { FancyButton, FancyButtonIcon, type FancyButtonProps, type FancyButtonStatus };\n",
      "type": "registry:ui"
    }
  ],
  "docs": "Install @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.",
  "type": "registry:ui"
}