-
Notifications
You must be signed in to change notification settings - Fork 11
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
components: add ConnectWalletButton, AddressView #10
base: main
Are you sure you want to change the base?
Changes from all commits
e16092c
7387734
dc84b56
c945306
32c5f4e
7cdcd6c
fff6061
623f96f
ff80146
9adef2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react" | ||
import { useWeb3 } from "../" | ||
|
||
type Props = { | ||
isShort?: boolean | ||
} | ||
|
||
/** | ||
* AddressView is used to display the current address. | ||
* It should be used inside Web3Provider so that it picks up address | ||
* @param isShort - If true, the address is displayed in short format: 0xff...abcd | ||
* @returns {JSX.Element} | ||
*/ | ||
export const AddressView = ({ isShort = true }: Props): JSX.Element => { | ||
const { address } = useWeb3() | ||
|
||
if (!address) return null | ||
theshadowagent marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// TODO: parse ENS here | ||
|
||
return <> | ||
{isShort ? `${address.slice(0, 4)}...${address.slice(-4)}` : address} | ||
</> | ||
|
||
} | ||
|
||
export default AddressView |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useEffect, useState } from "react" | ||
import { useWeb3 } from "@3rdweb/hooks" | ||
|
||
import { LogoutOutlined } from "@mui/icons-material" | ||
import { Box, Button, Typography } from "@mui/material" | ||
|
||
import ConnectWeb3Modal from "./ConnectWeb3Modal" | ||
import AddressView from "./AddressView" | ||
|
||
type ButtonProps = { | ||
children: JSX.Element | ||
onClick: () => void | ||
} | ||
|
||
type Props = { | ||
autoOpen: boolean | ||
showDisconnect?: boolean | ||
renderButton?: (props: ButtonProps) => JSX.Element | ||
} | ||
|
||
const defaultRenderButton = ({ children, onClick }: ButtonProps) => ( | ||
<Button variant="text" onClick={onClick} sx={{ px: 3, mx: 2, color: "rgba(0,0,0,0.6)" }}> | ||
{children} | ||
</Button> | ||
) | ||
|
||
/** | ||
* ConnectWallet | ||
* @param param0 - autoOpen - If true, the modal will be opened on page load | ||
* @param param0 - showDisconnect - If true, a button will be shown to disconnect | ||
* @param param0 - renderButton - If provided, you can control how a button is rendered | ||
* @returns {JSX.Element} | ||
*/ | ||
export const ProfileView = ({ autoOpen = false, showDisconnect = true, renderButton = defaultRenderButton }: Props): JSX.Element => { | ||
|
||
const [open, setOpen] = useState(false) | ||
|
||
useEffect(() => { | ||
// we do it here instead of default value so that the modal is opened on page load ONLY | ||
if (autoOpen) { | ||
setOpen(true) | ||
} | ||
}, []) | ||
|
||
const { address, disconnectWallet } = useWeb3() | ||
|
||
const copyAddress = () => { | ||
navigator.clipboard.writeText(address) | ||
|
||
// TODO: show a snackbar | ||
window.alert("Address copied to clipboard") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
return <Box display="flex" alignItems="center" sx={{ color: "rgba(0,0,0,0.6)" }}> | ||
<ConnectWeb3Modal open={open} setOpen={setOpen} /> | ||
|
||
{!address && (renderButton({ | ||
children: <>Connect Wallet</>, | ||
onClick: () => setOpen(true), | ||
}))} | ||
|
||
{/* <Box > */} | ||
{address && <Typography | ||
sx={{ cursor: "pointer" }} | ||
fontWeight={500} | ||
onClick={copyAddress} | ||
> | ||
<AddressView /> | ||
</Typography>} | ||
|
||
{showDisconnect && address && (renderButton({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Popup might also be better from a UX perspective because users can accidentally log out if this button is so visible. I heard stories of 10x difference in log out rates because of unaware users who just click on things. Can be added in the future release |
||
children: <LogoutOutlined />, | ||
onClick: () => disconnectWallet(), | ||
}))} | ||
|
||
</Box> | ||
} | ||
|
||
export default ProfileView |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How to style
<AddressView />
? Consider wrapping in @mui<Box />
and passing styling props:sx
,style
,className