Skip to content

Commit c06473a

Browse files
AlanFosterfacebook-github-bot
authored andcommitted
Update network inspector to have smarter scroll stickiness (#21952)
Summary: When making use of the network inspector on a react-native app, it can be quite annoying that as new requests come in the network inspector instantly sticks to the bottom. This PR makes this logic smarter by allowing the user to be scrolled away from the bottom by two rows to override this automatic scrolling to the bottom logic. Pull Request resolved: #21952 Differential Revision: D14162762 Pulled By: cpojer fbshipit-source-id: ad49858509dd74a817ebabab54fdacc99773bf22
1 parent 28f1648 commit c06473a

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

Libraries/Inspector/NetworkOverlay.js

+41-6
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ class NetworkOverlay extends React.Component<Props, State> {
8888
_requestsListView: ?React.ElementRef<typeof FlatList>;
8989
_detailScrollView: ?React.ElementRef<typeof ScrollView>;
9090

91+
// Metrics are used to decide when if the request list should be sticky, and
92+
// scroll to the bottom as new network requests come in, or if the user has
93+
// intentionally scrolled away from the bottom - to instead flash the scroll bar
94+
// and keep the current position
95+
_requestsListViewScrollMetrics = {
96+
offset: 0,
97+
visibleLength: 0,
98+
contentLength: 0,
99+
};
100+
91101
// Map of `socketId` -> `index in `this.state.requests`.
92102
_socketIdMap = {};
93103
// Map of `xhr._index` -> `index in `this.state.requests`.
@@ -121,7 +131,7 @@ class NetworkOverlay extends React.Component<Props, State> {
121131
{
122132
requests: this.state.requests.concat(_xhr),
123133
},
124-
this._scrollRequestsToBottom,
134+
this._indicateAdditionalRequests,
125135
);
126136
});
127137

@@ -214,7 +224,7 @@ class NetworkOverlay extends React.Component<Props, State> {
214224
{
215225
requests: this.state.requests.concat(_webSocket),
216226
},
217-
this._scrollRequestsToBottom,
227+
this._indicateAdditionalRequests,
218228
);
219229
},
220230
);
@@ -383,11 +393,35 @@ class NetworkOverlay extends React.Component<Props, State> {
383393
);
384394
}
385395

386-
_scrollRequestsToBottom(): void {
396+
_indicateAdditionalRequests = (): void => {
387397
if (this._requestsListView) {
388-
this._requestsListView.scrollToEnd();
398+
const distanceFromEndThreshold = LISTVIEW_CELL_HEIGHT * 2;
399+
const {
400+
offset,
401+
visibleLength,
402+
contentLength,
403+
} = this._requestsListViewScrollMetrics;
404+
const distanceFromEnd = contentLength - visibleLength - offset;
405+
const isCloseToEnd = distanceFromEnd <= distanceFromEndThreshold;
406+
if (isCloseToEnd) {
407+
this._requestsListView.scrollToEnd();
408+
} else {
409+
this._requestsListView.flashScrollIndicators();
410+
}
389411
}
390-
}
412+
};
413+
414+
_captureRequestsListView = (listRef: ?FlatList<NetworkRequestInfo>): void => {
415+
this._requestsListView = listRef;
416+
};
417+
418+
_requestsListViewOnScroll = (e: Object): void => {
419+
this._requestsListViewScrollMetrics.offset = e.nativeEvent.contentOffset.y;
420+
this._requestsListViewScrollMetrics.visibleLength =
421+
e.nativeEvent.layoutMeasurement.height;
422+
this._requestsListViewScrollMetrics.contentLength =
423+
e.nativeEvent.contentSize.height;
424+
};
391425

392426
/**
393427
* Popup a scrollView to dynamically show detailed information of
@@ -446,7 +480,8 @@ class NetworkOverlay extends React.Component<Props, State> {
446480
</View>
447481

448482
<FlatList
449-
ref={listRef => (this._requestsListView = listRef)}
483+
ref={this._captureRequestsListView}
484+
onScroll={this._requestsListViewOnScroll}
450485
style={styles.listView}
451486
data={requests}
452487
renderItem={this._renderItem}

0 commit comments

Comments
 (0)