Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: tree shake chain and account modals #2126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-phones-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rainbow-me/rainbowkit": patch
---

Implemented tree shaking for chain and account modals to prevent them from bundling when unused.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React, {
Fragment,
ReactNode,
Suspense,
createContext,
lazy,
useCallback,
useContext,
useEffect,
Expand All @@ -9,38 +12,59 @@ 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), []),
};
}

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<ModalContextValue>({
accountModalOpen: false,
chainModalOpen: false,
connectModalOpen: false,
isWalletConnectModalOpen: false,
isUsingAccountModal: false,
isUsingChainModal: false,
setIsWalletConnectModalOpen: () => {},
setIsUsingChainModal: () => {},
setIsUsingAccountModal: () => {},
});

interface ModalProviderProps {
Expand All @@ -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] =
Expand Down Expand Up @@ -112,6 +140,8 @@ export function ModalProvider({ children }: ModalProviderProps) {
chainModalOpen,
connectModalOpen,
isWalletConnectModalOpen,
isUsingChainModal,
isUsingAccountModal,
openAccountModal:
isCurrentChainSupported && connectionStatus === 'connected'
? openAccountModal
Expand All @@ -124,6 +154,8 @@ export function ModalProvider({ children }: ModalProviderProps) {
? openConnectModal
: undefined,
setIsWalletConnectModalOpen,
setIsUsingChainModal,
setIsUsingAccountModal,
}),
[
connectionStatus,
Expand All @@ -133,15 +165,27 @@ export function ModalProvider({ children }: ModalProviderProps) {
openAccountModal,
openChainModal,
openConnectModal,
setIsUsingChainModal,
setIsUsingAccountModal,
isCurrentChainSupported,
isWalletConnectModalOpen,
isUsingAccountModal,
isUsingChainModal,
],
)}
>
{children}
<ConnectModal onClose={closeConnectModal} open={connectModalOpen} />
<AccountModal onClose={closeAccountModal} open={accountModalOpen} />
<ChainModal onClose={closeChainModal} open={chainModalOpen} />
{isUsingAccountModal && (
<Suspense fallback={<Fragment />}>
<AccountModal onClose={closeAccountModal} open={accountModalOpen} />
</Suspense>
)}
{isUsingChainModal && (
<Suspense fallback={<Fragment />}>
<ChainModal onClose={closeChainModal} open={chainModalOpen} />
</Suspense>
)}
</ModalContext.Provider>
);
}
Expand All @@ -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 };
}

Expand Down