From d69176a7ad6cfaeb1998e53f1e2d63a0895e6fdd Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Mon, 5 Oct 2026 04:31:12 +0800 Subject: [PATCH] fix(library): keep the shelf scroll position after opening a book (#6642) * fix(library): keep the shelf scroll position after opening a book Opening a book navigates to the reader and unmounts the library, so coming back always started the list or grid at the top. The stream now saves the virtual list's state (measured row sizes + scroll offset) when scrolling stops, keyed by the library URL, and restores it when it mounts again. Co-Authored-By: Claude Opus 5.5 * fix(library): save the shelf position on unmount and only restore onto the same rows Review follow-up. Save the Virtuoso snapshot in a layout cleanup when the stream unmounts, so a book tapped before a scroll settles still records the position, and keep each snapshot with the row keys that produced it: the measured sizes are per row index, so they are only restored onto the same rows. The last measured width seeds the first render, so a remounted grid starts with the column count it had when the snapshot was taken. Co-Authored-By: Claude Opus 5.5 * chore: bump foliate-js to 08db610 (readest/foliate-js#125) Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .../bookshelves/stream.browser.test.tsx | 50 +++++++++++++++++ .../src/app/library/components/Bookshelf.tsx | 1 + .../library/components/BookshelfStream.tsx | 53 +++++++++++++++++-- packages/foliate-js | 2 +- 4 files changed, 102 insertions(+), 4 deletions(-) diff --git a/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx b/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx index 4e3fcc64b..936787bc5 100644 --- a/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx +++ b/apps/readest-app/src/__tests__/bookshelves/stream.browser.test.tsx @@ -560,6 +560,56 @@ describe('mixed bookshelf stream in Chromium', () => { }); } } + it('returns to the same place after the library remounts (opening a book and coming back)', async () => { + const shelves = (count: number) => [ + section('reading', 'carousel', 12), + section('books', 'list', count), + section('more', 'grid', 60), + ]; + const stream = (count = 300) => ( +
+ +
+ ); + const scrollerOf = (root: HTMLElement) => + root.querySelector('[data-virtuoso-scroller]')!; + const firstVisible = (root: HTMLElement) => { + const top = scrollerOf(root).getBoundingClientRect().top; + return Array.from(root.querySelectorAll('[data-book]')).find( + (el) => el.getBoundingClientRect().bottom > top, + ); + }; + const visibleId = (root: HTMLElement) => { + const el = firstVisible(root); + return el && `${el.dataset['section']}:${el.dataset['book']}`; + }; + const first = render(stream()); + const scroller = scrollerOf(first.container); + await waitFor(() => expect(scroller.scrollHeight).toBeGreaterThan(8000)); + scroller.scrollTop = 6000; + fireEvent.scroll(scroller); + await waitFor(() => expect(visibleId(first.container)).toMatch(/^books:\d{2,}$/)); + // Unmount right away, as when a book is tapped before the scroll settles. + const scrollTop = scroller.scrollTop; + const book = visibleId(first.container); + first.unmount(); + + const second = render(stream()); + await waitFor(() => expect(scrollerOf(second.container).scrollTop).toBe(scrollTop)); + expect(visibleId(second.container)).toBe(book); + second.unmount(); + + // Different rows: the saved row sizes no longer apply, so start at the top. + const changed = render(stream(250)); + await waitFor(() => expect(visibleId(changed.container)).toBe('reading:0')); + expect(scrollerOf(changed.container).scrollTop).toBe(0); + }); it('keeps very large carousels horizontally virtualized and scrollable', async () => { document.documentElement.setAttribute('data-eink', 'true'); const { container, getByRole } = render( diff --git a/apps/readest-app/src/app/library/components/Bookshelf.tsx b/apps/readest-app/src/app/library/components/Bookshelf.tsx index 97d2f3f53..35b0df712 100644 --- a/apps/readest-app/src/app/library/components/Bookshelf.tsx +++ b/apps/readest-app/src/app/library/components/Bookshelf.tsx @@ -985,6 +985,7 @@ const Bookshelf: React.FC = ({ // WebKit when a search was cleared.
(); +// Seeds the first render after a remount, so the grid starts with the column +// count (and rows) it had when the snapshot was taken. +let lastStreamWidth = 0; type StreamRow = { key: string; section: ShelfSection; @@ -267,10 +290,12 @@ export default function BookshelfStream({ pageNavigation = false, hidePageButtons = false, navigationBottomInset = 0, + scrollKey, }: StreamProps) { const _ = useTranslation(); const root = useRef(null); - const [width, setWidth] = useState(0); + const virtuoso = useRef(null); + const [width, setWidth] = useState(lastStreamWidth); const [height, setHeight] = useState(0); const [scroller, setScroller] = useState(null); const handleScrollerRef = useCallback( @@ -287,6 +312,7 @@ export default function BookshelfStream({ const element = root.current; if (!element) return; const measure = () => { + lastStreamWidth = element.clientWidth; setWidth(element.clientWidth); setHeight(element.clientHeight); }; @@ -301,6 +327,25 @@ export default function BookshelfStream({ () => buildBookshelfRows(sections, columns, includeImport), [sections, columns, includeImport], ); + const rowKeys = useMemo(() => rows.map((row) => row.key).join('\n'), [rows]); + const [restoreState] = useState(() => { + const saved = scrollKey === undefined ? undefined : savedScrollStates.get(scrollKey); + return saved?.rowKeys === rowKeys ? saved.state : undefined; + }); + const latest = useRef({ scrollKey, rowKeys }); + useLayoutEffect(() => { + latest.current = { scrollKey, rowKeys }; + }); + // Save on unmount (opening a book leaves the library), even mid-scroll. A + // layout cleanup runs before Virtuoso's own handle is torn down. + useLayoutEffect( + () => () => { + const { scrollKey, rowKeys } = latest.current; + if (scrollKey === undefined) return; + virtuoso.current?.getState((state) => savedScrollStates.set(scrollKey, { rowKeys, state })); + }, + [], + ); // Virtuoso mounts its footer before it has measured the viewport and placed // any row, which flashed the import action at the top of the page on every // library load. Hold it back until these rows are on screen (or there are @@ -321,6 +366,8 @@ export default function BookshelfStream({ {/* Hide native scrollbars before the deferred overlay initializes. */}