Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/components/AddressDisplay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,40 @@ describe("AddressDisplay", () => {
render(<AddressDisplay address={address} label="Destination" />);
expect(screen.getByText("Destination")).toBeInTheDocument();
});

it("fires onCopy callback after successful clipboard write", async () => {
const onCopy = vi.fn();
render(<AddressDisplay address={address} onCopy={onCopy} />);

const copyBtn = screen.getByRole("button", { name: "Copy address" });
await act(async () => {
fireEvent.click(copyBtn);
});

expect(onCopy).toHaveBeenCalled();
});

it("adds select-all class to the address span when showFull is true", () => {
render(<AddressDisplay address={address} showFull />);
const el = screen.getByText(address);
expect(el.className).toContain("select-all");
});

it("does not add select-all class when showFull is false", () => {
render(<AddressDisplay address={address} />);
const el = screen.getByText("GBAMQXTQ...ZJQQQQ");
expect(el.className).not.toContain("select-all");
});

it("applies correct font size for sm size when showFull is true", () => {
render(<AddressDisplay address={address} showFull size="sm" />);
const el = screen.getByText(address);
expect(el.className).toContain("text-[10px]");
});

it("applies correct font size for lg size when showFull is true", () => {
render(<AddressDisplay address={address} showFull size="lg" />);
const el = screen.getByText(address);
expect(el.className).toContain("text-[13px]");
});
});
19 changes: 16 additions & 3 deletions src/components/AddressDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,48 @@ import { useState } from "react";
import { cn } from "@/lib/utils";
import { truncateAddress } from "@/lib/utils";

interface AddressDisplayProps {
export interface AddressDisplayProps {
address: string;
start?: number;
end?: number;
showFull?: boolean;
className?: string;
label?: string;
onCopy?: () => void;
size?: "sm" | "md" | "lg";
}

const sizeConfig = {
sm: { text: "text-[10px]", icon: 10 },
md: { text: "text-[11px]", icon: 12 },
lg: { text: "text-[13px]", icon: 14 },
} as const;

export function AddressDisplay({
address,
start = 8,
end = 6,
showFull = false,
className,
label,
onCopy,
size = "md",
}: AddressDisplayProps) {
const [copied, setCopied] = useState(false);

async function copy() {
try {
await navigator.clipboard.writeText(address);
setCopied(true);
onCopy?.();
setTimeout(() => setCopied(false), 2000);
} catch {
/* fallback */
}
}

const display = showFull ? address : truncateAddress(address, start, end);
const { text, icon: iconSize } = sizeConfig[size];

return (
<div className={cn("flex flex-col gap-1", className)}>
Expand All @@ -48,7 +60,8 @@ export function AddressDisplay({
data-address
className={cn(
"break-all leading-relaxed",
showFull ? "text-[11px]" : "",
showFull && text,
showFull && "select-all",
)}
title={address}
>
Expand All @@ -67,7 +80,7 @@ export function AddressDisplay({
>
<HugeiconsIcon
icon={copied ? Tick01Icon : Copy01Icon}
size={12}
size={iconSize}
color="currentColor"
strokeWidth={2}
/>
Expand Down
Loading