Commit 8c019911 authored by aballerr's avatar aballerr Committed by GitHub

fix: Fix to mobile wallet experience (#4508)

* fixing wallet on mobile and making useClickoutside more extensible
Co-authored-by: default avatarAlex Ball <alexball@UNISWAP-MAC-038.local>
parent 5e6e6be8
import { useState } from 'react' import { useState } from 'react'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { Z_INDEX } from 'theme'
import { useModalIsOpen } from '../../state/application/hooks' import { useModalIsOpen } from '../../state/application/hooks'
import { ApplicationModal } from '../../state/application/reducer' import { ApplicationModal } from '../../state/application/reducer'
...@@ -36,13 +37,13 @@ const WalletDropdownWrapper = styled.div` ...@@ -36,13 +37,13 @@ const WalletDropdownWrapper = styled.div`
position: absolute; position: absolute;
top: 65px; top: 65px;
right: 20px; right: 20px;
z-index: ${Z_INDEX.dropdown};
@media only screen and (max-width: ${({ theme }) => `${theme.breakpoint.sm}px`}) { @media only screen and (max-width: ${({ theme }) => `${theme.breakpoint.sm}px`}) {
top: unset; top: unset;
left: 0; left: 0;
right: 0; right: 0;
bottom: 56px; bottom: 56px;
z-index: 1;
} }
` `
......
...@@ -7,7 +7,6 @@ import WalletDropdown from 'components/WalletDropdown' ...@@ -7,7 +7,6 @@ import WalletDropdown from 'components/WalletDropdown'
import { getConnection } from 'connection/utils' import { getConnection } from 'connection/utils'
import { NavBarVariant, useNavBarFlag } from 'featureFlags/flags/navBar' import { NavBarVariant, useNavBarFlag } from 'featureFlags/flags/navBar'
import { Portal } from 'nft/components/common/Portal' import { Portal } from 'nft/components/common/Portal'
import { useIsMobile } from 'nft/hooks'
import { getIsValidSwapQuote } from 'pages/Swap' import { getIsValidSwapQuote } from 'pages/Swap'
import { darken } from 'polished' import { darken } from 'polished'
import { useMemo, useRef } from 'react' import { useMemo, useRef } from 'react'
...@@ -289,11 +288,11 @@ export default function Web3Status() { ...@@ -289,11 +288,11 @@ export default function Web3Status() {
const allTransactions = useAllTransactions() const allTransactions = useAllTransactions()
const ref = useRef<HTMLDivElement>(null) const ref = useRef<HTMLDivElement>(null)
const walletRef = useRef<HTMLDivElement>(null)
const closeModal = useCloseModal(ApplicationModal.WALLET_DROPDOWN) const closeModal = useCloseModal(ApplicationModal.WALLET_DROPDOWN)
const isOpen = useIsOpen() const isOpen = useIsOpen()
const isMobile = useIsMobile()
useOnClickOutside(ref, isOpen ? closeModal : undefined) useOnClickOutside(ref, isOpen ? closeModal : undefined, [walletRef])
const sortedRecentTransactions = useMemo(() => { const sortedRecentTransactions = useMemo(() => {
const txs = Object.values(allTransactions) const txs = Object.values(allTransactions)
...@@ -307,13 +306,11 @@ export default function Web3Status() { ...@@ -307,13 +306,11 @@ export default function Web3Status() {
<span ref={ref}> <span ref={ref}>
<Web3StatusInner /> <Web3StatusInner />
<WalletModal ENSName={ENSName ?? undefined} pendingTransactions={pending} confirmedTransactions={confirmed} /> <WalletModal ENSName={ENSName ?? undefined} pendingTransactions={pending} confirmedTransactions={confirmed} />
{isMobile ? ( <Portal>
<Portal> <span ref={walletRef}>
<WalletDropdown /> <WalletDropdown />
</Portal> </span>
) : ( </Portal>
<WalletDropdown />
)}
</span> </span>
) )
} }
...@@ -2,18 +2,27 @@ import { RefObject, useEffect, useRef } from 'react' ...@@ -2,18 +2,27 @@ import { RefObject, useEffect, useRef } from 'react'
export function useOnClickOutside<T extends HTMLElement>( export function useOnClickOutside<T extends HTMLElement>(
node: RefObject<T | undefined>, node: RefObject<T | undefined>,
handler: undefined | (() => void) handler: undefined | (() => void),
ignoredNodes: Array<RefObject<T | undefined>> = []
) { ) {
const handlerRef = useRef<undefined | (() => void)>(handler) const handlerRef = useRef<undefined | (() => void)>(handler)
useEffect(() => { useEffect(() => {
handlerRef.current = handler handlerRef.current = handler
}, [handler]) }, [handler])
useEffect(() => { useEffect(() => {
const handleClickOutside = (e: MouseEvent) => { const handleClickOutside = (e: MouseEvent) => {
if (node.current?.contains(e.target as Node) ?? false) { const nodeClicked = node.current?.contains(e.target as Node)
const ignoredNodeClicked = ignoredNodes.reduce(
(reducer, val) => reducer || !!val.current?.contains(e.target as Node),
false
)
if ((nodeClicked || ignoredNodeClicked) ?? false) {
return return
} }
if (handlerRef.current) handlerRef.current() if (handlerRef.current) handlerRef.current()
} }
...@@ -22,5 +31,5 @@ export function useOnClickOutside<T extends HTMLElement>( ...@@ -22,5 +31,5 @@ export function useOnClickOutside<T extends HTMLElement>(
return () => { return () => {
document.removeEventListener('mousedown', handleClickOutside) document.removeEventListener('mousedown', handleClickOutside)
} }
}, [node]) }, [node, ignoredNodes])
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment