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

Fix iOS scrolling mobile menu overlay attempt and improve events #656

Merged
merged 2 commits into from
Jun 8, 2022
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
6 changes: 3 additions & 3 deletions src/custom/components/AccountDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,15 @@ interface AccountDetailsProps {
confirmedTransactions: string[]
ENSName?: string
toggleWalletModal: () => void
closeOrdersPanel: () => void
handleCloseOrdersPanel: () => void
}

export default function AccountDetails({
pendingTransactions = [],
confirmedTransactions = [],
ENSName,
toggleWalletModal,
closeOrdersPanel,
handleCloseOrdersPanel,
}: AccountDetailsProps) {
const { account, connector, chainId: connectedChainId } = useActiveWeb3React()
const chainId = supportedChainId(connectedChainId)
Expand All @@ -148,7 +148,7 @@ export default function AccountDetails({
const handleDisconnectClick = () => {
;(connector as any).close()
localStorage.removeItem(STORAGE_KEY_LAST_PROVIDER)
closeOrdersPanel()
handleCloseOrdersPanel()
toggleWalletModal()
}

Expand Down
53 changes: 37 additions & 16 deletions src/custom/components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ReactGA from 'react-ga4'
import { useActiveWeb3React } from 'hooks/web3'
import { useNativeCurrencyBalances } from 'state/wallet/hooks'
import { useDarkModeManager } from 'state/user/hooks'
import { useMediaQuery, upToSmall, upToLarge } from 'hooks/useMediaQuery'
import { useMediaQuery, upToSmall, upToMedium, upToLarge, LargeAndUp } from 'hooks/useMediaQuery'
import { AMOUNT_PRECISION } from 'constants/index'
import { MAIN_MENU, MAIN_MENU_TYPE } from 'constants/mainMenu'
import { supportedChainId } from 'utils/supportedChainId'
Expand Down Expand Up @@ -77,32 +77,47 @@ export default function Header() {
}, [toggleDarkModeAux, darkMode])

const [isOrdersPanelOpen, setIsOrdersPanelOpen] = useState<boolean>(false)
const closeOrdersPanel = () => setIsOrdersPanelOpen(false)
const openOrdersPanel = () => account && setIsOrdersPanelOpen(true)
const handleOpenOrdersPanel = () => {
account && setIsOrdersPanelOpen(true)
account && isOrdersPanelOpen && addBodyClass('noScroll')
}
const handleCloseOrdersPanel = () => {
setIsOrdersPanelOpen(false)
!isOrdersPanelOpen && removeBodyClass('noScroll')
}

const history = useHistory()
const handleBalanceButtonClick = () => history.push('/account')
const isUpToLarge = useMediaQuery(upToLarge)
const isUpToMedium = useMediaQuery(upToMedium)
const isUpToSmall = useMediaQuery(upToSmall)
const isLargeAndUp = useMediaQuery(LargeAndUp)

const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false)
const handleMobileMenuOnClick = useCallback(
() => isUpToLarge && setIsMobileMenuOpen(!isMobileMenuOpen),
[isUpToLarge, isMobileMenuOpen]
)
const handleMobileMenuOnClick = useCallback(() => {
isUpToLarge && setIsMobileMenuOpen(!isMobileMenuOpen)
}, [isUpToLarge, isMobileMenuOpen])

const [isTouch, setIsTouch] = useState<boolean>(false)

// Toggle the 'noScroll' class on body, whenever the orders panel is open.
// This removes the inner scrollbar on the page body, to prevent showing double scrollbars.
useEffect(() => {
isUpToLarge && isMobileMenuOpen ? addBodyClass('noScroll') : removeBodyClass('noScroll')
isOrdersPanelOpen ? addBodyClass('noScroll') : removeBodyClass('noScroll')
}, [isOrdersPanelOpen, isMobileMenuOpen, isUpToLarge])
isMobileMenuOpen ? addBodyClass('noScroll') : removeBodyClass('noScroll')
// Set if device has touch capabilities
setIsTouch('ontouchstart' in document.documentElement)
}, [isOrdersPanelOpen, isMobileMenuOpen, isUpToLarge, isUpToMedium, isUpToSmall, isLargeAndUp])

