-
Notifications
You must be signed in to change notification settings - Fork 10
CAS-455 - Connect Wallet V2 #427
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
Changes from all commits
a8c51f1
80a4331
3954459
76680be
dc556cc
341343f
1749fa6
b1895b9
868f019
c56be1e
ffb9a87
0421cdc
1abb5fe
60ddf45
4cf8948
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,188 @@ | ||
| import React from 'react'; | ||
| import React, { useEffect, useRef, useState } from 'react'; | ||
| import Blockies from 'react-blockies'; | ||
| import { CopyToClipboard } from 'react-copy-to-clipboard'; | ||
| import { Web3Consumer } from '../contexts/Web3'; | ||
| import { Copy } from 'components/Svg'; | ||
| import { useMediaQuery, useOnClickOutside } from 'hooks'; | ||
| import { truncateAddress } from 'utils'; | ||
| import classnames from 'classnames'; | ||
| import Tooltip from './Tooltip'; | ||
|
|
||
| const SignInOutButton = ({ | ||
| user: { loggedIn, addr }, | ||
| openWalletModal, | ||
| injectedProvider, | ||
| closeModal, | ||
| expandContainer = false, | ||
| }) => { | ||
| const signInOrOut = async (event) => { | ||
| const notMobile = useMediaQuery(); | ||
|
|
||
| const [dropDownClass, setDropDownClass] = useState(''); | ||
| const [addressCopied, setAddressCopied] = useState(false); | ||
|
|
||
| const dropdownRef = useRef(); | ||
|
|
||
| const closeDropdown = (e) => { | ||
| setDropDownClass(''); | ||
| }; | ||
|
|
||
| useOnClickOutside(dropdownRef, closeDropdown); | ||
|
|
||
| const signOut = (event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| injectedProvider.unauthenticate(); | ||
| setDropDownClass(''); | ||
| }; | ||
|
|
||
| const connectWallet = (event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| if (closeModal) { | ||
| closeModal(); | ||
| } | ||
| if (loggedIn) { | ||
| injectedProvider.unauthenticate(); | ||
| } else { | ||
| openWalletModal(); | ||
| } | ||
| openWalletModal(); | ||
| }; | ||
|
|
||
| const openDropdown = (event) => { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| setDropDownClass('is-active'); | ||
| }; | ||
|
|
||
| const markAddressCopied = () => setAddressCopied(true); | ||
|
|
||
| useEffect(() => { | ||
| let timeout; | ||
| if (addressCopied) { | ||
| timeout = setTimeout(() => { | ||
| setAddressCopied(false); | ||
| }, 500); | ||
| } | ||
| return () => clearTimeout(timeout); | ||
| }, [addressCopied]); | ||
|
|
||
| const dropdownBackground = classnames('', { | ||
| 'wallet-connect-background': dropDownClass && !notMobile, | ||
| }); | ||
|
|
||
| const buttonClass = classnames( | ||
| 'wallet-connect button is-uppercase transition-all small-text rounded-sm', | ||
| { 'is-primary': !loggedIn }, | ||
| { 'px-2': !notMobile } | ||
| ); | ||
| const addressStyle = classnames('', { 'smaller-text': !notMobile }); | ||
|
|
||
| const containerAddressStyle = classnames('is-flex', { 'pl-2': !notMobile }); | ||
|
|
||
| const containerButtonStyle = classnames('is-flex is-align-items-center', { | ||
| 'flex-1 is-justify-content-space-between': notMobile, | ||
| }); | ||
|
|
||
| return ( | ||
| <button | ||
| onClick={signInOrOut} | ||
| className="wallet-connect button is-primary is-uppercase transition-all small-text" | ||
| > | ||
| {loggedIn ? ( | ||
| <> | ||
| <span className="is-hidden-mobile is-hidden-connect"> | ||
| {addr} - | ||
| </span> | ||
| <span>Disconnect</span> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <span>Connect</span> | ||
| <span className="is-hidden-mobile"> Wallet</span> | ||
| </> | ||
| )} | ||
| </button> | ||
| <> | ||
| <div className={dropdownBackground} /> | ||
| <div | ||
| className={`dropdown ${dropDownClass}`} | ||
| aria-haspopup="true" | ||
| aria-controls="dropdown-menu" | ||
| style={{ position: 'relative' }} | ||
| > | ||
| <div | ||
| className="dropdown-trigger is-flex is-justify-content-flex-end" | ||
| style={notMobile && expandContainer ? { width: '280px' } : {}} | ||
| > | ||
| <button | ||
| onClick={loggedIn ? openDropdown : connectWallet} | ||
| className={buttonClass} | ||
| style={ | ||
| notMobile | ||
| ? { | ||
| ...(loggedIn ? { width: '147px' } : { width: '206px' }), | ||
| height: '40px', | ||
| } | ||
| : { width: '119px', height: '32px' } | ||
| } | ||
| > | ||
| {loggedIn ? ( | ||
| <div className={containerButtonStyle}> | ||
| <Blockies | ||
| seed={addr} | ||
| size={notMobile ? 6.5 : 5} | ||
| scale={4} | ||
| className="blockies" | ||
| /> | ||
| <div className={containerAddressStyle}> | ||
| <p className={addressStyle}>{truncateAddress(addr, 4, 4)}</p> | ||
| </div> | ||
| </div> | ||
| ) : ( | ||
| <> | ||
| <span>Connect</span> | ||
| <span className="is-hidden-mobile"> Wallet</span> | ||
| </> | ||
| )} | ||
| </button> | ||
| </div> | ||
|
|
||
| <div | ||
| className="dropdown-menu wallet-connect-content" | ||
| id="dropdown-menu" | ||
| role="menu" | ||
| ref={dropdownRef} | ||
| style={!notMobile ? { left: '-160px' } : {}} | ||
| > | ||
| <div className="dropdown-content p-0" style={{ width: '277px' }}> | ||
| <div className="px-4 pt-4 pb-2"> | ||
| <Tooltip | ||
| classNames="is-flex is-flex-grow-1 is-align-items-center transition-all" | ||
| position="top" | ||
| text="Copied!" | ||
| alwaysVisible={true} | ||
| enabled={addressCopied} | ||
| > | ||
| <CopyToClipboard text={addr} onCopy={markAddressCopied}> | ||
| <div | ||
| className="columns flex-1 is-mobile m-0 px-4 py-0 rounded-sm button is-white border-light" | ||
| style={{ | ||
| borderColor: 'hsl(0deg, 0%, 86%)', | ||
| height: '32px', | ||
| }} | ||
| > | ||
| <div className="column p-0 is-flex is-align-items-center"> | ||
| <span className="small-text">{addr}</span> | ||
| </div> | ||
| <div className="column p-0 is-flex is-align-items-center is-narrow"> | ||
| <div | ||
| className="is-flex is-align-items-center py-0 px-1" | ||
| style={{ height: '23px' }} | ||
| > | ||
| <Copy /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </CopyToClipboard> | ||
| </Tooltip> | ||
| </div> | ||
|
|
||
| <hr className="dropdown-divider" /> | ||
| <div className="px-4 pb-4 pt-2"> | ||
| <div | ||
| className="button is-fullwidth rounded-sm is-uppercase is-flex small-text has-text-white has-background-black" | ||
| style={{ height: '32px' }} | ||
| onClick={signOut} | ||
| > | ||
| disconnect | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </> | ||
|
Contributor
Author
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. there's a upcoming update for this component, I thought about making it more modular but decided to wait for the update and see how it can be split into more components |
||
| ); | ||
| }; | ||
|
|
||
| const CurrentUser = ({ web3, closeModal }) => { | ||
| const CurrentUser = ({ web3, closeModal, expandContainer }) => { | ||
| const { user, injectedProvider, openWalletModal } = web3; | ||
| if (!user) { | ||
| return null; | ||
|
|
@@ -53,6 +195,7 @@ const CurrentUser = ({ web3, closeModal }) => { | |
| injectedProvider={injectedProvider} | ||
| openWalletModal={openWalletModal} | ||
| closeModal={closeModal} | ||
| expandContainer={expandContainer} | ||
| /> | ||
| </div> | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { useEffect } from 'react'; | ||
|
|
||
| export default function useOnClickOutside(ref, handler) { | ||
| useEffect( | ||
| () => { | ||
| const listener = (event) => { | ||
| // Do nothing if clicking ref's element or descendent elements | ||
| if (!ref.current || ref.current.contains(event.target)) { | ||
| return; | ||
| } | ||
| handler(event); | ||
| }; | ||
| document.addEventListener('mousedown', listener); | ||
| document.addEventListener('touchstart', listener); | ||
| return () => { | ||
| document.removeEventListener('mousedown', listener); | ||
| document.removeEventListener('touchstart', listener); | ||
| }; | ||
| }, | ||
| // Add ref and handler to effect dependencies | ||
| // It's worth noting that because passed in handler is a new | ||
| // function on every render that will cause this effect | ||
| // callback/cleanup to run every render. It's not a big deal | ||
| // but to optimize you can wrap handler in useCallback before | ||
| // passing it into this hook. | ||
| [ref, handler] | ||
| ); | ||
| } |
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.
by removing
data-tooltip'tooltip gets disabled