{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "token-icon",
  "title": "Token Icon",
  "description": "Token logo with fallbacks: the token list's logoURI (ipfs:// is resolved through a gateway), then the built-in web3icons icon for the symbol, then the symbol's first letters. A skeleton (skullet) stands in while a logo or icon loads. Optional chain badge in the corner via chainId. Labelled by the symbol for screen readers. Size in px.",
  "dependencies": [
    "@web3icons/react",
    "skullet"
  ],
  "registryDependencies": [
    "@0xui/0xui-utils",
    "@0xui/chain-badge",
    "avatar"
  ],
  "files": [
    {
      "path": "registry/default/components/token-icon.tsx",
      "content": "// Docs: https://0xui.io/components/token-icon.md\nimport { TokenIcon as Web3TokenIcon } from \"@web3icons/react/dynamic\";\nimport { useEffect, useState, type ReactNode } from \"react\";\nimport { SkeletonBox } from \"skullet/react\";\nimport { ChainIcon } from \"@/components/chain-badge\";\nimport { Avatar, AvatarFallback, AvatarImage } from \"@/components/ui/avatar\";\nimport { resolveUri } from \"@/lib/0xui-utils\";\nimport { cn } from \"@/lib/utils\";\n\nexport type TokenIconProps = {\n  /** Token symbol, e.g. \"USDC\". Used for the built-in icon and the lettered fallback. */\n  symbol: string;\n  /** Logo URL, e.g. `logoURI` from a token list. `ipfs://` works. Takes priority over the built-in icon. */\n  src?: string;\n  /** IPFS gateway for `ipfs://` logos. */\n  gateway?: string;\n  /** Show this chain's icon as a small badge in the corner. */\n  chainId?: number;\n  /** Diameter in px. */\n  size?: number;\n  /** Icon style for the built-in icons. */\n  variant?: \"background\" | \"branded\" | \"mono\";\n  className?: string;\n};\n\n/**\n * Token logo. Uses `src` when given, otherwise the web3icons icon for\n * `symbol`, otherwise the symbol's first letters. Optionally shows the\n * token's chain as a corner badge.\n */\nexport function TokenIcon({\n  symbol,\n  src,\n  gateway,\n  chainId,\n  size = 24,\n  variant = \"branded\",\n  className,\n}: TokenIconProps) {\n  const badge = Math.max(10, Math.round(size * 0.45));\n  const builtIn = (\n    <Web3TokenIcon\n      symbol={symbol.toLowerCase()}\n      variant={variant}\n      className=\"size-full rounded-full\"\n      fallback={<Letters symbol={symbol} />}\n    />\n  );\n\n  return (\n    <span\n      role=\"img\"\n      aria-label={symbol}\n      className={cn(\"relative inline-flex shrink-0\", className)}\n      style={{ width: size, height: size }}\n    >\n      {src ? (\n        <ListLogo key={src} src={resolveUri(src, gateway)} fallback={builtIn} />\n      ) : (\n        builtIn\n      )}\n      {chainId !== undefined && (\n        <span\n          className=\"absolute -right-0.5 -bottom-0.5 rounded-full bg-background p-px\"\n          style={{ width: badge, height: badge }}\n        >\n          <ChainIcon chainId={chainId} className=\"size-full\" />\n        </span>\n      )}\n    </span>\n  );\n}\n\n/**\n * The symbol's first letters. web3icons shows this both while an icon loads\n * and when it has none, so it starts as a skeleton and settles on the letters\n * only if no icon has turned up.\n */\nfunction Letters({ symbol }: { symbol: string }) {\n  const [settled, setSettled] = useState(false);\n  useEffect(() => {\n    const timer = setTimeout(() => setSettled(true), 800);\n    return () => clearTimeout(timer);\n  }, []);\n\n  return (\n    <Avatar className=\"size-full\">\n      {settled ? (\n        <AvatarFallback className=\"text-[0.6em] font-medium uppercase\">{symbol.slice(0, 2)}</AvatarFallback>\n      ) : (\n        <SkeletonBox width=\"100%\" height=\"100%\" radius=\"9999px\" />\n      )}\n    </Avatar>\n  );\n}\n\n/**\n * A token-list logo. These go dead often, so on error show `fallback`. The\n * fallback only mounts after a failure: rendering it while the logo loads\n * would fetch a built-in icon for every row of a long list.\n */\nfunction ListLogo({ src, fallback }: { src: string; fallback: ReactNode }) {\n  const [failed, setFailed] = useState(false);\n  if (failed) return <>{fallback}</>;\n  return (\n    <Avatar className=\"size-full\">\n      <AvatarImage\n        src={src}\n        alt=\"\"\n        onLoadingStatusChange={(status) => status === \"error\" && setFailed(true)}\n      />\n      <AvatarFallback className=\"bg-transparent\">\n        <SkeletonBox width=\"100%\" height=\"100%\" radius=\"9999px\" />\n      </AvatarFallback>\n    </Avatar>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/token-icon.md",
  "categories": [
    "tokens"
  ],
  "type": "registry:component"
}