const getMainMenu = useMemo(
() =>
MAIN_MENU.map(({ title, url, externalURL, items }: MAIN_MENU_TYPE, index) =>
!items && !externalURL && url ? (
<StyledNavLink key={index} to={url} onClick={handleMobileMenuOnClick}>
<StyledNavLink
key={index}
to={url}
onTouchStart={handleMobileMenuOnClick}
onClick={isTouch ? undefined : handleMobileMenuOnClick}
>
{title}
</StyledNavLink>
) : !items && externalURL && url ? (
Expand All @@ -120,8 +135,8 @@ export default function Header() {
<button
key={index}
onClick={() => {
toggleDarkMode()
handleMobileMenuOnClick()
toggleDarkMode()
}}
>
<SVG
Expand Down Expand Up @@ -156,7 +171,7 @@ export default function Header() {
</MenuDropdown>
) : null
),
[darkMode, handleMobileMenuOnClick, toggleDarkMode]
[darkMode, handleMobileMenuOnClick, toggleDarkMode, isTouch]
)

return (
Expand All @@ -182,7 +197,7 @@ export default function Header() {
isUpToSmall={isUpToSmall}
/>

<AccountElement active={!!account} onClick={openOrdersPanel}>
<AccountElement active={!!account} onClick={handleOpenOrdersPanel}>
{account && userEthBalance && (
<BalanceText>
{formatSmart(userEthBalance, AMOUNT_PRECISION) || '0'} {nativeToken}
Expand All @@ -193,8 +208,14 @@ export default function Header() {
</HeaderElement>
</HeaderControls>

{isUpToLarge && <MobileMenuIcon isMobileMenuOpen={isMobileMenuOpen} onTouchStart={handleMobileMenuOnClick} />}
{isOrdersPanelOpen && <OrdersPanel closeOrdersPanel={closeOrdersPanel} />}
{isUpToLarge && (
<MobileMenuIcon
isMobileMenuOpen={isMobileMenuOpen}
onTouchStart={handleMobileMenuOnClick}
onClick={isTouch ? undefined : handleMobileMenuOnClick} // Disable onClick on touch devices and use onTouchStart
/>
)}
{isOrdersPanelOpen && <OrdersPanel handleCloseOrdersPanel={handleCloseOrdersPanel} />}
</HeaderModWrapper>
</Wrapper>
)
Expand Down
31 changes: 16 additions & 15 deletions src/custom/components/Header/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ export const Wrapper = styled.div<{ isMobileMenuOpen: boolean }>`
css`
position: absolute;
top: 0;

&::before {
content: '';
width: 100%;
display: flex;
height: 60px;
background: ${({ theme }) => theme.bg4};
position: fixed;
top: 0;
left: 0;
z-index: 101;
}
`
}
`}
Expand Down Expand Up @@ -235,26 +247,15 @@ export const HeaderLinks = styled(HeaderLinksMod)<{ isMobileMenuOpen: boolean }>
background: ${({ theme }) => theme.bg4};
outline: 0;
padding: 60px 8px;
overflow-y: auto;
overflow-x: hidden;
overflow-y: scroll;
-webkit-overflow-scrolling: touch; // iOS scroll fix
transform: translate3d(0,0,0); // iOS scroll fix

${
isMobileMenuOpen &&
css`
display: flex;

&::before {
content: '';
width: 100%;
display: flex;
height: 60px;
background: ${({ theme }) => theme.bg4};
position: fixed;
top: 0;
left: 0;
z-index: 1;
}

// transform: translate3d(100%, 0, 0);
`
}
`};
Expand Down
10 changes: 5 additions & 5 deletions src/custom/components/OrdersPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ const isConfirmed = (data: TransactionAndOrder) =>
data.status === OrderStatus.FULFILLED || data.status === OrderStatus.EXPIRED || data.status === OrderStatus.CANCELLED

export interface OrdersPanelProps {
closeOrdersPanel: () => void
handleCloseOrdersPanel: () => void
}

export default function OrdersPanel({ closeOrdersPanel }: OrdersPanelProps) {
export default function OrdersPanel({ handleCloseOrdersPanel }: OrdersPanelProps) {
const walletInfo = useWalletInfo()
const toggleWalletModal = useWalletModalToggle()

Expand All @@ -177,20 +177,20 @@ export default function OrdersPanel({ closeOrdersPanel }: OrdersPanelProps) {

return (
<>
<SidebarBackground onClick={closeOrdersPanel} />
<SidebarBackground onClick={handleCloseOrdersPanel} />
<SideBar>
<Wrapper>
<Header>
<strong>Account</strong>
<CloseIcon onClick={closeOrdersPanel} />
<CloseIcon onClick={handleCloseOrdersPanel} />
</Header>

<AccountDetails
ENSName={ENSName}
pendingTransactions={pendingActivity}
confirmedTransactions={confirmedActivity}
toggleWalletModal={toggleWalletModal}
closeOrdersPanel={closeOrdersPanel}
handleCloseOrdersPanel={handleCloseOrdersPanel}
/>
</Wrapper>
</SideBar>
Expand Down
3 changes: 3 additions & 0 deletions src/custom/hooks/useMediaQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ export const useMediaQuery = (query: string) => {
}

export const upToSmall = `(max-width: ${MEDIA_WIDTHS.upToSmall}px)`
export const isMediumOnly = `(min-width: ${MEDIA_WIDTHS.upToSmall + 1}px) and (max-width: ${MEDIA_WIDTHS.upToMedium}px)`
export const upToMedium = `(max-width: ${MEDIA_WIDTHS.upToMedium}px)`
export const isLargeOnly = `(min-width: ${MEDIA_WIDTHS.upToMedium + 1}px) and (max-width: ${MEDIA_WIDTHS.upToLarge}px)`
export const upToLarge = `(max-width: ${MEDIA_WIDTHS.upToLarge}px)`
export const LargeAndUp = `(min-width: ${MEDIA_WIDTHS.upToLarge + 1}px)`
2 changes: 2 additions & 0 deletions src/custom/utils/toggleBodyClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export const toggleBodyClass = (className: string) => {
}

export const addBodyClass = (className: string) => {
console.log('addBodyClass ', className)
!document.body.classList.contains(className) && document.body.classList.add(className)
}

export const removeBodyClass = (className: string) => {
console.log('removeBodyClass ', className)
document.body.classList.contains(className) && document.body.classList.remove(className)
}