diff --git a/src/view/com/pager/TabBar.tsx b/src/view/com/pager/TabBar.tsx index 0ad3ad8b1a..171a0c2217 100644 --- a/src/view/com/pager/TabBar.tsx +++ b/src/view/com/pager/TabBar.tsx @@ -144,13 +144,31 @@ export function TabBar({ if (isSyncingScroll.get() === true) { const progressDiff = index - dragProgress.get() const offsetDiff = progressToOffset(progressDiff) - // TODO: Get into view if obscured - const offset = scrollX.get() + offsetDiff + let offset = scrollX.get() + offsetDiff + const itemLayout = layouts.get()[index] + if (itemLayout) { + if ( + itemLayout.x < offset || + itemLayout.x + itemLayout.width > offset + containerSize.get() + ) { + // If the proposed offset is still out of view, don't bother with + // proportional scroll and ensure the target is scrolled into view. + offset = progressToOffset(index) + } + } scrollTo(scrollElRef, offset, 0, true) } isSyncingScroll.set(true) }, - [isSyncingScroll, scrollElRef, scrollX, dragProgress, progressToOffset], + [ + isSyncingScroll, + scrollElRef, + scrollX, + dragProgress, + progressToOffset, + containerSize, + layouts, + ], ) const onItemLayout = useCallback(