Skip to content

Commit 7085b83

Browse files
committed
Reduce exposure of internals
1 parent 4a2fb2d commit 7085b83

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

packages/component/src/ScrollToBottom/Composer.js

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

1515
this.state = {
16-
atBottom: true,
17-
atEnd: true,
18-
atTop: true,
19-
mode: props.mode,
20-
handleUpdate: () => this.state.atEnd && this.state.scrollToEnd(),
21-
scrollTo: scrollTop => this.setState(() => ({ scrollTop })),
22-
scrollToBottom: () => this.state.scrollTo(this.state.target && (this.state.target.scrollHeight - this.state.target.offsetHeight)),
23-
scrollToEnd: () => this.state.mode === 'top' ? this.state.scrollToTop() : this.state.scrollToBottom(),
24-
scrollToTop: () => this.state.scrollTo(0),
16+
context: {
17+
_handleUpdate: () => {
18+
const { context } = this.state;
19+
20+
context.atEnd && context.scrollToEnd();
21+
},
22+
_setTarget: target => this.setState(() => ({ target })),
23+
atBottom: true,
24+
atEnd: true,
25+
atTop: true,
26+
mode: props.mode,
27+
scrollTo: scrollTop => this.setState(() => ({ scrollTop })),
28+
scrollToBottom: () => {
29+
const { context, target } = this.state;
30+
31+
context.scrollTo(target && (target.scrollHeight - target.offsetHeight));
32+
},
33+
scrollToEnd: () => {
34+
const { context } = this.state;
35+
36+
context.mode === 'top' ? context.scrollToTop() : context.scrollToBottom();
37+
},
38+
scrollToTop: () => this.state.context.scrollTo(0),
39+
threshold: 10
40+
},
2541
scrollTop: null,
26-
setTarget: target => this.setState(() => ({ target })),
27-
target: null,
28-
threshold: 10
42+
target: null
2943
};
3044
}
3145

3246
componentWillReceiveProps(nextProps) {
33-
this.setState(() => ({
34-
mode: nextProps.mode === 'top' ? 'top' : 'bottom',
35-
threshold: nextProps.threshold
47+
this.setState(({ context }) => ({
48+
context: {
49+
...context,
50+
mode: nextProps.mode === 'top' ? 'top' : 'bottom',
51+
threshold: nextProps.threshold
52+
}
3653
}));
3754
}
3855

3956
handleScroll() {
40-
this.setState(({ mode, target, threshold }) => {
57+
this.setState(({ context, target }) => {
4158
if (target) {
59+
const { mode, threshold } = context;
4260
const { offsetHeight, scrollHeight, scrollTop } = target;
4361
const atBottom = scrollHeight - scrollTop - offsetHeight <= threshold;
4462
const atTop = scrollTop <= threshold;
4563

4664
return {
47-
atBottom,
48-
atEnd: mode === 'top' ? atTop : atBottom,
49-
atTop
65+
context: {
66+
...context,
67+
atBottom,
68+
atEnd: mode === 'top' ? atTop : atBottom,
69+
atTop
70+
}
5071
};
5172
}
5273
});
@@ -60,7 +81,7 @@ export default class Composer extends React.Component {
6081
const { scrollTop, target } = this.state;
6182

6283
return (
63-
<Context.Provider value={ this.state }>
84+
<Context.Provider value={ this.state.context }>
6485
{ this.props.children }
6586
{
6687
target &&
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import React from 'react';
22

33
export default React.createContext({
4+
_handleUpdate: () => 0,
5+
_setTarget: () => 0,
46
atBottom: true,
57
atEnd: true,
68
atTop: true,
79
mode: 'bottom',
8-
handleUpdate: () => 0,
910
scrollTo: () => 0,
1011
scrollToBottom: () => 0,
1112
scrollToEnd: () => 0,
1213
scrollToTop: () => 0,
13-
scrollTop: null,
14-
setTarget: () => 0,
15-
target: null,
1614
threshold: 10
1715
});

packages/component/src/ScrollToBottom/Panel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const ROOT_CSS = css({
1212

1313
export default class Panel extends React.PureComponent {
1414
componentDidUpdate() {
15-
this.context && this.context.handleUpdate();
15+
this.context && this.context._handleUpdate();
1616
}
1717

1818
render() {
@@ -26,7 +26,7 @@ export default class Panel extends React.PureComponent {
2626
return (
2727
<div
2828
className={ classNames(ROOT_CSS + '', props.className) }
29-
ref={ context.setTarget }
29+
ref={ context._setTarget }
3030
>
3131
{ props.children }
3232
</div>

0 commit comments

Comments
 (0)