@@ -24,8 +24,14 @@ class Sidebar extends React.Component {
24
24
}
25
25
this . state = { isEnabled : true , mode : globalState . sidebar . mode }
26
26
this . lastScrollY = null
27
+ this . scrollUpRate = {
28
+ firstEventTime : null ,
29
+ lastEventTime : null ,
30
+ firstScrollY : null
31
+ }
27
32
28
33
this . handleScroll = this . handleScroll . bind ( this )
34
+ this . calcScrollUpRate = this . calcScrollUpRate . bind ( this )
29
35
}
30
36
31
37
changeMode ( newMode ) {
@@ -93,15 +99,51 @@ class Sidebar extends React.Component {
93
99
window . removeEventListener ( 'scroll' , this . handleScroll ) ;
94
100
}
95
101
96
- handleScroll ( ) {
97
- const isScrollUp = this . lastScrollY && window . scrollY < this . lastScrollY
98
- this . lastScrollY = window . scrollY
99
- const isEnabled = isScrollUp || scrollY <= window . innerHeight
102
+ handleScroll ( event ) {
103
+ const scrollY = window . scrollY
104
+
105
+ const isScrollUp = this . lastScrollY && this . lastScrollY > scrollY
106
+ this . lastScrollY = scrollY
107
+ const isFastScrollUp = this . calcScrollUpRate ( event . timeStamp , scrollY , isScrollUp )
108
+
109
+ const isInTopOfPage = scrollY <= window . innerHeight
110
+ const isEnabled = isInTopOfPage || isFastScrollUp || ( this . state . isEnabled && isScrollUp )
111
+
100
112
if ( isEnabled != this . state . isEnabled ) {
101
113
this . setState ( { isEnabled} )
102
114
}
103
115
}
104
116
117
+ calcScrollUpRate ( eventTime , scrollY , isScrollUp ) {
118
+
119
+ const maxEventTimeGap = 100
120
+
121
+ if ( isScrollUp ) {
122
+ if ( this . scrollUpRate . firstScrollY &&
123
+ maxEventTimeGap > eventTime - this . scrollUpRate . lastEventTime ) {
124
+ this . scrollUpRate . lastEventTime = eventTime
125
+ } else {
126
+ this . scrollUpRate = {
127
+ firstEventTime : eventTime ,
128
+ lastEventTime : eventTime ,
129
+ firstScrollY : scrollY
130
+ }
131
+ }
132
+
133
+ const pixels = this . scrollUpRate . firstScrollY - scrollY
134
+ const millis = this . scrollUpRate . lastEventTime - this . scrollUpRate . firstEventTime
135
+
136
+ return pixels > 50 && ( millis / pixels ) < 10
137
+ } else {
138
+ this . scrollUpRate = {
139
+ firstEventTime : null ,
140
+ lastEventTime : null ,
141
+ firstScrollY : null
142
+ }
143
+ return false
144
+ }
145
+ }
146
+
105
147
renderProfileImg ( author ) {
106
148
return (
107
149
< Toggle isEnabled = { this . valueByMode ( true , false , true ) } >
0 commit comments