What It Does
- 👁 Display an address in a compact way while retaining the ability to see it in full.
- 👉 Lets users copy the address to the clipboard and open it in a block explorer.
- 🏷 ENS resolution works out of the box if wagmi is present.
- 🌈 Six themes to choose from or to customize as desired.
- 🎹 Accessible: keyboard navigation behaves as expected.
- 💆♀️ Zero configuration: just import and drop
<Addreth />
in your app. - 🪚 Customize the default configuration with
<AddrethConfig />
(optional). - 📦 Small: 6.4 KB, styles and themes included, and no external dependencies.
How It Looks
Design by Paty Davila.
Getting Started
npm i -S addreth
pnpm add addreth
yarn add addreth
import { Addreth } from "addreth";
function App() {
return (
<main>
<Addreth address="0x…" />
</main>
);
}
From here, the easiest is to check out the demo page demo page. You can also find the API documentation below.
API
<Addreth />
The <Addreth />
component only requires the address
prop to be set. It will display the address in a compact way while provide a convenient set of features. Multiple props are available to customize it in different ways:
address
The address
prop is the only required prop, and it must be a valid Ethereum address.
<Addreth address="0x0000000000000000000000000000000000000000" />;
actions
The actions
prop allows to control the action buttons inside the badge. It can be set to "all"
(default), "copy"
, "explorer"
or "none"
.
<Addreth address="0x…" actions="copy" />;
ens
The ens
prop allows to control whether to use the ENS name resolution, and it is enabled by default.
This is an alias for icon="identicon"
and label="address"
.
<Addreth address="0x…" ens={false} />;
explorer
The explorer
prop allows to generate the name and URL of a given block explorer (e.g. Etherscan).
<Addreth
address="0x…"
explorer={(address) => ({
name: "Base",
url: `https://basescan.com/address/${address}`,
})}
font
and fontMono
The font
and fontMono
props allow to control the font names used for the badge and buttons. If fontMono
is specified, it will be applied to the address specifically. If neither font
nor fontMono
are specified, the fonts will be inherited from the web page.
<Addreth address="0x…" font="Anonymous Pro" />;
icon
The icon
prop allows to control the icon displayed in the badge. It can be set to "ens"
(default), "identicon"
, false
or null
.
- If set to
"ens"
and the app uses wagmi, the ENS avatar corresponding to the address, if it exists, will be displayed. Otherwise, the icon will fallback to "identicon"
. - If set to
"identicon"
, the identicon corresponding to the address will be displayed. - If set to
false
or null
, no icon will be displayed. - If set to a function, it will be called with the address as argument and must return either React element to replace the icon entirely, or a string to provide an image URL.
<Addreth address="0x…" icon="ens" />;
<Addreth address="0x…" icon="identicon" />;
<Addreth address="0x…" icon={false} />;
<Addreth
address="0x…"
icon={(address) => `https://example.com/identicon/${address}.svg`}
/>;
<Addreth
address="0x…"
icon={(address) => (
<img src={`https://example.com/identicon/${address}.svg`} />
)}
/>;
label
The label
prop allows to control the label displayed in the badge. It can be set to "ens"
(default), "address"
or a function.
- If set to
"ens"
and the app uses wagmi, the ENS name corresponding to the address, if it exists, will be displayed. Otherwise, the label will fallback to "address"
. - If set to
"address"
, the address will be displayed, shortened to shortenAddress
characters on each side (4 by default). - If set to a function, it will be called with the address as argument and must return a React node.
<Addreth address="0x…" label="ens" />;
<Addreth address="0x…" label="address" />;
<Addreth
address="0x…"
label={(address) => <b>{address}</b>}
/>;
maxWidth
The maxWidth
prop allows to control the maximum width of the badge. If not specified, the badge will adapt to its parent width.
<Addreth address="0x…" maxWidth={200} />;
popupNode
The popupNode
prop allows to control the node used to render the popup. It defaults to document.body
.
shortenAddress
The shortenAddress
prop allows to control the number of first and last characters to show for the address. It defaults to 4
. Set it to false
to display the full address.
<Addreth address="0x…" shortenAddress={6} />;
stylesId
The stylesId
prop allows to control the ID attribute of the style element used to inject the CSS in the page. It defaults to "addreth-styles"
.
theme
The theme
prop allows to control the theme used for the badge and buttons. It can be set to one of the provided themes, or used to define a custom theme.
Available themes:
light
(default)dark
unified-light
unified-dark
simple-light
simple-dark
You can also define a custom theme by passing an object. If base
is provided, it will extend from that theme. Otherwise, it will extend the default theme (light
), or the parent one if provided (see AddrethConfig
to check how to define a parent config).
type ThemeDeclaration = {
base?: ThemeName;
textColor?: Color;
secondaryColor?: Color;
focusColor?: Color;
fontSize?: number;
badgeBackground?: Color;
badgeRadius?: number;
badgeIconRadius?: number;
badgeGap?: number;
badgeHeight?: number;
badgePadding?: number;
badgeLabelPadding?: number;
popupBackground?: Color;
popupRadius?: number;
popupShadow?: string;
};
See theme.ts for more details.
underline
Display the label underlined.
<Addreth address="0x…" underline />;
uppercase
Display the label in uppercase.
<Addreth address="0x…" uppercase />;
<AddrethConfig />
Having to wrap <Addreth />
in order to provide your desired default configuration can be tedious, which is why <AddrethConfig />
is provided. It allows to customize the default configuration of <Addreth />
and support the same props, except address
.
import { Addreth, AddrethConfig } from "addreth";
function App() {
return (
<AddrethConfig font="Anonymous Pro" theme="dark">
<Addreth address="0x…" />
</AddrethConfig>
);
}
FAQ
Does it work with Ethers.js or other Ethereum libraries?
You can wrap the component and set icon
and label
to anything you want, including ENS name and avatar coming from another library.
Next.js gives me an error about not being able to resolve wagmi
.
Next.js tries to resolve all imports including when using import()
which addreth uses to detect the presence of wagmi. A solution is to tell Next.js to ignore this dependency by adding this to your next.config.js:
webpack(conf) {
conf.resolve.fallback = { wagmi: false };
return conf;
}
License
MIT