Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
add copy-address-on-click to blocky displays
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin Kennedy committed Oct 19, 2023
1 parent 145cdf3 commit c269957
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions src/AddressIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,62 @@
import { Avatar, Tooltip } from "@mui/material";
import { Avatar, Snackbar, Tooltip } from "@mui/material";
import { blo } from "blo";
import React, { type FunctionComponent } from "react";
import React, { useState, type FunctionComponent } from "react";

export const AddressIcon: FunctionComponent<{
address: `0x${string}`;
}> = (props: { address: `0x${string}` }) => {
const [open, setOpen] = useState(false);

// stolen from https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript#30810322
function copy(text: string): undefined {
const textArea = document.createElement("textarea");
textArea.value = text;

// Avoid scrolling to bottom
textArea.style.top = "0";
textArea.style.left = "0";
textArea.style.position = "fixed";

document.body.appendChild(textArea);
textArea.focus();
textArea.select();

try {
const successful = document.execCommand("copy");
if (successful) {
setOpen(true);
}
} catch (err) {
console.error("Fallback: Oops, unable to copy", err);
}

document.body.removeChild(textArea);
return undefined;
}

return (
<Tooltip title={props.address}>
<Avatar
{...props}
src={blo(props.address)}
sx={{ width: 48, height: 48 }}
<div>
<Tooltip title={props.address}>
<Avatar
{...props}
src={blo(props.address)}
sx={{ width: 48, height: 48 }}
onClick={() => {
copy(props.address);
}}
></Avatar>
</Tooltip>
<Snackbar
open={open}
autoHideDuration={1500}
onClose={() => {
setOpen(false);
}}
message={(() => {
return `Copied ${props.address}`;
})()}
/>
</Tooltip>
</div>
);
};

Expand Down

0 comments on commit c269957

Please sign in to comment.