0.15.35 • Published 8 months ago

@solana/wallet-adapter-react v0.15.35

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
8 months ago

@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;
    }
    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}
        </>
    );
}
gamba-alphagamba-alpha-reactgamba-beta-react@banditbadgod/react@deliquid/react@cardinal/namespace-components@infinitebrahmanuniverse/nolb-_sol@everything-registry/sub-chunk-850@keval6706/commonggsdk-candy-shop-sdkjupyter_swapjupyter_swap_widgetjupiter-swap-widgetith-react-providersivory-fund-cryptoivory-netprinceivory-npm-testivory-pay-moonshot-npmivorypay-checkoutivorypay-checkout-moon-caesarivorypay-fund-cryptoivorypay-fund-crypto-npm-netivorypay112-fund-crypto-npmivorypay113-fund-crypto-npmivorypay114-fund-crypto-npmlaudantiumaccusantiumlevelingdsov-utilsf222rontest_npm_something_elsegamba-game-pack-v2gamba-platform-templategamba-reactgamba-react-ui-v2gamba-react-v2oppala-ve-de-cuppalakyzr-marketplace-sdkkana-widgetninoxmintmy-random-test-package76notstratareactnimbus-swapnimbus-swap-widgetmpl-commonmy-app-ivoryplay2earnpool-party-wallet-adapterpolar-auth-complexiapluto-reactmedusa-payment-streampayoyster-commonoyster-common-sotatekreact-solana@cubist-collective/cubist-games-lib@dewicats/connect-button@dicksonp/open-wallet-adapter@dicksonp/test-rollup@dgrlabs/metaplex-commoncandy-machine-clientcherry-wallet-adapter-react-uitest-vite-package-v2test-vite-pkg-v2@drappi/metaplex-js-commonchomping-glass-teathud-sol-toolsstreamnft-solstreamnft-sol-teststreamnfttechstreamnfttech_solstreamnfttech_sol_teststreamnfttechteststream-swap-uisolstaketestsdkforstreammoneycoinopreacttedddfgdsst-ui@cosmic-lab/data-sourcetech-solana-connectorbuddy.link@magicblock-labs/gum-react-sdk@lifi/widget@kyraa/solana@plutohq/pluto-react@pngfi/react-hooks@plege/react@plege/react9@neptuneprotocol/neptune-sdk@muckington/react-components-web3@muckington/react-components-web3-solana@puneethgowda/react@elementspay/circle-widgets-sdk-canaryziyar-metaspace@fractalwagmi/wallet-adapter-react-ui@quyx/react@renec-foundation/sdk-easytoken@renec-foundation/wallet-adapter-react@noxal/web-commonuse-solana-gameurchin-web3-cmsvoluptatumneque@oysterr/common
0.15.34-alpha.2

10 months ago

0.15.34-alpha.3

9 months ago

0.15.35

8 months ago

0.15.33

10 months ago

0.15.34

9 months ago

0.15.33-alpha.0

11 months ago

0.15.33-alpha.1

11 months ago

0.15.29

1 year ago

0.15.31

1 year ago

0.15.32

1 year ago

0.15.30

1 year ago

0.15.28

1 year ago

0.15.26

1 year ago

0.15.27

1 year ago

0.15.20

2 years ago

0.15.24

1 year ago

0.15.25

1 year ago

0.15.22

2 years ago

0.15.23

2 years ago

0.15.21-rc.0

2 years ago

0.15.21-rc.1

2 years ago

0.15.21-rc.2

2 years ago

0.15.21-rc.7

2 years ago

0.15.21-rc.8

2 years ago

0.15.21-rc.9

2 years ago

0.15.21-rc.3

2 years ago

0.15.21-rc.4

2 years ago

0.15.21-rc.5

2 years ago

0.15.21-rc.6

2 years ago

0.15.18

2 years ago

0.15.19

2 years ago

0.15.17-alpha.0

2 years ago

0.15.14

2 years ago

0.15.17

2 years ago

0.15.15

2 years ago

0.15.16

2 years ago

0.15.13

2 years ago

0.15.11

2 years ago

0.15.12

2 years ago

0.15.6

2 years ago

0.15.7

2 years ago

0.15.8

2 years ago

0.15.9

2 years ago

0.15.10

2 years ago

0.15.5

2 years ago

0.15.4

2 years ago

0.15.0

2 years ago

0.15.1

2 years ago

0.15.2

2 years ago

0.15.3

2 years ago

0.14.0

2 years ago

0.14.1

2 years ago

0.7.2

2 years ago

0.13.0

3 years ago

0.13.1

3 years ago

0.12.6

3 years ago

0.12.4

3 years ago

0.12.5

3 years ago

0.12.3

3 years ago

0.12.2

3 years ago

0.12.1

3 years ago

0.11.0

3 years ago

0.12.0

3 years ago

0.10.3

3 years ago

0.10.1

3 years ago

0.10.2

3 years ago

0.10.0

3 years ago

0.9.2

3 years ago

0.9.0

3 years ago

0.9.1

3 years ago

0.8.1

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.8.0

3 years ago

0.7.1

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago