Files
Arkie-Library-Frontend/src/components/ScrollToTop.tsx
TerryM 4f6b4a498f fix: scope back-to-top to browse, speed up reveal, reset scroll on sort change
- BackToTop now only mounts on the /browse feed (covers all / latest /
  popular / search) instead of every route.
- Reveal animation duration cut 0.4s -> 0.25s so scrolled-in content
  appears faster.
- ScrollToTop also watches `search`, so switching between sort views on
  the same /browse path (e.g. 全部资料 <-> 热门资料) returns to the top.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-29 13:22:40 +08:00

23 lines
769 B
TypeScript

import { useEffect } from "react";
import { useLocation } from "react-router-dom";
/**
* Resets the window to the top on every route change. React Router does not
* restore scroll on client navigation, so without this a short new page would
* clamp to wherever the previous (taller) page was scrolled — e.g. landing at
* the bottom of a category page after clicking a card far down the home grid.
*
* Skips navigations that carry a hash (`#post-<id>`, `#categories`, …) so
* anchor / deep-link targets keep their own scroll handling.
*/
export function ScrollToTop() {
const { pathname, search, hash } = useLocation();
useEffect(() => {
if (hash) return;
window.scrollTo({ top: 0, left: 0 });
}, [pathname, search, hash]);
return null;
}