{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chain-selector",
  "title": "Chain Selector",
  "description": "Network switcher for wagmi apps, as a popover (ChainSelector) or dialog (ChainSelectorDialog). Shows the current chain with its icon, lists every chain in the wagmi config (optionally grouped into mainnet/testnet tabs), marks the active chain, shows a per-row spinner while switching, and surfaces wallet errors inline; a rejected request shows as cancelled, not as an error. Wraps useSwitchChain, useChains and useChainId. Flags a wallet on an unsupported chain as 'Wrong network', and takes a chainMeta slot for per-chain detail. Icon slots are swappable or can be hidden; iconOnly mode keeps an accessible label. Before hydration the trigger is a skeleton (skullet) in its own shape, so server HTML never shows wallet state and nothing moves when the chain appears.",
  "dependencies": [
    "wagmi",
    "viem",
    "lucide-react",
    "skullet"
  ],
  "registryDependencies": [
    "popover",
    "dialog",
    "tabs",
    "@0xui/chain-badge",
    "@0xui/morph-text",
    "@0xui/use-mounted",
    "@0xui/0xui-utils"
  ],
  "files": [
    {
      "path": "registry/default/components/chain-selector.tsx",
      "content": "// Docs: https://0xui.io/components/chain-selector.md\nimport {\n  Popover,\n  PopoverContent,\n  PopoverTrigger,\n} from \"@/components/ui/popover\";\nimport {\n  Dialog,\n  DialogContent,\n  DialogHeader,\n  DialogTitle,\n  DialogTrigger,\n} from \"@/components/ui/dialog\";\nimport { Tabs, TabsList, TabsTrigger, TabsContent } from \"@/components/ui/tabs\";\nimport { cn } from \"@/lib/utils\";\nimport {\n  CheckIcon,\n  ChevronDownIcon,\n  Loader2Icon,\n  TriangleAlertIcon,\n} from \"lucide-react\";\nimport { useState, type ComponentType, type ReactNode } from \"react\";\nimport { SkeletonBox } from \"skullet/react\";\nimport { type Chain } from \"viem\";\nimport { useChainId, useChains, useConnection, useSwitchChain } from \"wagmi\";\nimport { ChainIcon, type ChainIconProps } from \"@/components/chain-badge\";\nimport { MorphText } from \"@/components/morph-text\";\nimport { useMounted } from \"@/hooks/use-mounted\";\nimport { isUserRejection } from \"@/lib/0xui-utils\";\n\ntype IconStyleProps =\n  | {\n      /** Icon style. */\n      variant: \"mono\";\n      /** Color for mono variant icons. Only applies when variant is \"mono\". */\n      monoColor?: string;\n    }\n  | {\n      /** Icon style. */\n      variant?: \"branded\" | \"background\";\n      monoColor?: never;\n    };\n\nexport type { ChainIconProps };\n\nexport type ChainSelectorProps = {\n  /** Custom chain icon component. Receives chainId, className, and variant props. Pass false to hide. */\n  chainIcon?: ComponentType<ChainIconProps> | false;\n  /** Custom chevron icon component. Receives className prop. Pass false to hide. */\n  chevronIcon?: ComponentType<{ className?: string }> | false;\n  /** Custom loader icon component. Receives className prop. Pass false to hide. */\n  loaderIcon?: ComponentType<{ className?: string }> | false;\n  /** Disable all icons. Overrides individual icon props. */\n  disableIcons?: boolean;\n  /** Show only icons without visible labels. Label remains accessible via tooltip and screen readers. */\n  iconOnly?: boolean;\n  /** Group chains by network type (mainnet/testnet) with tabs. */\n  groupByNetwork?: boolean;\n  /** Extra detail under each chain's name, e.g. bridge time or fees. */\n  chainMeta?: (chain: Chain) => ReactNode;\n} & IconStyleProps;\n\nfunction groupChainsByNetwork(chains: readonly Chain[]) {\n  const mainnet: Chain[] = [];\n  const testnet: Chain[] = [];\n\n  chains.forEach((chain) => {\n    if ((chain as Chain & { testnet?: boolean }).testnet) {\n      testnet.push(chain);\n    } else {\n      mainnet.push(chain);\n    }\n  });\n\n  return { mainnet, testnet };\n}\n\nfunction useChainSwitcher() {\n  const [open, setOpen] = useState(false);\n  const mounted = useMounted();\n\n  const chainId = useChainId();\n  const chains = useChains();\n  const connection = useConnection();\n  // The wallet can be on a chain the app doesn't configure. useChainId would\n  // still report the last configured chain, so check the connection directly.\n  const unsupported =\n    connection.status === \"connected\" &&\n    connection.chainId !== undefined &&\n    !chains.some((c) => c.id === connection.chainId);\n  const currentChain = unsupported\n    ? undefined\n    : chains.find((c) => c.id === chainId);\n\n  const { mutateAsync, isPending, variables, error, reset } = useSwitchChain();\n  const pendingChainId = isPending ? variables?.chainId : undefined;\n  const cancelled = isUserRejection(error);\n\n  const onSwitch = async (id: number) => {\n    try {\n      // Narrow to the chain-id union when the app registers its config type.\n      await mutateAsync({ chainId: id as (typeof chains)[number][\"id\"] });\n      setOpen(false);\n    } catch {\n      // Surfaced via `error` below.\n    }\n  };\n\n  const onOpenChange = (next: boolean) => {\n    setOpen(next);\n    if (next) reset();\n  };\n\n  return {\n    open,\n    onOpenChange,\n    mounted,\n    chains,\n    currentChain,\n    onSwitch,\n    isPending,\n    pendingChainId,\n    error: cancelled ? null : error,\n    cancelled,\n    unsupported: mounted && unsupported,\n  };\n}\n\nfunction resolveIcons({\n  chainIcon,\n  chevronIcon,\n  loaderIcon,\n  disableIcons,\n}: Pick<\n  ChainSelectorProps,\n  \"chainIcon\" | \"chevronIcon\" | \"loaderIcon\" | \"disableIcons\"\n>) {\n  if (disableIcons) return {};\n  return {\n    ChainIcon: chainIcon === false ? undefined : chainIcon || ChainIcon,\n    ChevronIcon:\n      chevronIcon === false ? undefined : chevronIcon || ChevronDownIcon,\n    LoaderIcon: loaderIcon === false ? undefined : loaderIcon || Loader2Icon,\n  };\n}\n\nfunction chainIconProps(\n  chainId: number,\n  variant: \"branded\" | \"mono\" | \"background\",\n  monoColor: string | undefined,\n  className?: string\n): ChainIconProps {\n  return { chainId, variant, monoColor, className };\n}\n\nconst triggerClassName = ({\n  iconOnly,\n  unsupported,\n  ChainIcon,\n  ChevronIcon,\n}: {\n  iconOnly?: boolean;\n  unsupported?: boolean;\n  ChainIcon?: unknown;\n  ChevronIcon?: unknown;\n}) =>\n  cn(\n    \"flex items-center gap-2 justify-between border transition-all w-fit py-2 px-3 rounded-full hover:bg-accent/10 active:scale-97 cursor-pointer\",\n    iconOnly ? \"min-w-fit gap-1 p-1\" : \"min-w-30\",\n    // Bare text needs more room on the pill's curve than an icon does.\n    !iconOnly && !ChainIcon && !unsupported && \"pl-4\",\n    !iconOnly && !ChevronIcon && \"pr-4\",\n    unsupported && \"border-destructive/50 text-destructive\"\n  );\n\nfunction TriggerContent({\n  mounted,\n  unsupported,\n  currentChain,\n  chainName,\n  iconOnly,\n  open,\n  isPending,\n  variant,\n  monoColor,\n  ChainIcon,\n  ChevronIcon,\n  LoaderIcon,\n}: {\n  mounted: boolean;\n  unsupported: boolean;\n  currentChain: Chain | undefined;\n  chainName: string;\n  iconOnly?: boolean;\n  open: boolean;\n  isPending: boolean;\n  variant: \"branded\" | \"mono\" | \"background\";\n  monoColor?: string;\n  ChainIcon?: ComponentType<ChainIconProps>;\n  ChevronIcon?: ComponentType<{ className?: string }>;\n  LoaderIcon?: ComponentType<{ className?: string }>;\n}) {\n  return (\n    <>\n      {unsupported && (\n        <TriangleAlertIcon aria-hidden className=\"size-5 shrink-0\" />\n      )}\n      {/* Before mount the chain isn't known: a skeleton in the trigger's own\n          shape, so the name and icon arrive without moving anything. */}\n      {!mounted && ChainIcon && (\n        <SkeletonBox width=\"1.5rem\" height=\"1.5rem\" radius=\"9999px\" style={{ flexShrink: 0 }} />\n      )}\n      {mounted && currentChain && ChainIcon && (\n        <div className=\"size-6\">\n          <ChainIcon\n            {...chainIconProps(\n              currentChain.id,\n              variant,\n              monoColor,\n              \"rounded-full\"\n            )}\n          />\n        </div>\n      )}\n      <div className=\"flex items-center gap-2\">\n        {mounted ? (\n          <span className={cn(iconOnly && \"sr-only\")}>\n            <MorphText disabled={iconOnly}>{chainName}</MorphText>\n          </span>\n        ) : (\n          <span className={cn(iconOnly && \"sr-only\")}>\n            <span className=\"sr-only\">{chainName}</span>\n            {/* About the width of \"Ethereum\", inline so it takes a line of text's height. */}\n            {!iconOnly && (\n              <SkeletonBox\n                width=\"4.5em\"\n                height=\"1em\"\n                style={{ display: \"inline-block\", verticalAlign: \"middle\" }}\n              />\n            )}\n          </span>\n        )}\n        {ChevronIcon && (\n          <span\n            className={cn(\n              \"transition-transform h-fit w-fit\",\n              open && \"rotate-180\",\n              isPending && \"hidden\"\n            )}\n          >\n            <ChevronIcon className=\"size-4 shrink-0\" />\n          </span>\n        )}\n        {isPending && LoaderIcon && (\n          <LoaderIcon className=\"size-4 shrink-0 animate-spin\" />\n        )}\n      </div>\n    </>\n  );\n}\n\nfunction ChainItem({\n  chain,\n  meta,\n  isActive,\n  isSwitching,\n  disabled,\n  onSwitch,\n  ChainIcon,\n  variant,\n  monoColor,\n  LoaderIcon,\n}: {\n  chain: Chain;\n  meta?: ReactNode;\n  isActive: boolean;\n  isSwitching: boolean;\n  disabled: boolean;\n  onSwitch: (chainId: number) => void;\n  ChainIcon?: ComponentType<ChainIconProps>;\n  variant: \"branded\" | \"mono\" | \"background\";\n  monoColor?: string;\n  LoaderIcon?: ComponentType<{ className?: string }>;\n}) {\n  return (\n    <button\n      onClick={() => onSwitch(chain.id)}\n      disabled={disabled || isActive}\n      aria-current={isActive || undefined}\n      className={cn(\n        \"flex items-center w-full gap-2 p-2 rounded-md hover:bg-accent/20 group active:scale-97 cursor-pointer disabled:cursor-default disabled:opacity-50\",\n        isActive && \"bg-accent/20 opacity-100\"\n      )}\n    >\n      {ChainIcon && (\n        <ChainIcon\n          {...chainIconProps(chain.id, variant, monoColor, \"rounded-full border\")}\n        />\n      )}\n      <div className=\"flex min-w-0 flex-col items-start text-left\">\n        <span className=\"text-sm\">{chain.name}</span>\n        {meta && (\n          <span className=\"text-xs text-muted-foreground\">{meta}</span>\n        )}\n      </div>\n      {isActive && <CheckIcon className=\"size-4 ml-auto shrink-0\" />}\n      {isSwitching && LoaderIcon && (\n        <LoaderIcon className=\"size-4 ml-auto shrink-0 animate-spin\" />\n      )}\n    </button>\n  );\n}\n\nfunction ChainsGrid({\n  chains,\n  chainMeta,\n  currentChainId,\n  pendingChainId,\n  isPending,\n  onSwitch,\n  ChainIcon,\n  LoaderIcon,\n  variant,\n  monoColor,\n}: {\n  chains: readonly Chain[];\n  chainMeta?: (chain: Chain) => ReactNode;\n  currentChainId?: number;\n  pendingChainId?: number;\n  isPending: boolean;\n  onSwitch: (chainId: number) => void;\n  ChainIcon?: ComponentType<ChainIconProps>;\n  LoaderIcon?: ComponentType<{ className?: string }>;\n  variant: \"branded\" | \"mono\" | \"background\";\n  monoColor?: string;\n}) {\n  return (\n    <div className=\"grid grid-cols-2 gap-x-4 gap-y-2 w-full\">\n      {chains.map((chain) => (\n        <ChainItem\n          key={chain.id}\n          chain={chain}\n          meta={chainMeta?.(chain)}\n          isActive={chain.id === currentChainId}\n          isSwitching={chain.id === pendingChainId}\n          disabled={isPending}\n          onSwitch={onSwitch}\n          ChainIcon={ChainIcon}\n          LoaderIcon={LoaderIcon}\n          variant={variant}\n          monoColor={monoColor}\n        />\n      ))}\n    </div>\n  );\n}\n\nfunction ChainListContent({\n  mounted,\n  chains,\n  groupByNetwork,\n  error,\n  cancelled,\n  unsupported,\n  ...gridProps\n}: {\n  mounted: boolean;\n  chains: readonly Chain[];\n  groupByNetwork?: boolean;\n  error: { message: string; shortMessage?: string } | null;\n  cancelled: boolean;\n  unsupported: boolean;\n  chainMeta?: (chain: Chain) => ReactNode;\n  currentChainId?: number;\n  pendingChainId?: number;\n  isPending: boolean;\n  onSwitch: (chainId: number) => void;\n  ChainIcon?: ComponentType<ChainIconProps>;\n  LoaderIcon?: ComponentType<{ className?: string }>;\n  variant: \"branded\" | \"mono\" | \"background\";\n  monoColor?: string;\n}) {\n  if (!mounted) {\n    return (\n      <div className=\"flex flex-col gap-1\">\n        <span className=\"sr-only\">Loading chains</span>\n        {[0, 1, 2].map((row) => (\n          <SkeletonBox key={row} width=\"100%\" height=\"2.25rem\" />\n        ))}\n      </div>\n    );\n  }\n\n  const { mainnet, testnet } = groupByNetwork\n    ? groupChainsByNetwork(chains)\n    : { mainnet: chains, testnet: [] as Chain[] };\n\n  const empty = (label: string) => (\n    <p className=\"text-sm text-muted-foreground text-center py-4\">\n      No {label} chains available\n    </p>\n  );\n\n  return (\n    <>\n      {error && (\n        <p role=\"alert\" className=\"text-sm text-destructive\">\n          {error.shortMessage ?? error.message}\n        </p>\n      )}\n      {unsupported && (\n        <p className=\"text-sm text-destructive\">\n          Your wallet is on a network this app doesn't support. Pick one below.\n        </p>\n      )}\n      {cancelled && (\n        <p role=\"status\" className=\"text-sm text-muted-foreground\">\n          Request cancelled in wallet\n        </p>\n      )}\n      {groupByNetwork && (mainnet.length > 0 || testnet.length > 0) ? (\n        <Tabs defaultValue=\"mainnet\" className=\"w-full\">\n          <TabsList className=\"grid w-full grid-cols-2\">\n            <TabsTrigger value=\"mainnet\" disabled={mainnet.length === 0}>\n              Mainnet\n            </TabsTrigger>\n            <TabsTrigger value=\"testnet\" disabled={testnet.length === 0}>\n              Testnet\n            </TabsTrigger>\n          </TabsList>\n          <TabsContent value=\"mainnet\" className=\"mt-2\">\n            {mainnet.length > 0 ? (\n              <ChainsGrid chains={mainnet} {...gridProps} />\n            ) : (\n              empty(\"mainnet\")\n            )}\n          </TabsContent>\n          <TabsContent value=\"testnet\" className=\"mt-2\">\n            {testnet.length > 0 ? (\n              <ChainsGrid chains={testnet} {...gridProps} />\n            ) : (\n              empty(\"testnet\")\n            )}\n          </TabsContent>\n        </Tabs>\n      ) : (\n        <ChainsGrid chains={chains} {...gridProps} />\n      )}\n    </>\n  );\n}\n\nfunction useChainSelectorParts(props: ChainSelectorProps) {\n  const {\n    disableIcons = false,\n    iconOnly = false,\n    variant = \"background\",\n    monoColor,\n    groupByNetwork = false,\n  } = props;\n\n  const switcher = useChainSwitcher();\n  const icons = resolveIcons(props);\n\n  const chainName = !switcher.mounted\n    ? \"Select Chain\"\n    : switcher.unsupported\n      ? \"Wrong network\"\n      : switcher.currentChain?.name || \"Select Chain\";\n\n  return {\n    ...switcher,\n    ...icons,\n    chainName,\n    ariaLabel: iconOnly ? chainName : undefined,\n    tooltipTitle: iconOnly ? chainName : undefined,\n    iconOnly,\n    disableIcons,\n    variant,\n    monoColor,\n    groupByNetwork,\n    chainMeta: props.chainMeta,\n  };\n}\n\nexport function ChainSelector(props: ChainSelectorProps) {\n  const s = useChainSelectorParts(props);\n\n  return (\n    <Popover open={s.open} onOpenChange={s.onOpenChange}>\n      <PopoverTrigger\n        className={triggerClassName(s)}\n        aria-label={s.ariaLabel}\n        title={s.tooltipTitle}\n      >\n        <TriggerContent\n          mounted={s.mounted}\n          unsupported={s.unsupported}\n          currentChain={s.currentChain}\n          chainName={s.chainName}\n          iconOnly={s.iconOnly}\n          open={s.open}\n          isPending={s.isPending}\n          variant={s.variant}\n          monoColor={s.monoColor}\n          ChainIcon={s.ChainIcon}\n          ChevronIcon={s.ChevronIcon}\n          LoaderIcon={s.LoaderIcon}\n        />\n      </PopoverTrigger>\n      <PopoverContent className=\"min-w-48 flex flex-col gap-4 w-full\">\n        <div className=\"text-sm font-medium\">Select Network</div>\n        <ChainListContent\n          mounted={s.mounted}\n          chains={s.chains}\n          groupByNetwork={s.groupByNetwork}\n          error={s.error}\n          cancelled={s.cancelled}\n          unsupported={s.unsupported}\n          chainMeta={s.chainMeta}\n          currentChainId={s.currentChain?.id}\n          pendingChainId={s.pendingChainId}\n          isPending={s.isPending}\n          onSwitch={s.onSwitch}\n          ChainIcon={s.ChainIcon}\n          LoaderIcon={s.LoaderIcon}\n          variant={s.variant}\n          monoColor={s.monoColor}\n        />\n      </PopoverContent>\n    </Popover>\n  );\n}\n\nexport function ChainSelectorDialog(props: ChainSelectorProps) {\n  const s = useChainSelectorParts(props);\n\n  return (\n    <Dialog open={s.open} onOpenChange={s.onOpenChange}>\n      <DialogTrigger\n        className={triggerClassName(s)}\n        aria-label={s.ariaLabel}\n        title={s.tooltipTitle}\n      >\n        <TriggerContent\n          mounted={s.mounted}\n          unsupported={s.unsupported}\n          currentChain={s.currentChain}\n          chainName={s.chainName}\n          iconOnly={s.iconOnly}\n          open={s.open}\n          isPending={s.isPending}\n          variant={s.variant}\n          monoColor={s.monoColor}\n          ChainIcon={s.ChainIcon}\n          ChevronIcon={s.ChevronIcon}\n          LoaderIcon={s.LoaderIcon}\n        />\n      </DialogTrigger>\n      <DialogContent className=\"min-w-96\">\n        <DialogHeader>\n          <DialogTitle>Select Network</DialogTitle>\n        </DialogHeader>\n        <ChainListContent\n          mounted={s.mounted}\n          chains={s.chains}\n          groupByNetwork={s.groupByNetwork}\n          error={s.error}\n          cancelled={s.cancelled}\n          unsupported={s.unsupported}\n          chainMeta={s.chainMeta}\n          currentChainId={s.currentChain?.id}\n          pendingChainId={s.pendingChainId}\n          isPending={s.isPending}\n          onSwitch={s.onSwitch}\n          ChainIcon={s.ChainIcon}\n          LoaderIcon={s.LoaderIcon}\n          variant={s.variant}\n          monoColor={s.monoColor}\n        />\n      </DialogContent>\n    </Dialog>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "docs": "Docs: https://0xui.io/components/chain-selector.md",
  "categories": [
    "network"
  ],
  "type": "registry:component"
}