@@ -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 &&
0 commit comments