'use client'; import { Address, Avatar, EthBalance, Identity, Name } from '@/identity'; import { cn } from '@/styles/theme'; import { useFloating, autoUpdate, offset, flip, shift, FloatingPortal, } from '@floating-ui/react'; import { useEffect } from 'react'; import type { WalletAdvancedQrReceiveProps, WalletAdvancedSwapProps, } from '../types'; import { WalletDropdownContent } from './WalletDropdownContent'; import { WalletDropdownDisconnect } from './WalletDropdownDisconnect'; import { WalletDropdownLink } from './WalletDropdownLink'; import { useWalletContext } from './WalletProvider'; import { useAccount } from 'wagmi'; import { Token } from '@/token'; import { useOutsideClick } from '@/internal/hooks/useOutsideClick'; export type WalletDropdownProps = { children?: React.ReactNode; /** Optional className override for top div element */ className?: string; classNames?: { container?: string; qr?: WalletAdvancedQrReceiveProps['classNames']; swap?: WalletAdvancedSwapProps['classNames']; }; swappableTokens?: Token[]; }; const defaultWalletDropdownChildren = ( <>
Wallet ); export function WalletDropdown({ children, className, classNames, swappableTokens, }: WalletDropdownProps) { const { breakpoint, isSubComponentOpen, showSubComponentAbove, alignSubComponentRight, connectRef, handleClose, } = useWalletContext(); const { address } = useAccount(); const { refs, floatingStyles } = useFloating({ open: isSubComponentOpen, placement: showSubComponentAbove ? alignSubComponentRight ? 'top-end' : 'top-start' : alignSubComponentRight ? 'bottom-end' : 'bottom-start', middleware: [ offset(6), flip({ fallbackPlacements: [ 'top-start', 'top-end', 'bottom-start', 'bottom-end', ], }), shift({ padding: 8 }), ], whileElementsMounted: autoUpdate, }); useOutsideClick(refs.floating, handleClose); useEffect(() => { if (connectRef?.current) { refs.setReference(connectRef.current); } }, [connectRef, refs]); if (!address || !breakpoint || !isSubComponentOpen) { return null; } return (
{children || defaultWalletDropdownChildren}
); }