Skip to content

Commit bc2d5e9

Browse files
committed
fix(core): scroll-spy select last element on end scroll
1 parent 3f64067 commit bc2d5e9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

core/pfe-core/controllers/scroll-spy-controller.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ export interface ScrollSpyControllerOptions extends IntersectionObserverInit {
3030
}
3131

3232
export class ScrollSpyController implements ReactiveController {
33+
static #instances = new Set<ScrollSpyController>;
34+
35+
static {
36+
addEventListener('scroll', () => {
37+
if (Math.round(window.innerHeight + window.scrollY) >= document.body.scrollHeight) {
38+
this.#instances.forEach(ssc => {
39+
ssc.#setActive(ssc.#linkChildren.at(-1));
40+
});
41+
}
42+
}, { passive: true });
43+
}
44+
3345
#tagNames: string[];
3446
#activeAttribute: string;
3547

@@ -104,9 +116,15 @@ export class ScrollSpyController implements ReactiveController {
104116
}
105117

106118
hostConnected(): void {
119+
ScrollSpyController.#instances.add(this);
107120
this.#initIo();
108121
}
109122

123+
hostDisconnected(): void {
124+
ScrollSpyController.#instances.delete(this);
125+
this.#io?.disconnect();
126+
}
127+
110128
#initializing = true;
111129

112130
async #initIo() {

0 commit comments

Comments
 (0)