Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 51a0075

Browse files
Improve log scrolling.
Now, we only keep scrolled to the bottom if the user scrolls to the bottom. If the user scrolls up, we maintain their scroll position.
1 parent 0220556 commit 51a0075

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/view/cli.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class LogOutput extends Component {
6969
super(props);
7070
this._refresh = true;
7171
this._ref = React.createRef();
72+
this.state = {
73+
maxOffset: undefined,
74+
};
7275
}
7376

7477
shouldComponentUpdate() {
@@ -86,17 +89,34 @@ class LogOutput extends Component {
8689

8790
componentWillUnmount() {
8891
clearTimeout(this._tLast);
89-
clearTimeout(this._tScroll);
92+
clearInterval(this._tScroll);
9093
}
9194

9295
get printLogs() {
93-
this._tScroll = setTimeout(() => this._ref.current.scrollToEnd(), 50);
9496
return this.props.logs;
9597
}
9698

99+
onScroll(scrollEvent) {
100+
const offset = scrollEvent.nativeEvent.contentOffset.y;
101+
if (this.state.maxOffset === undefined || this.state.maxOffset <= offset) {
102+
this.setState({ maxOffset: offset - 10 })
103+
if (this._tScroll === undefined) {
104+
this._tScroll = setInterval(() => this._ref.current.scrollToEnd(), 1000);
105+
}
106+
} else if (offset < this.state.maxOffset && this._tScroll) {
107+
clearInterval(this._tScroll);
108+
this._tScroll = undefined;
109+
}
110+
}
111+
97112
render() {
98113
return (
99-
<ScrollView ref={this._ref} contentContainerStyle={logStyles.content}>
114+
<ScrollView
115+
ref={this._ref}
116+
contentContainerStyle={logStyles.content}
117+
onScroll={event => this.onScroll(event)}
118+
scrollEventThrottle={300}
119+
>
100120
<Text style={logStyles.text}>{this.printLogs}</Text>
101121
</ScrollView>
102122
);

0 commit comments

Comments
 (0)