@@ -103,7 +103,7 @@ class TabBar extends PureComponent {
103
103
*/
104
104
centerSelected : PropTypes . bool ,
105
105
/**
106
- * (Experimental) Pass to optimize loading time by measuring tab bar items text
106
+ * (Experimental) Pass to optimize loading time by measuring tab bar items text
107
107
* instead of waiting for onLayout
108
108
*/
109
109
optimize : PropTypes . bool
@@ -171,13 +171,27 @@ class TabBar extends PureComponent {
171
171
return centerSelected ? this . containerWidth / 2 - guesstimateCenterValue : 0 ;
172
172
}
173
173
174
+ getSnapBreakpoints ( ) {
175
+ const { centerSelected} = this . props ;
176
+ const { itemsWidths, itemsOffsets} = this . state ;
177
+
178
+ if ( itemsWidths && centerSelected ) {
179
+ return _ . times ( itemsWidths . length , ( index ) => {
180
+ const screenCenter = this . containerWidth / 2 ;
181
+ const itemOffset = itemsOffsets [ index ] ;
182
+ const itemWidth = itemsWidths [ index ] ;
183
+ return itemOffset - screenCenter + itemWidth / 2 ;
184
+ } ) ;
185
+ }
186
+ }
187
+
174
188
measureItems = async ( ) => {
175
189
const { labelStyle} = this . props ;
176
190
const measuring = _ . map ( this . props . items , ( item ) => {
177
191
return Typography . measureTextSize ( item . label , labelStyle ) ;
178
192
} ) ;
179
193
const results = await Promise . all ( measuring ) ;
180
- const widths = _ . map ( results , item => item . width + Spacings . s4 * 2 ) ;
194
+ const widths = _ . map ( results , ( item ) => item . width + Spacings . s4 * 2 ) ;
181
195
const offsets = [ ] ;
182
196
_ . forEach ( widths , ( width , index ) => {
183
197
if ( index === 0 ) {
@@ -190,7 +204,7 @@ class TabBar extends PureComponent {
190
204
this . _itemsOffsets = offsets ;
191
205
// TODO: consider saving this setState and ride registerTabItems setState
192
206
this . setItemsLayouts ( ) ;
193
- }
207
+ } ;
194
208
195
209
registerTabItems ( ) {
196
210
const { registerTabItems} = this . context ;
@@ -248,12 +262,12 @@ class TabBar extends PureComponent {
248
262
249
263
setItemsLayouts = ( ) => {
250
264
const { selectedIndex} = this . context ;
251
- const itemsOffsets = _ . map ( this . _itemsOffsets , offset => offset + INDICATOR_INSET ) ;
265
+ const itemsOffsets = _ . map ( this . _itemsOffsets , ( offset ) => offset + INDICATOR_INSET ) ;
252
266
const itemsWidths = _ . map ( this . _itemsWidths , ( width ) => width - INDICATOR_INSET * 2 ) ;
253
267
254
268
this . setState ( { itemsWidths, itemsOffsets} ) ;
255
269
this . focusSelected ( [ selectedIndex ] , false ) ;
256
- }
270
+ } ;
257
271
258
272
onScroll = ( { nativeEvent : { contentOffset} } ) => {
259
273
this . tabBarScrollOffset = contentOffset . x ;
@@ -375,8 +389,10 @@ class TabBar extends PureComponent {
375
389
scrollEnabled = { scrollEnabled }
376
390
onContentSizeChange = { this . onContentSizeChange }
377
391
onScroll = { this . onScroll }
378
- scrollEventThrottle = { 100 }
392
+ scrollEventThrottle = { 16 }
379
393
testID = { testID }
394
+ snapToOffsets = { this . getSnapBreakpoints ( ) }
395
+ decelerationRate = { 'fast' }
380
396
>
381
397
< View style = { [ styles . tabBar , height && { height} , { paddingHorizontal : this . centerOffset } ] } >
382
398
{ this . renderTabBarItems ( ) }
0 commit comments