Skip to content

Commit bb7a508

Browse files
committed
Make the re-appearance of the top bar on mobile a bit less sensitive
1 parent eece98a commit bb7a508

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/components/Sidebar/index.jsx

+46-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ class Sidebar extends React.Component {
2424
}
2525
this.state = { isEnabled: true, mode: globalState.sidebar.mode }
2626
this.lastScrollY = null
27+
this.scrollUpRate = {
28+
firstEventTime: null,
29+
lastEventTime: null,
30+
firstScrollY: null
31+
}
2732

2833
this.handleScroll = this.handleScroll.bind(this)
34+
this.calcScrollUpRate = this.calcScrollUpRate.bind(this)
2935
}
3036

3137
changeMode(newMode) {
@@ -93,15 +99,51 @@ class Sidebar extends React.Component {
9399
window.removeEventListener('scroll', this.handleScroll);
94100
}
95101

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+
100112
if (isEnabled != this.state.isEnabled) {
101113
this.setState({isEnabled})
102114
}
103115
}
104116

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+
105147
renderProfileImg(author) {
106148
return (
107149
<Toggle isEnabled={this.valueByMode(true, false, true)}>

0 commit comments

Comments
 (0)