feat(entry-layout): implement swipe-to-close functionality and enhance entry content navigation

- Added swipe and scroll gesture detection to close the entry content when scrolled up significantly.
- Introduced a scroll hint button to guide users on how to exit the entry view.
- Refactored AIEntryLayout to improve animation handling and user experience during entry transitions.
- Updated EntryScrollingAndNavigationHandler to streamline scroll detection logic and remove unnecessary state management.

These changes collectively enhance the interaction model for entry content, providing a more intuitive and responsive user experience.
This commit is contained in:
DIYgod
2025-08-15 20:05:12 +08:00
parent e420266014
commit 955ee5441c
2 changed files with 125 additions and 191 deletions
@@ -1,9 +1,10 @@
import { Spring } from "@follow/components/constants/spring.js"
import { Button } from "@follow/components/ui/button/index.js"
import { PanelSplitter } from "@follow/components/ui/divider/index.js"
import { defaultUISettings } from "@follow/shared/settings/defaults"
import { cn } from "@follow/utils"
import { AnimatePresence } from "motion/react"
import { useMemo, useRef } from "react"
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
import { useResizable } from "react-resizable-layout"
import { useParams } from "react-router"
@@ -11,6 +12,7 @@ import { AIChatPanelStyle, useAIChatPanelStyle } from "~/atoms/settings/ai"
import { getUISettings, setUISetting } from "~/atoms/settings/ui"
import { m } from "~/components/common/Motion"
import { ROUTE_ENTRY_PENDING } from "~/constants"
import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry"
import { AIChatLayout } from "~/modules/app-layout/ai/AIChatLayout"
import { EntryContent } from "~/modules/entry-content/components/entry-content"
import { AppLayoutGridContainerProvider } from "~/providers/app-grid-layout-container-provider"
@@ -20,11 +22,95 @@ import { EntryColumn } from "./index"
const AIEntryLayoutImpl = () => {
const { entryId } = useParams()
const navigate = useNavigateEntry()
const panelStyle = useAIChatPanelStyle()
const realEntryId = entryId === ROUTE_ENTRY_PENDING ? "" : entryId
// Swipe/scroll to close functionality
const entryContentRef = useRef<HTMLDivElement>(null)
const accumulatedDelta = useRef(0)
const isScrollingAtTop = useRef(false)
const [showScrollHint, setShowScrollHint] = useState(false)
const handleCloseGesture = useCallback(() => {
navigate({ entryId: null })
}, [navigate])
const handleWheel = useCallback(
(e: WheelEvent) => {
if (!realEntryId || !entryContentRef.current) return
// Find the actual scroll viewport element with correct Radix UI attribute
const entryContentElement = entryContentRef.current.querySelector(
"[data-radix-scroll-area-viewport]",
) as HTMLElement
const scrollElement = entryContentElement || entryContentRef.current
// Check if we're at the top of the content
const scrollTop = scrollElement?.scrollTop || 0
isScrollingAtTop.current = scrollTop === 0
setShowScrollHint(scrollTop === 0)
// Handle trackpad/mouse wheel: upward scroll (deltaY < 0) or downward swipe gesture
// On macOS trackpad, natural scrolling makes upward finger movement negative deltaY
if (e.deltaY < 0 && isScrollingAtTop.current) {
e.preventDefault()
accumulatedDelta.current += Math.abs(e.deltaY)
// Close when accumulated scroll exceeds threshold (150px for trackpad sensitivity)
if (accumulatedDelta.current > 1000) {
handleCloseGesture()
accumulatedDelta.current = 0
}
} else {
// Reset accumulation when scrolling down or not at top
accumulatedDelta.current = 0
}
},
[realEntryId, handleCloseGesture],
)
useEffect(() => {
if (!realEntryId || !entryContentRef.current) return
const element = entryContentRef.current
// Find the scroll area viewport element with correct Radix UI attribute
const scrollViewport = element.querySelector("[data-radix-scroll-area-viewport]") as HTMLElement
// Add wheel event listener to both the main container and scroll viewport
// This ensures the gesture works in both header area and scrollable content
const elementsToListen: HTMLElement[] = [element]
if (scrollViewport) {
elementsToListen.push(scrollViewport)
}
elementsToListen.forEach((el) => {
el.addEventListener("wheel", handleWheel, { passive: false })
})
// Initial scroll position check for hint visibility
const initialCheckScrollPosition = () => {
const scrollTop = scrollViewport?.scrollTop || element.scrollTop || 0
setShowScrollHint(scrollTop === 0)
}
// Check initial position
initialCheckScrollPosition()
// Add scroll listener for hint visibility
const scrollElement = scrollViewport || element
scrollElement.addEventListener("scroll", initialCheckScrollPosition, { passive: true })
return () => {
elementsToListen.forEach((el) => {
el.removeEventListener("wheel", handleWheel)
})
scrollElement.removeEventListener("scroll", initialCheckScrollPosition)
}
}, [realEntryId, handleWheel])
// AI chat resizable panel configuration
const aiColWidth = useMemo(() => getUISettings().aiColWidth, [])
const startDragPosition = useRef(0)
@@ -54,17 +140,39 @@ const AIEntryLayoutImpl = () => {
<EntryColumn key="entry-list" />
{/* Entry content overlay with exit animation */}
<AnimatePresence mode="popLayout">
<AnimatePresence mode="wait">
{realEntryId && (
<m.div
lcpOptimization
initial={{ y: 150, opacity: 0, scale: 0.98 }}
animate={{ y: 0, opacity: 1, scale: 1 }}
exit={{ y: 150, opacity: 0, scale: 0.98 }}
ref={entryContentRef}
key={realEntryId}
initial={{ y: "100%" }}
animate={{ y: 0 }}
exit={{ y: "100%" }}
transition={Spring.presets.smooth}
className="bg-theme-background absolute inset-0 z-10 border-l"
>
<EntryContent entryId={realEntryId} className="h-full" />
{/* Scroll hint indicator */}
<div className="center z-50 pt-2">
<Button
variant="ghost"
size="sm"
onClick={() => navigate({ entryId: null })}
buttonClassName="transform cursor-pointer select-none"
aria-label="Scroll up or click to exit"
>
<div className="text-text flex items-center gap-2 rounded-full font-medium">
<i
className={cn(
"text-base",
showScrollHint ? "i-mgc-up-cute-re" : "i-mgc-close-cute-re",
)}
/>
<span>{showScrollHint ? "Scroll up to exit" : "Click to exit"}</span>
</div>
</Button>
</div>
<EntryContent entryId={realEntryId} className="h-[calc(100%-2.25rem)]" />
</m.div>
)}
</AnimatePresence>
@@ -30,12 +30,7 @@ export const EntryScrollingAndNavigationHandler = ({
scrollAnimationRef: React.RefObject<JSAnimation<any> | null>
}) => {
const isAlreadyScrolledBottomRef = useRef(false)
const isAlreadyScrolledTopRef = useRef(false)
const lastTouchYRef = useRef<number | null>(null)
const wheelEndTimerRef = useRef<number | null>(null)
const wheelGestureCompleteRef = useRef(false)
const [showKeepScrollingPanel, setShowKeepScrollingPanel] = useState(false)
const [showBackToTimelinePanel, setShowBackToTimelinePanel] = useState(false)
const when = useGlobalFocusableScopeSelector(FocusablePresets.isEntryRender)
@@ -80,180 +75,52 @@ export const EntryScrollingAndNavigationHandler = ({
{ enabled: when && useBackHandler },
)
const navigateBackToTimeline = useEventCallback(() => {
navigateToTimeline({ entryId: null })
setShowBackToTimelinePanel(false)
isAlreadyScrolledTopRef.current = false
wheelGestureCompleteRef.current = false
if (wheelEndTimerRef.current) {
clearTimeout(wheelEndTimerRef.current)
wheelEndTimerRef.current = null
}
})
const { highlightBoundary } = useFocusActions()
const smoothScrollTo = useSmoothScroll()
const navigateToNext = useEventCallback(() => {
EventBus.dispatch(COMMAND_ID.timeline.switchToNext)
setShowKeepScrollingPanel(false)
setShowBackToTimelinePanel(false)
isAlreadyScrolledBottomRef.current = false
isAlreadyScrolledTopRef.current = false
wheelGestureCompleteRef.current = false
if (wheelEndTimerRef.current) {
clearTimeout(wheelEndTimerRef.current)
wheelEndTimerRef.current = null
}
if (scrollerRef.current) {
smoothScrollTo(0, scrollerRef.current)
}
})
useEffect(() => {
// Only check bottom scroll for keyboard navigation
const checkBottomScrollOnly = ($scroller: HTMLDivElement) => {
const checkScrollBottom = ($scroller: HTMLDivElement) => {
const currentScroll = $scroller.scrollTop
const { scrollHeight, clientHeight } = $scroller
// Check if already scrolled to bottom and user continues scrolling down
if (isAlreadyScrolledBottomRef.current) {
navigateToNext()
return
}
if (scrollHeight && clientHeight) {
// Check bottom scroll
const isAtBottom = Math.abs(currentScroll + clientHeight - scrollHeight) < 2
isAlreadyScrolledBottomRef.current = isAtBottom
setShowKeepScrollingPanel(isAtBottom)
isAlreadyScrolledBottomRef.current =
Math.abs(currentScroll + clientHeight - scrollHeight) < 2
setShowKeepScrollingPanel(isAlreadyScrolledBottomRef.current)
}
}
const handleWheelEvent = (event: WheelEvent) => {
const $scroller = scrollerRef.current
if (!$scroller) return
// Clear existing timer
if (wheelEndTimerRef.current) {
clearTimeout(wheelEndTimerRef.current)
}
// If scrolled to top and user tries to scroll up more
if ($scroller.scrollTop <= 2 && event.deltaY < 0) {
if (
isAlreadyScrolledTopRef.current &&
showBackToTimelinePanel &&
wheelGestureCompleteRef.current
) {
// User completed a previous wheel gesture and is starting a new one
navigateBackToTimeline()
return
}
// First time reaching top in this wheel gesture, just show the panel
if (!isAlreadyScrolledTopRef.current) {
isAlreadyScrolledTopRef.current = true
setShowBackToTimelinePanel(true)
wheelGestureCompleteRef.current = false
}
} else if ($scroller.scrollTop > 2) {
// Reset states when user scrolls away from top
isAlreadyScrolledTopRef.current = false
setShowBackToTimelinePanel(false)
wheelGestureCompleteRef.current = false
}
// Set timer to detect end of wheel gesture
wheelEndTimerRef.current = setTimeout(() => {
wheelGestureCompleteRef.current = true
}, 150) // 150ms after last wheel event
// Reset bottom state when wheel is used
if (event.deltaY !== 0) {
isAlreadyScrolledBottomRef.current = false
setShowKeepScrollingPanel(false)
}
}
const handleTouchStart = (event: TouchEvent) => {
// Reset all scroll states when touch starts
const checkScrollBottomByWheel = () => {
isAlreadyScrolledBottomRef.current = false
isAlreadyScrolledTopRef.current = false
wheelGestureCompleteRef.current = false
setShowKeepScrollingPanel(false)
setShowBackToTimelinePanel(false)
// Clear wheel timer
if (wheelEndTimerRef.current) {
clearTimeout(wheelEndTimerRef.current)
wheelEndTimerRef.current = null
}
// Store initial touch position
if (event.touches[0]) {
lastTouchYRef.current = event.touches[0].clientY
}
}
const handleTouchMove = (event: TouchEvent) => {
const $scroller = scrollerRef.current
if (!$scroller || !event.touches[0]) return
const { scrollTop } = $scroller
// Check if at top and trying to scroll up via touch
if (scrollTop <= 2) {
const { clientY } = event.touches[0]
if (
isAlreadyScrolledTopRef.current &&
showBackToTimelinePanel &&
lastTouchYRef.current !== null
) {
// Additional upward touch gesture at top when panel is already showing
const deltaY = clientY - lastTouchYRef.current
if (deltaY > 20) {
// Threshold for upward swipe
navigateBackToTimeline()
return
}
}
isAlreadyScrolledTopRef.current = true
setShowBackToTimelinePanel(true)
lastTouchYRef.current = clientY
} else {
isAlreadyScrolledTopRef.current = false
setShowBackToTimelinePanel(false)
}
}
scrollerRef.current?.addEventListener("wheel", handleWheelEvent)
scrollerRef.current?.addEventListener("touchstart", handleTouchStart)
scrollerRef.current?.addEventListener("touchmove", handleTouchMove)
scrollerRef.current?.addEventListener("wheel", checkScrollBottomByWheel)
const cleanupScrollAnimation = () => {
scrollAnimationRef.current?.stop()
scrollAnimationRef.current = null
}
const cleanupTimers = () => {
if (wheelEndTimerRef.current) {
clearTimeout(wheelEndTimerRef.current)
wheelEndTimerRef.current = null
}
}
return combineCleanupFunctions(
() => {
scrollerRef.current?.removeEventListener("wheel", handleWheelEvent)
scrollerRef.current?.removeEventListener("touchstart", handleTouchStart)
scrollerRef.current?.removeEventListener("touchmove", handleTouchMove)
scrollerRef.current?.removeEventListener("wheel", checkScrollBottomByWheel)
},
cleanupScrollAnimation,
cleanupTimers,
EventBus.subscribe(COMMAND_ID.entryRender.scrollUp, () => {
const $scroller = scrollerRef.current
if (!$scroller) return
// Reset top scroll states for keyboard navigation
isAlreadyScrolledTopRef.current = false
setShowBackToTimelinePanel(false)
wheelGestureCompleteRef.current = false
const currentScroll = $scroller.scrollTop
// Smart scroll distance: larger viewports get larger scroll distances
// But cap it at a reasonable maximum for very large screens
@@ -263,18 +130,13 @@ export const EntryScrollingAndNavigationHandler = ({
cleanupScrollAnimation()
const targetScroll = Math.max(0, currentScroll - delta)
smoothScrollTo(targetScroll, $scroller)
checkBottomScrollOnly($scroller)
checkScrollBottom($scroller)
}),
EventBus.subscribe(COMMAND_ID.entryRender.scrollDown, () => {
const $scroller = scrollerRef.current
if (!$scroller) return
// Reset top scroll states for keyboard navigation
isAlreadyScrolledTopRef.current = false
setShowBackToTimelinePanel(false)
wheelGestureCompleteRef.current = false
const currentScroll = $scroller.scrollTop
// Smart scroll distance: larger viewports get larger scroll distances
// But cap it at a reasonable maximum for very large screens
@@ -287,7 +149,7 @@ export const EntryScrollingAndNavigationHandler = ({
currentScroll + delta,
)
smoothScrollTo(targetScroll, $scroller)
checkBottomScrollOnly($scroller)
checkScrollBottom($scroller)
}),
EventBus.subscribe(
COMMAND_ID.layout.focusToEntryRender,
@@ -304,15 +166,7 @@ export const EntryScrollingAndNavigationHandler = ({
},
),
)
}, [
highlightBoundary,
navigateToNext,
navigateBackToTimeline,
scrollAnimationRef,
scrollerRef,
smoothScrollTo,
showBackToTimelinePanel,
])
}, [highlightBoundary, navigateToNext, scrollAnimationRef, scrollerRef, smoothScrollTo])
return (
<AnimatePresence>
@@ -344,34 +198,6 @@ export const EntryScrollingAndNavigationHandler = ({
</button>
</m.div>
)}
{showBackToTimelinePanel && (
<m.div
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={Spring.presets.smooth}
className={clsx(
"pointer-events-none absolute !right-1/2 z-40 !translate-x-1/2",
"top-12",
"backdrop-blur-background rounded-full border px-3.5 py-2",
"border-border/40 bg-material-ultra-thin/70 shadow-[0_1px_2px_rgba(0,0,0,0.06),0_8px_24px_rgba(0,0,0,0.08)]",
"hover:bg-material-thin/70 hover:border-border/60 active:scale-[0.98]",
)}
>
<button
onClick={navigateBackToTimeline}
type="button"
className={"group pointer-events-auto flex items-center gap-2"}
>
<i className="i-mingcute-arrow-up-fill text-text/90 mr-1 size-5" />
<span className="text-text/90 text-left text-[13px] font-medium">
Reached the top.
<br />
Scroll up once more to go back to timeline
</span>
</button>
</m.div>
)}
</AnimatePresence>
)
}