npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solana/wallet-adapter-react

v0.15.35

Published

## Creating a custom connect button

Downloads

158,175

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 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}
        </>
    );
}