Skip to content

Commit 56a9e37

Browse files
committed
v2.2.0 (onSectionEnter)
1 parent 1a19ab0 commit 56a9e37

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-update-url-on-scroll",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"description": "Provide updating the URL/hash on page scrolling.",
55
"main": "lib/index.js",
66
"files": [

src/Manager.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Manager {
2525
this.forceHashUpdate = debounce(this.handleHashChange, 1);
2626

2727
this.basePath = this.getBasePath();
28+
this.basePathName = window.location.pathname;
2829
this.baseTitle = document.title;
2930
this.imagesAreLoaded = false;
3031

@@ -36,6 +37,7 @@ class Manager {
3637

3738
const incrementCounter = () => {
3839
counter++;
40+
3941
if (counter === len) {
4042
this.imagesAreLoaded = true;
4143
const event = new Event(EVENT_IMAGES_LOADED);
@@ -101,6 +103,16 @@ class Manager {
101103
this.addListeners();
102104
}
103105
this.forceHashUpdate();
106+
107+
// check if this anchor is the current one
108+
if (window.location.href.endsWith(`${name}${hash ? `#${hash}` : ''}`)) {
109+
this.basePath = this.basePath.replace(`/${name}`, '');
110+
}
111+
if (window.location.pathname.endsWith(`/${name}`)) {
112+
this.basePathName = this.basePathName.replace(`/${name}`, '');
113+
if (this.basePathName === '') this.basePathName = '/';
114+
}
115+
104116
this.anchors[id] = {
105117
id,
106118
component: element,
@@ -109,54 +121,53 @@ class Manager {
109121
title,
110122
exact
111123
};
112-
113-
// check if this anchor is the current one
114-
if (window.location.href.endsWith(`${name}${hash ? `#${hash}` : ''}`)) {
115-
this.basePath = this.basePath.replace(`/${name}`, '');
116-
}
117124
}
118125

119126
removeAnchor = (id) => {
120-
delete this.anchors[id]
127+
delete this.anchors[id];
121128
// if this is the last anchor, remove listeners
122129
if (Object.keys(this.anchors).length === 0) {
123-
this.removeListeners()
130+
this.removeListeners();
124131
}
125132
}
126133

127134
onSectionChange = (newAnchor, oldAnchor) => {
128135
const {onSectionEnter} = this.config;
136+
const getPath = (anchor) => anchor.name
137+
? (anchor.exact ? `/${anchor.name}` : `${this.basePathName !== '/' ? this.basePathName : ''}/${anchor.name}`)
138+
: this.basePathName;
129139

130140
if (typeof onSectionEnter === 'function') {
131-
onSectionEnter(
132-
/* new state */
133-
newAnchor ? { ...this.anchors[newAnchor], id: newAnchor } : null,
134-
/* old state */
135-
oldAnchor ? { ...this.anchors[oldAnchor], id: oldAnchor } : null
136-
);
141+
const nextState = newAnchor ? { ...this.anchors[newAnchor], id: newAnchor } : {};
142+
nextState.path = getPath(nextState);
143+
144+
const prevState = oldAnchor ? { ...this.anchors[oldAnchor], id: oldAnchor } : {};
145+
prevState.path = getPath(prevState);
146+
147+
onSectionEnter(nextState, prevState);
137148
}
138149
}
139150

140151
handleScroll = () => {
141152
const {offset, keepLastAnchorHash, affectHistory} = this.config;
142-
const bestAnchorId = getBestAnchorGivenScrollLocation(this.anchors, -offset);
143-
const currentHash = getHash({manager: this});
153+
const nextAnchor = getBestAnchorGivenScrollLocation(this.anchors, -offset);
154+
const prevAnchor = getHash({manager: this});
144155

145-
if (bestAnchorId && currentHash !== bestAnchorId) {
156+
if (nextAnchor && prevAnchor !== nextAnchor) {
146157
this.forcedHash = true;
147158

148159
updateHash({
149-
anchor: this.anchors[bestAnchorId],
160+
anchor: this.anchors[nextAnchor],
150161
affectHistory,
151162
manager: this
152163
});
153164

154-
this.onSectionChange(bestAnchorId, currentHash);
165+
this.onSectionChange(nextAnchor, prevAnchor);
155166

156-
} else if (!bestAnchorId && !keepLastAnchorHash) {
167+
} else if (!nextAnchor && !keepLastAnchorHash) {
157168
removeHash({manager: this});
158-
if (this.anchors[currentHash]) {
159-
this.onSectionChange(null, currentHash);
169+
if (this.anchors[prevAnchor]) {
170+
this.onSectionChange(null, prevAnchor);
160171
}
161172
}
162173
}

0 commit comments

Comments
 (0)