refactor: allow note list swipe gesture on android to start from anywhere, and close keyboard on any swipe gesture

This commit is contained in:
Aman Harwara
2023-05-15 16:01:18 +05:30
parent f94921336b
commit 2ee7fef80b
3 changed files with 20 additions and 7 deletions

View File

@@ -9,7 +9,7 @@ import {
} from '@standardnotes/ui-services' } from '@standardnotes/ui-services'
import { WebApplication } from '@/Application/WebApplication' import { WebApplication } from '@/Application/WebApplication'
import { PANEL_NAME_NOTES } from '@/Constants/Constants' import { PANEL_NAME_NOTES } from '@/Constants/Constants'
import { FileItem, PrefKey, WebAppEvent } from '@standardnotes/snjs' import { FileItem, Platform, PrefKey, WebAppEvent } from '@standardnotes/snjs'
import { observer } from 'mobx-react-lite' import { observer } from 'mobx-react-lite'
import { forwardRef, useCallback, useEffect, useMemo } from 'react' import { forwardRef, useCallback, useEffect, useMemo } from 'react'
import ContentList from '@/Components/ContentListView/ContentList' import ContentList from '@/Components/ContentListView/ContentList'
@@ -305,7 +305,9 @@ const ContentListView = forwardRef<HTMLDivElement, Props>(
} }
}, [selectedUuids, innerRef, isCurrentNoteTemplate, renderedItems, panes]) }, [selectedUuids, innerRef, isCurrentNoteTemplate, renderedItems, panes])
const [setElement] = usePaneSwipeGesture('right', () => setPaneLayout(PaneLayout.TagSelection)) const [setElement] = usePaneSwipeGesture('right', () => setPaneLayout(PaneLayout.TagSelection), {
requiresStartFromEdge: application.platform !== Platform.Android,
})
return ( return (
<div <div

View File

@@ -36,8 +36,12 @@ const supportsPassive = (() => {
export const usePaneSwipeGesture = ( export const usePaneSwipeGesture = (
direction: 'left' | 'right', direction: 'left' | 'right',
onSwipeEnd: (element: HTMLElement) => void, onSwipeEnd: (element: HTMLElement) => void,
gesture: 'pan' | 'swipe' = 'pan', options?: {
gesture?: 'swipe' | 'pan'
requiresStartFromEdge?: boolean
},
) => { ) => {
const { gesture = 'pan', requiresStartFromEdge = true } = options || {}
const application = useApplication() const application = useApplication()
const underlayElementRef = useRef<HTMLElement | null>(null) const underlayElementRef = useRef<HTMLElement | null>(null)
@@ -100,10 +104,11 @@ export const usePaneSwipeGesture = (
const touch = event.touches[0] const touch = event.touches[0]
startX = touch.clientX startX = touch.clientX
if ( const isStartOutOfThreshold =
(direction === 'right' && startX > TouchStartThreshold) || (direction === 'right' && startX > TouchStartThreshold) ||
(direction === 'left' && startX < TouchStartThreshold) (direction === 'left' && startX < TouchStartThreshold)
) {
if (isStartOutOfThreshold && requiresStartFromEdge) {
canceled = true canceled = true
return return
} }
@@ -197,6 +202,10 @@ export const usePaneSwipeGesture = (
closestScrollContainer.style.overflowY = 'hidden' closestScrollContainer.style.overflowY = 'hidden'
} }
if (document.activeElement) {
;(document.activeElement as HTMLElement).blur()
}
if (adjustedGesture === 'pan') { if (adjustedGesture === 'pan') {
const x = const x =
direction === 'right' ? Math.max(deltaX - TouchMoveThreshold, 0) : Math.min(deltaX + TouchMoveThreshold, 0) direction === 'right' ? Math.max(deltaX - TouchMoveThreshold, 0) : Math.min(deltaX + TouchMoveThreshold, 0)
@@ -265,7 +274,7 @@ export const usePaneSwipeGesture = (
element.removeEventListener('touchend', touchEndListener) element.removeEventListener('touchend', touchEndListener)
disposeUnderlay() disposeUnderlay()
} }
}, [direction, element, isMobileScreen, onSwipeEndRef, isEnabled, adjustedGesture]) }, [direction, element, isMobileScreen, onSwipeEndRef, isEnabled, adjustedGesture, requiresStartFromEdge])
return [setElement] return [setElement]
} }

View File

@@ -52,7 +52,9 @@ const Navigation = forwardRef<HTMLDivElement, Props>(({ application, className,
setPaneLayout(PaneLayout.ItemSelection) setPaneLayout(PaneLayout.ItemSelection)
element.style.left = '0' element.style.left = '0'
}, },
'swipe', {
gesture: 'swipe',
},
) )
const { hasBottomInset } = useAvailableSafeAreaPadding() const { hasBottomInset } = useAvailableSafeAreaPadding()