forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyAddressButton.tsx
More file actions
51 lines (47 loc) · 1.37 KB
/
Copy pathCopyAddressButton.tsx
File metadata and controls
51 lines (47 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use client";
import { CheckIcon, CopyIcon } from "@chakra-ui/icons";
import { IconButton, Tooltip } from "@chakra-ui/react";
import { useState } from "react";
/**
* Small copy-to-clipboard icon button for a Stellar address (issue #236).
* Stellar addresses are 56 characters — manual selection is error-prone,
* so every place an address is displayed should offer a one-click copy.
*/
export default function CopyAddressButton({
address,
size = "xs",
}: {
address: string;
size?: "2xs" | "xs" | "sm";
}) {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(address);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
// Clipboard may be unavailable in hardened browser contexts.
}
};
return (
<Tooltip label={copied ? "Copied!" : "Copy address"} hasArrow fontSize="xs">
<IconButton
aria-label="Copy address"
icon={copied ? <CheckIcon /> : <CopyIcon />}
onClick={(e) => {
e.stopPropagation();
e.preventDefault();
void handleCopy();
}}
size={size}
variant="ghost"
color={copied ? "app.accent" : "app.muted"}
_hover={{ color: "app.accent", bg: "app.surfaceHover" }}
minW="auto"
h="auto"
p={1}
/>
</Tooltip>
);
}