Skip to content

Commit 627f7db

Browse files
committed
fix(mobile): scroll selected section tabs into view
1 parent de0514b commit 627f7db

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

‎apps/mobile/src/components/ui/Tabs.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GlassView } from "expo-glass-effect";
2-
import { useState, type ReactNode } from "react";
2+
import { useEffect, useRef, useState, type ComponentRef, type ReactNode } from "react";
33
import { Pressable, ScrollView, View } from "react-native";
44
import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated";
55
import { withUniwind } from "uniwind";
@@ -27,8 +27,13 @@ export interface TabsProps<Value extends string> {
2727
export function Tabs<Value extends string>(props: TabsProps<Value>) {
2828
const { themeAppearance } = useAppearancePreferences();
2929
const reduceMotion = useReducedMotion();
30+
const scrollView = useRef<ComponentRef<typeof ScrollView>>(null);
3031
const [frames, setFrames] = useState<Partial<Record<Value, { x: number; width: number }>>>({});
3132
const frame = frames[props.selected];
33+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- keeps the selected tab visible in the native scroll viewport
34+
useEffect(() => {
35+
if (frame) scrollView.current?.scrollTo({ x: frame.x, animated: false });
36+
}, [frame]);
3237
const duration = reduceMotion ? 0 : 220;
3338
const indicatorStyle = useAnimatedStyle(() => ({
3439
width: withTiming(frame?.width ?? 0, { duration }),
@@ -37,6 +42,7 @@ export function Tabs<Value extends string>(props: TabsProps<Value>) {
3742

3843
return (
3944
<ScrollView
45+
ref={scrollView}
4046
horizontal
4147
showsHorizontalScrollIndicator={false}
4248
className={cn("grow-0", props.className)}

0 commit comments

Comments
 (0)