Skip to content

Commit 3a3d731

Browse files
committed
Scroll to top
1 parent 0f8cbb1 commit 3a3d731

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

packages/component/src/ScrollToBottom/AutoHideFollowButton.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ const ROOT_CSS = css({
2626

2727
export default props =>
2828
<Context.Consumer>
29-
{ context => !context.bottom &&
29+
{ context => !context.atEnd &&
3030
<button
3131
className={ ROOT_CSS + '' }
32-
onClick={ context.scrollToBottom }
32+
onClick={ context.scrollToEnd }
3333
/>
3434
}
3535
</Context.Consumer>

packages/component/src/ScrollToBottom/Composer.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,40 @@ export default class ScrollToBottomComposer extends React.Component {
1313
this.handleScrollEnd = this.handleScrollEnd.bind(this);
1414

1515
this.state = {
16-
bottom: true,
17-
handleUpdate: () => this.state.bottom && this.state.scrollToBottom(),
16+
atBottom: true,
17+
atEnd: true,
18+
atTop: true,
19+
mode: props.mode,
20+
handleUpdate: () => this.state.atEnd && this.state.scrollToEnd(),
1821
scrollToBottom: () => this.setState(state => ({
19-
scrollTop: state.target && state.target && (state.target.scrollHeight - state.target.offsetHeight)
22+
scrollTop: state.target && (state.target.scrollHeight - state.target.offsetHeight)
23+
})),
24+
scrollToEnd: () => this.state.mode === 'top' ? this.state.scrollToTop() : this.state.scrollToBottom(),
25+
scrollToTop: () => this.setState(state => ({
26+
scrollTop: 0
2027
})),
2128
scrollTop: null,
2229
setTarget: target => this.setState(() => ({ target })),
2330
target: null
2431
};
2532
}
2633

34+
componentWillReceiveProps(nextProps) {
35+
this.setState(() => ({ mode: nextProps.mode === 'top' ? 'top' : 'bottom' }));
36+
}
37+
2738
handleScroll() {
28-
this.setState(({ target }) => {
39+
this.setState(({ mode, target }) => {
2940
if (target) {
3041
const { offsetHeight, scrollHeight, scrollTop } = target;
31-
const bottom = scrollHeight - scrollTop - offsetHeight <= this.props.threshold;
42+
const atBottom = scrollHeight - scrollTop - offsetHeight <= this.props.threshold;
43+
const atTop = scrollTop <= this.props.threshold;
3244

33-
return { bottom };
45+
return {
46+
atBottom,
47+
atEnd: mode === 'top' ? atTop : atBottom,
48+
atTop
49+
};
3450
}
3551
});
3652
}

0 commit comments

Comments
 (0)