Skip to content

Commit

Permalink
feat: Add openModalOnWrongNetwork option to customize whether displ…
Browse files Browse the repository at this point in the history
…ay SwitchNetworkModal if on the wrong network
  • Loading branch information
wenty22 committed Jun 21, 2024
1 parent ab14858 commit 36affe5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-dolls-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@node-real/walletkit': patch
---

Add `openModalOnWrongNetwork` option to customize whether display SwitchNetworkModal if on the wrong network
3 changes: 1 addition & 2 deletions packages/walletkit/__dev__/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const client = createClient(

const options: WalletKitOptions = {
initialChainId: 204,
hideInnerModal: true,
closeModalAfterSwitchingNetwork: true,
};

export default function App() {
Expand All @@ -66,7 +66,6 @@ export default function App() {
<WalletKitButton />
<Example />
<SwitchNetworkModal />
<WalletKitEmbeddedModal />
</WalletKitProvider>
</WagmiConfig>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useEffect, useMemo, useState } from 'react';
import { useAccount, useNetwork } from 'wagmi';
import { SwitchNetworkModalContext, SwitchNetworkModalOpenOptions } from './context';
import { toast } from '@/base/components/toast';
import { useWalletKitContext } from '@/components/WalletKitProvider/context';

export interface SwitchNetworkProviderProps {
children: React.ReactNode;
Expand All @@ -16,6 +17,7 @@ export function SwitchNetworkProvider(props: SwitchNetworkProviderProps) {

const { chain } = useNetwork();
const { isConnected } = useAccount();
const { options } = useWalletKitContext();

const value = useMemo(() => {
return {
Expand All @@ -42,7 +44,7 @@ export function SwitchNetworkProvider(props: SwitchNetworkProviderProps) {
}, [isClosable, isConnected, isOpen, onClose, onOpen]);

useEffect(() => {
if (isConnected) {
if (isConnected && options.openModalOnWrongNetwork) {
const timer = setTimeout(() => {
if (chain?.unsupported) {
value.onOpen({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export interface WalletKitOptions {
closeModalOnEsc?: boolean;
closeModalOnOverlayClick?: boolean;

openModalOnWrongNetwork?: boolean;

walletDownloadUrl?: string;
chainsConfig?: ChainProps[];
onClickWallet?: (connector: Connector, e?: React.MouseEvent) => undefined | boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export function getDefaultProviderOptions(options: WalletKitOptions) {
closeModalOnEsc: true,
closeModalOnOverlayClick: true,

openModalOnWrongNetwork: true,

walletDownloadUrl: `https://trustwallet.com/`,
onError,
...restOptions,
Expand Down
4 changes: 4 additions & 0 deletions packages/walletkit/src/wallets/custom/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class CustomConnector extends InjectedConnector {
this.id = id;
this.shimDisconnectKey = `${this.id}.shimDisconnect`;
}

public async getProvider() {
return this.options.getProvider();
}
}
3 changes: 3 additions & 0 deletions website/src/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ interface WalletKitOptions {
closeModalOnEsc?: boolean;
closeModalOnOverlayClick?: boolean;

// Display SwitchNetworkModal if on the wrong network
openModalOnWrongNetwork?: boolean;

walletDownloadUrl?: string;
chainsConfig?: ChainProps[];

Expand Down

0 comments on commit 36affe5

Please sign in to comment.