Commit 708a813f authored by Brendan Wong's avatar Brendan Wong Committed by GitHub

feat: MP closes on scroll down (#6682)

* feat: MP closes on scroll down

* fix: make dismissal smoother

* fix: implement react gestures and fix scroll

* fix: fix drag when not starting from top

* fix: double scroll on mobile

* fix: comments for clarity

* fix: mobile scrolling?

* remove console logs

* potential fix?

* remove change again

* maybe fix?

* cope

* even more cope

* even more more cope

* copiest

* make less buggy

* nope

* maybe?

* HOLD

* maybe

* try 2

* maybe

* hopefully

* test

* another try

* cope

* redo

* attempt 2

* hmm

* maybe

* I THINK
parent 18b50283
...@@ -40,7 +40,6 @@ const AuthenticatedHeaderWrapper = styled.div` ...@@ -40,7 +40,6 @@ const AuthenticatedHeaderWrapper = styled.div`
display: flex; display: flex;
flex-direction: column; flex-direction: column;
flex: 1; flex: 1;
overflow: auto;
` `
const HeaderButton = styled(ThemeButton)` const HeaderButton = styled(ThemeButton)`
......
import { BrowserEvent, InterfaceEventName } from '@uniswap/analytics-events' import { BrowserEvent, InterfaceEventName } from '@uniswap/analytics-events'
import { TraceEvent } from 'analytics' import { TraceEvent } from 'analytics'
import { ScrollBarStyles } from 'components/Common' import { ScrollBarStyles } from 'components/Common'
import useDisableScrolling from 'hooks/useDisableScrolling'
import { useWindowSize } from 'hooks/useWindowSize' import { useWindowSize } from 'hooks/useWindowSize'
import { atom } from 'jotai' import { atom } from 'jotai'
import { useAtomValue, useUpdateAtom } from 'jotai/utils' import { useAtomValue, useUpdateAtom } from 'jotai/utils'
import { useCallback, useEffect, useRef } from 'react' import { useCallback, useEffect, useRef, useState } from 'react'
import { ChevronsRight } from 'react-feather' import { ChevronsRight } from 'react-feather'
import { useGesture } from 'react-use-gesture'
import styled from 'styled-components/macro' import styled from 'styled-components/macro'
import { BREAKPOINTS, ClickableStyle } from 'theme' import { BREAKPOINTS, ClickableStyle } from 'theme'
import { Z_INDEX } from 'theme/zIndex' import { Z_INDEX } from 'theme/zIndex'
import { isMobile } from 'utils/userAgent'
import DefaultMenu from './DefaultMenu' import DefaultMenu from './DefaultMenu'
...@@ -181,21 +184,53 @@ function AccountDrawer() { ...@@ -181,21 +184,53 @@ function AccountDrawer() {
} }
}, [walletDrawerOpen, toggleWalletDrawer]) }, [walletDrawerOpen, toggleWalletDrawer])
// close on escape keypress // useStates for detecting swipe gestures
useEffect(() => { const [yPosition, setYPosition] = useState(0)
const escapeKeyDownHandler = (event: KeyboardEvent) => { const [dragStartTop, setDragStartTop] = useState(true)
if (event.key === 'Escape' && walletDrawerOpen) { useDisableScrolling(walletDrawerOpen)
event.preventDefault()
// useGesture hook for detecting swipe gestures
const bind = useGesture({
// if the drawer is open and the user is dragging down, close the drawer
onDrag: (state) => {
// if the user is dragging up, set dragStartTop to false
if (state.movement[1] < 0) {
setDragStartTop(false)
if (scrollRef.current) {
scrollRef.current.style.overflowY = 'auto'
}
} else if (
(state.movement[1] > 300 || (state.velocity > 3 && state.direction[1] > 0)) &&
walletDrawerOpen &&
dragStartTop
) {
toggleWalletDrawer() toggleWalletDrawer()
} else if (walletDrawerOpen && dragStartTop && state.movement[1] > 0) {
setYPosition(state.movement[1])
if (scrollRef.current) {
scrollRef.current.style.overflowY = 'hidden'
}
} }
} },
// reset the yPosition when the user stops dragging
document.addEventListener('keydown', escapeKeyDownHandler) onDragEnd: (state) => {
setYPosition(0)
return () => { if (scrollRef.current) {
document.removeEventListener('keydown', escapeKeyDownHandler) scrollRef.current.style.overflowY = 'auto'
} }
}, [walletDrawerOpen, toggleWalletDrawer]) },
// set dragStartTop to true if the user starts dragging from the top of the drawer
onDragStart: (state) => {
if (!scrollRef.current?.scrollTop || scrollRef.current?.scrollTop < 30) {
setDragStartTop(true)
} else {
setDragStartTop(false)
if (scrollRef.current) {
scrollRef.current.style.overflowY = 'auto'
}
}
},
})
return ( return (
<Container> <Container>
...@@ -211,7 +246,15 @@ function AccountDrawer() { ...@@ -211,7 +246,15 @@ function AccountDrawer() {
</TraceEvent> </TraceEvent>
)} )}
<Scrim onClick={toggleWalletDrawer} open={walletDrawerOpen} /> <Scrim onClick={toggleWalletDrawer} open={walletDrawerOpen} />
<AccountDrawerWrapper open={walletDrawerOpen}> <AccountDrawerWrapper
open={walletDrawerOpen}
{...(isMobile
? {
...bind(),
style: { transform: `translateY(${yPosition}px)` },
}
: {})}
>
{/* id used for child InfiniteScrolls to reference when it has reached the bottom of the component */} {/* id used for child InfiniteScrolls to reference when it has reached the bottom of the component */}
<AccountDrawerScrollWrapper ref={scrollRef} id="wallet-dropdown-scroll-wrapper"> <AccountDrawerScrollWrapper ref={scrollRef} id="wallet-dropdown-scroll-wrapper">
<DefaultMenu /> <DefaultMenu />
......
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