Skip to content

Commit 268e6c5

Browse files
committed
fix: improve scroll-to-center reliability with requestAnimationFrame
- Replace setTimeout with requestAnimationFrame for more reliable DOM synchronization - Add proper boundary checking to prevent scrolling past container limits - Ensure scroll position stays within valid bounds using Math.min/max
1 parent 9a33478 commit 268e6c5

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

webview-ui/src/components/chat/ModeSelector.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ export const ModeSelector = ({
159159
searchInputRef.current.focus()
160160
}
161161

162-
// Scroll to selected item with a small delay to ensure DOM is ready
163-
setTimeout(() => {
162+
requestAnimationFrame(() => {
164163
if (selectedItemRef.current && scrollContainerRef.current) {
165164
const container = scrollContainerRef.current
166165
const item = selectedItemRef.current
@@ -173,12 +172,16 @@ export const ModeSelector = ({
173172
// Center the item in the container
174173
const scrollPosition = itemTop - containerHeight / 2 + itemHeight / 2
175174

175+
// Ensure we don't scroll past boundaries
176+
const maxScroll = container.scrollHeight - containerHeight
177+
const finalScrollPosition = Math.min(Math.max(0, scrollPosition), maxScroll)
178+
176179
container.scrollTo({
177-
top: Math.max(0, scrollPosition),
180+
top: finalScrollPosition,
178181
behavior: "instant",
179182
})
180183
}
181-
}, 0)
184+
})
182185
}
183186
}, [open])
184187

0 commit comments

Comments
 (0)