Skip to content

Instantly share code, notes, and snippets.

@wchargin
Last active August 8, 2023 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wchargin/af58d954969f14f9dc0cff47ee25e62d to your computer and use it in GitHub Desktop.
Save wchargin/af58d954969f14f9dc0cff47ee25e62d to your computer and use it in GitHub Desktop.
// Visually looks like an ellipsis, like "0x123...def", but the underlying text
// actually has the whole address, so when you select it to copy-paste you get
// the whole thing.
const ShortAddress = ({ address, ensAddress }) => {
if (ensAddress != null) return <span>{ensAddress}</span>;
const nibbles = 3; // to keep, at each side
const prefix = "0x".length;
const start = address.slice(0, prefix + nibbles);
const mid = address.slice(prefix + nibbles, -nibbles);
const end = address.slice(-nibbles);
const ellipsisWidth = "0.8em";
return (
<span className="relative">
{/* first, the ellipsis in the middle (displayed, not copied) */}
<span className="absolute select-none pointer-events-none z-[1]">
<span className="invisible">{start}</span>
<span
className="inline-flex flex-grow justify-around"
style={{ width: ellipsisWidth }}
>
<span>.</span>
<span>.</span>
<span>.</span>
</span>
</span>
{/* now, the full address */}
<span>{start.slice(0, -1)}</span>
<span style={{ letterSpacing: ellipsisWidth }}>{start.slice(-1)}</span>
<span className="text-[0px]">{mid}</span>
<span>{end}</span>
</span>
);
};
export default ShortAddress;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment