Skip to content

Commit e12903a

Browse files
chore: update readme
1 parent 9eefeae commit e12903a

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

README.md

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,8 @@ export interface Tab<T, Y> extends Omit<TouchableOpacityProps, 'onPress'> {
848848
icon?: React.FC<Y>;
849849
iconProps?: Y;
850850
titleStyle?: TextStyle;
851+
852+
onPress: (routeName: T) => void;
851853
}
852854
```
853855

@@ -859,8 +861,8 @@ export interface Tab<T, Y> extends Omit<TouchableOpacityProps, 'onPress'> {
859861
| name | description | type | default |
860862
| ------ | ------ | ------ | ---- |
861863
| tabs | required | Tab<T, Y>[] | - |
862-
| activeTab | required | T | - |
863-
| onTabPress | required | T | - |
864+
| activeTab | The active tab is managed through the state. Pass the activeTab prop to enable the active tab indicator animation when scrollAnim is not provided. | T | - |
865+
| scrollAnim | From 0 to 1 / tabs.lenght. Make the indicator responsive to scrolling. See more in the bottom of Tabs section. | SharedValue<number> | - |
864866
| badgeSize | - | SMALL or BIG | BIG |
865867
| animConfig | - | (routeName: T) => void | - |
866868
| tabIconProps | - | Y | - |
@@ -884,8 +886,8 @@ export interface Tab<T, Y> extends Omit<TouchableOpacityProps, 'onPress'> {
884886
| name | description | type | default |
885887
| ------ | ------ | ------ | ---- |
886888
| tabs | required | Tab<T, Y>[] | - |
887-
| activeTab | required | T | - |
888-
| onTabPress | required | T | - |
889+
| activeTab | The active tab is managed through the state. Pass the activeTab prop to enable the active tab indicator animation when scrollAnim is not provided. | T | - |
890+
| scrollAnim | From 0 to 1 / tabs.lenght. Make the indicator responsive to scrolling. See more in the bottom of Tabs section. | SharedValue<number> | - |
889891
| badgeSize | - | SMALL or BIG | BIG |
890892
| animConfig | - | (routeName: T) => void | - |
891893
| tabIconProps | - | Y | - |
@@ -900,6 +902,59 @@ export interface Tab<T, Y> extends Omit<TouchableOpacityProps, 'onPress'> {
900902
![secondary tabs with badges](https://ik.imagekit.io/Computools/rn-material-components/primary_tabs_with_badges.png?updatedAt=1735922619944)
901903

902904
</details>
905+
<br />
906+
907+
To make the indicator responsive to scrolling, handle the scrollAnim state in the parent component and pass it as a prop to the Tabs component. This allows for seamless synchronization between the scrolling behavior and the indicator movement.
908+
909+
See the example:
910+
911+
```
912+
const ParentComponent = () => {
913+
const {width: windowWidth} = useWindowDimensions();
914+
915+
const acitveViewAnim = useSharedValue(0);
916+
const scrollViewRef = React.useRef<AnimatedScrollView>(null);
917+
918+
const tabs: Tab[] = [<--- your tabs --->]
919+
const maxOutput = 1 / tabs.lenght; // The maximum output is calculated as 1 / tabsCount, where tabsCount represents the total number of tabs.
920+
921+
const handleScrollToScreen1 = () => {
922+
acitveViewAnim.value = withTiming(0);
923+
scrollViewRef.current?.scrollTo({x: 0});
924+
};
925+
926+
const handleScrollToScreen2 = () => {
927+
acitveViewAnim.value = withTiming(maxOutput);
928+
scrollViewRef.current?.scrollToEnd();
929+
};
930+
931+
932+
const scrollHandler = useAnimatedScrollHandler(
933+
{
934+
onScroll: (e) => {
935+
acitveViewAnim.value = interpolate(e.contentOffset.x, [0, windowWidth], [0, maxOutput]);
936+
},
937+
onEndDrag: (e) => {
938+
if (e.contentOffset.x > maxOutput) {
939+
runOnJS(handleScrollToScreen2)();
940+
} else {
941+
runOnJS(handleScrollToScreen1)();
942+
}
943+
},
944+
},
945+
[windowWidth]
946+
);
947+
948+
return (
949+
<SecondaryTabs scrollAnim={acitveViewAnim} tabs={tabs}/>
950+
<Animated.ScrollView horizontal ref={scrollViewRef} bounces={false} showsHorizontalScrollIndicator={false} onScroll={scrollHandler}>
951+
<Text style={{width: windowWidth, paddingStart: 20}}>Screen 1</Text>
952+
<Text style={{width: windowWidth, paddingStart: 20}}>Screen 2</Text>
953+
</Animated.ScrollView>
954+
)
955+
};
956+
```
957+
903958
</details>
904959
</details>
905960
<details><summary>Sheets</summary>

0 commit comments

Comments
 (0)