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

feat: add more props to connect wallet button to further customise it… #798

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
58 changes: 43 additions & 15 deletions packages/react-kit/src/components/wallet2/web3Status/index.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -121,18 +131,21 @@ function Web3StatusInner({
disabled={Boolean(switchingChain)}
data-testid="web3-status-connected"
onClick={handleWalletDropdownClick}
theme={successButtonTheme}
theme={connectedButtonTheme}
>
<StatusIcon
account={account}
size={24}
connection={connection}
showMiniIcons={showOnlyIcon}
/>

{showStatusIcon && (
<StatusIcon
account={account}
size={24}
connection={connection}
showMiniIcons={showOnlyIcon}
/>
)}
{leftConnectedChild}
<AddressAndChevronContainer>
<Text>{ENSName || formatAddress(account)}</Text>
</AddressAndChevronContainer>
{rightConnectedChild}
</BaseButton>
);
}
Expand Down Expand Up @@ -163,31 +176,46 @@ function Web3StatusInner({
onClick={handleWalletDropdownClick}
theme={errorButtonTheme}
>
Wrong network
{leftWrongNetworkChild}
{wrongNetworkChild}
{rightWrongNetworkChild}
</BaseButton>
</Tooltip>
) : (
<BaseButton
onClick={handleWalletDropdownClick}
data-testid="navbar-connect-wallet"
{...getCommonWalletButtonProps(isXXS)}
theme={successButtonTheme}
theme={connectWalletButtonTheme}
style={{
...getCommonWalletButtonProps(isXXS).style
}}
>
Connect Wallet
{leftConnectWalletChild}
{connectWalletChild}
{rightConnectWalletChild}
</BaseButton>
)}
</Grid>
);
}
type SuccessButtonTheme = Omit<BaseButtonTheme, "color" | "background"> &
Required<Pick<BaseButtonTheme, "color" | "background">>;

export type ConnectWalletProps = {
showOnlyIcon?: boolean;
errorButtonTheme: BaseButtonTheme;
successButtonTheme: Omit<BaseButtonTheme, "color" | "background"> &
Required<Pick<BaseButtonTheme, "color" | "background">>;
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
Expand Down
30 changes: 26 additions & 4 deletions packages/react-kit/src/stories/ConnectWallet.stories.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)"
Expand All @@ -38,6 +39,9 @@ const ColorGlobalStyle = createGlobalStyle<{ color: CSSProperties["color"] }>`
}
`;
const Component = ({
showStatusIcon,
rightConnectedChild,
connectWalletChild,
textColor,
chainSelectorBackgroundColor,
connectWalletBorderRadius,
Expand All @@ -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;
Expand Down Expand Up @@ -118,7 +125,19 @@ const Component = ({
config={config}
/>
<ConnectWallet
successButtonTheme={{
showStatusIcon={showStatusIcon}
connectWalletChild={connectWalletChild}
rightConnectedChild={rightConnectedChild}
connectWalletButtonTheme={{
...(connectWalletSuccessButtonThemeKey
? bosonButtonThemes({ withBosonStyle: false })[
connectWalletSuccessButtonThemeKey
]
: successButtonTheme),
borderRadius: connectWalletBorderRadius,
gap: "2px"
}}
connectedButtonTheme={{
...(connectWalletSuccessButtonThemeKey
? bosonButtonThemes({ withBosonStyle: false })[
connectWalletSuccessButtonThemeKey
Expand Down Expand Up @@ -285,6 +304,9 @@ export const CustomTheme = {
walletHoverFocusBackgroundColor: "#e89f0e",
walletHoverColor: "#ff0000",
magicLoginButtonThemeKey: "orangeInverse",
magicLoginButtonBorderRadiusPx: "50"
magicLoginButtonBorderRadiusPx: "50",
showStatusIcon: false,
connectWalletChild: <>Connect</>,
rightConnectedChild: <Wallet />
}
};
Loading