{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "nft-card",
  "title": "NFT Card",
  "description": "Card for an ERC-721 or ERC-1155 token: reads tokenURI or uri (with {id} substitution) and the collection name in one batch, fetches metadata from HTTP, IPFS (configurable gateway), Arweave or inline data: URIs, and shows the image, name, collection, token id and optional traits. Skeleton measured from the real layout (skullet) while loading and a placeholder for missing images or tokens. Exports useNft, fetchNftMetadata, resolveUri and expandTokenUri.",
  "dependencies": [
    "wagmi",
    "viem",
    "@tanstack/react-query",
    "lucide-react",
    "skullet"
  ],
  "registryDependencies": [
    "@0xui/0xui-utils"
  ],
  "files": [
    {
      "path": "registry/default/components/nft-card.tsx",
      "content": "// Docs: https://0xui.io/components/nft-card.md\nimport { useQuery } from \"@tanstack/react-query\";\nimport { ImageOffIcon } from \"lucide-react\";\nimport { useState } from \"react\";\nimport { Skeleton } from \"skullet/react\";\nimport { parseAbi, type Address } from \"viem\";\nimport { useReadContracts } from \"wagmi\";\nimport { resolveUri } from \"@/lib/0xui-utils\";\nimport { cn } from \"@/lib/utils\";\n\nconst nftAbi = parseAbi([\n  \"function tokenURI(uint256 tokenId) view returns (string)\",\n  \"function uri(uint256 id) view returns (string)\",\n  \"function name() view returns (string)\",\n]);\n\nexport type NftMetadata = {\n  name?: string;\n  description?: string;\n  image?: string;\n  image_url?: string;\n  attributes?: { trait_type?: string; value?: unknown }[];\n};\n\nexport { resolveUri };\n\n/** ERC-1155 URIs may contain `{id}`: the token id as 64 lowercase hex characters. */\nexport function expandTokenUri(uri: string, tokenId: bigint) {\n  return uri.replace(\"{id}\", tokenId.toString(16).padStart(64, \"0\"));\n}\n\n/**\n * Read token metadata from an HTTP, IPFS, Arweave or inline `data:` URI.\n * `fetch` decodes `data:` URIs (base64 or not) on its own.\n */\nexport async function fetchNftMetadata(uri: string, gateway?: string): Promise<NftMetadata> {\n  const response = await fetch(resolveUri(uri, gateway));\n  if (!response.ok) throw new Error(`Metadata request failed (${response.status})`);\n  return response.json();\n}\n\n/** Token URI, collection name and metadata for an ERC-721 or ERC-1155 token. */\nexport function useNft({\n  contract,\n  tokenId,\n  chainId,\n  gateway,\n}: {\n  contract: Address;\n  tokenId: bigint;\n  chainId?: number;\n  gateway?: string;\n}) {\n  const reads = useReadContracts({\n    allowFailure: true,\n    contracts: [\n      { address: contract, abi: nftAbi, functionName: \"tokenURI\", args: [tokenId], chainId },\n      { address: contract, abi: nftAbi, functionName: \"uri\", args: [tokenId], chainId },\n      { address: contract, abi: nftAbi, functionName: \"name\", chainId },\n    ],\n  });\n  const [erc721, erc1155, name] = reads.data ?? [];\n  const rawUri =\n    erc721?.status === \"success\" ? erc721.result : erc1155?.status === \"success\" ? erc1155.result : undefined;\n  const uri = rawUri ? expandTokenUri(rawUri, tokenId) : undefined;\n\n  const metadata = useQuery({\n    queryKey: [\"0xui\", \"nft-metadata\", uri, gateway],\n    queryFn: () => fetchNftMetadata(uri!, gateway),\n    enabled: Boolean(uri),\n    staleTime: Number.POSITIVE_INFINITY,\n    retry: 1,\n  });\n\n  const image = metadata.data?.image ?? metadata.data?.image_url;\n  return {\n    collection: name?.status === \"success\" ? name.result : undefined,\n    metadata: metadata.data,\n    image: image ? resolveUri(image, gateway) : undefined,\n    isLoading: reads.isLoading || metadata.isLoading,\n    /** No token URI: not an NFT contract, or the token doesn't exist. */\n    notFound: (reads.isSuccess && !uri) || reads.isError,\n    /** The token exists, but its metadata host failed (often a rate-limited IPFS gateway). */\n    metadataError: metadata.isError,\n  };\n}\n\nexport type NftCardProps = {\n  contract: Address;\n  tokenId: bigint;\n  chainId?: number;\n  /** IPFS gateway with trailing slash. */\n  gateway?: string;\n  /** Show the token's traits. */\n  showAttributes?: boolean;\n  className?: string;\n};\n\n/**\n * NFT card for an ERC-721 or ERC-1155 token: image, name, collection and\n * token id, from on-chain `tokenURI`/`uri` and its metadata. Handles IPFS,\n * Arweave and inline `data:` metadata, with a skeleton while loading and a\n * placeholder when the image can't load.\n */\nexport function NftCard({ contract, tokenId, chainId, gateway, showAttributes = false, className }: NftCardProps) {\n  const nft = useNft({ contract, tokenId, chainId, gateway });\n  const [imageFailed, setImageFailed] = useState(false);\n  const title = nft.metadata?.name ?? `#${tokenId}`;\n  const phase = nft.isLoading\n    ? \"loading\"\n    : nft.notFound\n      ? \"not-found\"\n      : nft.metadataError\n        ? \"metadata-error\"\n        : \"ready\";\n\n  return (\n    <article\n      data-slot=\"nft-card\"\n      data-phase={phase}\n      className={cn(\"w-full max-w-64 overflow-hidden rounded-2xl border\", className)}\n    >\n      {/* While loading, the card is laid out with stand-in text and measured into a skeleton. */}\n      <Skeleton loading={phase === \"loading\"} label=\"Loading NFT\">\n        <div className=\"relative aspect-square bg-muted\">\n          {phase === \"loading\" ? (\n            // One block for the image, not a bar per element.\n            <div skullet-block=\"\" className=\"size-full\" />\n          ) : nft.image && !imageFailed ? (\n            <img\n              src={nft.image}\n              alt={title}\n              loading=\"lazy\"\n              onError={() => setImageFailed(true)}\n              className=\"size-full object-cover\"\n            />\n          ) : (\n            <div className=\"flex size-full flex-col items-center justify-center gap-2 text-xs text-muted-foreground\">\n              <ImageOffIcon aria-hidden className=\"size-6\" />\n              {phase === \"not-found\"\n                ? \"Token not found\"\n                : phase === \"metadata-error\"\n                  ? \"Metadata unavailable\"\n                  : \"No image\"}\n            </div>\n          )}\n        </div>\n        <div className=\"flex flex-col gap-0.5 p-3\">\n          <h3 className=\"truncate text-sm font-semibold\">{phase === \"loading\" ? \"Sample token name\" : title}</h3>\n          <p className=\"truncate text-xs text-muted-foreground\">\n            {phase === \"loading\" ? \"Sample collection\" : (nft.collection ?? \"Unknown collection\")} · #\n            {tokenId.toString()}\n          </p>\n          {showAttributes && nft.metadata?.attributes && nft.metadata.attributes.length > 0 && (\n            <dl className=\"mt-2 flex flex-wrap gap-1.5\">\n              {nft.metadata.attributes.map((attr, i) => (\n                <div key={i} className=\"rounded-md bg-muted px-2 py-1 text-xs\">\n                  <dt className=\"text-muted-foreground\">{attr.trait_type ?? \"Trait\"}</dt>\n                  <dd className=\"font-medium\">{String(attr.value)}</dd>\n                </div>\n              ))}\n            </dl>\n          )}\n        </div>\n      </Skeleton>\n    </article>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/nft-card.md",
  "categories": [
    "tokens"
  ],
  "type": "registry:component"
}