Skip to content

Commit f9899a5

Browse files
author
Clément Vollet
authored
Make ScrollableTabBar more Android-like
- Center text vertically in tab - Center active tab horizontally in container when possible Also, allow for multiple tabs with the same label. Tested on iOS and Android.
1 parent 5e8bfe0 commit f9899a5

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

ScrollableTabBar.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,24 @@ const ScrollableTabBar = React.createClass({
7878
},
7979

8080
updateTabPanel(position, pageOffset) {
81-
const absolutePageOffset = pageOffset * this._tabsMeasurements[position].width;
82-
let newScrollX = this._tabsMeasurements[position].left + absolutePageOffset;
83-
84-
newScrollX -= this.props.scrollOffset;
81+
const containerWidth = this._containerMeasurements.width;
82+
const tabWidth = this._tabsMeasurements[position].width;
83+
const nextTabMeasurements = this._tabsMeasurements[position + 1];
84+
const nextTabWidth = nextTabMeasurements && nextTabMeasurements.width || 0;
85+
const tabOffset = this._tabsMeasurements[position].left;
86+
const absolutePageOffset = pageOffset * tabWidth;
87+
let newScrollX = tabOffset + absolutePageOffset;
88+
89+
// center tab and smooth tab change (for when tabWidth changes a lot between two tabs)
90+
newScrollX -= (containerWidth - (1 - pageOffset) * tabWidth - pageOffset * nextTabWidth ) / 2 ;
8591
newScrollX = newScrollX >= 0 ? newScrollX : 0;
8692

8793
if (Platform.OS === 'android') {
88-
this._scrollView.scrollTo({x: newScrollX, y: 0, });
94+
this._scrollView.scrollTo({x: newScrollX, y: 0, animated: false, });
8995
} else {
9096
const rightBoundScroll = this._tabContainerMeasurements.width - (this._containerMeasurements.width);
9197
newScrollX = newScrollX > rightBoundScroll ? rightBoundScroll : newScrollX;
92-
this._scrollView.scrollTo({x: newScrollX, y: 0, });
98+
this._scrollView.scrollTo({x: newScrollX, y: 0, animated: false, });
9399
}
94100

95101
},
@@ -120,7 +126,7 @@ const ScrollableTabBar = React.createClass({
120126
const fontWeight = isTabActive ? 'bold' : 'normal';
121127

122128
return <TouchableOpacity
123-
key={name}
129+
key={`${name}_${page}`}
124130
ref={'tab_' + page}
125131
accessible={true}
126132
accessibilityLabel={name}
@@ -167,7 +173,6 @@ const ScrollableTabBar = React.createClass({
167173
showsHorizontalScrollIndicator={false}
168174
showsVerticalScrollIndicator={false}
169175
directionalLockEnabled={true}
170-
scrollEventThrottle={16}
171176
bounces={false}
172177
>
173178
<View
@@ -203,7 +208,6 @@ const styles = StyleSheet.create({
203208
height: 49,
204209
alignItems: 'center',
205210
justifyContent: 'center',
206-
paddingBottom: 30,
207211
paddingLeft: 20,
208212
paddingRight: 20,
209213
},

0 commit comments

Comments
 (0)