From a59cf4a4cffa51edd5e8da5008e38d8476535404 Mon Sep 17 00:00:00 2001 From: Zeke Date: Sun, 29 Dec 2024 23:36:45 +0800 Subject: [PATCH] feat: Scroll to top when re-navigating to Discover page while already on it (#2371) --- .../modules/app-layout/subview/index.desktop.tsx | 16 +++++++++++++++- changelog/next.md | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/renderer/src/modules/app-layout/subview/index.desktop.tsx b/apps/renderer/src/modules/app-layout/subview/index.desktop.tsx index 5a3475c0a..814e1640f 100644 --- a/apps/renderer/src/modules/app-layout/subview/index.desktop.tsx +++ b/apps/renderer/src/modules/app-layout/subview/index.desktop.tsx @@ -1,11 +1,12 @@ import { getReadonlyRoute } from "@follow/components/atoms/route.js" import { MotionButtonBase } from "@follow/components/ui/button/index.js" import { ScrollArea } from "@follow/components/ui/scroll-area/index.js" +import { Routes } from "@follow/constants" import { springScrollTo } from "@follow/utils/scroller" import { cn, getOS } from "@follow/utils/utils" import { useEffect, useRef, useState } from "react" import { useTranslation } from "react-i18next" -import { Outlet, useNavigate } from "react-router" +import { NavigationType, Outlet, useLocation, useNavigate, useNavigationType } from "react-router" import { getSidebarActiveView, setSidebarActiveView } from "~/atoms/sidebar" import { FABContainer, FABPortable } from "~/components/ui/fab" @@ -20,6 +21,19 @@ export function SubviewLayout() { const title = useSubViewTitleValue() const [scrollRef, setRef] = useState(null as HTMLDivElement | null) const [isTitleSticky, setIsTitleSticky] = useState(false) + const navigationType = useNavigationType() + const location = useLocation() + + useEffect(() => { + // Scroll to top search bar when re-navigating to Discover page while already on it + if ( + navigationType === NavigationType.Replace && + location.pathname === Routes.Discover && + scrollRef + ) { + springScrollTo(0, scrollRef) + } + }, [location, navigationType, scrollRef]) useEffect(() => { const $scroll = scrollRef diff --git a/changelog/next.md b/changelog/next.md index 17888d80b..d3f10162a 100644 --- a/changelog/next.md +++ b/changelog/next.md @@ -2,6 +2,8 @@ ## New Features +- Scroll to top when re-navigating to Discover page while already on it + ## Improvements ## Bug Fixes