From 3025c93950f2dcabeefbde3e4290de2996a4a59e Mon Sep 17 00:00:00 2001 From: MK Date: Thu, 1 Aug 2024 11:05:10 +0100 Subject: [PATCH] chore: tree shake modals --- .changeset/wise-phones-refuse.md | 5 ++ .../RainbowKitProvider/ModalContext.tsx | 82 +++++++++++++++++-- 2 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 .changeset/wise-phones-refuse.md diff --git a/.changeset/wise-phones-refuse.md b/.changeset/wise-phones-refuse.md new file mode 100644 index 0000000000..ab2da7691a --- /dev/null +++ b/.changeset/wise-phones-refuse.md @@ -0,0 +1,5 @@ +--- +"@rainbow-me/rainbowkit": patch +--- + +Implemented tree shaking for chain and account modals to prevent them from bundling when unused. \ No newline at end of file diff --git a/packages/rainbowkit/src/components/RainbowKitProvider/ModalContext.tsx b/packages/rainbowkit/src/components/RainbowKitProvider/ModalContext.tsx index f8952a021d..496c373bcf 100644 --- a/packages/rainbowkit/src/components/RainbowKitProvider/ModalContext.tsx +++ b/packages/rainbowkit/src/components/RainbowKitProvider/ModalContext.tsx @@ -1,6 +1,9 @@ import React, { + Fragment, ReactNode, + Suspense, createContext, + lazy, useCallback, useContext, useEffect, @@ -9,18 +12,31 @@ import React, { } from 'react'; import { useAccount, useAccountEffect, useConfig } from 'wagmi'; import { useConnectionStatus } from '../../hooks/useConnectionStatus'; -import { AccountModal } from '../AccountModal/AccountModal'; -import { ChainModal } from '../ChainModal/ChainModal'; import { ConnectModal } from '../ConnectModal/ConnectModal'; import { useAuthenticationStatus } from './AuthenticationContext'; +const AccountModal = lazy(() => + import('../AccountModal/AccountModal').then((module) => ({ + default: module.AccountModal, + })), +); + +const ChainModal = lazy(() => + import('../ChainModal/ChainModal').then((module) => ({ + default: module.ChainModal, + })), +); + function useModalStateValue() { const [isModalOpen, setModalOpen] = useState(false); + const [isUsing, setIsUsing] = useState(false); return { closeModal: useCallback(() => setModalOpen(false), []), isModalOpen, openModal: useCallback(() => setModalOpen(true), []), + isUsing, + setIsUsing: useCallback((isUsing: boolean) => setIsUsing(isUsing), []), }; } @@ -28,11 +44,15 @@ interface ModalContextValue { accountModalOpen: boolean; chainModalOpen: boolean; connectModalOpen: boolean; + isUsingChainModal: boolean; + isUsingAccountModal: boolean; + isWalletConnectModalOpen: boolean; openAccountModal?: () => void; openChainModal?: () => void; openConnectModal?: () => void; - isWalletConnectModalOpen: boolean; setIsWalletConnectModalOpen: (isWalletConnectModalOpen: boolean) => void; + setIsUsingChainModal: (isUsingChainModal: boolean) => void; + setIsUsingAccountModal: (isUsingAccountModal: boolean) => void; } const ModalContext = createContext({ @@ -40,7 +60,11 @@ const ModalContext = createContext({ chainModalOpen: false, connectModalOpen: false, isWalletConnectModalOpen: false, + isUsingAccountModal: false, + isUsingChainModal: false, setIsWalletConnectModalOpen: () => {}, + setIsUsingChainModal: () => {}, + setIsUsingAccountModal: () => {}, }); interface ModalProviderProps { @@ -58,12 +82,16 @@ export function ModalProvider({ children }: ModalProviderProps) { closeModal: closeAccountModal, isModalOpen: accountModalOpen, openModal: openAccountModal, + isUsing: isUsingAccountModal, + setIsUsing: setIsUsingAccountModal, } = useModalStateValue(); const { closeModal: closeChainModal, isModalOpen: chainModalOpen, openModal: openChainModal, + isUsing: isUsingChainModal, + setIsUsing: setIsUsingChainModal, } = useModalStateValue(); const [isWalletConnectModalOpen, setIsWalletConnectModalOpen] = @@ -112,6 +140,8 @@ export function ModalProvider({ children }: ModalProviderProps) { chainModalOpen, connectModalOpen, isWalletConnectModalOpen, + isUsingChainModal, + isUsingAccountModal, openAccountModal: isCurrentChainSupported && connectionStatus === 'connected' ? openAccountModal @@ -124,6 +154,8 @@ export function ModalProvider({ children }: ModalProviderProps) { ? openConnectModal : undefined, setIsWalletConnectModalOpen, + setIsUsingChainModal, + setIsUsingAccountModal, }), [ connectionStatus, @@ -133,15 +165,27 @@ export function ModalProvider({ children }: ModalProviderProps) { openAccountModal, openChainModal, openConnectModal, + setIsUsingChainModal, + setIsUsingAccountModal, isCurrentChainSupported, isWalletConnectModalOpen, + isUsingAccountModal, + isUsingChainModal, ], )} > {children} - - + {isUsingAccountModal && ( + }> + + + )} + {isUsingChainModal && ( + }> + + + )} ); } @@ -158,12 +202,36 @@ export function useModalState() { } export function useAccountModal() { - const { accountModalOpen, openAccountModal } = useContext(ModalContext); + const { + accountModalOpen, + openAccountModal, + isUsingAccountModal, + setIsUsingAccountModal, + } = useContext(ModalContext); + + useEffect(() => { + if (!isUsingAccountModal) { + setIsUsingAccountModal(true); + } + }, [setIsUsingAccountModal, isUsingAccountModal]); + return { accountModalOpen, openAccountModal }; } export function useChainModal() { - const { chainModalOpen, openChainModal } = useContext(ModalContext); + const { + chainModalOpen, + openChainModal, + isUsingChainModal, + setIsUsingChainModal, + } = useContext(ModalContext); + + useEffect(() => { + if (!isUsingChainModal) { + setIsUsingChainModal(true); + } + }, [setIsUsingChainModal, isUsingChainModal]); + return { chainModalOpen, openChainModal }; }