Skip to content

Commit 22f9a41

Browse files
authored
Merge pull request #15 from pepsivontwist/master
Fixed issue with scroll event not unmounting when component unmounts
2 parents 4a59345 + 8621b6b commit 22f9a41

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class ScrollspyNav extends Component {
1111
this.headerBackground = this.props.headerBackground === "true" ? true : false;
1212
this.offset = this.props.offset || 0;
1313

14+
this.onScroll = this.onScroll.bind(this);
15+
1416
if(this.props.router && this.props.router === "HashRouter") {
1517
this.homeDefaultLink = "#/";
1618
this.hashIdentifier = "#/#";
@@ -20,6 +22,25 @@ class ScrollspyNav extends Component {
2022
}
2123
}
2224

25+
onScroll() {
26+
let scrollSectionOffsetTop;
27+
this.scrollTargetIds.map((sectionID, index) => {
28+
scrollSectionOffsetTop = document.getElementById(sectionID).offsetTop - (this.headerBackground ? document.querySelector("div[data-nav='list']").scrollHeight : 0);
29+
30+
if (window.pageYOffset >= scrollSectionOffsetTop && window.pageYOffset < scrollSectionOffsetTop + document.getElementById(sectionID).scrollHeight) {
31+
this.getNavLinkElement(sectionID).classList.add(this.activeNavClass);
32+
this.clearOtherNavLinkActiveStyle(sectionID)
33+
} else {
34+
this.getNavLinkElement(sectionID).classList.remove(this.activeNavClass);
35+
}
36+
37+
if (window.innerHeight + window.pageYOffset >= document.body.scrollHeight && index === this.scrollTargetIds.length - 1) {
38+
this.getNavLinkElement(sectionID).classList.add(this.activeNavClass);
39+
this.clearOtherNavLinkActiveStyle(sectionID);
40+
}
41+
});
42+
}
43+
2344
easeInOutQuad(current_time, start, change, duration) {
2445
current_time /= duration/2;
2546
if (current_time < 1) return change/2*current_time*current_time + start;
@@ -79,8 +100,8 @@ class ScrollspyNav extends Component {
79100
}
80101

81102
componentDidMount() {
82-
if (document.querySelector(`a[href='${this.homeDefaultLink}']`)) {
83-
document.querySelector(`a[href='${this.homeDefaultLink}']`).addEventListener("click", (event) => {
103+
if (document.querySelector(`a[href='${this.homeDefaultLink}#']`)) {
104+
document.querySelector(`a[href='${this.homeDefaultLink}#']`).addEventListener("click", (event) => {
84105
event.preventDefault();
85106
this.scrollTo(window.pageYOffset, 0, this.scrollDuration);
86107
window.location.hash = "";
@@ -101,24 +122,11 @@ class ScrollspyNav extends Component {
101122
});
102123
})
103124

104-
window.addEventListener("scroll", () => {
105-
let scrollSectionOffsetTop;
106-
this.scrollTargetIds.map((sectionID, index) => {
107-
scrollSectionOffsetTop = document.getElementById(sectionID).offsetTop - (this.headerBackground ? document.querySelector("div[data-nav='list']").scrollHeight : 0);
108-
109-
if (window.pageYOffset >= scrollSectionOffsetTop && window.pageYOffset < scrollSectionOffsetTop + document.getElementById(sectionID).scrollHeight) {
110-
this.getNavLinkElement(sectionID).classList.add(this.activeNavClass);
111-
this.clearOtherNavLinkActiveStyle(sectionID)
112-
} else {
113-
this.getNavLinkElement(sectionID).classList.remove(this.activeNavClass);
114-
}
125+
window.addEventListener("scroll", this.onScroll);
126+
}
115127

116-
if (window.innerHeight + window.pageYOffset >= document.body.scrollHeight && index === this.scrollTargetIds.length - 1) {
117-
this.getNavLinkElement(sectionID).classList.add(this.activeNavClass);
118-
this.clearOtherNavLinkActiveStyle(sectionID);
119-
}
120-
});
121-
});
128+
componentWillUnmount() {
129+
window.removeEventListener("scroll", this.onScroll);
122130
}
123131

124132
render() {

0 commit comments

Comments
 (0)