Skip to content

Commit 759af46

Browse files
committed
Move createRef to composer
1 parent 2dcb548 commit 759af46

File tree

2 files changed

+23
-34
lines changed

2 files changed

+23
-34
lines changed

packages/component/src/ScrollToBottom/Composer.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ import Context from './Context';
55
import EventSpy from '../EventSpy';
66
import SpineTo from '../SpineTo';
77

8-
function isBottom(current, threshold) {
9-
const { offsetHeight, scrollHeight, scrollTop } = current;
10-
11-
return scrollHeight - scrollTop - offsetHeight <= threshold;
12-
}
13-
148
export default class ScrollToBottomComposer extends React.Component {
159
constructor(props) {
1610
super(props);
@@ -26,21 +20,25 @@ export default class ScrollToBottomComposer extends React.Component {
2620
scrollTop: this.state.target && this.state.target.current && (this.state.target.current.scrollHeight - this.state.target.current.offsetHeight)
2721
})),
2822
scrollTop: null,
29-
setTarget: target => this.setState(() => ({ target })),
30-
target: null
23+
target: React.createRef()
3124
};
3225
}
3326

3427
handleScroll() {
35-
this.setState(() => ({
36-
bottom: isBottom(this.state.target.current, this.props.threshold)
37-
}));
28+
this.setState(() => {
29+
const { current } = this.state.target;
30+
31+
if (current) {
32+
const { offsetHeight, scrollHeight, scrollTop } = current;
33+
const bottom = scrollHeight - scrollTop - offsetHeight <= this.props.threshold;
34+
35+
return { bottom };
36+
}
37+
});
3838
}
3939

4040
handleScrollEnd() {
41-
this.setState(() => ({
42-
scrollTop: null
43-
}));
41+
this.setState(() => ({ scrollTop: null }));
4442
}
4543

4644
render() {
@@ -50,15 +48,16 @@ export default class ScrollToBottomComposer extends React.Component {
5048
<EventSpy
5149
name="scroll"
5250
onEvent={ this.handleScroll }
53-
target={ this.state.target && this.state.target.current }
51+
target={ this.state.target.current }
5452
/>
55-
{ typeof this.state.scrollTop === 'number' &&
56-
<SpineTo
57-
name="scrollTop"
58-
onEnd={ this.handleScrollEnd }
59-
target={ this.state.target && this.state.target.current }
60-
value={ this.state.scrollTop }
61-
/>
53+
{
54+
typeof this.state.scrollTop === 'number' &&
55+
<SpineTo
56+
name="scrollTop"
57+
onEnd={ this.handleScrollEnd }
58+
target={ this.state.target.current }
59+
value={ this.state.scrollTop }
60+
/>
6261
}
6362
</Context.Provider>
6463
);

packages/component/src/ScrollToBottom/Panel.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,8 @@ const ROOT_CSS = css({
1111
});
1212

1313
export default class ScrollToBottomPanel extends React.PureComponent {
14-
constructor(props) {
15-
super(props);
16-
17-
this.containerRef = React.createRef();
18-
}
19-
20-
componentDidMount() {
21-
this.context.setTarget(this.containerRef);
22-
}
23-
2414
componentDidUpdate() {
25-
this.context.handleUpdate();
15+
this.context && this.context.handleUpdate();
2616
}
2717

2818
render() {
@@ -36,7 +26,7 @@ export default class ScrollToBottomPanel extends React.PureComponent {
3626
return (
3727
<div
3828
className={ classNames(ROOT_CSS + '', props.className) }
39-
ref={ this.containerRef }
29+
ref={ context.target }
4030
>
4131
{ props.children }
4232
</div>

0 commit comments

Comments
 (0)