Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: address tooltip #1188

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import truncateAddress from "@/utils/truncate-address";
import { useConnection } from "@account-kit/react";
import { useMemo } from "react";
import {
TooltipProvider,
Tooltip,
TooltipTrigger,
TooltipContent,
TooltipArrow,
} from "@radix-ui/react-tooltip";
import { useState } from "react";

export const UserAddressLink = ({ address }: { address: string | null }) => {
const connection = useConnection();
const [showCopied, setShowCopied] = useState(false);
const [isOpen, setIsOpen] = useState(false);
const truncatedAddress = truncateAddress(address ?? "");
const addressBlockExplorerUrl = useMemo(() => {
if (!address || !connection.chain.blockExplorers) {
return null;
}

return `${connection.chain.blockExplorers?.default.url}/address/${address}`;
}, [address, connection]);
const handleClick = async () => {
if (!address) return;
await navigator.clipboard.writeText(address);
setShowCopied(true);
setIsOpen(true);
setTimeout(() => setShowCopied(false), 2000);
};

return (
<a
target="_blank"
className="text-fg-primary underline text-md md:text-sm"
href={addressBlockExplorerUrl ?? "#"}
>
{truncatedAddress}
</a>
<TooltipProvider>
<Tooltip open={isOpen} onOpenChange={setIsOpen}>
<TooltipTrigger asChild>
<button
onClick={handleClick}
className="text-fg-primary underline text-md md:text-sm"
>
{truncatedAddress}
</button>
</TooltipTrigger>
<TooltipContent
align="start"
alignOffset={-16}
className="bg-foreground text-background px-3 py-2 rounded-md"
>
<TooltipArrow />
<p className="text-xs">{showCopied ? "Copied!" : address}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
);
};
Loading