Skip to content

Commit

Permalink
feat: make wallet connect responsive
Browse files Browse the repository at this point in the history
  • Loading branch information
ink-victor committed Dec 15, 2024
1 parent eed9c69 commit 0baa12d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { PropsWithChildren } from "react";
import { classNames, variantClassNames } from "../../util/classes";
import { Slot, Slottable } from "../Slot/Slot";
import { useIsUnderWindowBreakpoint } from "../../hooks/useWindowBreakpoint";

export interface ButtonProps
extends PropsWithChildren,
Expand All @@ -25,12 +26,14 @@ export const Button: React.FC<ButtonProps> = ({
iconRight,
...restProps
}: ButtonProps) => {
const isSmallWindow = useIsUnderWindowBreakpoint({ size: "sm" });
const Component = asChild ? Slot : "button";
const iconClasses = classNames(
"ink:size-3 ink:-my-1",
variant === "wallet" &&
!isSmallWindow &&
classNames(
"*:ink:object-cover ink:*:w-full ink:*:h-full ink:*:rounded-full",
"ink:*:object-cover ink:*:w-full ink:*:h-full ink:*:rounded-full",
variantClassNames(size, {
sm: "ink:size-4",
md: "ink:size-5",
Expand Down Expand Up @@ -75,6 +78,12 @@ export const Button: React.FC<ButtonProps> = ({
sm: "ink:pr-0.5",
md: "ink:pr-0.5",
lg: "ink:pr-1",
}),
isSmallWindow &&
variantClassNames(size, {
sm: "ink:px-0.5",
md: "ink:px-0.5",
lg: "ink:px-1",
})
),
}),
Expand Down
1 change: 0 additions & 1 deletion src/components/Wallet/ConnectWallet.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const meta: Meta<ConnectWalletProps> = {
component: ConnectWallet,
tags: ["autodocs"],
argTypes: {},
args: {},
};

export default meta;
Expand Down
41 changes: 39 additions & 2 deletions src/components/Wallet/ConnectWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { trimAddress } from "../../util/trim";
import { inkSepolia } from "wagmi/chains";
import { useEnsImageOrDefault } from "../../hooks/useEnsImageOrDefault";
import { useEnsNameOrDefault } from "../../hooks/useEnsNameOrDefault";
import { useIsUnderWindowBreakpoint } from "../../hooks/useWindowBreakpoint";

export interface ConnectWalletProps {
className?: string;
Expand All @@ -22,6 +23,7 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({ className }) => {
const { connect, connectors } = useConnect();
const ensName = useEnsNameOrDefault({ address });
const ensImage = useEnsImageOrDefault({ address });
const isSmallWindow = useIsUnderWindowBreakpoint({ size: "sm" });

return (
<Popover>
Expand All @@ -30,12 +32,18 @@ export const ConnectWallet: React.FC<ConnectWalletProps> = ({ className }) => {
variant={isConnected ? "wallet" : "primary"}
className={className}
iconLeft={
isConnected && address ? (
isConnected && address && !isSmallWindow ? (
<img src={ensImage} alt={`avatar for ${ensName}`} />
) : undefined
}
rounded={isSmallWindow ? "full" : "default"}
>
{isConnected && address ? ensName : "Connect"}
<ConnectWalletContent
isSmallWindow={isSmallWindow}
isConnected={isConnected}
ensName={ensName}
ensImage={ensImage}
/>
</Button>
</PopoverButton>

Expand Down Expand Up @@ -126,3 +134,32 @@ const ConnectedWalletSection = ({ address }: { address: Address }) => {
</>
);
};

const ConnectWalletContent = ({
isSmallWindow,
isConnected,
ensName,
ensImage,
}: {
isSmallWindow: boolean;
isConnected: boolean;
ensName: string;
ensImage: string;
}) => {
if (isConnected) {
if (isSmallWindow) {
return (
<div className="ink:size-5 ink:-my-1 ink:*:object-cover ink:*:w-full ink:*:h-full ink:*:rounded-full ink:px-0">
<img src={ensImage} alt={`avatar for ${ensName}`} />
</div>
);
}
return ensName;
}

if (isSmallWindow) {
return <InkIcon.Wallet className="ink:size-3" />;
}

return "Connect";
};
1 change: 1 addition & 0 deletions src/hooks/useWindowBreakpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from "react";
import { useWindowSize } from "./useWindowSize";

const BREAKPOINTS = {
sm: 640,
md: 768,
lg: 1024,
};
Expand Down
3 changes: 3 additions & 0 deletions src/icons/Type=Wallet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export { default as SocialX } from "./Type=SocialX.svg?react";
export { default as Success } from "./Type=Success.svg?react";
export { default as Sun } from "./Type=Sun.svg?react";
export { default as Swap } from "./Type=Swap.svg?react";
export { default as Wallet } from "./Type=Wallet.svg?react";

0 comments on commit 0baa12d

Please sign in to comment.