Skip to content

Commit e9c23ad

Browse files
committed
[SDK] Add onClose callback to details modal (#5605)
CNCT-2444 CNCT-2444 <!-- start pr-codex --> --- ## PR-Codex overview This PR primarily introduces an `onClose` callback for the `ConnectButton` component's details modal, enhancing its functionality. It also improves the rendering of various components and adds tests for new features. ### Detailed summary - Added `onClose` callback to `ConnectButtonProps` for modal closure. - Updated `ConnectButton` to utilize the `onClose` callback. - Improved test coverage for `NetworkSwitcherButton` and `ChainIcon`. - Enhanced `fetchChainIcon` functionality to handle various icon resolvers. - Introduced `ChainActiveDot` for visual representation of active chains. - Updated `NetworkSelector` and `DetailsModal` components for better user experience. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent ce3e850 commit e9c23ad

File tree

13 files changed

+887
-236
lines changed

13 files changed

+887
-236
lines changed

.changeset/metal-mails-ring.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
- Add onClose callback to Connect Details modal
6+
7+
```tsx
8+
<ConnectButton
9+
detailsModal={{
10+
onClose: (screen: string) => {
11+
// The last screen name that was being shown when user closed the modal
12+
console.log({ screen });
13+
}
14+
}}
15+
/>
16+
```
17+
18+
- Small fix for ChainIcon: Always resolve IPFS URI
19+
20+
- Improve test coverage

packages/thirdweb/src/react/core/hooks/connection/ConnectButtonProps.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,12 @@ export type ConnectButton_detailsModalOptions = {
339339
*/
340340
allowLinkingProfiles?: boolean;
341341
};
342+
343+
/**
344+
* @param screen The screen's name that was last shown when user closed the modal
345+
* @returns
346+
*/
347+
onClose?: (screen: string) => void;
342348
};
343349

344350
/**

packages/thirdweb/src/react/web/ui/ConnectWallet/ConnectButton.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,17 @@ const TW_CONNECT_WALLET = "tw-connect-wallet";
262262
* />
263263
* ```
264264
*
265+
* ### Callback for when the details modal is closed
266+
* ```tsx
267+
* <ConnectButton
268+
* detailsModal={{
269+
* onClose: (screen: string) => {
270+
* console.log({ screen });
271+
* }
272+
* }}
273+
* />
274+
* ```
275+
*
265276
* @param props
266277
* Props for the `ConnectButton` component
267278
*

packages/thirdweb/src/react/web/ui/ConnectWallet/Details.test.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
ConnectedWalletDetails,
2222
DetailsModal,
2323
InAppWalletUserInfo,
24+
NetworkSwitcherButton,
25+
StyledChevronRightIcon,
2426
SwitchNetworkButton,
2527
detailsBtn_formatFiatBalanceForButton,
2628
detailsBtn_formatTokenBalanceForButton,
@@ -584,4 +586,57 @@ describe("Details Modal", () => {
584586
expect(closeModalMock).toHaveBeenCalled();
585587
});
586588
});
589+
590+
it("NetworkSwitcherButton should not render if no active chain", () => {
591+
const { container } = render(
592+
<AccountProvider address={VITALIK_WALLET} client={client}>
593+
<NetworkSwitcherButton
594+
setScreen={(scr) => console.log(scr)}
595+
disableSwitchChain={false}
596+
displayBalanceToken={undefined}
597+
client={client}
598+
/>
599+
</AccountProvider>,
600+
);
601+
602+
const element = container.querySelector(
603+
".tw-internal-network-switcher-button",
604+
);
605+
expect(element).toBeFalsy();
606+
});
607+
608+
it("NetworkSwitcherButton should render if there is an active chain", async () => {
609+
vi.mock(
610+
"../../../../react/core/hooks/wallets/useActiveWalletChain.js",
611+
() => ({
612+
useActiveWalletChain: vi.fn(),
613+
}),
614+
);
615+
vi.mocked(useActiveWalletChain).mockReturnValue(base);
616+
const { container } = render(
617+
<AccountProvider address={VITALIK_WALLET} client={client}>
618+
<NetworkSwitcherButton
619+
setScreen={(scr) => console.log(scr)}
620+
disableSwitchChain={false}
621+
displayBalanceToken={undefined}
622+
client={client}
623+
/>
624+
</AccountProvider>,
625+
);
626+
await waitFor(
627+
() => {
628+
const element = container.querySelector(
629+
".tw-internal-network-switcher-button",
630+
);
631+
expect(element).toBeTruthy();
632+
},
633+
{ timeout: 2000 },
634+
);
635+
});
636+
637+
it("StyledChevronRightIcon should render a svg element", () => {
638+
const { container } = render(<StyledChevronRightIcon />);
639+
const svg = container.querySelector("svg");
640+
expect(svg).toBeTruthy();
641+
});
587642
});

0 commit comments

Comments
 (0)