{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "connect-wallet",
  "title": "Connect Wallet",
  "description": "Wallet picker for wagmi: 'Connect wallet' button plus dialog listing the config's connectors and EIP-6963 browser extensions with their icons. Recent wallet first, then installed (badged), max N shown with an 'Other wallets' overflow, generic injected hidden when extensions announce themselves. Per-wallet spinner while connecting, a rejected request shows as cancelled (not an error), other errors inline. Exports ConnectWalletDialog (bring your own trigger), useConnectWallet and useWalletOptions. data-phase: idle | connecting | cancelled | error. On small screens the picker opens as a bottom drawer (mode: auto, dialog or drawer).",
  "dependencies": [
    "wagmi",
    "@web3icons/react",
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "@0xui/responsive-dialog",
    "avatar",
    "@0xui/0xui-utils"
  ],
  "files": [
    {
      "path": "registry/default/components/connect-wallet.tsx",
      "content": "// Docs: https://0xui.io/components/connect-wallet.md\nimport { WalletIcon } from \"@web3icons/react/dynamic\";\nimport { ChevronRightIcon, Loader2Icon } from \"lucide-react\";\nimport { useEffect, useMemo, useState, type ComponentProps, type ReactNode } from \"react\";\nimport { useConfig, useConnect, useConnectors, type Connector } from \"wagmi\";\nimport { Avatar, AvatarFallback } from \"@/components/ui/avatar\";\nimport { ResponsiveDialog, type ResponsiveDialogMode } from \"@/components/responsive-dialog\";\nimport { Button } from \"@/components/ui/button\";\nimport { errorCode, isUserRejection } from \"@/lib/0xui-utils\";\nimport { cn } from \"@/lib/utils\";\n\nexport type ConnectPhase = \"idle\" | \"connecting\" | \"cancelled\" | \"error\";\n\nexport type WalletOption = {\n  connector: Connector;\n  id: string;\n  name: string;\n  /** Icon URL from the wallet itself (EIP-6963), when it provides one. */\n  icon?: string;\n  /** Announced by an installed browser extension (EIP-6963). */\n  installed: boolean;\n  /** The wallet used for the last successful connection. */\n  recent: boolean;\n};\n\n/**\n * The connectors from your wagmi config as an ordered wallet list: recent\n * first, then installed, then the rest. The generic \"injected\" connector is\n * dropped when installed wallets announce themselves (EIP-6963), since it\n * would duplicate one of them.\n */\nexport function useWalletOptions() {\n  const connectors = useConnectors();\n  const config = useConfig();\n  const [recentId, setRecentId] = useState<string>();\n\n  useEffect(() => {\n    let active = true;\n    Promise.resolve(config.storage?.getItem(\"recentConnectorId\")).then((id) => {\n      if (active) setRecentId(id ?? undefined);\n    });\n    return () => {\n      active = false;\n    };\n  }, [config]);\n\n  return useMemo(() => {\n    const announced = connectors.some((c) => c.rdns);\n    const options: WalletOption[] = connectors\n      .filter((c) => !(announced && c.id === \"injected\"))\n      .map((connector) => ({\n        connector,\n        id: connector.id,\n        name: connector.id === \"injected\" ? \"Browser wallet\" : connector.name,\n        icon: connector.icon,\n        installed: Boolean(connector.rdns),\n        recent: connector.id === recentId,\n      }));\n    const rank = (o: WalletOption) => (o.recent ? 0 : o.installed ? 1 : 2);\n    return options\n      .map((o, index) => ({ o, index }))\n      .sort((a, b) => rank(a.o) - rank(b.o) || a.index - b.index)\n      .map(({ o }) => o);\n  }, [connectors, recentId]);\n}\n\n/** Wallet list plus connect state. Build your own connect UI on this. */\nexport function useConnectWallet({\n  onConnect,\n}: { onConnect?: (connector: Connector) => void } = {}) {\n  const wallets = useWalletOptions();\n  const { mutateAsync, isPending, variables, error, reset } = useConnect();\n\n  // We always pass a Connector, but the variable type also allows a connector factory.\n  const pendingConnector = isPending ? variables?.connector : undefined;\n  const pendingId = pendingConnector && \"id\" in pendingConnector ? pendingConnector.id : undefined;\n  const phase: ConnectPhase = isPending\n    ? \"connecting\"\n    : isUserRejection(error)\n      ? \"cancelled\"\n      : error\n        ? \"error\"\n        : \"idle\";\n\n  const connect = async (wallet: WalletOption) => {\n    try {\n      await mutateAsync({ connector: wallet.connector });\n      onConnect?.(wallet.connector);\n      return true;\n    } catch {\n      // Surfaced via `phase` and `error`.\n      return false;\n    }\n  };\n\n  return { wallets, connect, phase, pendingId, error, reset };\n}\n\nexport type ConnectWalletDialogProps = {\n  open?: boolean;\n  defaultOpen?: boolean;\n  onOpenChange?: (open: boolean) => void;\n  /** Called after a wallet connects. The dialog closes itself. */\n  onConnect?: (connector: Connector) => void;\n  /** Dialog title. */\n  title?: ReactNode;\n  /** Wallets shown before \"Other wallets\". */\n  maxVisible?: number;\n  /** Small print under the list. Pass null to hide. */\n  footer?: ReactNode;\n  /** `auto` is a bottom drawer on small screens and a dialog otherwise. `dialog` and `drawer` force one. */\n  mode?: ResponsiveDialogMode;\n};\n\nconst defaultFooter =\n  \"Connecting shares your public address with this app. It can't move funds without your approval.\";\n\n/** The wallet picker on its own. Use it to open the picker from your own trigger. */\nexport function ConnectWalletDialog({\n  open: openProp,\n  defaultOpen = false,\n  onOpenChange,\n  onConnect,\n  title = \"Connect a wallet\",\n  maxVisible = 4,\n  footer = defaultFooter,\n  mode,\n}: ConnectWalletDialogProps) {\n  const [openState, setOpenState] = useState(defaultOpen);\n  const open = openProp ?? openState;\n  const [showAll, setShowAll] = useState(false);\n\n  const setOpen = (next: boolean) => {\n    setOpenState(next);\n    onOpenChange?.(next);\n  };\n\n  const { wallets, connect, phase, pendingId, error, reset } = useConnectWallet({\n    onConnect: (connector) => {\n      setOpen(false);\n      onConnect?.(connector);\n    },\n  });\n\n  // Fresh state every time the dialog opens.\n  useEffect(() => {\n    if (open) {\n      reset();\n      setShowAll(false);\n    }\n  }, [open, reset]);\n\n  const visible = showAll ? wallets : wallets.slice(0, maxVisible);\n  const hiddenCount = wallets.length - visible.length;\n  const pendingName = wallets.find((w) => w.id === pendingId)?.name;\n\n  return (\n    <ResponsiveDialog\n      open={open}\n      onOpenChange={setOpen}\n      title={title}\n      description=\"Choose a wallet to connect to this app.\"\n      mode={mode}\n      className=\"sm:max-w-sm\"\n      data-phase={phase}\n      data-error={errorCode(phase, error)}\n    >\n      {wallets.length === 0 ? (\n        <p className=\"text-sm text-muted-foreground\">\n          No wallets found. Install a browser wallet such as MetaMask or Rabby, then reload the\n          page.\n        </p>\n      ) : (\n        <ul className=\"-mx-2 flex flex-col gap-1\">\n          {visible.map((wallet) => (\n            <li key={wallet.id}>\n              <WalletRow\n                wallet={wallet}\n                pending={wallet.id === pendingId}\n                disabled={phase === \"connecting\"}\n                onSelect={() => connect(wallet)}\n              />\n            </li>\n          ))}\n        </ul>\n      )}\n\n      {hiddenCount > 0 && (\n        <Button variant=\"ghost\" className=\"-mt-2 self-start\" onClick={() => setShowAll(true)}>\n          Other wallets ({hiddenCount})\n        </Button>\n      )}\n\n      <div aria-live=\"polite\" className=\"text-sm empty:hidden\">\n        {phase === \"connecting\" && pendingName && (\n          <p className=\"text-muted-foreground\">Confirm the connection in {pendingName}.</p>\n        )}\n        {phase === \"cancelled\" && (\n          <p className=\"text-muted-foreground\">\n            Request cancelled. Try again or pick another wallet.\n          </p>\n        )}\n      </div>\n      {phase === \"error\" && error && (\n        <p role=\"alert\" className=\"text-sm text-destructive\">\n          {(error as { shortMessage?: string }).shortMessage ?? error.message}\n        </p>\n      )}\n\n      {footer && <p className=\"text-xs text-muted-foreground\">{footer}</p>}\n    </ResponsiveDialog>\n  );\n}\n\nfunction WalletRow({\n  wallet,\n  pending,\n  disabled,\n  onSelect,\n}: {\n  wallet: WalletOption;\n  pending: boolean;\n  disabled: boolean;\n  onSelect: () => void;\n}) {\n  return (\n    <button\n      type=\"button\"\n      onClick={onSelect}\n      disabled={disabled}\n      aria-busy={pending || undefined}\n      className={cn(\n        \"flex min-h-12 w-full items-center gap-3 rounded-lg px-2 py-1.5 text-left text-sm font-medium outline-none\",\n        \"hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50 disabled:opacity-60\",\n        pending && \"bg-muted disabled:opacity-100\",\n      )}\n    >\n      <WalletOptionIcon wallet={wallet} />\n      <span className=\"flex-1 truncate\">{wallet.name}</span>\n      {wallet.recent ? (\n        <WalletBadge>Recent</WalletBadge>\n      ) : wallet.installed ? (\n        <WalletBadge>Installed</WalletBadge>\n      ) : null}\n      {pending ? (\n        <Loader2Icon aria-hidden className=\"size-4 shrink-0 animate-spin\" />\n      ) : (\n        <ChevronRightIcon aria-hidden className=\"size-4 shrink-0 text-muted-foreground\" />\n      )}\n    </button>\n  );\n}\n\nfunction WalletBadge({ children }: { children: ReactNode }) {\n  return (\n    <span className=\"rounded-full bg-muted px-2 py-0.5 text-xs font-normal text-muted-foreground\">\n      {children}\n    </span>\n  );\n}\n\n/** Wallet logo: the wallet's own icon, then web3icons by name, then initials. */\nexport function WalletOptionIcon({\n  wallet,\n  className,\n}: {\n  wallet: Pick<WalletOption, \"name\" | \"icon\">;\n  className?: string;\n}) {\n  // Decorative: the wallet name is always next to it.\n  const fallback = (\n    <Avatar aria-hidden className={cn(\"size-8 rounded-lg\", className)}>\n      <AvatarFallback className=\"rounded-lg text-xs\">{initials(wallet.name)}</AvatarFallback>\n    </Avatar>\n  );\n\n  if (wallet.icon) {\n    return <img src={wallet.icon} alt=\"\" className={cn(\"size-8 shrink-0 rounded-lg\", className)} />;\n  }\n  return (\n    <WalletIcon\n      aria-hidden\n      name={wallet.name}\n      variant=\"background\"\n      className={cn(\"size-8 shrink-0 rounded-lg\", className)}\n      fallback={fallback}\n    />\n  );\n}\n\nfunction initials(name: string) {\n  return name\n    .split(/\\s+/)\n    .slice(0, 2)\n    .map((word) => word[0])\n    .join(\"\")\n    .toUpperCase();\n}\n\nexport type ConnectWalletProps = Omit<ComponentProps<typeof Button>, \"onClick\"> &\n  Pick<\n    ConnectWalletDialogProps,\n    | \"open\"\n    | \"defaultOpen\"\n    | \"onOpenChange\"\n    | \"onConnect\"\n    | \"title\"\n    | \"maxVisible\"\n    | \"footer\"\n    | \"mode\"\n  >;\n\n/** \"Connect wallet\" button that opens the wallet picker. */\nexport function ConnectWallet({\n  open: openProp,\n  defaultOpen = false,\n  onOpenChange,\n  onConnect,\n  title,\n  maxVisible,\n  footer,\n  mode,\n  children = \"Connect wallet\",\n  ...buttonProps\n}: ConnectWalletProps) {\n  const [openState, setOpenState] = useState(defaultOpen);\n  const open = openProp ?? openState;\n  const setOpen = (next: boolean) => {\n    setOpenState(next);\n    onOpenChange?.(next);\n  };\n\n  return (\n    <>\n      <Button aria-haspopup=\"dialog\" {...buttonProps} onClick={() => setOpen(true)}>\n        {children}\n      </Button>\n      <ConnectWalletDialog\n        open={open}\n        onOpenChange={setOpen}\n        onConnect={onConnect}\n        title={title}\n        maxVisible={maxVisible}\n        footer={footer}\n        mode={mode}\n      />\n    </>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/connect-wallet.md",
  "categories": [
    "wallet"
  ],
  "type": "registry:component"
}