Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ export const useScrollToSelectedItem = ({
}: UseScrollToSelectedItemParams): void => {
// Track the previous targetPathOrId to detect actual changes
const prevTargetPathOrIdRef = useRef<string | undefined>(undefined);
// Track whether initial scroll has been completed successfully
const hasInitialScrolledRef = useRef(false);

useEffect(() => {
// Only scroll when targetPathOrId actually changes, not on items change alone
// This prevents unwanted scrolling when creating a new page (items update but targetPathOrId stays the same)
if (targetPathOrId === prevTargetPathOrIdRef.current) return;
const targetChanged = targetPathOrId !== prevTargetPathOrIdRef.current;

// Skip if target hasn't changed AND initial scroll is already done
// This allows retrying scroll when items are loaded, but prevents unwanted scrolling
// when creating a new page (items update but targetPathOrId stays the same after initial scroll)
if (!targetChanged && hasInitialScrolledRef.current) return;

prevTargetPathOrIdRef.current = targetPathOrId;

if (targetPathOrId == null) return;
Expand All @@ -33,6 +39,7 @@ export const useScrollToSelectedItem = ({
});

if (selectedIndex !== -1) {
hasInitialScrolledRef.current = true;
// Use a small delay to ensure the virtualizer is ready
setTimeout(() => {
virtualizer.scrollToIndex(selectedIndex, {
Expand Down
Loading