@xandeum/wallet-adapter-react
v0.15.66
Published
## Creating a custom connect button
Downloads
3
Readme
@solana/wallet-adapter-react
Creating a custom connect button
This package exports a series of hooks that you can use to create custom wallet connection buttons. They manage the state of the wallet connection for you, and return helper methods that you can attach to your event handlers.
What follows is the documentation for useWalletMultiButton()
.
States
no-wallet
In this state you are neither connected nor is there a wallet selected. Allow your users to select from the list ofwallets
, then callonSelectWallet()
with the name of the wallet they chose.has-wallet
This state implies that there is a wallet selected, but that your app is not connected to it. Render a connect button that callsonConnect()
when clicked.disconnecting
When in this state, the last-connected wallet is in mid-disconnection.connected
In this state, you have access to the connectedpublicKey
and anonDisconnect()
method that you can call to disconnect from the wallet. At any time you can callonSelectWallet()
to change wallets.connecting
When in this state, the wallet is in mid-connection.
Functions
onConnect
Connects the currently selected wallet. Available in thehas-wallet
state.onDisconnect
Disconnects the currently selected wallet. Available in thehas-wallet
,connected
, andconnecting
states.onSelectWallet()
Calls theonSelectWallet()
function that you supplied as config touseWalletMultiButton
. That function receives the list ofwallets
and offers you anonSelectWallet()
callback that you can call with the name of the wallet to switch to.
Example
function CustomConnectButton() {
const [walletModalConfig, setWalletModalConfig] = useState<Readonly<{
onSelectWallet(walletName: WalletName): void;
wallets: Wallet[];
}> | null>(null);
const { buttonState, onConnect, onDisconnect, onSelectWallet } = useWalletMultiButton({
onSelectWallet: setWalletModalConfig,
});
let label;
switch (buttonState) {
case 'connected':
label = 'Disconnect';
break;
case 'connecting':
label = 'Connecting';
break;
case 'disconnecting':
label = 'Disconnecting';
break;
case 'has-wallet':
label = 'Connect';
break;
case 'no-wallet':
label = 'Select Wallet';
break;
}
const handleClick = useCallback(() => {
switch (buttonState) {
case 'connected':
return onDisconnect;
case 'connecting':
case 'disconnecting':
break;
case 'has-wallet':
return onConnect;
case 'no-wallet':
return onSelectWallet;
break;
}
}, [buttonState, onDisconnect, onConnect, onSelectWallet]);
return (
<>
<button disabled={buttonState === 'connecting' || buttonState === 'disconnecting'} onClick={handleClick}>
{label}
</button>
{walletModalConfig ? (
<Modal>
{walletModalConfig.wallets.map((wallet) => (
<button
key={wallet.adapter.name}
onClick={() => {
walletModalConfig.onSelectWallet(wallet.adapter.name);
setWalletModalConfig(null);
}}
>
{wallet.adapter.name}
</button>
))}
</Modal>
) : null}
</>
);
}