Skip to content

Commit

Permalink
feat: property on scrollable MaterialTabBar to keep tab in the center
Browse files Browse the repository at this point in the history
  • Loading branch information
andreialecu committed May 4, 2022
1 parent 41fb49b commit 6d35e31
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/MaterialTabBar/TabBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const MaterialTabBar = <T extends TabName = any>({
activeColor,
tabStyle,
width: customWidth,
keepActiveTabCentered,
}: MaterialTabBarProps<T>): React.ReactElement => {
const tabBarRef = useAnimatedRef<Animated.ScrollView>()
const windowWidth = useWindowDimensions().width
Expand Down Expand Up @@ -167,6 +168,7 @@ const MaterialTabBar = <T extends TabName = any>({
const halfTab = itemsLayout[index.value].width / 2
const offset = itemsLayout[index.value].x
if (
keepActiveTabCentered ||
offset < tabsOffset.value ||
offset > tabsOffset.value + width - 2 * halfTab
) {
Expand Down
5 changes: 5 additions & 0 deletions src/MaterialTabBar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export type MaterialTabBarProps<N extends TabName> = TabBarProps<N> & {
* Custom width of the tabbar. Defaults to the window width.
*/
width?: number

/**
* Whether to keep the currently active tab centered in a scrollable tab bar
*/
keepActiveTabCentered?: boolean
}

export type ItemLayout = {
Expand Down

0 comments on commit 6d35e31

Please sign in to comment.