connectButton
UI Component for Connecting to Plug Wallet
ConnectPlugButton.tsx
ConnectPlugButton.tsx
This React component provides a simple, user-friendly button to connect your application to the Plug Internet Computer wallet.
It uses the usePlug
hook to handle wallet detection, connection, and disconnection.
Props
type Props = {
whitelist?: string[]; // List of canister IDs the wallet should authorize
host?: string; // Network host (e.g., "https://ic0.app" for ICP mainnet)
className?: string; // Optional CSS class for custom styling
};
whitelist
— Canisters that the Plug wallet will allow access to.host
— Target ICP network (mainnet or local development).className
— Optional custom CSS class for button styling.
Logic
Initialize Wallet State
const plug = usePlug({ whitelist, host });
Uses the
usePlug
hook to check if Plug is available and manage its connection state.
Shorten Principal
const short = (p?: string) => (p ? `${p.slice(0, 5)}...${p.slice(-5)}` : "");
Helper function to display a shortened version of the user’s principal.
When Plug Is Not Installed
if (!plug.available) { return <a href="https://plugwallet.ooo/">Install Plug</a>; }
Shows an Install Plug link directing users to the official site.
When Plug Is Connected
if (plug.connected) { return ( <div> Connected: {short(plug.principal)} <button onClick={plug.disconnect}>Disconnect</button> </div> ); }
Displays the connected principal and a Disconnect button.
When Plug Is Available but Not Connected
return ( <button onClick={() => plug.connect({ whitelist, host })}> Connect Plug </button> );
Shows a Connect Plug button that triggers the connection flow.
Last updated