Dev Inoue 748767d685 docs: fix and simplify button handler example (#1054) 9 meses atrás
..
__mocks__ e632bdf06a Update mocks in tests 2 anos atrás
src e25e7971fd Fix: MWA Select but No Connect Bug (#1049) 9 meses atrás
CHANGELOG.md 292d8b34cb Revert "Version Packages" (#885) 1 ano atrás
LICENSE 10655364d6 move packages 4 anos atrás
README.md 748767d685 docs: fix and simplify button handler example (#1054) 9 meses atrás
jest.config.js 8949881175 Repair tests and run in CI (#571) 3 anos atrás
jest.resolver.cjs 8949881175 Repair tests and run in CI (#571) 3 anos atrás
package.json d7aa33d96f `pnpm` to version 9 (#963) 1 ano atrás
tsconfig.cjs.json 6943bdd819 Select the Mobile Wallet Adapter by default when on mobile 3 anos atrás
tsconfig.esm.json 6943bdd819 Select the Mobile Wallet Adapter by default when on mobile 3 anos atrás
tsconfig.json 2e1977314c Configure Turborepo to build, test, and lint everything 2 anos atrás
tsconfig.tests.json 33af6643ac update tsconfig 3 anos atrás

README.md

@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 of wallets, then call onSelectWallet() 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 calls onConnect() when clicked.
  • disconnecting \ When in this state, the last-connected wallet is in mid-disconnection.
  • connected \ In this state, you have access to the connected publicKey and an onDisconnect() method that you can call to disconnect from the wallet. At any time you can call onSelectWallet() to change wallets.
  • connecting \ When in this state, the wallet is in mid-connection.

Functions

  • onConnect \ Connects the currently selected wallet. Available in the has-wallet state.
  • onDisconnect \ Disconnects the currently selected wallet. Available in the has-wallet, connected, and connecting states.
  • onSelectWallet() \ Calls the onSelectWallet() function that you supplied as config to useWalletMultiButton. That function receives the list of wallets and offers you an onSelectWallet() 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;
    }
    return (
        <>
            <button
                disabled={buttonState === 'connecting' || buttonState === 'disconnecting'}
                onClick={() => {
                    switch (buttonState) {
                        case 'connected':
                            onDisconnect?.();
                            break;
                        case 'connecting':
                        case 'disconnecting':
                            break;
                        case 'has-wallet':
                            onConnect?.();
                            break;
                        case 'no-wallet':
                            onSelectWallet?.();
                            break;
                    }
                }}
            >
                {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}
        </>
    );
}