From e8fab20b421ca2b05d53a6c2608ade595eed4db7 Mon Sep 17 00:00:00 2001 From: zlareb1 Date: Fri, 20 Nov 2026 16:36:02 +0000 Subject: [PATCH] Keep final-page focus requests within their DOM commit A pending passive effect can clear a request before its loading state commits. Hand focus over in a layout effect and reproduce the lost request before passive effects flush. Signed-off-by: zlareb1 --- frontend/src/lib/useLastPageFocus.test.tsx | 20 ++++++++++++++++++-- frontend/src/lib/useLastPageFocus.ts | 5 +++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/frontend/src/lib/useLastPageFocus.test.tsx b/frontend/src/lib/useLastPageFocus.test.tsx index 7e14a7e645..2481f15465 100644 --- a/frontend/src/lib/useLastPageFocus.test.tsx +++ b/frontend/src/lib/useLastPageFocus.test.tsx @@ -1,4 +1,4 @@ -import { useRef, useState } from "react"; +import { useLayoutEffect, useRef, useState } from "react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, it } from "vitest"; import { act, cleanup, render, screen } from "../test/rtl"; @@ -8,12 +8,13 @@ import { useLastPageFocus } from "./useLastPageFocus"; // ends the read with these rows shown and says whether a page is left. let finish: (rows: string[], more: boolean) => void = () => {}; -function List({ first }: { first: string[] }) { +function List({ first, startOnCommit = false }: { first: string[]; startOnCommit?: boolean }) { const [rows, setRows] = useState(first); const [loading, setLoading] = useState(false); const [more, setMore] = useState(true); const box = useRef(null); const search = useRef(null); + const loadMore = useRef(null); const mark = useLastPageFocus({ loading, more, @@ -23,6 +24,13 @@ function List({ first }: { first: string[] }) { ), fallback: () => search.current, }); + useLayoutEffect(() => { + if (!startOnCommit) return; + // Start before the previous render's passive effects flush. + loadMore.current?.focus(); + mark(first); + setLoading(true); + }, []); finish = (next, left) => { setRows(next); setMore(left); @@ -42,6 +50,7 @@ function List({ first }: { first: string[] }) { {more && (