From 86ff8b5b6d900ccd87385c7585acdb62661d03c4 Mon Sep 17 00:00:00 2001 From: Albert Folch Date: Mon, 30 Sep 2024 12:51:41 +0200 Subject: [PATCH] feat: add more props to connect wallet button to further customise its look --- .../components/wallet2/web3Status/index.tsx | 58 ++++++++++++++----- .../src/stories/ConnectWallet.stories.tsx | 30 ++++++++-- 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/packages/react-kit/src/components/wallet2/web3Status/index.tsx b/packages/react-kit/src/components/wallet2/web3Status/index.tsx index 45248a800..815981297 100644 --- a/packages/react-kit/src/components/wallet2/web3Status/index.tsx +++ b/packages/react-kit/src/components/wallet2/web3Status/index.tsx @@ -1,5 +1,5 @@ import { useWeb3React } from "@web3-react/core"; -import React, { memo, useCallback, useEffect, useRef } from "react"; +import React, { ReactNode, memo, useCallback, useEffect, useRef } from "react"; import styled from "styled-components"; import { useIsMagicLoggedIn } from "../../../hooks"; @@ -64,7 +64,17 @@ const getCommonWalletButtonProps = (isXXS: boolean) => function Web3StatusInner({ showOnlyIcon, errorButtonTheme, - successButtonTheme + connectedButtonTheme, + connectWalletButtonTheme, + connectWalletChild = <>Connect Wallet, + showStatusIcon = true, + wrongNetworkChild = <>Wrong network, + leftConnectWalletChild, + leftConnectedChild, + leftWrongNetworkChild, + rightConnectWalletChild, + rightConnectedChild, + rightWrongNetworkChild }: ConnectWalletProps) { const switchingChain = useAppSelector( (state) => state.wallets.switchingChain @@ -121,18 +131,21 @@ function Web3StatusInner({ disabled={Boolean(switchingChain)} data-testid="web3-status-connected" onClick={handleWalletDropdownClick} - theme={successButtonTheme} + theme={connectedButtonTheme} > - - + {showStatusIcon && ( + + )} + {leftConnectedChild} {ENSName || formatAddress(account)} + {rightConnectedChild} ); } @@ -163,7 +176,9 @@ function Web3StatusInner({ onClick={handleWalletDropdownClick} theme={errorButtonTheme} > - Wrong network + {leftWrongNetworkChild} + {wrongNetworkChild} + {rightWrongNetworkChild} ) : ( @@ -171,23 +186,36 @@ function Web3StatusInner({ onClick={handleWalletDropdownClick} data-testid="navbar-connect-wallet" {...getCommonWalletButtonProps(isXXS)} - theme={successButtonTheme} + theme={connectWalletButtonTheme} style={{ ...getCommonWalletButtonProps(isXXS).style }} > - Connect Wallet + {leftConnectWalletChild} + {connectWalletChild} + {rightConnectWalletChild} )} ); } +type SuccessButtonTheme = Omit & + Required>; export type ConnectWalletProps = { showOnlyIcon?: boolean; errorButtonTheme: BaseButtonTheme; - successButtonTheme: Omit & - Required>; + connectedButtonTheme: SuccessButtonTheme; + connectWalletButtonTheme: SuccessButtonTheme; + connectWalletChild?: ReactNode; + wrongNetworkChild?: ReactNode; + showStatusIcon?: boolean; + leftConnectedChild?: ReactNode; + rightConnectedChild?: ReactNode; + leftWrongNetworkChild?: ReactNode; + rightWrongNetworkChild?: ReactNode; + leftConnectWalletChild?: ReactNode; + rightConnectWalletChild?: ReactNode; }; export const ConnectWallet = memo(function Web3Status( props: ConnectWalletProps diff --git a/packages/react-kit/src/stories/ConnectWallet.stories.tsx b/packages/react-kit/src/stories/ConnectWallet.stories.tsx index 6138ab565..b7693576f 100644 --- a/packages/react-kit/src/stories/ConnectWallet.stories.tsx +++ b/packages/react-kit/src/stories/ConnectWallet.stories.tsx @@ -1,5 +1,5 @@ import { fn } from "@storybook/test"; -import React from "react"; +import React, { ReactNode } from "react"; import { Meta } from "@storybook/react"; import { ChainSelector, @@ -17,8 +17,9 @@ import { import { HashRouter, Route, Routes } from "react-router-dom"; import { bosonButtonThemeKeys } from "../components/ui/ThemedButton"; import { CSSProperties, createGlobalStyle } from "styled-components"; +import { Wallet } from "phosphor-react"; const colors = theme.colors.light; -const successButtonTheme: ConnectWalletProps["successButtonTheme"] = { +const successButtonTheme: ConnectWalletProps["connectWalletButtonTheme"] = { ...bosonButtonThemes({ withBosonStyle: false })["primary"], color: "inherit", background: "var(--buttonBgColor)" @@ -38,6 +39,9 @@ const ColorGlobalStyle = createGlobalStyle<{ color: CSSProperties["color"] }>` } `; const Component = ({ + showStatusIcon, + rightConnectedChild, + connectWalletChild, textColor, chainSelectorBackgroundColor, connectWalletBorderRadius, @@ -59,7 +63,10 @@ const Component = ({ magicLoginButtonBorderRadiusPx, onUserDisconnect }: { + showStatusIcon: boolean; + rightConnectedChild?: ReactNode; textColor: string; + connectWalletChild: string; chainSelectorBackgroundColor: string | undefined; connectWalletBorderRadius: string | undefined; connectWalletSuccessButtonThemeKey: string | undefined; @@ -118,7 +125,19 @@ const Component = ({ config={config} /> Connect, + rightConnectedChild: } };